From 718569d6a16a3103c0c1d4acf592c3c0cee1cdb3 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 6 Sep 2013 21:18:48 -0700 Subject: [PATCH] Fix DeadlineTimer for InterruptibleThread::wait timeout --- .../beast_core/thread/beast_DeadlineTimer.cpp | 1 - .../thread/beast_InterruptibleThread.cpp | 52 ++++--------------- .../thread/beast_InterruptibleThread.h | 16 ++---- 3 files changed, 12 insertions(+), 57 deletions(-) diff --git a/modules/beast_core/thread/beast_DeadlineTimer.cpp b/modules/beast_core/thread/beast_DeadlineTimer.cpp index 4872a7656..e5d644a68 100644 --- a/modules/beast_core/thread/beast_DeadlineTimer.cpp +++ b/modules/beast_core/thread/beast_DeadlineTimer.cpp @@ -170,7 +170,6 @@ public: // is extremely short, or if a listener wastes too much time in // their callback. } - } } } diff --git a/modules/beast_core/thread/beast_InterruptibleThread.cpp b/modules/beast_core/thread/beast_InterruptibleThread.cpp index e732b3cba..f0d1f0706 100644 --- a/modules/beast_core/thread/beast_InterruptibleThread.cpp +++ b/modules/beast_core/thread/beast_InterruptibleThread.cpp @@ -66,7 +66,7 @@ void InterruptibleThread::join () m_thread.stopThread (-1); } -bool InterruptibleThread::wait (int milliSeconds) +void InterruptibleThread::wait () { // Can only be called from the corresponding thread of execution. // @@ -87,8 +87,7 @@ bool InterruptibleThread::wait (int milliSeconds) interrupted = true; break; } - else if (m_state.tryChangeState (stateRun, stateWait) || - m_state.tryChangeState (stateReturn, stateWait)) + else if (m_state.tryChangeState (stateRun, stateWait)) { // Transitioned to wait. Caller must wait now. // @@ -101,33 +100,12 @@ bool InterruptibleThread::wait (int milliSeconds) { bassert (m_state == stateWait); - interrupted = m_thread.wait (milliSeconds); + m_thread.wait (); - if (! interrupted) - { - // The wait timed out - // - 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); - } + // The event became signalled. + // + bassert (m_state == stateRun || m_state == stateInterrupt); } - - return interrupted; } void InterruptibleThread::interrupt () @@ -137,8 +115,7 @@ void InterruptibleThread::interrupt () int const state = m_state; if (state == stateInterrupt || - state == stateReturn || - m_state.tryChangeState (stateRun, stateInterrupt)) + m_state.tryChangeState (stateRun, stateInterrupt)) { // Thread will see this at next interruption point. // @@ -158,19 +135,8 @@ bool InterruptibleThread::interruptionPoint () // bassert (isTheCurrentThread ()); - if (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__)); - } + // It is impossible for this function to be called while in the wait state. + check_precondition (m_state != stateWait); bool const interrupted = m_state.tryChangeState (stateInterrupt, stateRun); diff --git a/modules/beast_core/thread/beast_InterruptibleThread.h b/modules/beast_core/thread/beast_InterruptibleThread.h index bf78b45f6..658d48654 100644 --- a/modules/beast_core/thread/beast_InterruptibleThread.h +++ b/modules/beast_core/thread/beast_InterruptibleThread.h @@ -71,20 +71,11 @@ public: */ void join (); - /** Wait for interrupt or timeout. - - This call blocks until the thread is interrupted, or until the timeout - expires if milliSeconds is non-negative. - + /** Wait for interrupt. + This call blocks until the thread is interrupted. 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. @@ -159,7 +150,6 @@ private: { stateRun, stateInterrupt, - stateReturn, stateWait };