From 48bd904028b1bc4bc89c76e18b57a95ad27a1184 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Mon, 14 Apr 2014 01:42:03 +0200 Subject: [PATCH] Common::Event: Implement in terms of Common::Flag to get rid of a volatile and optimize with early returns and atomic swaps --- Source/Core/Common/Event.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Source/Core/Common/Event.h b/Source/Core/Common/Event.h index 1203eb38c0..8499fdb1e8 100644 --- a/Source/Core/Common/Event.h +++ b/Source/Core/Common/Event.h @@ -12,6 +12,7 @@ #pragma once +#include "Common/Flag.h" #include "Common/StdConditionVariable.h" #include "Common/StdMutex.h" @@ -20,38 +21,35 @@ namespace Common { class Event { public: - Event() - : is_set(false) - {} - void Set() { - std::lock_guard lk(m_mutex); - if (!is_set) + if (m_flag.TestAndSet()) { - is_set = true; + std::lock_guard lk(m_mutex); m_condvar.notify_one(); } } void Wait() { + if (m_flag.TestAndClear()) + return; + std::unique_lock lk(m_mutex); - m_condvar.wait(lk, [&]{ return is_set; }); - is_set = false; + m_condvar.wait(lk, [&]{ return m_flag.IsSet(); }); + m_flag.Clear(); } void Reset() { - std::unique_lock lk(m_mutex); // no other action required, since wait loops on // the predicate and any lingering signal will get // cleared on the first iteration - is_set = false; + m_flag.Clear(); } private: - volatile bool is_set; + Flag m_flag; std::condition_variable m_condvar; std::mutex m_mutex; };