X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Ftimer.cpp;h=e0af57cc78fcc41363632e169e33a8219653e9f1;hp=3d4aec5ff4a8f6307ceaea732484e8c779b89f10;hb=be8ea216d23bf36bdfb2d3e302638782575fc136;hpb=ff52b5032a3f040de5d3a48f953f2943d7a223b3 diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 3d4aec5..e0af57c 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,8 +1,5 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ +#include +#include #include "timer.h" #include "utils.h" @@ -11,107 +8,167 @@ using namespace std; namespace Msp { namespace Time { -Timer::Timer(const Time::TimeDelta &d): - interval(d), - timeout(now()+d) +Timer::Timer(): + sem(1), + blocking(false) +{ } + +Timer::~Timer() { - MutexLock l(set_mutex); - timers.insert(this); - thread.nudge(); + for(vector::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); + MutexLock l(mutex); + slots.push_back(s); + push_heap(slots.begin(), slots.end()); + if(blocking) + sem.signal(); + return *s; } -Timer::ThreadProxy Timer::thread; -Mutex Timer::set_mutex; -set Timer::timers; +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()); + if(blocking) + sem.signal(); + return *s; +} -Timer::Thread::Thread(): - done(false) +void Timer::cancel(Slot &slot) { - launch(); + 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; + } } -/** -Notifies the thread that a change in the timers occurred. -*/ -void Timer::Thread::nudge() +void Timer::tick(bool block) { - sem.signal(); + if(block) + tick(); + else + tick(zero); } -/** -Tells the thread to finish and terminate gracefully. This function will return -after the thread has terminated. -*/ -void Timer::Thread::finish() +void Timer::tick() { - if(!done) - { - done=true; - sem.signal(); - } + do_tick(-sec); +} - join(); +void Timer::tick(const TimeDelta &timeout) +{ + if(timeout=zero) + deadline = now()+timeout; + + 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; + stamp = next->get_timeout(); + if(stamp<=t) + break; } - } - set_mutex.unlock(); - if(next) - { - const TimeStamp t=now(); - if(next_ts<=t || sem.wait(next_ts-t)==1) + if(timeout && (!deadline || tsignal_timeout.emit()) - next->timeout+=next->interval; + SetFlag setf(blocking); + mutex.unlock(); + if(stamp && (!deadline || stampsignal_timeout.emit() && next->increment()) + { + MutexLock l(mutex); + slots.push_back(next); + push_heap(slots.begin(), slots.end()); } else - sem.wait(); + 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