diff options
author | worktycho <work.tycho@gmail.com> | 2015-07-04 15:43:00 +0200 |
---|---|---|
committer | worktycho <work.tycho@gmail.com> | 2015-07-04 15:43:00 +0200 |
commit | 106e06617afea6e07e99c7674b2d10ece96b1344 (patch) | |
tree | bc1237dd50c603f1f5e806574d8496f4f361b29c /src/OSSupport/Event.cpp | |
parent | Merge pull request #2312 from SamJBarney/master (diff) | |
parent | Fixed minor errors in Tycho's code (diff) | |
download | cuberite-106e06617afea6e07e99c7674b2d10ece96b1344.tar cuberite-106e06617afea6e07e99c7674b2d10ece96b1344.tar.gz cuberite-106e06617afea6e07e99c7674b2d10ece96b1344.tar.bz2 cuberite-106e06617afea6e07e99c7674b2d10ece96b1344.tar.lz cuberite-106e06617afea6e07e99c7674b2d10ece96b1344.tar.xz cuberite-106e06617afea6e07e99c7674b2d10ece96b1344.tar.zst cuberite-106e06617afea6e07e99c7674b2d10ece96b1344.zip |
Diffstat (limited to 'src/OSSupport/Event.cpp')
-rw-r--r-- | src/OSSupport/Event.cpp | 56 |
1 files changed, 21 insertions, 35 deletions
diff --git a/src/OSSupport/Event.cpp b/src/OSSupport/Event.cpp index 38144ead3..4c2adea3c 100644 --- a/src/OSSupport/Event.cpp +++ b/src/OSSupport/Event.cpp @@ -13,7 +13,7 @@ cEvent::cEvent(void) : - m_ShouldWait(true) + m_ShouldContinue(false) { } @@ -23,12 +23,11 @@ cEvent::cEvent(void) : void cEvent::Wait(void) { - std::unique_lock<std::mutex> Lock(m_Mutex); - while (m_ShouldWait) { - m_CondVar.wait(Lock); + std::unique_lock<std::mutex> Lock(m_Mutex); + m_CondVar.wait(Lock, [this](){ return m_ShouldContinue.load(); }); } - m_ShouldWait = true; + m_ShouldContinue = false; } @@ -38,33 +37,13 @@ void cEvent::Wait(void) bool cEvent::Wait(unsigned a_TimeoutMSec) { auto dst = std::chrono::system_clock::now() + std::chrono::milliseconds(a_TimeoutMSec); - std::unique_lock<std::mutex> Lock(m_Mutex); // We assume that this lock is acquired without much delay - we are the only user of the mutex - while (m_ShouldWait && (std::chrono::system_clock::now() <= dst)) + bool Result; { - switch (m_CondVar.wait_until(Lock, dst)) - { - case std::cv_status::no_timeout: - { - // The wait was successful, check for spurious wakeup: - if (!m_ShouldWait) - { - m_ShouldWait = true; - return true; - } - // This was a spurious wakeup, wait again: - continue; - } - - case std::cv_status::timeout: - { - // The wait timed out, return failure: - return false; - } - } // switch (wait_until()) - } // while (m_ShouldWait && not timeout) - - // The wait timed out in the while condition: - return false; + std::unique_lock<std::mutex> Lock(m_Mutex); // We assume that this lock is acquired without much delay - we are the only user of the mutex + Result = m_CondVar.wait_until(Lock, dst, [this](){ return m_ShouldContinue.load(); }); + } + m_ShouldContinue = false; + return Result; } @@ -73,13 +52,20 @@ bool cEvent::Wait(unsigned a_TimeoutMSec) void cEvent::Set(void) { - { - std::unique_lock<std::mutex> Lock(m_Mutex); - m_ShouldWait = false; - } + m_ShouldContinue = true; m_CondVar.notify_one(); } +void cEvent::SetAll(void) +{ + m_ShouldContinue = true; + m_CondVar.notify_all(); +} + + + + + |