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