X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftime%2Ftimer.cpp;h=9471463216206b701d285562bb41e0af1901fafc;hb=5b0c36c9c6c9c30f1eb42186fed7acc7e99faf3e;hp=641d55f03b03a71d4b2a3a41cf04a7824aa56898;hpb=e1ea831a640fba534e7e42e399f04cdf681ef8d3;p=libs%2Fcore.git diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 641d55f..9471463 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,9 +1,11 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions +/* $Id$ + +This file is part of libmspcore +Copyright © 2006, 2009 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ -#include + +#include #include "timer.h" #include "utils.h" @@ -12,107 +14,129 @@ using namespace std; namespace Msp { namespace Time { -Timer::Timer(const Time::TimeDelta &d): - interval(d), - timeout(now()+d) -{ - MutexLock l(set_mutex); - timers.insert(this); - thread.nudge(); -} - Timer::~Timer() { - MutexLock l(set_mutex); - timers.erase(this); - thread.nudge(); + for(vector::iterator i=slots.begin(); i!=slots.end(); ++i) + delete i->slot; } -Timer::ThreadProxy Timer::thread; -Mutex Timer::set_mutex; -set Timer::timers; - -Timer::Thread::Thread(): - done(false) -{ - launch(); -} - -/** -Notifies the thread that a change in the timers occurred. -*/ -void Timer::Thread::nudge() +Timer::Slot &Timer::add(const TimeDelta &td) { + Slot *s=new Slot(td); + mutex.lock(); + slots.push_back(s); + push_heap(slots.begin(), slots.end()); + mutex.unlock(); sem.signal(); + return *s; } -/** -Tells the thread to finish and terminate gracefully. This function will return -after the thread has terminated. -*/ -void Timer::Thread::finish() +Timer::Slot &Timer::add(const TimeStamp &ts) { - if(!done) + Slot *s=new Slot(ts); { - done=true; - sem.signal(); + MutexLock l(mutex); + slots.push_back(s); + push_heap(slots.begin(), slots.end()); } + sem.signal(); + return *s; +} - join(); +void Timer::cancel(Slot &slot) +{ + MutexLock l(mutex); + for(vector::iterator i=slots.begin(); i!=slots.end(); ++i) + if(i->slot==&slot) + { + delete i->slot; + slots.erase(i); + make_heap(slots.begin(), slots.end()); + return; + } } -void Timer::Thread::main() +void Timer::tick(bool block) { - while(!done) + Slot *next=0; { - set_mutex.lock(); - Timer *next=0; - TimeStamp next_ts; - for(set::iterator i=timers.begin(); i!=timers.end(); ++i) + MutexLock l(mutex); + while(1) { - const TimeStamp &ts=(*i)->get_timeout(); - if(tsslot; + const TimeStamp &stamp=next->get_timeout(); + const TimeStamp t=now(); + if(stamp<=t) + break; + else if(block) + sem.wait(stamp-t); + else + return; } - set_mutex.unlock(); - if(next) + pop_heap(slots.begin(), slots.end()); + slots.pop_back(); + } + + try + { + if(next->signal_timeout.emit() && next->increment()) { - const TimeStamp t=now(); - if(next_ts<=t || sem.wait(next_ts-t)==1) - { - if(next->signal_timeout.emit()) - next->timeout+=next->interval; - else - delete next; - } + MutexLock l(mutex); + slots.push_back(next); + push_heap(slots.begin(), slots.end()); } else - pause(); + delete next; + } + catch(...) + { + delete next; + throw; } } -/** -Creates the thread if it doesn't exist, otherwise nudges it. -*/ -void Timer::ThreadProxy::nudge() +TimeStamp Timer::get_next_timeout() const { - if(!thread) - thread=new Thread(); - else - thread->nudge(); + if(slots.empty()) + return TimeStamp(); + return slots.begin()->slot->get_timeout(); } -Timer::ThreadProxy::~ThreadProxy() + +Timer::Slot::Slot(const TimeDelta &td): + interval(td), + timeout(now()+interval) +{ } + +Timer::Slot::Slot(const TimeStamp &ts): + timeout(ts) +{ } + +bool Timer::Slot::increment() { - if(thread) - { - thread->finish(); - delete thread; - } + if(!interval) + return false; + timeout+=interval; + return true; +} + + +Timer::SlotProxy::SlotProxy(Slot *s): + slot(s) +{ } + +bool Timer::SlotProxy::operator<(const SlotProxy &sp) const +{ + return slot->get_timeout()>sp.slot->get_timeout(); } } // namespace Time