X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftime%2Ftimer.cpp;h=f26900b8d4a589769c707b59513de6aa2d8dc129;hb=87edba02a23b1338934e36fac407966b25fcf811;hp=9471463216206b701d285562bb41e0af1901fafc;hpb=5889d53f8f073ff0e1e1ebbd786abecd8352a7b3;p=libs%2Fcore.git diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 9471463..f26900b 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006, 2009 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #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::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; }