]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timer.cpp
Add LogicError exception class
[libs/core.git] / source / time / timer.cpp
index 9fc82f57e872bf0b0c1f1dcc3ee8f974003eafd5..9471463216206b701d285562bb41e0af1901fafc 100644 (file)
@@ -1,9 +1,11 @@
-/*
+/* $Id$
+
 This file is part of libmspcore     
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
+Copyright © 2006, 2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <algorithm>
 #include "timer.h"
 #include "utils.h"
 
@@ -12,24 +14,18 @@ using namespace std;
 namespace Msp {
 namespace Time {
 
-Timer::Timer():
-       slots(slot_compare)
-{ }
-
 Timer::~Timer()
 {
-       while(!slots.empty())
-       {
-               delete slots.top();
-               slots.pop();
-       }
+       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
+               delete i->slot;
 }
 
 Timer::Slot &Timer::add(const TimeDelta &td)
 {
        Slot *s=new Slot(td);
        mutex.lock();
-       slots.push(s);
+       slots.push_back(s);
+       push_heap(slots.begin(), slots.end());
        mutex.unlock();
        sem.signal();
        return *s;
@@ -38,41 +34,81 @@ Timer::Slot &Timer::add(const TimeDelta &td)
 Timer::Slot &Timer::add(const TimeStamp &ts)
 {
        Slot *s=new Slot(ts);
-       mutex.lock();
-       slots.push(s);
-       mutex.unlock();
+       {
+               MutexLock l(mutex);
+               slots.push_back(s);
+               push_heap(slots.begin(), slots.end());
+       }
        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;
+               }
+}
+
 void Timer::tick(bool block)
 {
-       if(slots.empty())
+       Slot *next=0;
        {
-               if(block)
-                       sem.wait();
-               return;
+               MutexLock l(mutex);
+               while(1)
+               {
+                       if(slots.empty())
+                       {
+                               if(block)
+                                       sem.wait();
+                               else
+                                       return;
+                       }
+
+                       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);
+                       else
+                               return;
+               }
+
+               pop_heap(slots.begin(), slots.end());
+               slots.pop_back();
        }
 
-       mutex.lock();
-       Slot *next=slots.top();
-       mutex.unlock();
-
-       const TimeStamp &stamp=next->get_timeout();
-       const TimeStamp t=now();
-       if(stamp<=t || (block && sem.wait(stamp-t)==1))
+       try
        {
-               slots.pop();
                if(next->signal_timeout.emit() && next->increment())
-                       slots.push(next);
+               {
+                       MutexLock l(mutex);
+                       slots.push_back(next);
+                       push_heap(slots.begin(), slots.end());
+               }
                else
                        delete next;
        }
+       catch(...)
+       {
+               delete next;
+               throw;
+       }
 }
 
-bool Timer::slot_compare(Slot *a, Slot *b)
+TimeStamp Timer::get_next_timeout() const
 {
-       return *a<*b;
+       if(slots.empty())
+               return TimeStamp();
+       return slots.begin()->slot->get_timeout();
 }
 
 
@@ -93,9 +129,14 @@ bool Timer::Slot::increment()
        return true;
 }
 
-bool Timer::Slot::operator<(const Slot &other) const
+
+Timer::SlotProxy::SlotProxy(Slot *s):
+       slot(s)
+{ }
+
+bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
 {
-       return timeout<other.timeout;
+       return slot->get_timeout()>sp.slot->get_timeout();
 }
 
 } // namespace Time