summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/sys/Timer.cpp
diff options
context:
space:
mode:
authorAndrew Stitcher <astitcher@apache.org>2009-06-23 20:02:35 +0000
committerAndrew Stitcher <astitcher@apache.org>2009-06-23 20:02:35 +0000
commit043622065972dee6a008fa20cc70d0fb73e666d4 (patch)
tree95c994fb4f95382abfb0b3640f7cd2e962c93068 /cpp/src/qpid/sys/Timer.cpp
parent92231d488633e10efddb84b8423fc368f21516e0 (diff)
downloadqpid-python-043622065972dee6a008fa20cc70d0fb73e666d4.tar.gz
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
Diffstat (limited to 'cpp/src/qpid/sys/Timer.cpp')
-rw-r--r--cpp/src/qpid/sys/Timer.cpp13
1 files changed, 11 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);
}
}
}