Fix DeadlineTimer for InterruptibleThread::wait timeout

This commit is contained in:
Vinnie Falco
2013-09-06 21:18:48 -07:00
parent a56c01d044
commit 718569d6a1
3 changed files with 12 additions and 57 deletions

View File

@@ -170,7 +170,6 @@ public:
// is extremely short, or if a listener wastes too much time in // is extremely short, or if a listener wastes too much time in
// their callback. // their callback.
} }
}
} }
} }

View File

@@ -66,7 +66,7 @@ void InterruptibleThread::join ()
m_thread.stopThread (-1); m_thread.stopThread (-1);
} }
bool InterruptibleThread::wait (int milliSeconds) void InterruptibleThread::wait ()
{ {
// Can only be called from the corresponding thread of execution. // Can only be called from the corresponding thread of execution.
// //
@@ -87,8 +87,7 @@ bool InterruptibleThread::wait (int milliSeconds)
interrupted = true; interrupted = true;
break; break;
} }
else if (m_state.tryChangeState (stateRun, stateWait) || else if (m_state.tryChangeState (stateRun, stateWait))
m_state.tryChangeState (stateReturn, stateWait))
{ {
// Transitioned to wait. Caller must wait now. // Transitioned to wait. Caller must wait now.
// //
@@ -101,33 +100,12 @@ bool InterruptibleThread::wait (int milliSeconds)
{ {
bassert (m_state == stateWait); bassert (m_state == stateWait);
interrupted = m_thread.wait (milliSeconds); m_thread.wait ();
if (! interrupted) // The event became signalled.
{ //
// The wait timed out bassert (m_state == stateRun || m_state == stateInterrupt);
//
if (m_state.tryChangeState (stateWait, stateRun))
{
interrupted = false;
}
else
{
bassert (m_state == stateInterrupt);
interrupted = true;
}
}
else
{
// The event became signalled, which can only
// happen via m_event.notify() in interrupt()
//
bassert (m_state == stateRun);
}
} }
return interrupted;
} }
void InterruptibleThread::interrupt () void InterruptibleThread::interrupt ()
@@ -137,8 +115,7 @@ void InterruptibleThread::interrupt ()
int const state = m_state; int const state = m_state;
if (state == stateInterrupt || if (state == stateInterrupt ||
state == stateReturn || m_state.tryChangeState (stateRun, stateInterrupt))
m_state.tryChangeState (stateRun, stateInterrupt))
{ {
// Thread will see this at next interruption point. // Thread will see this at next interruption point.
// //
@@ -158,19 +135,8 @@ bool InterruptibleThread::interruptionPoint ()
// //
bassert (isTheCurrentThread ()); bassert (isTheCurrentThread ());
if (m_state == stateWait) // It is impossible for this function to be called while in the wait state.
{ check_precondition (m_state != stateWait);
// It is impossible for this function to be called while in the wait state.
//
Throw (Error ().fail (__FILE__, __LINE__));
}
else if (m_state == stateReturn)
{
// If this goes off it means the thread called the
// interruption a second time after already getting interrupted.
//
Throw (Error ().fail (__FILE__, __LINE__));
}
bool const interrupted = m_state.tryChangeState (stateInterrupt, stateRun); bool const interrupted = m_state.tryChangeState (stateInterrupt, stateRun);

View File

@@ -71,20 +71,11 @@ public:
*/ */
void join (); void join ();
/** Wait for interrupt or timeout. /** Wait for interrupt.
This call blocks until the thread is interrupted.
This call blocks until the thread is interrupted, or until the timeout
expires if milliSeconds is non-negative.
May only be called by the thread of execution. May only be called by the thread of execution.
@param milliSeconds The amount of time to wait. Negative values mean
no timeout.
@return `true` if the interrupt occurred, or `false` if the
timeout expired.
*/ */
bool wait (int milliSeconds = -1); void wait ();
/** Interrupt the thread of execution. /** Interrupt the thread of execution.
@@ -159,7 +150,6 @@ private:
{ {
stateRun, stateRun,
stateInterrupt, stateInterrupt,
stateReturn,
stateWait stateWait
}; };