]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timer.cpp
Add move semantics to Variant
[libs/core.git] / source / time / timer.cpp
index 59360b63fa6e18a83fed699429bf7e76a75d0b75..eda782e0d92c5bf82c00418584e12917d03fa673 100644 (file)
@@ -1,4 +1,5 @@
-#include <algorithm>
+#include <msp/core/algorithm.h>
+#include <msp/core/raii.h>
 #include "timer.h"
 #include "utils.h"
 
@@ -7,70 +8,98 @@ using namespace std;
 namespace Msp {
 namespace Time {
 
+Timer::Timer():
+       sem(1)
+{ }
+
 Timer::~Timer()
 {
-       for(vector<SlotProxy>::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)
 {
        Slot *s = new Slot(td);
-       mutex.lock();
-       slots.push_back(s);
+       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;
 }
 
 void Timer::cancel(Slot &slot)
 {
        MutexLock l(mutex);
-       for(vector<SlotProxy>::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(const TimeDelta &timeout)
+{
+       if(timeout<zero)
+               throw invalid_argument("Timer::tick");
+
+       do_tick(timeout);
 }
 
-void Timer::tick(bool block)
+void Timer::do_tick(const TimeDelta &timeout)
 {
-       Slot *next = 0;
+       TimeStamp deadline;
+       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)
-                                       sem.wait();
-                               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)
-                               sem.wait(stamp-t);
+                       if(timeout && (!deadline || t<deadline))
+                       {
+                               SetFlag setf(blocking);
+                               mutex.unlock();
+                               if(stamp && (!deadline || stamp<deadline))
+                                       sem.wait(stamp-t);
+                               else if(deadline)
+                                       sem.wait(deadline-t);
+                               else
+                                       sem.wait();
+                               mutex.lock();
+                               // The slots may have changed while waiting so check again
+                               continue;
+                       }
                        else
                                return;
                }
@@ -84,7 +113,7 @@ void Timer::tick(bool block)
                if(next->signal_timeout.emit() && next->increment())
                {
                        MutexLock l(mutex);
-                       slots.push_back(next);
+                       slots.push_back({ next });
                        push_heap(slots.begin(), slots.end());
                }
                else
@@ -123,10 +152,6 @@ bool Timer::Slot::increment()
 }
 
 
-Timer::SlotProxy::SlotProxy(Slot *s):
-       slot(s)
-{ }
-
 bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
 {
        return slot->get_timeout()>sp.slot->get_timeout();