Add timeout to Semaphore::wait

This commit is contained in:
Vinnie Falco
2013-07-29 13:19:53 -07:00
parent 1fdaa6781b
commit cfa5a3b9ca
2 changed files with 33 additions and 21 deletions

View File

@@ -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!

View File

@@ -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: