]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timer.cpp
Fix Timer to use is Semaphore correctly
[libs/core.git] / source / time / timer.cpp
index 9471463216206b701d285562bb41e0af1901fafc..f26900b8d4a589769c707b59513de6aa2d8dc129 100644 (file)
@@ -1,10 +1,3 @@
-/* $Id$
-
-This file is part of libmspcore     
-Copyright © 2006, 2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <algorithm>
 #include "timer.h"
 #include "utils.h"
@@ -14,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)
@@ -22,24 +20,23 @@ Timer::~Timer()
 
 Timer::Slot &Timer::add(const TimeDelta &td)
 {
-       Slot *s=new Slot(td);
-       mutex.lock();
+       Slot *s = new Slot(td);
+       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();
+       Slot *s = new Slot(ts);
+       MutexLock l(mutex);
+       slots.push_back(s);
+       push_heap(slots.begin(), slots.end());
+       if(blocking)
+               sem.signal();
        return *s;
 }
 
@@ -58,7 +55,7 @@ void Timer::cancel(Slot &slot)
 
 void Timer::tick(bool block)
 {
-       Slot *next=0;
+       Slot *next = 0;
        {
                MutexLock l(mutex);
                while(1)
@@ -66,18 +63,28 @@ void Timer::tick(bool block)
                        if(slots.empty())
                        {
                                if(block)
+                               {
+                                       blocking = true;
+                                       mutex.unlock();
                                        sem.wait();
+                                       mutex.lock();
+                               }
                                else
                                        return;
                        }
 
-                       next=slots.begin()->slot;
-                       const TimeStamp &stamp=next->get_timeout();
-                       const TimeStamp t=now();
+                       next = slots.begin()->slot;
+                       const TimeStamp &stamp = next->get_timeout();
+                       const TimeStamp t = now();
                        if(stamp<=t)
                                break;
                        else if(block)
+                       {
+                               blocking = true;
+                               mutex.unlock();
                                sem.wait(stamp-t);
+                               mutex.lock();
+                       }
                        else
                                return;
                }
@@ -125,7 +132,7 @@ bool Timer::Slot::increment()
 {
        if(!interval)
                return false;
-       timeout+=interval;
+       timeout += interval;
        return true;
 }