diff --git a/Source/Core/Common/Event.h b/Source/Core/Common/Event.h index d2dc29a086..d86f501431 100644 --- a/Source/Core/Common/Event.h +++ b/Source/Core/Common/Event.h @@ -16,6 +16,7 @@ #include #endif +#include #include #include @@ -48,6 +49,20 @@ public: m_flag.Clear(); } + template + bool WaitFor(const std::chrono::duration& rel_time) + { + if (m_flag.TestAndClear()) + return true; + + std::unique_lock lk(m_mutex); + bool signaled = m_condvar.wait_for(lk, rel_time, + [&]{ return m_flag.IsSet(); }); + m_flag.Clear(); + + return signaled; + } + void Reset() { // no other action required, since wait loops on @@ -65,9 +80,31 @@ private: class Event final { public: - void Set() { m_event.set(); } - void Wait() { m_event.wait(); m_event.reset(); } - void Reset() { m_event.reset(); } + void Set() + { + m_event.set(); + } + + void Wait() + { + m_event.wait(); + m_event.reset(); + } + + template + bool WaitFor(const std::chrono::duration& rel_time) + { + bool signaled = m_event.wait( + (u32)std::chrono::duration_cast(rel_time).count() + ) == 0; + m_event.reset(); + return signaled; + } + + void Reset() + { + m_event.reset(); + } private: concurrency::event m_event;