X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftime%2Ftimer.cpp;h=c6e546fc78b782a127a4603076e7c0a0ae7f03d4;hb=cb8ed1df371834fb7766e0b7754cdf83c474d394;hp=9fc82f57e872bf0b0c1f1dcc3ee8f974003eafd5;hpb=3d1b0b44b2d75ed7d97b3588eefe61a9b511365c;p=libs%2Fcore.git diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 9fc82f5..c6e546f 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,4 +1,5 @@ -/* +/* $Id$ + This file is part of libmspcore Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL @@ -12,24 +13,17 @@ using namespace std; namespace Msp { namespace Time { -Timer::Timer(): - slots(slot_compare) -{ } - Timer::~Timer() { - while(!slots.empty()) - { - delete slots.top(); - slots.pop(); - } + for(set::iterator i=slots.begin(); i!=slots.end(); ++i) + delete i->slot; } Timer::Slot &Timer::add(const TimeDelta &td) { Slot *s=new Slot(td); mutex.lock(); - slots.push(s); + slots.insert(s); mutex.unlock(); sem.signal(); return *s; @@ -38,41 +32,54 @@ Timer::Slot &Timer::add(const TimeDelta &td) Timer::Slot &Timer::add(const TimeStamp &ts) { Slot *s=new Slot(ts); - mutex.lock(); - slots.push(s); - mutex.unlock(); + { + MutexLock l(mutex); + slots.insert(s); + } sem.signal(); return *s; } +void Timer::cancel(Slot &slot) +{ + MutexLock l(mutex); + if(slots.erase(&slot)) + delete &slot; +} + void Timer::tick(bool block) { if(slots.empty()) { if(block) sem.wait(); - return; + else + return; } - mutex.lock(); - Slot *next=slots.top(); - mutex.unlock(); + Slot *next; + { + MutexLock l(mutex); + next=slots.begin()->slot; + } const TimeStamp &stamp=next->get_timeout(); const TimeStamp t=now(); if(stamp<=t || (block && sem.wait(stamp-t)==1)) { - slots.pop(); + slots.erase(slots.begin()); if(next->signal_timeout.emit() && next->increment()) - slots.push(next); + slots.insert(next); else delete next; } } -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(); } @@ -93,9 +100,14 @@ bool Timer::Slot::increment() return true; } -bool Timer::Slot::operator<(const Slot &other) const + +Timer::SlotProxy::SlotProxy(Slot *s): + slot(s) +{ } + +bool Timer::SlotProxy::operator<(const SlotProxy &sp) const { - return timeoutget_timeout()get_timeout(); } } // namespace Time