Rewrite Time::Timer to not force the use of a thread, so it's more useful.
authorMikko Rasa <tdb@tdb.fi>
Fri, 19 Oct 2007 12:21:52 +0000 (12:21 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 19 Oct 2007 12:21:52 +0000 (12:21 +0000)
source/time/timer.cpp
source/time/timer.h

index e716d322c68bad10cad62efc1a8a8600e737bd17..9fc82f57e872bf0b0c1f1dcc3ee8f974003eafd5 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,90 @@ 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():
+       slots(slot_compare)
+{ }
 
 Timer::~Timer()
 {
-       MutexLock l(set_mutex);
-       timers.erase(this);
-       thread.nudge();
+       while(!slots.empty())
+       {
+               delete slots.top();
+               slots.pop();
+       }
 }
 
-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);
+       mutex.lock();
+       slots.push(s);
+       mutex.unlock();
+       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)
 {
+       Slot *s=new Slot(ts);
+       mutex.lock();
+       slots.push(s);
+       mutex.unlock();
        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::tick(bool block)
 {
-       if(!done)
+       if(slots.empty())
        {
-               done=true;
-               sem.signal();
+               if(block)
+                       sem.wait();
+               return;
        }
 
-       join();
-}
+       mutex.lock();
+       Slot *next=slots.top();
+       mutex.unlock();
 
-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.pop();
+               if(next->signal_timeout.emit() && next->increment())
+                       slots.push(next);
                else
-                       sem.wait();
+                       delete next;
        }
 }
 
-/**
-Creates the thread if it doesn't exist, otherwise nudges it.
-*/
-void Timer::ThreadProxy::nudge()
+bool Timer::slot_compare(Slot *a, Slot *b)
 {
-       if(!thread)
-               thread=new Thread();
-       else
-               thread->nudge();
+       return *a<*b;
 }
 
-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;
+}
+
+bool Timer::Slot::operator<(const Slot &other) const
+{
+       return timeout<other.timeout;
 }
 
 } // namespace Time
index 369a78c96e089a1e1413fe2a360b241b43b5f2b7..cfa00bb0d1511f42f09591b04dbc5527ee8deb91 100644 (file)
@@ -3,14 +3,14 @@ This file is part of libmspcore
 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
+
 #ifndef MSP_TIME_TIMER_H_
 #define MSP_TIME_TIMER_H_
 
-#include <set>
+#include <queue>
 #include <sigc++/sigc++.h>
 #include "../core/mutex.h"
 #include "../core/semaphore.h"
-#include "../core/thread.h"
 #include "timedelta.h"
 #include "timestamp.h"
 
@@ -18,58 +18,65 @@ namespace Msp {
 namespace Time {
 
 /**
-A class for executing functions periodically.  Every time the timeout is
-reached, signal_timeout will be emitted.  If the functor connected to this
-signal returns true, the timer is rescheduled by incrementing the timeout
-by the interval.  Otherwise the timer is canceled.
+A class for executing functions in a deferred or periodical fashion.  The add a
+timer, use one of the add functions and connect a functor to the timeout signal
+of the returned slot.
 
-A separate thread is used for running the timers.  All signal emissions will
-happen in this thread - be careful with your variables.
+This class is thread-safe, to allow running timers in a separate thread.
 */
 class Timer
 {
 public:
-       sigc::signal<bool> signal_timeout;
-
-       Timer(const Time::TimeDelta &);
-       const Time::TimeStamp &get_timeout() const { return timeout; }
-       ~Timer();
-private:
-       /**
-       A thread to run the timers independently of the rest of the program.
-       */
-       class Thread: public Msp::Thread
+       class Slot
        {
        public:
-               Thread();
-               void nudge();
-               void finish();
+               sigc::signal<bool> signal_timeout;
+       
        private:
-               bool done;
-               Semaphore sem;
-
-               void main();
+               TimeDelta interval;
+               TimeStamp timeout;
+       
+       public:
+               Slot(const TimeDelta &);
+               Slot(const TimeStamp &);
+               const TimeStamp &get_timeout() const { return timeout; }
+               bool increment();
+               bool operator<(const Slot &) const;
        };
 
+private:
+       typedef bool (*fSlotCompare)(Slot *, Slot *);
+
+       std::priority_queue<Slot *, std::vector<Slot *>, fSlotCompare> slots;
+       Semaphore sem;
+       Mutex mutex;
+
+public:
+       Timer();
+       ~Timer();
+
        /**
-       Proxy class to handle automatic starting and termination of the thread.
+       Adds a timer that will be executed periodically as long as the timeout
+       signal hander returns true.
        */
-       class ThreadProxy
-       {
-       public:
-               ThreadProxy(): thread(0) { }
-               void nudge();
-               ~ThreadProxy();
-       private:
-               Thread *thread;
-       };
+       Slot &add(const TimeDelta &);
 
-       Time::TimeDelta interval;
-       Time::TimeStamp timeout;
+       /**
+       Adds a timer that will be executed once at a specific time.  The return
+       value of the timeout signal handler is ignored.
+       */
+       Slot &add(const TimeStamp &);
+
+       /**
+       Checks all timers, executing any that have timed out.  If block is true,
+       waits until one times out.
 
-       static ThreadProxy thread;
-       static Mutex       set_mutex;
-       static std::set<Timer *> timers;
+       Note: If there are no active timers when a blocking tick is executed, it
+       won't return until a timer is added from another thread.
+       */
+       void tick(bool block=true);
+private:
+       static bool slot_compare(Slot *, Slot *);
 };
 
 } // namespace Time