diff --git a/Subtrees/beast/modules/beast_core/thread/beast_InterruptibleThread.cpp b/Subtrees/beast/modules/beast_core/thread/beast_InterruptibleThread.cpp index f0d1f07062..3f3c233b3f 100644 --- a/Subtrees/beast/modules/beast_core/thread/beast_InterruptibleThread.cpp +++ b/Subtrees/beast/modules/beast_core/thread/beast_InterruptibleThread.cpp @@ -66,57 +66,56 @@ void InterruptibleThread::join () m_thread.stopThread (-1); } +// Block until there is an interruption. +// This counts as an interruption point. +// void InterruptibleThread::wait () { - // Can only be called from the corresponding thread of execution. - // + // Can only be called from the thread of execution. bassert (isTheCurrentThread ()); - bool interrupted = false; - for (;;) { + // Impossible for us to already be in the wait state. bassert (m_state != stateWait); - // See if we are interrupted - // + // See if we are interrupted. if (m_state.tryChangeState (stateInterrupt, stateRun)) { - // We were interrupted, state is changed to Run. Caller must run now. - // - interrupted = true; - break; + // We were interrupted, so the wait is satisfied. + return; } - else if (m_state.tryChangeState (stateRun, stateWait)) + else { - // Transitioned to wait. Caller must wait now. - // - interrupted = false; - break; + // Try to get into the wait state. + if (m_state.tryChangeState (stateRun, stateWait)) + { + 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 () { for (;;) { - int const state = m_state; + int const state (m_state); if (state == 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. // 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 () { // Can only be called from the thread of execution. - // bassert (isTheCurrentThread ()); - // It is impossible for this function to be called while in the wait state. - check_precondition (m_state != stateWait); + // Impossible for this to be called in the wait state. + bassert (m_state != stateWait); - bool const interrupted = m_state.tryChangeState (stateInterrupt, stateRun); + bool const interrupted (m_state.tryChangeState (stateInterrupt, stateRun)); return interrupted; }