X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Ftimer.cpp;h=04c3074494e921d39f3edb54e53e378c8623e826;hp=641d55f03b03a71d4b2a3a41cf04a7824aa56898;hb=HEAD;hpb=e1ea831a640fba534e7e42e399f04cdf681ef8d3 diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 641d55f..eda782e 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,9 +1,5 @@ -/* -This file is part of libmspframework -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#include +#include +#include #include "timer.h" #include "utils.h" @@ -12,107 +8,153 @@ 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(): + sem(1) +{ } Timer::~Timer() { - MutexLock l(set_mutex); - timers.erase(this); - thread.nudge(); + for(const SlotProxy &s: slots) + delete s.slot; } -Timer::ThreadProxy Timer::thread; -Mutex Timer::set_mutex; -set Timer::timers; - -Timer::Thread::Thread(): - done(false) +Timer::Slot &Timer::add(const TimeDelta &td) { - launch(); + Slot *s = new Slot(td); + MutexLock l(mutex); + slots.push_back({ s }); + push_heap(slots.begin(), slots.end()); + if(blocking) + sem.signal(); + return *s; } -/** -Notifies the thread that a change in the timers occurred. -*/ -void Timer::Thread::nudge() +Timer::Slot &Timer::add(const TimeStamp &ts) { - 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; } -/** -Tells the thread to finish and terminate gracefully. This function will return -after the thread has terminated. -*/ -void Timer::Thread::finish() +void Timer::cancel(Slot &slot) { - if(!done) + MutexLock l(mutex); + auto i = find_member(slots, &slot, &SlotProxy::slot); + if(i!=slots.end()) { - done=true; - sem.signal(); + delete i->slot; + slots.erase(i); + make_heap(slots.begin(), slots.end()); } +} + +void Timer::tick() +{ + do_tick(-sec); +} + +void Timer::tick(const TimeDelta &timeout) +{ + if(timeout=zero) + deadline = now()+timeout; + + Slot *next = nullptr; { - 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 - 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(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() + +bool Timer::SlotProxy::operator<(const SlotProxy &sp) const { - if(thread) - { - thread->finish(); - delete thread; - } + return slot->get_timeout()>sp.slot->get_timeout(); } } // namespace Time