diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/common/log_manager.h | 2 | ||||
| -rw-r--r-- | src/common/std_condition_variable.h | 168 | ||||
| -rw-r--r-- | src/common/std_mutex.h | 362 | ||||
| -rw-r--r-- | src/common/std_thread.h | 314 | ||||
| -rw-r--r-- | src/common/thread.cpp | 6 | ||||
| -rw-r--r-- | src/common/thread.h | 7 | ||||
| -rw-r--r-- | src/core/core_timing.cpp | 2 |
8 files changed, 7 insertions, 857 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 55a5f9eba..868fda55e 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -49,9 +49,6 @@ set(HEADERS | |||
| 49 | msg_handler.h | 49 | msg_handler.h |
| 50 | platform.h | 50 | platform.h |
| 51 | scm_rev.h | 51 | scm_rev.h |
| 52 | std_condition_variable.h | ||
| 53 | std_mutex.h | ||
| 54 | std_thread.h | ||
| 55 | string_util.h | 52 | string_util.h |
| 56 | swap.h | 53 | swap.h |
| 57 | symbols.h | 54 | symbols.h |
diff --git a/src/common/log_manager.h b/src/common/log_manager.h index 81d808825..ce62d0361 100644 --- a/src/common/log_manager.h +++ b/src/common/log_manager.h | |||
| @@ -6,11 +6,11 @@ | |||
| 6 | 6 | ||
| 7 | #include "common/log.h" | 7 | #include "common/log.h" |
| 8 | #include "common/string_util.h" | 8 | #include "common/string_util.h" |
| 9 | #include "common/thread.h" | ||
| 10 | #include "common/file_util.h" | 9 | #include "common/file_util.h" |
| 11 | 10 | ||
| 12 | #include <cstring> | 11 | #include <cstring> |
| 13 | #include <set> | 12 | #include <set> |
| 13 | #include <mutex> | ||
| 14 | 14 | ||
| 15 | #define MAX_MESSAGES 8000 | 15 | #define MAX_MESSAGES 8000 |
| 16 | #define MAX_MSGLEN 1024 | 16 | #define MAX_MSGLEN 1024 |
diff --git a/src/common/std_condition_variable.h b/src/common/std_condition_variable.h deleted file mode 100644 index ad2022f5a..000000000 --- a/src/common/std_condition_variable.h +++ /dev/null | |||
| @@ -1,168 +0,0 @@ | |||
| 1 | |||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) | ||
| 5 | #define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) | ||
| 6 | |||
| 7 | #ifndef __has_include | ||
| 8 | #define __has_include(s) 0 | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ | ||
| 12 | |||
| 13 | // GCC 4.4 provides <condition_variable> | ||
| 14 | #include <condition_variable> | ||
| 15 | |||
| 16 | #elif __has_include(<condition_variable>) && !ANDROID | ||
| 17 | |||
| 18 | // clang and libc++ provide <condition_variable> on OSX. However, the version | ||
| 19 | // of libc++ bundled with OSX 10.7 and 10.8 is buggy: it uses _ as a variable. | ||
| 20 | // | ||
| 21 | // We work around this issue by undefining and redefining _. | ||
| 22 | |||
| 23 | #undef _ | ||
| 24 | #include <condition_variable> | ||
| 25 | #define _(s) wxGetTranslation((s)) | ||
| 26 | |||
| 27 | #else | ||
| 28 | |||
| 29 | // partial std::condition_variable implementation for win32/pthread | ||
| 30 | |||
| 31 | #include "common/std_mutex.h" | ||
| 32 | |||
| 33 | #if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) | ||
| 34 | #define USE_RVALUE_REFERENCES | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #if defined(_WIN32) && defined(_M_X64) | ||
| 38 | #define USE_CONDITION_VARIABLES | ||
| 39 | #elif defined(_WIN32) | ||
| 40 | #define USE_EVENTS | ||
| 41 | #endif | ||
| 42 | |||
| 43 | namespace std | ||
| 44 | { | ||
| 45 | |||
| 46 | class condition_variable | ||
| 47 | { | ||
| 48 | #if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) | ||
| 49 | typedef CONDITION_VARIABLE native_type; | ||
| 50 | #elif defined(_WIN32) | ||
| 51 | typedef HANDLE native_type; | ||
| 52 | #else | ||
| 53 | typedef pthread_cond_t native_type; | ||
| 54 | #endif | ||
| 55 | |||
| 56 | public: | ||
| 57 | |||
| 58 | #ifdef USE_EVENTS | ||
| 59 | typedef native_type native_handle_type; | ||
| 60 | #else | ||
| 61 | typedef native_type* native_handle_type; | ||
| 62 | #endif | ||
| 63 | |||
| 64 | condition_variable() | ||
| 65 | { | ||
| 66 | #if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) | ||
| 67 | InitializeConditionVariable(&m_handle); | ||
| 68 | #elif defined(_WIN32) | ||
| 69 | m_handle = CreateEvent(NULL, false, false, NULL); | ||
| 70 | #else | ||
| 71 | pthread_cond_init(&m_handle, NULL); | ||
| 72 | #endif | ||
| 73 | } | ||
| 74 | |||
| 75 | ~condition_variable() | ||
| 76 | { | ||
| 77 | #if defined(_WIN32) && !defined(USE_CONDITION_VARIABLES) | ||
| 78 | CloseHandle(m_handle); | ||
| 79 | #elif !defined(_WIN32) | ||
| 80 | pthread_cond_destroy(&m_handle); | ||
| 81 | #endif | ||
| 82 | } | ||
| 83 | |||
| 84 | condition_variable(const condition_variable&) /*= delete*/; | ||
| 85 | condition_variable& operator=(const condition_variable&) /*= delete*/; | ||
| 86 | |||
| 87 | void notify_one() | ||
| 88 | { | ||
| 89 | #if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) | ||
| 90 | WakeConditionVariable(&m_handle); | ||
| 91 | #elif defined(_WIN32) | ||
| 92 | SetEvent(m_handle); | ||
| 93 | #else | ||
| 94 | pthread_cond_signal(&m_handle); | ||
| 95 | #endif | ||
| 96 | } | ||
| 97 | |||
| 98 | void notify_all() | ||
| 99 | { | ||
| 100 | #if defined(_WIN32) && defined(USE_CONDITION_VARIABLES) | ||
| 101 | WakeAllConditionVariable(&m_handle); | ||
| 102 | #elif defined(_WIN32) | ||
| 103 | // TODO: broken | ||
| 104 | SetEvent(m_handle); | ||
| 105 | #else | ||
| 106 | pthread_cond_broadcast(&m_handle); | ||
| 107 | #endif | ||
| 108 | } | ||
| 109 | |||
| 110 | void wait(unique_lock<mutex>& lock) | ||
| 111 | { | ||
| 112 | #ifdef _WIN32 | ||
| 113 | #ifdef USE_SRWLOCKS | ||
| 114 | SleepConditionVariableSRW(&m_handle, lock.mutex()->native_handle(), INFINITE, 0); | ||
| 115 | #elif defined(USE_CONDITION_VARIABLES) | ||
| 116 | SleepConditionVariableCS(&m_handle, lock.mutex()->native_handle(), INFINITE); | ||
| 117 | #else | ||
| 118 | // TODO: broken, the unlock and wait need to be atomic | ||
| 119 | lock.unlock(); | ||
| 120 | WaitForSingleObject(m_handle, INFINITE); | ||
| 121 | lock.lock(); | ||
| 122 | #endif | ||
| 123 | #else | ||
| 124 | pthread_cond_wait(&m_handle, lock.mutex()->native_handle()); | ||
| 125 | #endif | ||
| 126 | } | ||
| 127 | |||
| 128 | template <class Predicate> | ||
| 129 | void wait(unique_lock<mutex>& lock, Predicate pred) | ||
| 130 | { | ||
| 131 | while (!pred()) | ||
| 132 | wait(lock); | ||
| 133 | } | ||
| 134 | |||
| 135 | //template <class Clock, class Duration> | ||
| 136 | //cv_status wait_until(unique_lock<mutex>& lock, | ||
| 137 | // const chrono::time_point<Clock, Duration>& abs_time); | ||
| 138 | |||
| 139 | //template <class Clock, class Duration, class Predicate> | ||
| 140 | // bool wait_until(unique_lock<mutex>& lock, | ||
| 141 | // const chrono::time_point<Clock, Duration>& abs_time, | ||
| 142 | // Predicate pred); | ||
| 143 | |||
| 144 | //template <class Rep, class Period> | ||
| 145 | //cv_status wait_for(unique_lock<mutex>& lock, | ||
| 146 | // const chrono::duration<Rep, Period>& rel_time); | ||
| 147 | |||
| 148 | //template <class Rep, class Period, class Predicate> | ||
| 149 | // bool wait_for(unique_lock<mutex>& lock, | ||
| 150 | // const chrono::duration<Rep, Period>& rel_time, | ||
| 151 | // Predicate pred); | ||
| 152 | |||
| 153 | native_handle_type native_handle() | ||
| 154 | { | ||
| 155 | #ifdef USE_EVENTS | ||
| 156 | return m_handle; | ||
| 157 | #else | ||
| 158 | return &m_handle; | ||
| 159 | #endif | ||
| 160 | } | ||
| 161 | |||
| 162 | private: | ||
| 163 | native_type m_handle; | ||
| 164 | }; | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | #endif | ||
diff --git a/src/common/std_mutex.h b/src/common/std_mutex.h deleted file mode 100644 index 5711d791b..000000000 --- a/src/common/std_mutex.h +++ /dev/null | |||
| @@ -1,362 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) | ||
| 4 | #define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) | ||
| 5 | |||
| 6 | #ifndef __has_include | ||
| 7 | #define __has_include(s) 0 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ | ||
| 11 | // GCC 4.4 provides <mutex> | ||
| 12 | #include <mutex> | ||
| 13 | #elif __has_include(<mutex>) && !ANDROID | ||
| 14 | // Clang + libc++ | ||
| 15 | #include <mutex> | ||
| 16 | #else | ||
| 17 | |||
| 18 | // partial <mutex> implementation for win32/pthread | ||
| 19 | |||
| 20 | #include <algorithm> | ||
| 21 | |||
| 22 | #if defined(_WIN32) | ||
| 23 | // WIN32 | ||
| 24 | #define WIN32_LEAN_AND_MEAN | ||
| 25 | #include <Windows.h> | ||
| 26 | |||
| 27 | #else | ||
| 28 | // POSIX | ||
| 29 | #include <pthread.h> | ||
| 30 | |||
| 31 | #endif | ||
| 32 | |||
| 33 | #if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) | ||
| 34 | #define USE_RVALUE_REFERENCES | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #if defined(_WIN32) && defined(_M_X64) | ||
| 38 | #define USE_SRWLOCKS | ||
| 39 | #endif | ||
| 40 | |||
| 41 | namespace std | ||
| 42 | { | ||
| 43 | |||
| 44 | class recursive_mutex | ||
| 45 | { | ||
| 46 | #ifdef _WIN32 | ||
| 47 | typedef CRITICAL_SECTION native_type; | ||
| 48 | #else | ||
| 49 | typedef pthread_mutex_t native_type; | ||
| 50 | #endif | ||
| 51 | |||
| 52 | public: | ||
| 53 | typedef native_type* native_handle_type; | ||
| 54 | |||
| 55 | recursive_mutex(const recursive_mutex&) /*= delete*/; | ||
| 56 | recursive_mutex& operator=(const recursive_mutex&) /*= delete*/; | ||
| 57 | |||
| 58 | recursive_mutex() | ||
| 59 | { | ||
| 60 | #ifdef _WIN32 | ||
| 61 | InitializeCriticalSection(&m_handle); | ||
| 62 | #else | ||
| 63 | pthread_mutexattr_t attr; | ||
| 64 | pthread_mutexattr_init(&attr); | ||
| 65 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | ||
| 66 | pthread_mutex_init(&m_handle, &attr); | ||
| 67 | #endif | ||
| 68 | } | ||
| 69 | |||
| 70 | ~recursive_mutex() | ||
| 71 | { | ||
| 72 | #ifdef _WIN32 | ||
| 73 | DeleteCriticalSection(&m_handle); | ||
| 74 | #else | ||
| 75 | pthread_mutex_destroy(&m_handle); | ||
| 76 | #endif | ||
| 77 | } | ||
| 78 | |||
| 79 | void lock() | ||
| 80 | { | ||
| 81 | #ifdef _WIN32 | ||
| 82 | EnterCriticalSection(&m_handle); | ||
| 83 | #else | ||
| 84 | pthread_mutex_lock(&m_handle); | ||
| 85 | #endif | ||
| 86 | } | ||
| 87 | |||
| 88 | void unlock() | ||
| 89 | { | ||
| 90 | #ifdef _WIN32 | ||
| 91 | LeaveCriticalSection(&m_handle); | ||
| 92 | #else | ||
| 93 | pthread_mutex_unlock(&m_handle); | ||
| 94 | #endif | ||
| 95 | } | ||
| 96 | |||
| 97 | bool try_lock() | ||
| 98 | { | ||
| 99 | #ifdef _WIN32 | ||
| 100 | return (0 != TryEnterCriticalSection(&m_handle)); | ||
| 101 | #else | ||
| 102 | return !pthread_mutex_trylock(&m_handle); | ||
| 103 | #endif | ||
| 104 | } | ||
| 105 | |||
| 106 | native_handle_type native_handle() | ||
| 107 | { | ||
| 108 | return &m_handle; | ||
| 109 | } | ||
| 110 | |||
| 111 | private: | ||
| 112 | native_type m_handle; | ||
| 113 | }; | ||
| 114 | |||
| 115 | #if !defined(_WIN32) || defined(USE_SRWLOCKS) | ||
| 116 | |||
| 117 | class mutex | ||
| 118 | { | ||
| 119 | #ifdef _WIN32 | ||
| 120 | typedef SRWLOCK native_type; | ||
| 121 | #else | ||
| 122 | typedef pthread_mutex_t native_type; | ||
| 123 | #endif | ||
| 124 | |||
| 125 | public: | ||
| 126 | typedef native_type* native_handle_type; | ||
| 127 | |||
| 128 | mutex(const mutex&) /*= delete*/; | ||
| 129 | mutex& operator=(const mutex&) /*= delete*/; | ||
| 130 | |||
| 131 | mutex() | ||
| 132 | { | ||
| 133 | #ifdef _WIN32 | ||
| 134 | InitializeSRWLock(&m_handle); | ||
| 135 | #else | ||
| 136 | pthread_mutex_init(&m_handle, NULL); | ||
| 137 | #endif | ||
| 138 | } | ||
| 139 | |||
| 140 | ~mutex() | ||
| 141 | { | ||
| 142 | #ifdef _WIN32 | ||
| 143 | #else | ||
| 144 | pthread_mutex_destroy(&m_handle); | ||
| 145 | #endif | ||
| 146 | } | ||
| 147 | |||
| 148 | void lock() | ||
| 149 | { | ||
| 150 | #ifdef _WIN32 | ||
| 151 | AcquireSRWLockExclusive(&m_handle); | ||
| 152 | #else | ||
| 153 | pthread_mutex_lock(&m_handle); | ||
| 154 | #endif | ||
| 155 | } | ||
| 156 | |||
| 157 | void unlock() | ||
| 158 | { | ||
| 159 | #ifdef _WIN32 | ||
| 160 | ReleaseSRWLockExclusive(&m_handle); | ||
| 161 | #else | ||
| 162 | pthread_mutex_unlock(&m_handle); | ||
| 163 | #endif | ||
| 164 | } | ||
| 165 | |||
| 166 | bool try_lock() | ||
| 167 | { | ||
| 168 | #ifdef _WIN32 | ||
| 169 | // XXX TryAcquireSRWLockExclusive requires Windows 7! | ||
| 170 | // return (0 != TryAcquireSRWLockExclusive(&m_handle)); | ||
| 171 | return false; | ||
| 172 | #else | ||
| 173 | return !pthread_mutex_trylock(&m_handle); | ||
| 174 | #endif | ||
| 175 | } | ||
| 176 | |||
| 177 | native_handle_type native_handle() | ||
| 178 | { | ||
| 179 | return &m_handle; | ||
| 180 | } | ||
| 181 | |||
| 182 | private: | ||
| 183 | native_type m_handle; | ||
| 184 | }; | ||
| 185 | |||
| 186 | #else | ||
| 187 | typedef recursive_mutex mutex; // just use CriticalSections | ||
| 188 | |||
| 189 | #endif | ||
| 190 | |||
| 191 | enum defer_lock_t { defer_lock }; | ||
| 192 | enum try_to_lock_t { try_to_lock }; | ||
| 193 | enum adopt_lock_t { adopt_lock }; | ||
| 194 | |||
| 195 | template <class Mutex> | ||
| 196 | class lock_guard | ||
| 197 | { | ||
| 198 | public: | ||
| 199 | typedef Mutex mutex_type; | ||
| 200 | |||
| 201 | explicit lock_guard(mutex_type& m) | ||
| 202 | : pm(m) | ||
| 203 | { | ||
| 204 | m.lock(); | ||
| 205 | } | ||
| 206 | |||
| 207 | lock_guard(mutex_type& m, adopt_lock_t) | ||
| 208 | : pm(m) | ||
| 209 | { | ||
| 210 | } | ||
| 211 | |||
| 212 | ~lock_guard() | ||
| 213 | { | ||
| 214 | pm.unlock(); | ||
| 215 | } | ||
| 216 | |||
| 217 | lock_guard(lock_guard const&) /*= delete*/; | ||
| 218 | lock_guard& operator=(lock_guard const&) /*= delete*/; | ||
| 219 | |||
| 220 | private: | ||
| 221 | mutex_type& pm; | ||
| 222 | }; | ||
| 223 | |||
| 224 | template <class Mutex> | ||
| 225 | class unique_lock | ||
| 226 | { | ||
| 227 | public: | ||
| 228 | typedef Mutex mutex_type; | ||
| 229 | |||
| 230 | unique_lock() | ||
| 231 | : pm(NULL), owns(false) | ||
| 232 | {} | ||
| 233 | |||
| 234 | /*explicit*/ unique_lock(mutex_type& m) | ||
| 235 | : pm(&m), owns(true) | ||
| 236 | { | ||
| 237 | m.lock(); | ||
| 238 | } | ||
| 239 | |||
| 240 | unique_lock(mutex_type& m, defer_lock_t) | ||
| 241 | : pm(&m), owns(false) | ||
| 242 | {} | ||
| 243 | |||
| 244 | unique_lock(mutex_type& m, try_to_lock_t) | ||
| 245 | : pm(&m), owns(m.try_lock()) | ||
| 246 | {} | ||
| 247 | |||
| 248 | unique_lock(mutex_type& m, adopt_lock_t) | ||
| 249 | : pm(&m), owns(true) | ||
| 250 | {} | ||
| 251 | |||
| 252 | //template <class Clock, class Duration> | ||
| 253 | //unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); | ||
| 254 | |||
| 255 | //template <class Rep, class Period> | ||
| 256 | //unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); | ||
| 257 | |||
| 258 | ~unique_lock() | ||
| 259 | { | ||
| 260 | if (owns_lock()) | ||
| 261 | mutex()->unlock(); | ||
| 262 | } | ||
| 263 | |||
| 264 | #ifdef USE_RVALUE_REFERENCES | ||
| 265 | unique_lock& operator=(const unique_lock&) /*= delete*/; | ||
| 266 | |||
| 267 | unique_lock& operator=(unique_lock&& other) | ||
| 268 | { | ||
| 269 | #else | ||
| 270 | unique_lock& operator=(const unique_lock& u) | ||
| 271 | { | ||
| 272 | // ugly const_cast to get around lack of rvalue references | ||
| 273 | unique_lock& other = const_cast<unique_lock&>(u); | ||
| 274 | #endif | ||
| 275 | swap(other); | ||
| 276 | return *this; | ||
| 277 | } | ||
| 278 | |||
| 279 | #ifdef USE_RVALUE_REFERENCES | ||
| 280 | unique_lock(const unique_lock&) /*= delete*/; | ||
| 281 | |||
| 282 | unique_lock(unique_lock&& other) | ||
| 283 | : pm(NULL), owns(false) | ||
| 284 | { | ||
| 285 | #else | ||
| 286 | unique_lock(const unique_lock& u) | ||
| 287 | : pm(NULL), owns(false) | ||
| 288 | { | ||
| 289 | // ugly const_cast to get around lack of rvalue references | ||
| 290 | unique_lock& other = const_cast<unique_lock&>(u); | ||
| 291 | #endif | ||
| 292 | swap(other); | ||
| 293 | } | ||
| 294 | |||
| 295 | void lock() | ||
| 296 | { | ||
| 297 | mutex()->lock(); | ||
| 298 | owns = true; | ||
| 299 | } | ||
| 300 | |||
| 301 | bool try_lock() | ||
| 302 | { | ||
| 303 | owns = mutex()->try_lock(); | ||
| 304 | return owns; | ||
| 305 | } | ||
| 306 | |||
| 307 | //template <class Rep, class Period> | ||
| 308 | //bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); | ||
| 309 | //template <class Clock, class Duration> | ||
| 310 | //bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); | ||
| 311 | |||
| 312 | void unlock() | ||
| 313 | { | ||
| 314 | mutex()->unlock(); | ||
| 315 | owns = false; | ||
| 316 | } | ||
| 317 | |||
| 318 | void swap(unique_lock& u) | ||
| 319 | { | ||
| 320 | std::swap(pm, u.pm); | ||
| 321 | std::swap(owns, u.owns); | ||
| 322 | } | ||
| 323 | |||
| 324 | mutex_type* release() | ||
| 325 | { | ||
| 326 | auto const ret = mutex(); | ||
| 327 | |||
| 328 | pm = NULL; | ||
| 329 | owns = false; | ||
| 330 | |||
| 331 | return ret; | ||
| 332 | } | ||
| 333 | |||
| 334 | bool owns_lock() const | ||
| 335 | { | ||
| 336 | return owns; | ||
| 337 | } | ||
| 338 | |||
| 339 | //explicit operator bool () const | ||
| 340 | //{ | ||
| 341 | // return owns_lock(); | ||
| 342 | //} | ||
| 343 | |||
| 344 | mutex_type* mutex() const | ||
| 345 | { | ||
| 346 | return pm; | ||
| 347 | } | ||
| 348 | |||
| 349 | private: | ||
| 350 | mutex_type* pm; | ||
| 351 | bool owns; | ||
| 352 | }; | ||
| 353 | |||
| 354 | template <class Mutex> | ||
| 355 | void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) | ||
| 356 | { | ||
| 357 | x.swap(y); | ||
| 358 | } | ||
| 359 | |||
| 360 | } | ||
| 361 | |||
| 362 | #endif | ||
diff --git a/src/common/std_thread.h b/src/common/std_thread.h deleted file mode 100644 index ce1336ee7..000000000 --- a/src/common/std_thread.h +++ /dev/null | |||
| @@ -1,314 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z)) | ||
| 4 | #define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) | ||
| 5 | |||
| 6 | #ifndef __has_include | ||
| 7 | #define __has_include(s) 0 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__ | ||
| 11 | // GCC 4.4 provides <thread> | ||
| 12 | #ifndef _GLIBCXX_USE_SCHED_YIELD | ||
| 13 | #define _GLIBCXX_USE_SCHED_YIELD | ||
| 14 | #endif | ||
| 15 | #include <thread> | ||
| 16 | #elif __has_include(<thread>) && !ANDROID | ||
| 17 | // Clang + libc++ | ||
| 18 | #include <thread> | ||
| 19 | #else | ||
| 20 | |||
| 21 | // partial std::thread implementation for win32/pthread | ||
| 22 | |||
| 23 | #include <algorithm> | ||
| 24 | |||
| 25 | #if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__) | ||
| 26 | #define USE_RVALUE_REFERENCES | ||
| 27 | #endif | ||
| 28 | |||
| 29 | #ifdef __APPLE__ | ||
| 30 | #import <Foundation/NSAutoreleasePool.h> | ||
| 31 | #endif | ||
| 32 | |||
| 33 | #if defined(_WIN32) | ||
| 34 | // WIN32 | ||
| 35 | |||
| 36 | #define WIN32_LEAN_AND_MEAN | ||
| 37 | #include <Windows.h> | ||
| 38 | |||
| 39 | #if defined(_MSC_VER) && defined(_MT) | ||
| 40 | // When linking with LIBCMT (the multithreaded C library), Microsoft recommends | ||
| 41 | // using _beginthreadex instead of CreateThread. | ||
| 42 | #define USE_BEGINTHREADEX | ||
| 43 | #include <process.h> | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #ifdef USE_BEGINTHREADEX | ||
| 47 | #define THREAD_ID unsigned | ||
| 48 | #define THREAD_RETURN unsigned __stdcall | ||
| 49 | #else | ||
| 50 | #define THREAD_ID DWORD | ||
| 51 | #define THREAD_RETURN DWORD WINAPI | ||
| 52 | #endif | ||
| 53 | #define THREAD_HANDLE HANDLE | ||
| 54 | |||
| 55 | #else | ||
| 56 | // PTHREAD | ||
| 57 | |||
| 58 | #include <unistd.h> | ||
| 59 | |||
| 60 | #ifndef _POSIX_THREADS | ||
| 61 | #error unsupported platform (no pthreads?) | ||
| 62 | #endif | ||
| 63 | |||
| 64 | #include <pthread.h> | ||
| 65 | |||
| 66 | #define THREAD_ID pthread_t | ||
| 67 | #define THREAD_HANDLE pthread_t | ||
| 68 | #define THREAD_RETURN void* | ||
| 69 | |||
| 70 | #endif | ||
| 71 | |||
| 72 | namespace std | ||
| 73 | { | ||
| 74 | |||
| 75 | class thread | ||
| 76 | { | ||
| 77 | public: | ||
| 78 | typedef THREAD_HANDLE native_handle_type; | ||
| 79 | |||
| 80 | class id | ||
| 81 | { | ||
| 82 | friend class thread; | ||
| 83 | public: | ||
| 84 | id() : m_thread(0) {} | ||
| 85 | id(THREAD_ID _id) : m_thread(_id) {} | ||
| 86 | |||
| 87 | bool operator==(const id& rhs) const | ||
| 88 | { | ||
| 89 | return m_thread == rhs.m_thread; | ||
| 90 | } | ||
| 91 | |||
| 92 | bool operator!=(const id& rhs) const | ||
| 93 | { | ||
| 94 | return !(*this == rhs); | ||
| 95 | } | ||
| 96 | |||
| 97 | bool operator<(const id& rhs) const | ||
| 98 | { | ||
| 99 | return m_thread < rhs.m_thread; | ||
| 100 | } | ||
| 101 | |||
| 102 | private: | ||
| 103 | THREAD_ID m_thread; | ||
| 104 | }; | ||
| 105 | |||
| 106 | // no variadic template support in msvc | ||
| 107 | //template <typename C, typename... A> | ||
| 108 | //thread(C&& func, A&&... args); | ||
| 109 | |||
| 110 | template <typename C> | ||
| 111 | thread(C func) | ||
| 112 | { | ||
| 113 | StartThread(new Func<C>(func)); | ||
| 114 | } | ||
| 115 | |||
| 116 | template <typename C, typename A> | ||
| 117 | thread(C func, A arg) | ||
| 118 | { | ||
| 119 | StartThread(new FuncArg<C, A>(func, arg)); | ||
| 120 | } | ||
| 121 | |||
| 122 | thread() /*= default;*/ {} | ||
| 123 | |||
| 124 | #ifdef USE_RVALUE_REFERENCES | ||
| 125 | thread(const thread&) /*= delete*/; | ||
| 126 | |||
| 127 | thread(thread&& other) | ||
| 128 | { | ||
| 129 | #else | ||
| 130 | thread(const thread& t) | ||
| 131 | { | ||
| 132 | // ugly const_cast to get around lack of rvalue references | ||
| 133 | thread& other = const_cast<thread&>(t); | ||
| 134 | #endif | ||
| 135 | swap(other); | ||
| 136 | } | ||
| 137 | |||
| 138 | #ifdef USE_RVALUE_REFERENCES | ||
| 139 | thread& operator=(const thread&) /*= delete*/; | ||
| 140 | |||
| 141 | thread& operator=(thread&& other) | ||
| 142 | { | ||
| 143 | #else | ||
| 144 | thread& operator=(const thread& t) | ||
| 145 | { | ||
| 146 | // ugly const_cast to get around lack of rvalue references | ||
| 147 | thread& other = const_cast<thread&>(t); | ||
| 148 | #endif | ||
| 149 | if (joinable()) | ||
| 150 | detach(); | ||
| 151 | swap(other); | ||
| 152 | return *this; | ||
| 153 | } | ||
| 154 | |||
| 155 | ~thread() | ||
| 156 | { | ||
| 157 | if (joinable()) | ||
| 158 | detach(); | ||
| 159 | } | ||
| 160 | |||
| 161 | bool joinable() const | ||
| 162 | { | ||
| 163 | return m_id != id(); | ||
| 164 | } | ||
| 165 | |||
| 166 | id get_id() const | ||
| 167 | { | ||
| 168 | return m_id; | ||
| 169 | } | ||
| 170 | |||
| 171 | native_handle_type native_handle() | ||
| 172 | { | ||
| 173 | #ifdef _WIN32 | ||
| 174 | return m_handle; | ||
| 175 | #else | ||
| 176 | return m_id.m_thread; | ||
| 177 | #endif | ||
| 178 | } | ||
| 179 | |||
| 180 | void join() | ||
| 181 | { | ||
| 182 | #ifdef _WIN32 | ||
| 183 | WaitForSingleObject(m_handle, INFINITE); | ||
| 184 | detach(); | ||
| 185 | #else | ||
| 186 | pthread_join(m_id.m_thread, NULL); | ||
| 187 | m_id = id(); | ||
| 188 | #endif | ||
| 189 | } | ||
| 190 | |||
| 191 | void detach() | ||
| 192 | { | ||
| 193 | #ifdef _WIN32 | ||
| 194 | CloseHandle(m_handle); | ||
| 195 | #else | ||
| 196 | pthread_detach(m_id.m_thread); | ||
| 197 | #endif | ||
| 198 | m_id = id(); | ||
| 199 | } | ||
| 200 | |||
| 201 | void swap(thread& other) | ||
| 202 | { | ||
| 203 | std::swap(m_id, other.m_id); | ||
| 204 | #ifdef _WIN32 | ||
| 205 | std::swap(m_handle, other.m_handle); | ||
| 206 | #endif | ||
| 207 | } | ||
| 208 | |||
| 209 | static unsigned hardware_concurrency() | ||
| 210 | { | ||
| 211 | #ifdef _WIN32 | ||
| 212 | SYSTEM_INFO sysinfo; | ||
| 213 | GetSystemInfo(&sysinfo); | ||
| 214 | return static_cast<unsigned>(sysinfo.dwNumberOfProcessors); | ||
| 215 | #else | ||
| 216 | return 0; | ||
| 217 | #endif | ||
| 218 | } | ||
| 219 | |||
| 220 | private: | ||
| 221 | id m_id; | ||
| 222 | |||
| 223 | #ifdef _WIN32 | ||
| 224 | native_handle_type m_handle; | ||
| 225 | #endif | ||
| 226 | |||
| 227 | template <typename F> | ||
| 228 | void StartThread(F* param) | ||
| 229 | { | ||
| 230 | #ifdef USE_BEGINTHREADEX | ||
| 231 | m_handle = (HANDLE)_beginthreadex(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread); | ||
| 232 | #elif defined(_WIN32) | ||
| 233 | m_handle = CreateThread(NULL, 0, &RunAndDelete<F>, param, 0, &m_id.m_thread); | ||
| 234 | #else | ||
| 235 | pthread_attr_t attr; | ||
| 236 | pthread_attr_init(&attr); | ||
| 237 | pthread_attr_setstacksize(&attr, 1024 * 1024); | ||
| 238 | if (pthread_create(&m_id.m_thread, &attr, &RunAndDelete<F>, param)) | ||
| 239 | m_id = id(); | ||
| 240 | #endif | ||
| 241 | } | ||
| 242 | |||
| 243 | template <typename C> | ||
| 244 | class Func | ||
| 245 | { | ||
| 246 | public: | ||
| 247 | Func(C _func) : func(_func) {} | ||
| 248 | |||
| 249 | void Run() { func(); } | ||
| 250 | |||
| 251 | private: | ||
| 252 | C const func; | ||
| 253 | }; | ||
| 254 | |||
| 255 | template <typename C, typename A> | ||
| 256 | class FuncArg | ||
| 257 | { | ||
| 258 | public: | ||
| 259 | FuncArg(C _func, A _arg) : func(_func), arg(_arg) {} | ||
| 260 | |||
| 261 | void Run() { func(arg); } | ||
| 262 | |||
| 263 | private: | ||
| 264 | C const func; | ||
| 265 | A arg; | ||
| 266 | }; | ||
| 267 | |||
| 268 | template <typename F> | ||
| 269 | static THREAD_RETURN RunAndDelete(void* param) | ||
| 270 | { | ||
| 271 | #ifdef __APPLE__ | ||
| 272 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | ||
| 273 | #endif | ||
| 274 | static_cast<F*>(param)->Run(); | ||
| 275 | delete static_cast<F*>(param); | ||
| 276 | #ifdef __APPLE__ | ||
| 277 | [pool release]; | ||
| 278 | #endif | ||
| 279 | return 0; | ||
| 280 | } | ||
| 281 | }; | ||
| 282 | |||
| 283 | namespace this_thread | ||
| 284 | { | ||
| 285 | |||
| 286 | inline void yield() | ||
| 287 | { | ||
| 288 | #ifdef _WIN32 | ||
| 289 | SwitchToThread(); | ||
| 290 | #else | ||
| 291 | sleep(0); | ||
| 292 | #endif | ||
| 293 | } | ||
| 294 | |||
| 295 | inline thread::id get_id() | ||
| 296 | { | ||
| 297 | #ifdef _WIN32 | ||
| 298 | return GetCurrentThreadId(); | ||
| 299 | #else | ||
| 300 | return pthread_self(); | ||
| 301 | #endif | ||
| 302 | } | ||
| 303 | |||
| 304 | } // namespace this_thread | ||
| 305 | |||
| 306 | } // namespace std | ||
| 307 | |||
| 308 | #undef USE_RVALUE_REFERENCES | ||
| 309 | #undef USE_BEGINTHREADEX | ||
| 310 | #undef THREAD_ID | ||
| 311 | #undef THREAD_RETURN | ||
| 312 | #undef THREAD_HANDLE | ||
| 313 | |||
| 314 | #endif | ||
diff --git a/src/common/thread.cpp b/src/common/thread.cpp index 7341035c2..830795182 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp | |||
| @@ -9,10 +9,8 @@ | |||
| 9 | #include <mach/mach.h> | 9 | #include <mach/mach.h> |
| 10 | #elif defined(BSD4_4) || defined(__OpenBSD__) | 10 | #elif defined(BSD4_4) || defined(__OpenBSD__) |
| 11 | #include <pthread_np.h> | 11 | #include <pthread_np.h> |
| 12 | #endif | 12 | #elif defined(_WIN32) |
| 13 | 13 | #include <Windows.h> | |
| 14 | #ifdef USE_BEGINTHREADEX | ||
| 15 | #include <process.h> | ||
| 16 | #endif | 14 | #endif |
| 17 | 15 | ||
| 18 | namespace Common | 16 | namespace Common |
diff --git a/src/common/thread.h b/src/common/thread.h index dbb9da53b..f7ace21b4 100644 --- a/src/common/thread.h +++ b/src/common/thread.h | |||
| @@ -4,14 +4,13 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include "common/std_condition_variable.h" | ||
| 8 | #include "common/std_mutex.h" | ||
| 9 | #include "common/std_thread.h" | ||
| 10 | |||
| 11 | // Don't include common.h here as it will break LogManager | 7 | // Don't include common.h here as it will break LogManager |
| 12 | #include "common/common_types.h" | 8 | #include "common/common_types.h" |
| 13 | #include <cstdio> | 9 | #include <cstdio> |
| 14 | #include <cstring> | 10 | #include <cstring> |
| 11 | #include <thread> | ||
| 12 | #include <condition_variable> | ||
| 13 | #include <mutex> | ||
| 15 | 14 | ||
| 16 | // This may not be defined outside _WIN32 | 15 | // This may not be defined outside _WIN32 |
| 17 | #ifndef _WIN32 | 16 | #ifndef _WIN32 |
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index c30e36732..25fccce76 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp | |||
| @@ -5,9 +5,9 @@ | |||
| 5 | #include <vector> | 5 | #include <vector> |
| 6 | #include <cstdio> | 6 | #include <cstdio> |
| 7 | #include <atomic> | 7 | #include <atomic> |
| 8 | #include <mutex> | ||
| 8 | 9 | ||
| 9 | #include "common/msg_handler.h" | 10 | #include "common/msg_handler.h" |
| 10 | #include "common/std_mutex.h" | ||
| 11 | #include "common/chunk_file.h" | 11 | #include "common/chunk_file.h" |
| 12 | 12 | ||
| 13 | #include "core/core_timing.h" | 13 | #include "core/core_timing.h" |