]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timer.cpp
Move non-oneliner functions out of RefPtr class declaration
[libs/core.git] / source / time / timer.cpp
index 3d4aec5ff4a8f6307ceaea732484e8c779b89f10..e0af57cc78fcc41363632e169e33a8219653e9f1 100644 (file)
@@ -1,8 +1,5 @@
-/*
-This file is part of libmspframework     
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
+#include <algorithm>
+#include <msp/core/raii.h>
 #include "timer.h"
 #include "utils.h"
 
@@ -11,107 +8,167 @@ using namespace std;
 namespace Msp {
 namespace Time {
 
-Timer::Timer(const Time::TimeDelta &d):
-       interval(d),
-       timeout(now()+d)
+Timer::Timer():
+       sem(1),
+       blocking(false)
+{ }
+
+Timer::~Timer()
 {
-       MutexLock l(set_mutex);
-       timers.insert(this);
-       thread.nudge();
+       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
+               delete i->slot;
 }
 
-Timer::~Timer()
+Timer::Slot &Timer::add(const TimeDelta &td)
 {
-       MutexLock l(set_mutex);
-       timers.erase(this);
-       thread.nudge();
+       Slot *s = new Slot(td);
+       MutexLock l(mutex);
+       slots.push_back(s);
+       push_heap(slots.begin(), slots.end());
+       if(blocking)
+               sem.signal();
+       return *s;
 }
 
-Timer::ThreadProxy Timer::thread;
-Mutex Timer::set_mutex;
-set<Timer *> Timer::timers;
+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());
+       if(blocking)
+               sem.signal();
+       return *s;
+}
 
-Timer::Thread::Thread():
-       done(false)
+void Timer::cancel(Slot &slot)
 {
-       launch();
+       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;
+               }
 }
 
-/**
-Notifies the thread that a change in the timers occurred.
-*/
-void Timer::Thread::nudge()
+void Timer::tick(bool block)
 {
-       sem.signal();
+       if(block)
+               tick();
+       else
+               tick(zero);
 }
 
-/**
-Tells the thread to finish and terminate gracefully.  This function will return
-after the thread has terminated.
-*/
-void Timer::Thread::finish()
+void Timer::tick()
 {
-       if(!done)
-       {
-               done=true;
-               sem.signal();
-       }
+       do_tick(-sec);
+}
 
-       join();
+void Timer::tick(const TimeDelta &timeout)
+{
+       if(timeout<zero)
+               throw invalid_argument("Timer::tick");
+
+       do_tick(timeout);
 }
 
-void Timer::Thread::main()
+void Timer::do_tick(const TimeDelta &timeout)
 {
-       while(!done)
+       TimeStamp deadline;
+       if(timeout>=zero)
+               deadline = now()+timeout;
+
+       Slot *next = 0;
        {
-               set_mutex.lock();
-               Timer     *next=0;
-               TimeStamp next_ts;
-               for(set<Timer *>::iterator i=timers.begin(); i!=timers.end(); ++i)
+               MutexLock l(mutex);
+               while(1)
                {
-                       const TimeStamp &ts=(*i)->get_timeout();
-                       if(ts<next_ts || !next)
+                       TimeStamp stamp;
+                       TimeStamp t = now();
+                       if(!slots.empty())
                        {
-                               next_ts=ts;
-                               next=*i;
+                               next = slots.begin()->slot;
+                               stamp = next->get_timeout();
+                               if(stamp<=t)
+                                       break;
                        }
-               }
-               set_mutex.unlock();
 
-               if(next)
-               {
-                       const TimeStamp t=now();
-                       if(next_ts<=t || sem.wait(next_ts-t)==1)
+                       if(timeout && (!deadline || t<deadline))
                        {
-                               if(next->signal_timeout.emit())
-                                       next->timeout+=next->interval;
+                               SetFlag setf(blocking);
+                               mutex.unlock();
+                               if(stamp && (!deadline || stamp<deadline))
+                                       sem.wait(stamp-t);
+                               else if(deadline)
+                                       sem.wait(deadline-t);
                                else
-                                       delete next;
+                                       sem.wait();
+                               mutex.lock();
+                               // The slots may have changed while waiting so check again
+                               continue;
                        }
+                       else
+                               return;
+               }
+
+               pop_heap(slots.begin(), slots.end());
+               slots.pop_back();
+       }
+
+       try
+       {
+               if(next->signal_timeout.emit() && next->increment())
+               {
+                       MutexLock l(mutex);
+                       slots.push_back(next);
+                       push_heap(slots.begin(), slots.end());
                }
                else
-                       sem.wait();
+                       delete next;
+       }
+       catch(...)
+       {
+               delete next;
+               throw;
        }
 }
 
-/**
-Creates the thread if it doesn't exist, otherwise nudges it.
-*/
-void Timer::ThreadProxy::nudge()
+TimeStamp Timer::get_next_timeout() const
 {
-       if(!thread)
-               thread=new Thread();
-       else
-               thread->nudge();
+       if(slots.empty())
+               return TimeStamp();
+       return slots.begin()->slot->get_timeout();
 }
 
-Timer::ThreadProxy::~ThreadProxy()
+
+Timer::Slot::Slot(const TimeDelta &td):
+       interval(td),
+       timeout(now()+interval)
+{ }
+
+Timer::Slot::Slot(const TimeStamp &ts):
+       timeout(ts)
+{ }
+
+bool Timer::Slot::increment()
 {
-       if(thread)
-       {
-               thread->finish();
-               delete thread;
-       }
+       if(!interval)
+               return false;
+       timeout += interval;
+       return true;
+}
+
+
+Timer::SlotProxy::SlotProxy(Slot *s):
+       slot(s)
+{ }
+
+bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
+{
+       return slot->get_timeout()>sp.slot->get_timeout();
 }
 
 } // namespace Time