diff options
Diffstat (limited to 'src/common/thread.h')
| -rw-r--r-- | src/common/thread.h | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/src/common/thread.h b/src/common/thread.h index bbfa8befa..b189dc764 100644 --- a/src/common/thread.h +++ b/src/common/thread.h | |||
| @@ -4,10 +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> |
| 10 | #include <thread> | ||
| 11 | 11 | ||
| 12 | #include "common/common_types.h" | 12 | #include "common/common_types.h" |
| 13 | 13 | ||
| @@ -17,17 +17,17 @@ | |||
| 17 | // backwards compat support. | 17 | // backwards compat support. |
| 18 | // WARNING: This only works correctly with POD types. | 18 | // WARNING: This only works correctly with POD types. |
| 19 | #if defined(__clang__) | 19 | #if defined(__clang__) |
| 20 | # if !__has_feature(cxx_thread_local) | 20 | #if !__has_feature(cxx_thread_local) |
| 21 | # define thread_local __thread | 21 | #define thread_local __thread |
| 22 | # endif | 22 | #endif |
| 23 | #elif defined(__GNUC__) | 23 | #elif defined(__GNUC__) |
| 24 | # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8) | 24 | #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8) |
| 25 | # define thread_local __thread | 25 | #define thread_local __thread |
| 26 | # endif | 26 | #endif |
| 27 | #elif defined(_MSC_VER) | 27 | #elif defined(_MSC_VER) |
| 28 | # if _MSC_VER < 1900 | 28 | #if _MSC_VER < 1900 |
| 29 | # define thread_local __declspec(thread) | 29 | #define thread_local __declspec(thread) |
| 30 | # endif | 30 | #endif |
| 31 | #endif | 31 | #endif |
| 32 | 32 | ||
| 33 | namespace Common { | 33 | namespace Common { |
| @@ -39,7 +39,8 @@ void SetCurrentThreadAffinity(u32 mask); | |||
| 39 | 39 | ||
| 40 | class Event { | 40 | class Event { |
| 41 | public: | 41 | public: |
| 42 | Event() : is_set(false) {} | 42 | Event() : is_set(false) { |
| 43 | } | ||
| 43 | 44 | ||
| 44 | void Set() { | 45 | void Set() { |
| 45 | std::lock_guard<std::mutex> lk(mutex); | 46 | std::lock_guard<std::mutex> lk(mutex); |
| @@ -51,13 +52,14 @@ public: | |||
| 51 | 52 | ||
| 52 | void Wait() { | 53 | void Wait() { |
| 53 | std::unique_lock<std::mutex> lk(mutex); | 54 | std::unique_lock<std::mutex> lk(mutex); |
| 54 | condvar.wait(lk, [&]{ return is_set; }); | 55 | condvar.wait(lk, [&] { return is_set; }); |
| 55 | is_set = false; | 56 | is_set = false; |
| 56 | } | 57 | } |
| 57 | 58 | ||
| 58 | void Reset() { | 59 | void Reset() { |
| 59 | std::unique_lock<std::mutex> lk(mutex); | 60 | 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 | 61 | // no other action required, since wait loops on the predicate and any lingering signal will |
| 62 | // get cleared on the first iteration | ||
| 61 | is_set = false; | 63 | is_set = false; |
| 62 | } | 64 | } |
| 63 | 65 | ||
| @@ -69,7 +71,8 @@ private: | |||
| 69 | 71 | ||
| 70 | class Barrier { | 72 | class Barrier { |
| 71 | public: | 73 | public: |
| 72 | explicit Barrier(size_t count_) : count(count_), waiting(0), generation(0) {} | 74 | explicit Barrier(size_t count_) : count(count_), waiting(0), generation(0) { |
| 75 | } | ||
| 73 | 76 | ||
| 74 | /// Blocks until all "count" threads have called Sync() | 77 | /// Blocks until all "count" threads have called Sync() |
| 75 | void Sync() { | 78 | void Sync() { |
| @@ -81,7 +84,8 @@ public: | |||
| 81 | waiting = 0; | 84 | waiting = 0; |
| 82 | condvar.notify_all(); | 85 | condvar.notify_all(); |
| 83 | } else { | 86 | } else { |
| 84 | condvar.wait(lk, [this, current_generation]{ return current_generation != generation; }); | 87 | condvar.wait(lk, |
| 88 | [this, current_generation] { return current_generation != generation; }); | ||
| 85 | } | 89 | } |
| 86 | } | 90 | } |
| 87 | 91 | ||
| @@ -94,7 +98,7 @@ private: | |||
| 94 | }; | 98 | }; |
| 95 | 99 | ||
| 96 | void SleepCurrentThread(int ms); | 100 | void SleepCurrentThread(int ms); |
| 97 | void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms | 101 | void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms |
| 98 | 102 | ||
| 99 | // Use this function during a spin-wait to make the current thread | 103 | // Use this function during a spin-wait to make the current thread |
| 100 | // relax while another thread is working. This may be more efficient | 104 | // relax while another thread is working. This may be more efficient |
| @@ -103,6 +107,6 @@ inline void YieldCPU() { | |||
| 103 | std::this_thread::yield(); | 107 | std::this_thread::yield(); |
| 104 | } | 108 | } |
| 105 | 109 | ||
| 106 | void SetCurrentThreadName(const char *name); | 110 | void SetCurrentThreadName(const char* name); |
| 107 | 111 | ||
| 108 | } // namespace Common | 112 | } // namespace Common |