]> git.tdb.fi Git - libs/core.git/commitdiff
Refactor Timer::tick
authorMikko Rasa <tdb@tdb.fi>
Fri, 25 Dec 2015 10:13:57 +0000 (12:13 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 25 Dec 2015 10:13:57 +0000 (12:13 +0200)
The previous version had some bugs: the blocking flag was not reset and
it might have tried to execute a canceled (and thus deleted) slot.

source/time/timer.cpp

index f26900b8d4a589769c707b59513de6aa2d8dc129..7c9567ecfecccafe826f8d1fcd52d0bb82d6f899 100644 (file)
@@ -1,4 +1,5 @@
 #include <algorithm>
+#include <msp/core/raii.h>
 #include "timer.h"
 #include "utils.h"
 
@@ -60,30 +61,27 @@ void Timer::tick(bool block)
                MutexLock l(mutex);
                while(1)
                {
-                       if(slots.empty())
+                       TimeStamp stamp;
+                       TimeStamp t = now();
+                       if(!slots.empty())
                        {
-                               if(block)
-                               {
-                                       blocking = true;
-                                       mutex.unlock();
-                                       sem.wait();
-                                       mutex.lock();
-                               }
-                               else
-                                       return;
+                               next = slots.begin()->slot;
+                               stamp = next->get_timeout();
+                               if(stamp<=t)
+                                       break;
                        }
 
-                       next = slots.begin()->slot;
-                       const TimeStamp &stamp = next->get_timeout();
-                       const TimeStamp t = now();
-                       if(stamp<=t)
-                               break;
-                       else if(block)
+                       if(block)
                        {
-                               blocking = true;
+                               SetFlag setf(blocking);
                                mutex.unlock();
-                               sem.wait(stamp-t);
+                               if(stamp)
+                                       sem.wait(stamp-t);
+                               else
+                                       sem.wait();
                                mutex.lock();
+                               // The slots may have changed while waiting so check again
+                               continue;
                        }
                        else
                                return;