diff options
Diffstat (limited to 'cpp')
| -rw-r--r-- | cpp/src/qpid/sys/Timer.cpp | 13 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/Timer.h | 2 |
2 files changed, 13 insertions, 2 deletions
diff --git a/cpp/src/qpid/sys/Timer.cpp b/cpp/src/qpid/sys/Timer.cpp index 7df4379e7c..6967d812ae 100644 --- a/cpp/src/qpid/sys/Timer.cpp +++ b/cpp/src/qpid/sys/Timer.cpp @@ -19,6 +19,7 @@ * */ #include "Timer.h" +#include "Mutex.h" #include <iostream> #include <numeric> @@ -62,7 +63,10 @@ void TimerTask::setupNextFire() { void TimerTask::restart() { nextFireTime = AbsTime(AbsTime::now(), period); } void TimerTask::delayTill(AbsTime time) { period = 0; nextFireTime = max(nextFireTime, time); } -void TimerTask::cancel() { cancelled = true; } +void TimerTask::cancel() { + ScopedLock<Mutex> l(callbackLock); + cancelled = true; +} bool TimerTask::isCancelled() const { return cancelled; } Timer::Timer() : @@ -85,16 +89,21 @@ void Timer::run() } else { intrusive_ptr<TimerTask> t = tasks.top(); tasks.pop(); + { + ScopedLock<Mutex> l(t->callbackLock); if (t->isCancelled()) { + continue; } else if(t->readyToFire()) { Monitor::ScopedUnlock u(monitor); t->fireTask(); + continue; } else { // If the timer was adjusted into the future it might no longer // be the next event, so push and then get top to make sure tasks.push(t); - monitor.wait(tasks.top()->nextFireTime); } + } + monitor.wait(tasks.top()->nextFireTime); } } } diff --git a/cpp/src/qpid/sys/Timer.h b/cpp/src/qpid/sys/Timer.h index dab2f55edb..b5bf5d8a4c 100644 --- a/cpp/src/qpid/sys/Timer.h +++ b/cpp/src/qpid/sys/Timer.h @@ -22,6 +22,7 @@ #define sys_Timer #include "qpid/sys/Monitor.h" +#include "qpid/sys/Mutex.h" #include "qpid/sys/Thread.h" #include "qpid/sys/Runnable.h" #include "qpid/RefCounted.h" @@ -43,6 +44,7 @@ class TimerTask : public RefCounted { Duration period; AbsTime nextFireTime; + Mutex callbackLock; volatile bool cancelled; bool readyToFire() const; |
