From 658d5b6f71d3a275f9977733aa07b6ff0d2c6348 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 20 Aug 2014 20:15:52 -0700 Subject: [PATCH] This version of hsEvent more accurately matches the previous (Win32) behavior, and also serves as a proper binary semaphore. --- Sources/Plasma/CoreLib/hsThread.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsThread.h b/Sources/Plasma/CoreLib/hsThread.h index 59514e18..838961b0 100644 --- a/Sources/Plasma/CoreLib/hsThread.h +++ b/Sources/Plasma/CoreLib/hsThread.h @@ -175,19 +175,34 @@ class hsEvent { std::mutex fMutex; std::condition_variable fCondition; + bool fEvent; public: - hsEvent() { } + hsEvent() : fEvent(false) { } inline void Wait() { std::unique_lock lock(fMutex); - fCondition.wait(lock); + fCondition.wait(lock, [this]() { return fEvent; }); + fEvent = false; + } + + template + inline bool Wait(const std::chrono::duration<_Rep, _Period> &duration) + { + std::unique_lock lock(fMutex); + + bool result = fCondition.wait_for(lock, duration, [this]() { return fEvent; }); + if (result) + fEvent = false; + + return result; } inline void Signal() { std::unique_lock lock(fMutex); + fEvent = true; fCondition.notify_one(); } };