]> git.tdb.fi Git - libs/core.git/commitdiff
Fix Timer to use is Semaphore correctly
authorMikko Rasa <tdb@tdb.fi>
Sat, 20 Apr 2013 14:47:50 +0000 (17:47 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 20 Apr 2013 14:47:50 +0000 (17:47 +0300)
Besides still assuming the old condvar-style semantics, it was also
horribly broken and contained potential deadlocks and race conditions.
Not to mention unusable due to a missing constructor.

source/time/timer.cpp
source/time/timer.h

index 59360b63fa6e18a83fed699429bf7e76a75d0b75..f26900b8d4a589769c707b59513de6aa2d8dc129 100644 (file)
@@ -7,6 +7,11 @@ using namespace std;
 namespace Msp {
 namespace Time {
 
+Timer::Timer():
+       sem(1),
+       blocking(false)
+{ }
+
 Timer::~Timer()
 {
        for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
@@ -16,23 +21,22 @@ Timer::~Timer()
 Timer::Slot &Timer::add(const TimeDelta &td)
 {
        Slot *s = new Slot(td);
-       mutex.lock();
+       MutexLock l(mutex);
        slots.push_back(s);
        push_heap(slots.begin(), slots.end());
-       mutex.unlock();
-       sem.signal();
+       if(blocking)
+               sem.signal();
        return *s;
 }
 
 Timer::Slot &Timer::add(const TimeStamp &ts)
 {
        Slot *s = new Slot(ts);
-       {
-               MutexLock l(mutex);
-               slots.push_back(s);
-               push_heap(slots.begin(), slots.end());
-       }
-       sem.signal();
+       MutexLock l(mutex);
+       slots.push_back(s);
+       push_heap(slots.begin(), slots.end());
+       if(blocking)
+               sem.signal();
        return *s;
 }
 
@@ -59,7 +63,12 @@ void Timer::tick(bool block)
                        if(slots.empty())
                        {
                                if(block)
+                               {
+                                       blocking = true;
+                                       mutex.unlock();
                                        sem.wait();
+                                       mutex.lock();
+                               }
                                else
                                        return;
                        }
@@ -70,7 +79,12 @@ void Timer::tick(bool block)
                        if(stamp<=t)
                                break;
                        else if(block)
+                       {
+                               blocking = true;
+                               mutex.unlock();
                                sem.wait(stamp-t);
+                               mutex.lock();
+                       }
                        else
                                return;
                }
index 7566c3dd03a6a86eef6619c2d4ddd24ddbb373ca..90d870ca9a3afbd44b155f5ccfb5b85747c02442 100644 (file)
@@ -49,8 +49,10 @@ private:
        std::vector<SlotProxy> slots;
        Semaphore sem;
        Mutex mutex;
+       bool blocking;
 
 public:
+       Timer();
        ~Timer();
 
        /** Adds a timer that will be executed periodically as long as the timeout