You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2009/07/10 23:41:55 UTC

svn commit: r793120 - in /qpid/trunk/qpid/cpp/src/qpid/sys: Timer.cpp Timer.h

Author: astitcher
Date: Fri Jul 10 21:41:54 2009
New Revision: 793120

URL: http://svn.apache.org/viewvc?rev=793120&view=rev
Log:
Allow TimerTasks to be delayed correctly by separating out the time
used to sort in the priority queue and the firing time

Modified:
    qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp
    qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp?rev=793120&r1=793119&r2=793120&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp Fri Jul 10 21:41:54 2009
@@ -30,12 +30,14 @@
 namespace sys {
 
 TimerTask::TimerTask(Duration timeout) :
+    sortTime(AbsTime::FarFuture()),
     period(timeout),
     nextFireTime(AbsTime::now(), timeout),
     cancelled(false)
 {}
 
 TimerTask::TimerTask(AbsTime time) :
+    sortTime(AbsTime::FarFuture()),
     period(0),
     nextFireTime(time),
     cancelled(false)
@@ -60,7 +62,7 @@
 }
 
 // Only allow tasks to be delayed
-void TimerTask::restart() { nextFireTime = AbsTime(AbsTime::now(), period); }
+void TimerTask::restart() { nextFireTime = max(nextFireTime, AbsTime(AbsTime::now(), period)); }
 void TimerTask::delayTill(AbsTime time) { period = 0; nextFireTime = max(nextFireTime, time); }
 
 void TimerTask::cancel() {
@@ -91,7 +93,7 @@
             tasks.pop();
             {
             ScopedLock<Mutex> l(t->callbackLock);
-            if (t->isCancelled()) {
+            if (t->cancelled) {
                 continue;
             } else if(t->readyToFire()) {
                 Monitor::ScopedUnlock u(monitor);
@@ -100,6 +102,9 @@
             } 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
+                // You can only push events into the future
+                assert(!(t->nextFireTime < t->sortTime));
+                t->sortTime = t->nextFireTime;
                 tasks.push(t);
             }
             }
@@ -111,6 +116,7 @@
 void Timer::add(intrusive_ptr<TimerTask> task)
 {
     Monitor::ScopedLock l(monitor);
+    task->sortTime = task->nextFireTime;
     tasks.push(task);
     monitor.notify();
 }
@@ -139,7 +145,7 @@
                        const intrusive_ptr<TimerTask>& b)
 {
     // Lower priority if time is later
-    return a.get() && b.get() && a->nextFireTime > b->nextFireTime;
+    return a.get() && b.get() && a->sortTime > b->sortTime;
 }
 
 }}

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h?rev=793120&r1=793119&r2=793120&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h Fri Jul 10 21:41:54 2009
@@ -42,6 +42,7 @@
     friend bool operator<(const boost::intrusive_ptr<TimerTask>&,
                 const boost::intrusive_ptr<TimerTask>&);
 
+    AbsTime sortTime;
     Duration period;
     AbsTime nextFireTime;
     Mutex callbackLock;



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org