From cfa5a3b9cae953c16eaf551cdb5d27b3bfe69aa2 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 29 Jul 2013 13:19:53 -0700 Subject: [PATCH] Add timeout to Semaphore::wait --- .../beast_basics/threads/beast_Semaphore.cpp | 46 +++++++++++-------- .../beast_basics/threads/beast_Semaphore.h | 8 +++- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/Subtrees/beast/modules/beast_basics/threads/beast_Semaphore.cpp b/Subtrees/beast/modules/beast_basics/threads/beast_Semaphore.cpp index 9ca4d986e..bf97ee516 100644 --- a/Subtrees/beast/modules/beast_basics/threads/beast_Semaphore.cpp +++ b/Subtrees/beast/modules/beast_basics/threads/beast_Semaphore.cpp @@ -17,23 +17,6 @@ */ //============================================================================== -Semaphore::WaitingThread::WaitingThread () - : m_event (false) // auto-reset -{ -} - -void Semaphore::WaitingThread::wait () -{ - m_event.wait (); -} - -void Semaphore::WaitingThread::signal () -{ - m_event.signal (); -} - -//============================================================================== - Semaphore::Semaphore (int initialCount) : m_counter (initialCount) { @@ -75,8 +58,10 @@ void Semaphore::signal (int amount) } } -void Semaphore::wait () +bool Semaphore::wait (int timeOutMilliseconds) { + bool signaled = true; + // Always prepare the WaitingThread object first, either // from the delete list or through a new allocation. // @@ -107,11 +92,34 @@ void Semaphore::wait () if (waitingThread != nullptr) { // Yes so do it. - waitingThread->wait (); + signaled = waitingThread->wait (timeOutMilliseconds); // If the wait is satisfied, then we've been taken off the // waiting list so put waitingThread back in the delete list. // m_deleteList.push_front (waitingThread); } + + return signaled; } + +//------------------------------------------------------------------------------ + +Semaphore::WaitingThread::WaitingThread () + : m_event (false) // auto-reset +{ +} + +bool Semaphore::WaitingThread::wait (int timeOutMilliseconds) +{ + return m_event.wait (timeOutMilliseconds); +} + +void Semaphore::WaitingThread::signal () +{ + m_event.signal (); +} + +//------------------------------------------------------------------------------ + +// VFALCO TODO Unit Tests! diff --git a/Subtrees/beast/modules/beast_basics/threads/beast_Semaphore.h b/Subtrees/beast/modules/beast_basics/threads/beast_Semaphore.h index de78a0a1e..9990757ee 100644 --- a/Subtrees/beast/modules/beast_basics/threads/beast_Semaphore.h +++ b/Subtrees/beast/modules/beast_basics/threads/beast_Semaphore.h @@ -49,8 +49,12 @@ public: void signal (int amount = 1); /** Wait for a resource. + + A negative time-out value means that the method will wait indefinitely. + + @returns true if the event has been signalled, false if the timeout expires. */ - void wait (); + bool wait (int timeOutMilliseconds = -1); private: class WaitingThread @@ -60,7 +64,7 @@ private: public: WaitingThread (); - void wait (); + bool wait (int timeOutMilliseconds); void signal (); private: