From 043622065972dee6a008fa20cc70d0fb73e666d4 Mon Sep 17 00:00:00 2001 From: Andrew Stitcher Date: Tue, 23 Jun 2009 20:02:35 +0000 Subject: Add blocking to sys::Timer so that timer callback and cancel can't happen interleaved git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@787813 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/sys/Timer.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'cpp/src/qpid/sys/Timer.cpp') 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 #include @@ -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 l(callbackLock); + cancelled = true; +} bool TimerTask::isCancelled() const { return cancelled; } Timer::Timer() : @@ -85,16 +89,21 @@ void Timer::run() } else { intrusive_ptr t = tasks.top(); tasks.pop(); + { + ScopedLock 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); } } } -- cgit v1.2.1