From aad3371b3d1831dd0525a51e40f637f00be6ff87 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 20 Apr 2013 17:47:50 +0300 Subject: [PATCH] Fix Timer to use is Semaphore correctly Besides still assuming the old condvar-style semantics, it was also horribly broken and contained potential deadlocks and race conditions. Not to mention unusable due to a missing constructor. --- source/time/timer.cpp | 32 +++++++++++++++++++++++--------- source/time/timer.h | 2 ++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/source/time/timer.cpp b/source/time/timer.cpp index 59360b6..f26900b 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -7,6 +7,11 @@ using namespace std; namespace Msp { namespace Time { +Timer::Timer(): + sem(1), + blocking(false) +{ } + Timer::~Timer() { for(vector::iterator i=slots.begin(); i!=slots.end(); ++i) @@ -16,23 +21,22 @@ Timer::~Timer() Timer::Slot &Timer::add(const TimeDelta &td) { Slot *s = new Slot(td); - mutex.lock(); + MutexLock l(mutex); slots.push_back(s); push_heap(slots.begin(), slots.end()); - mutex.unlock(); - sem.signal(); + if(blocking) + sem.signal(); return *s; } Timer::Slot &Timer::add(const TimeStamp &ts) { Slot *s = new Slot(ts); - { - MutexLock l(mutex); - slots.push_back(s); - push_heap(slots.begin(), slots.end()); - } - sem.signal(); + MutexLock l(mutex); + slots.push_back(s); + push_heap(slots.begin(), slots.end()); + if(blocking) + sem.signal(); return *s; } @@ -59,7 +63,12 @@ void Timer::tick(bool block) if(slots.empty()) { if(block) + { + blocking = true; + mutex.unlock(); sem.wait(); + mutex.lock(); + } else return; } @@ -70,7 +79,12 @@ void Timer::tick(bool block) if(stamp<=t) break; else if(block) + { + blocking = true; + mutex.unlock(); sem.wait(stamp-t); + mutex.lock(); + } else return; } diff --git a/source/time/timer.h b/source/time/timer.h index 7566c3d..90d870c 100644 --- a/source/time/timer.h +++ b/source/time/timer.h @@ -49,8 +49,10 @@ private: std::vector slots; Semaphore sem; Mutex mutex; + bool blocking; public: + Timer(); ~Timer(); /** Adds a timer that will be executed periodically as long as the timeout -- 2.45.2