From 82472d09acacba9fcf31765e4abbfddd3ffbbcd1 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 23 Oct 2014 11:20:25 +0200 Subject: Reimplemented cEvent using C++11 primitives. Fixes #1523. --- src/OSSupport/Event.h | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'src/OSSupport/Event.h') diff --git a/src/OSSupport/Event.h b/src/OSSupport/Event.h index e2fa65a05..5818115be 100644 --- a/src/OSSupport/Event.h +++ b/src/OSSupport/Event.h @@ -1,16 +1,17 @@ // Event.h -// Interfaces to the cEvent object representing an OS-specific synchronization primitive that can be waited-for -// Implemented as an Event on Win and as a 1-semaphore on *nix +// Interfaces to the cEvent object representing a synchronization primitive that can be waited-for +// Implemented using C++11 condition variable and mutex #pragma once -#ifndef CEVENT_H_INCLUDED -#define CEVENT_H_INCLUDED + +#include +#include @@ -20,9 +21,13 @@ class cEvent { public: cEvent(void); - ~cEvent(); + /** Waits until the event has been set. + If the event has been set before it has been waited for, Wait() returns immediately. */ void Wait(void); + + /** Sets the event - releases one thread that has been waiting in Wait(). + If there was no thread waiting, the next call to Wait() will not block. */ void Set (void); /** Waits for the event until either it is signalled, or the (relative) timeout is passed. @@ -31,20 +36,16 @@ public: private: - #ifdef _WIN32 - HANDLE m_Event; - #else - sem_t * m_Event; - bool m_bIsNamed; - #endif -} ; - - - + /** Used for checking for spurious wakeups. */ + bool m_ShouldWait; + /** Mutex protecting m_ShouldWait from multithreaded access. */ + std::mutex m_Mutex; + /** The condition variable used as the Event. */ + std::condition_variable m_CondVar; +} ; -#endif // CEVENT_H_INCLUDED -- cgit v1.2.3 From c65bb6341dfc25ae937bd12c9e41855fb27fdccb Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 7 Dec 2014 21:37:47 +0100 Subject: Fixed integer overflow problems. The event would overflow when requesting a 60 minute timeout. --- src/OSSupport/Event.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/OSSupport/Event.h') diff --git a/src/OSSupport/Event.h b/src/OSSupport/Event.h index 5818115be..572388a3f 100644 --- a/src/OSSupport/Event.h +++ b/src/OSSupport/Event.h @@ -32,7 +32,7 @@ public: /** Waits for the event until either it is signalled, or the (relative) timeout is passed. Returns true if the event was signalled, false if the timeout was hit or there was an error. */ - bool Wait(int a_TimeoutMSec); + bool Wait(unsigned a_TimeoutMSec); private: -- cgit v1.2.3