From bb49a2de46849a9ffa509148661d4c574c43fe24 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 19 Jul 2008 13:00:56 +0000 Subject: [PATCH] Allow canceling timers Use MutexLocks for more robustness Add get_next_timeout function --- source/time/timer.cpp | 57 ++++++++++++++++++++++++++----------------- source/time/timer.h | 23 +++++++++++------ 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 9fc82f5..efe22cc 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -12,24 +12,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 +31,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 +99,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 diff --git a/source/time/timer.h b/source/time/timer.h index cfa00bb..8b4e595 100644 --- a/source/time/timer.h +++ b/source/time/timer.h @@ -7,7 +7,7 @@ Distributed under the LGPL #ifndef MSP_TIME_TIMER_H_ #define MSP_TIME_TIMER_H_ -#include +#include #include #include "../core/mutex.h" #include "../core/semaphore.h" @@ -41,18 +41,22 @@ public: Slot(const TimeStamp &); const TimeStamp &get_timeout() const { return timeout; } bool increment(); - bool operator<(const Slot &) const; }; private: - typedef bool (*fSlotCompare)(Slot *, Slot *); + struct SlotProxy + { + Slot *slot; + + SlotProxy(Slot *); + bool operator<(const SlotProxy &) const; + }; - std::priority_queue, fSlotCompare> slots; + std::set slots; Semaphore sem; Mutex mutex; public: - Timer(); ~Timer(); /** @@ -67,6 +71,11 @@ public: */ Slot &add(const TimeStamp &); + /** + Cancels a previously added timer. + */ + void cancel(Slot &); + /** Checks all timers, executing any that have timed out. If block is true, waits until one times out. @@ -75,8 +84,8 @@ public: won't return until a timer is added from another thread. */ void tick(bool block=true); -private: - static bool slot_compare(Slot *, Slot *); + + TimeStamp get_next_timeout() const; }; } // namespace Time -- 2.45.2