X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Ftimer.cpp;h=04c3074494e921d39f3edb54e53e378c8623e826;hp=c6e546fc78b782a127a4603076e7c0a0ae7f03d4;hb=HEAD;hpb=cfc8e0b7b15ea505bd6a6a9599cbc5ce1e316963 diff --git a/source/time/timer.cpp b/source/time/timer.cpp index c6e546f..eda782e 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,10 +1,5 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - +#include +#include #include "timer.h" #include "utils.h" @@ -13,66 +8,122 @@ using namespace std; namespace Msp { namespace Time { +Timer::Timer(): + sem(1) +{ } + Timer::~Timer() { - for(set::iterator i=slots.begin(); i!=slots.end(); ++i) - delete i->slot; + for(const SlotProxy &s: slots) + delete s.slot; } Timer::Slot &Timer::add(const TimeDelta &td) { - Slot *s=new Slot(td); - mutex.lock(); - slots.insert(s); - mutex.unlock(); - sem.signal(); + 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::Slot &Timer::add(const TimeStamp &ts) { - Slot *s=new Slot(ts); - { - MutexLock l(mutex); - slots.insert(s); - } - 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; } void Timer::cancel(Slot &slot) { MutexLock l(mutex); - if(slots.erase(&slot)) - delete &slot; + auto i = find_member(slots, &slot, &SlotProxy::slot); + if(i!=slots.end()) + { + delete i->slot; + slots.erase(i); + make_heap(slots.begin(), slots.end()); + } } -void Timer::tick(bool block) +void Timer::tick() { - if(slots.empty()) - { - if(block) - sem.wait(); - else - return; - } + do_tick(-sec); +} + +void Timer::tick(const TimeDelta &timeout) +{ + if(timeout=zero) + deadline = now()+timeout; - Slot *next; + Slot *next = nullptr; { MutexLock l(mutex); - next=slots.begin()->slot; + while(1) + { + TimeStamp stamp; + TimeStamp t = now(); + if(!slots.empty()) + { + next = slots.begin()->slot; + stamp = next->get_timeout(); + if(stamp<=t) + break; + } + + if(timeout && (!deadline || tget_timeout(); - const TimeStamp t=now(); - if(stamp<=t || (block && sem.wait(stamp-t)==1)) + try { - slots.erase(slots.begin()); if(next->signal_timeout.emit() && next->increment()) - slots.insert(next); + { + MutexLock l(mutex); + slots.push_back({ next }); + push_heap(slots.begin(), slots.end()); + } else delete next; } + catch(...) + { + delete next; + throw; + } } TimeStamp Timer::get_next_timeout() const @@ -96,18 +147,14 @@ bool Timer::Slot::increment() { if(!interval) return false; - timeout+=interval; + timeout += interval; return true; } -Timer::SlotProxy::SlotProxy(Slot *s): - slot(s) -{ } - bool Timer::SlotProxy::operator<(const SlotProxy &sp) const { - return slot->get_timeout()get_timeout(); + return slot->get_timeout()>sp.slot->get_timeout(); } } // namespace Time