diff --git a/Common/Thread/Waitable.h b/Common/Thread/Waitable.h index 9008b2fa21..dcceefd046 100644 --- a/Common/Thread/Waitable.h +++ b/Common/Thread/Waitable.h @@ -1,7 +1,8 @@ #pragma once -#include +#include #include +#include #include "Common/Thread/ThreadManager.h" @@ -11,22 +12,29 @@ public: triggered_ = false; } + ~LimitedWaitable() { + // Make sure no one is still waiting, and any notify lock is released. + Notify(); + } + void Wait() override { + if (triggered_) + return; + std::unique_lock lock(mutex_); - if (!triggered_) { - cond_.wait(lock, [&] { return triggered_.load(); }); - } + cond_.wait(lock, [&] { return triggered_.load(); }); } bool WaitFor(double budget) { + if (triggered_) + return true; + uint32_t us = budget > 0 ? (uint32_t)(budget * 1000000.0) : 0; + if (us == 0) + return false; + std::unique_lock lock(mutex_); - if (!triggered_) { - if (us == 0) - return false; - cond_.wait_for(lock, std::chrono::microseconds(us), [&] { return triggered_.load(); }); - } - return triggered_; + return cond_.wait_for(lock, std::chrono::microseconds(us), [&] { return triggered_.load(); }); } void Notify() {