]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timer.cpp
Fix Timer to use is Semaphore correctly
[libs/core.git] / source / time / timer.cpp
index e716d322c68bad10cad62efc1a8a8600e737bd17..f26900b8d4a589769c707b59513de6aa2d8dc129 100644 (file)
@@ -1,8 +1,4 @@
-/*
-This file is part of libmspcore     
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
+#include <algorithm>
 #include "timer.h"
 #include "utils.h"
 
@@ -11,107 +7,143 @@ using namespace std;
 namespace Msp {
 namespace Time {
 
-Timer::Timer(const Time::TimeDelta &d):
-       interval(d),
-       timeout(now()+d)
-{
-       MutexLock l(set_mutex);
-       timers.insert(this);
-       thread.nudge();
-}
+Timer::Timer():
+       sem(1),
+       blocking(false)
+{ }
 
 Timer::~Timer()
 {
-       MutexLock l(set_mutex);
-       timers.erase(this);
-       thread.nudge();
+       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
+               delete i->slot;
 }
 
-Timer::ThreadProxy Timer::thread;
-Mutex Timer::set_mutex;
-set<Timer *> Timer::timers;
-
-Timer::Thread::Thread():
-       done(false)
+Timer::Slot &Timer::add(const TimeDelta &td)
 {
-       launch();
+       Slot *s = new Slot(td);
+       MutexLock l(mutex);
+       slots.push_back(s);
+       push_heap(slots.begin(), slots.end());
+       if(blocking)
+               sem.signal();
+       return *s;
 }
 
-/**
-Notifies the thread that a change in the timers occurred.
-*/
-void Timer::Thread::nudge()
+Timer::Slot &Timer::add(const TimeStamp &ts)
 {
-       sem.signal();
+       Slot *s = new Slot(ts);
+       MutexLock l(mutex);
+       slots.push_back(s);
+       push_heap(slots.begin(), slots.end());
+       if(blocking)
+               sem.signal();
+       return *s;
 }
 
-/**
-Tells the thread to finish and terminate gracefully.  This function will return
-after the thread has terminated.
-*/
-void Timer::Thread::finish()
+void Timer::cancel(Slot &slot)
 {
-       if(!done)
-       {
-               done=true;
-               sem.signal();
-       }
-
-       join();
+       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;
+               }
 }
 
-void Timer::Thread::main()
+void Timer::tick(bool block)
 {
-       while(!done)
+       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)
+                       if(slots.empty())
                        {
-                               next_ts=ts;
-                               next=*i;
+                               if(block)
+                               {
+                                       blocking = true;
+                                       mutex.unlock();
+                                       sem.wait();
+                                       mutex.lock();
+                               }
+                               else
+                                       return;
                        }
-               }
-               set_mutex.unlock();
 
-               if(next)
-               {
-                       const TimeStamp t=now();
-                       if(next_ts<=t || sem.wait(next_ts-t)==1)
+                       next = slots.begin()->slot;
+                       const TimeStamp &stamp = next->get_timeout();
+                       const TimeStamp t = now();
+                       if(stamp<=t)
+                               break;
+                       else if(block)
                        {
-                               if(next->signal_timeout.emit())
-                                       next->timeout+=next->interval;
-                               else
-                                       delete next;
+                               blocking = true;
+                               mutex.unlock();
+                               sem.wait(stamp-t);
+                               mutex.lock();
                        }
+                       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;
+       }
+}
+
+TimeStamp Timer::get_next_timeout() const
+{
+       if(slots.empty())
+               return TimeStamp();
+       return slots.begin()->slot->get_timeout();
 }
 
-/**
-Creates the thread if it doesn't exist, otherwise nudges it.
-*/
-void Timer::ThreadProxy::nudge()
+
+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=new Thread();
-       else
-               thread->nudge();
+       if(!interval)
+               return false;
+       timeout += interval;
+       return true;
 }
 
-Timer::ThreadProxy::~ThreadProxy()
+
+Timer::SlotProxy::SlotProxy(Slot *s):
+       slot(s)
+{ }
+
+bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
 {
-       if(thread)
-       {
-               thread->finish();
-               delete thread;
-       }
+       return slot->get_timeout()>sp.slot->get_timeout();
 }
 
 } // namespace Time