summaryrefslogtreecommitdiff
path: root/src/common/thread.h
diff options
context:
space:
mode:
authorGravatar MerryMage2016-04-14 12:54:06 +0100
committerGravatar MerryMage2016-04-14 13:59:58 +0100
commit3c710f9b104acf218bce1054bd325f9a2515f8ca (patch)
treec94ba9cccdc9bb0e36d5ad3f95880e239762006a /src/common/thread.h
parentcommon/thread: Correct code style (diff)
downloadyuzu-3c710f9b104acf218bce1054bd325f9a2515f8ca.tar.gz
yuzu-3c710f9b104acf218bce1054bd325f9a2515f8ca.tar.xz
yuzu-3c710f9b104acf218bce1054bd325f9a2515f8ca.zip
Thread: Make Barrier reusable
Diffstat (limited to 'src/common/thread.h')
-rw-r--r--src/common/thread.h10
1 files changed, 5 insertions, 5 deletions
diff --git a/src/common/thread.h b/src/common/thread.h
index 9bd0e8e7f..bbfa8befa 100644
--- a/src/common/thread.h
+++ b/src/common/thread.h
@@ -69,20 +69,19 @@ private:
69 69
70class Barrier { 70class Barrier {
71public: 71public:
72 explicit Barrier(size_t count_) : count(count_), waiting(0) {} 72 explicit Barrier(size_t count_) : count(count_), waiting(0), generation(0) {}
73 73
74 /// Blocks until all "count" threads have called Sync() 74 /// Blocks until all "count" threads have called Sync()
75 void Sync() { 75 void Sync() {
76 std::unique_lock<std::mutex> lk(mutex); 76 std::unique_lock<std::mutex> lk(mutex);
77 77 const size_t current_generation = generation;
78 // TODO: broken when next round of Sync()s
79 // is entered before all waiting threads return from the notify_all
80 78
81 if (++waiting == count) { 79 if (++waiting == count) {
80 generation++;
82 waiting = 0; 81 waiting = 0;
83 condvar.notify_all(); 82 condvar.notify_all();
84 } else { 83 } else {
85 condvar.wait(lk, [&]{ return waiting == 0; }); 84 condvar.wait(lk, [this, current_generation]{ return current_generation != generation; });
86 } 85 }
87 } 86 }
88 87
@@ -91,6 +90,7 @@ private:
91 std::mutex mutex; 90 std::mutex mutex;
92 const size_t count; 91 const size_t count;
93 size_t waiting; 92 size_t waiting;
93 size_t generation; // Incremented once each time the barrier is used
94}; 94};
95 95
96void SleepCurrentThread(int ms); 96void SleepCurrentThread(int ms);