From: Mikko Rasa Date: Fri, 25 Dec 2015 10:13:57 +0000 (+0200) Subject: Refactor Timer::tick X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ac26a7e3db27d27ea322ccc17bb093b348b30ae8;p=libs%2Fcore.git Refactor Timer::tick The previous version had some bugs: the blocking flag was not reset and it might have tried to execute a canceled (and thus deleted) slot. --- diff --git a/source/time/timer.cpp b/source/time/timer.cpp index f26900b..7c9567e 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -1,4 +1,5 @@ #include +#include #include "timer.h" #include "utils.h" @@ -60,30 +61,27 @@ void Timer::tick(bool block) 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(block) { - blocking = true; + SetFlag setf(blocking); mutex.unlock(); - sem.wait(stamp-t); + if(stamp) + sem.wait(stamp-t); + else + sem.wait(); mutex.lock(); + // The slots may have changed while waiting so check again + continue; } else return;