diff options
Diffstat (limited to 'src/common/thread.h')
| -rw-r--r-- | src/common/thread.h | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/src/common/thread.h b/src/common/thread.h index bbfa8befa..9c08be7e3 100644 --- a/src/common/thread.h +++ b/src/common/thread.h | |||
| @@ -4,11 +4,10 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <cstddef> | ||
| 8 | #include <thread> | ||
| 9 | #include <condition_variable> | 7 | #include <condition_variable> |
| 8 | #include <cstddef> | ||
| 10 | #include <mutex> | 9 | #include <mutex> |
| 11 | 10 | #include <thread> | |
| 12 | #include "common/common_types.h" | 11 | #include "common/common_types.h" |
| 13 | 12 | ||
| 14 | // Support for C++11's thread_local keyword was surprisingly spotty in compilers until very | 13 | // Support for C++11's thread_local keyword was surprisingly spotty in compilers until very |
| @@ -17,17 +16,17 @@ | |||
| 17 | // backwards compat support. | 16 | // backwards compat support. |
| 18 | // WARNING: This only works correctly with POD types. | 17 | // WARNING: This only works correctly with POD types. |
| 19 | #if defined(__clang__) | 18 | #if defined(__clang__) |
| 20 | # if !__has_feature(cxx_thread_local) | 19 | #if !__has_feature(cxx_thread_local) |
| 21 | # define thread_local __thread | 20 | #define thread_local __thread |
| 22 | # endif | 21 | #endif |
| 23 | #elif defined(__GNUC__) | 22 | #elif defined(__GNUC__) |
| 24 | # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8) | 23 | #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8) |
| 25 | # define thread_local __thread | 24 | #define thread_local __thread |
| 26 | # endif | 25 | #endif |
| 27 | #elif defined(_MSC_VER) | 26 | #elif defined(_MSC_VER) |
| 28 | # if _MSC_VER < 1900 | 27 | #if _MSC_VER < 1900 |
| 29 | # define thread_local __declspec(thread) | 28 | #define thread_local __declspec(thread) |
| 30 | # endif | 29 | #endif |
| 31 | #endif | 30 | #endif |
| 32 | 31 | ||
| 33 | namespace Common { | 32 | namespace Common { |
| @@ -51,13 +50,14 @@ public: | |||
| 51 | 50 | ||
| 52 | void Wait() { | 51 | void Wait() { |
| 53 | std::unique_lock<std::mutex> lk(mutex); | 52 | std::unique_lock<std::mutex> lk(mutex); |
| 54 | condvar.wait(lk, [&]{ return is_set; }); | 53 | condvar.wait(lk, [&] { return is_set; }); |
| 55 | is_set = false; | 54 | is_set = false; |
| 56 | } | 55 | } |
| 57 | 56 | ||
| 58 | void Reset() { | 57 | void Reset() { |
| 59 | std::unique_lock<std::mutex> lk(mutex); | 58 | std::unique_lock<std::mutex> lk(mutex); |
| 60 | // no other action required, since wait loops on the predicate and any lingering signal will get cleared on the first iteration | 59 | // no other action required, since wait loops on the predicate and any lingering signal will |
| 60 | // get cleared on the first iteration | ||
| 61 | is_set = false; | 61 | is_set = false; |
| 62 | } | 62 | } |
| 63 | 63 | ||
| @@ -81,7 +81,8 @@ public: | |||
| 81 | waiting = 0; | 81 | waiting = 0; |
| 82 | condvar.notify_all(); | 82 | condvar.notify_all(); |
| 83 | } else { | 83 | } else { |
| 84 | condvar.wait(lk, [this, current_generation]{ return current_generation != generation; }); | 84 | condvar.wait(lk, |
| 85 | [this, current_generation] { return current_generation != generation; }); | ||
| 85 | } | 86 | } |
| 86 | } | 87 | } |
| 87 | 88 | ||
| @@ -94,7 +95,7 @@ private: | |||
| 94 | }; | 95 | }; |
| 95 | 96 | ||
| 96 | void SleepCurrentThread(int ms); | 97 | void SleepCurrentThread(int ms); |
| 97 | void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms | 98 | void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms |
| 98 | 99 | ||
| 99 | // Use this function during a spin-wait to make the current thread | 100 | // Use this function during a spin-wait to make the current thread |
| 100 | // relax while another thread is working. This may be more efficient | 101 | // relax while another thread is working. This may be more efficient |
| @@ -103,6 +104,6 @@ inline void YieldCPU() { | |||
| 103 | std::this_thread::yield(); | 104 | std::this_thread::yield(); |
| 104 | } | 105 | } |
| 105 | 106 | ||
| 106 | void SetCurrentThreadName(const char *name); | 107 | void SetCurrentThreadName(const char* name); |
| 107 | 108 | ||
| 108 | } // namespace Common | 109 | } // namespace Common |