X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Ftimer.cpp;h=04c3074494e921d39f3edb54e53e378c8623e826;hp=9fc82f57e872bf0b0c1f1dcc3ee8f974003eafd5;hb=HEAD;hpb=3d1b0b44b2d75ed7d97b3588eefe61a9b511365c diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 9fc82f5..eda782e 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,9 +1,5 @@ -/* -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 +9,128 @@ namespace Msp { namespace Time { Timer::Timer(): - slots(slot_compare) + sem(1) { } Timer::~Timer() { - while(!slots.empty()) - { - delete slots.top(); - slots.pop(); - } + for(const SlotProxy &s: slots) + delete s.slot; } Timer::Slot &Timer::add(const TimeDelta &td) { - Slot *s=new Slot(td); - mutex.lock(); - slots.push(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); - mutex.lock(); - slots.push(s); - mutex.unlock(); - 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::tick(bool block) +void Timer::cancel(Slot &slot) { - if(slots.empty()) + MutexLock l(mutex); + auto i = find_member(slots, &slot, &SlotProxy::slot); + if(i!=slots.end()) { - if(block) - sem.wait(); - return; + 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; + { + MutexLock l(mutex); + 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.pop(); if(next->signal_timeout.emit() && next->increment()) - slots.push(next); + { + MutexLock l(mutex); + slots.push_back({ next }); + push_heap(slots.begin(), slots.end()); + } else delete next; } + catch(...) + { + delete next; + throw; + } } -bool Timer::slot_compare(Slot *a, Slot *b) +TimeStamp Timer::get_next_timeout() const { - return *a<*b; + if(slots.empty()) + return TimeStamp(); + return slots.begin()->slot->get_timeout(); } @@ -89,13 +147,14 @@ bool Timer::Slot::increment() { if(!interval) return false; - timeout+=interval; + timeout += interval; return true; } -bool Timer::Slot::operator<(const Slot &other) const + +bool Timer::SlotProxy::operator<(const SlotProxy &sp) const { - return timeoutget_timeout()>sp.slot->get_timeout(); } } // namespace Time