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