X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Ftimer.cpp;h=04c3074494e921d39f3edb54e53e378c8623e826;hp=f26900b8d4a589769c707b59513de6aa2d8dc129;hb=41363aed34382386f915f17c1a961750b4fdcb14;hpb=aad3371b3d1831dd0525a51e40f637f00be6ff87 diff --git a/source/time/timer.cpp b/source/time/timer.cpp index f26900b..04c3074 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,4 +1,5 @@ -#include +#include +#include #include "timer.h" #include "utils.h" @@ -8,14 +9,13 @@ namespace Msp { namespace Time { Timer::Timer(): - sem(1), - blocking(false) + sem(1) { } Timer::~Timer() { - for(vector::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) @@ -43,47 +43,62 @@ Timer::Slot &Timer::add(const TimeStamp &ts) void Timer::cancel(Slot &slot) { 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; - } + 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() +{ + do_tick(-sec); } -void Timer::tick(bool block) +void Timer::tick(const TimeDelta &timeout) { - Slot *next = 0; + if(timeout=zero) + deadline = now()+timeout; + + Slot *next = nullptr; { MutexLock l(mutex); while(1) { - if(slots.empty()) + TimeStamp stamp; + TimeStamp t = now(); + if(!slots.empty()) { - if(block) - { - blocking = true; - mutex.unlock(); - sem.wait(); - mutex.lock(); - } - else - return; + next = slots.begin()->slot; + stamp = next->get_timeout(); + if(stamp<=t) + break; } - next = slots.begin()->slot; - const TimeStamp &stamp = next->get_timeout(); - const TimeStamp t = now(); - if(stamp<=t) - break; - else if(block) + if(timeout && (!deadline || t