Tidy up contract checks in InterruptibleThread

This commit is contained in:
Vinnie Falco
2013-09-07 08:48:52 -07:00
parent e927160ff5
commit 4a30b259b2

View File

@@ -66,57 +66,56 @@ void InterruptibleThread::join ()
m_thread.stopThread (-1); m_thread.stopThread (-1);
} }
// Block until there is an interruption.
// This counts as an interruption point.
//
void InterruptibleThread::wait () void InterruptibleThread::wait ()
{ {
// Can only be called from the corresponding thread of execution. // Can only be called from the thread of execution.
//
bassert (isTheCurrentThread ()); bassert (isTheCurrentThread ());
bool interrupted = false;
for (;;) for (;;)
{ {
// Impossible for us to already be in the wait state.
bassert (m_state != stateWait); bassert (m_state != stateWait);
// See if we are interrupted // See if we are interrupted.
//
if (m_state.tryChangeState (stateInterrupt, stateRun)) if (m_state.tryChangeState (stateInterrupt, stateRun))
{ {
// We were interrupted, state is changed to Run. Caller must run now. // We were interrupted, so the wait is satisfied.
// return;
interrupted = true;
break;
} }
else if (m_state.tryChangeState (stateRun, stateWait)) else
{ {
// Transitioned to wait. Caller must wait now. // Try to get into the wait state.
// if (m_state.tryChangeState (stateRun, stateWait))
interrupted = false; {
break; bassert (m_state == stateWait);
// Got into the wait state so block until interrupt.
m_thread.wait ();
// Event is signalled means we were
// interrupted, so the wait is satisfied.
bassert (m_state != stateWait || m_thread.threadShouldExit ());
return;
}
} }
} }
if (! interrupted)
{
bassert (m_state == stateWait);
m_thread.wait ();
// The event became signalled.
//
bassert (m_state == stateRun || m_state == stateInterrupt);
}
} }
void InterruptibleThread::interrupt () void InterruptibleThread::interrupt ()
{ {
for (;;) for (;;)
{ {
int const state = m_state; int const state (m_state);
if (state == stateInterrupt || if (state == stateInterrupt ||
m_state.tryChangeState (stateRun, stateInterrupt)) m_state.tryChangeState (stateRun, stateInterrupt))
{ {
// We got into the interrupt state, the thead
// will see this at the next interruption point.
//
// Thread will see this at next interruption point. // Thread will see this at next interruption point.
// //
break; break;
@@ -129,16 +128,18 @@ void InterruptibleThread::interrupt ()
} }
} }
// Returns true if the thead function should stop
// its activities as soon as possible and return.
//
bool InterruptibleThread::interruptionPoint () bool InterruptibleThread::interruptionPoint ()
{ {
// Can only be called from the thread of execution. // Can only be called from the thread of execution.
//
bassert (isTheCurrentThread ()); bassert (isTheCurrentThread ());
// It is impossible for this function to be called while in the wait state. // Impossible for this to be called in the wait state.
check_precondition (m_state != stateWait); bassert (m_state != stateWait);
bool const interrupted = m_state.tryChangeState (stateInterrupt, stateRun); bool const interrupted (m_state.tryChangeState (stateInterrupt, stateRun));
return interrupted; return interrupted;
} }