]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timer.cpp
Rewrite Timer to use a heap instead of set to deal with duplicate timeouts
[libs/core.git] / source / time / timer.cpp
index c6e546fc78b782a127a4603076e7c0a0ae7f03d4..9471463216206b701d285562bb41e0af1901fafc 100644 (file)
@@ -1,10 +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"
 
@@ -15,7 +16,7 @@ namespace Time {
 
 Timer::~Timer()
 {
-       for(set<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
+       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
                delete i->slot;
 }
 
@@ -23,7 +24,8 @@ Timer::Slot &Timer::add(const TimeDelta &td)
 {
        Slot *s=new Slot(td);
        mutex.lock();
-       slots.insert(s);
+       slots.push_back(s);
+       push_heap(slots.begin(), slots.end());
        mutex.unlock();
        sem.signal();
        return *s;
@@ -34,7 +36,8 @@ Timer::Slot &Timer::add(const TimeStamp &ts)
        Slot *s=new Slot(ts);
        {
                MutexLock l(mutex);
-               slots.insert(s);
+               slots.push_back(s);
+               push_heap(slots.begin(), slots.end());
        }
        sem.signal();
        return *s;
@@ -43,36 +46,62 @@ Timer::Slot &Timer::add(const TimeStamp &ts)
 void Timer::cancel(Slot &slot)
 {
        MutexLock l(mutex);
-       if(slots.erase(&slot))
-               delete &slot;
+       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())
-       {
-               if(block)
-                       sem.wait();
-               else
-                       return;
-       }
-
-       Slot *next;
+       Slot *next=0;
        {
                MutexLock l(mutex);
-               next=slots.begin()->slot;
+               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();
        }
 
-       const TimeStamp &stamp=next->get_timeout();
-       const TimeStamp t=now();
-       if(stamp<=t || (block && sem.wait(stamp-t)==1))
+       try
        {
-               slots.erase(slots.begin());
                if(next->signal_timeout.emit() && next->increment())
-                       slots.insert(next);
+               {
+                       MutexLock l(mutex);
+                       slots.push_back(next);
+                       push_heap(slots.begin(), slots.end());
+               }
                else
                        delete next;
        }
+       catch(...)
+       {
+               delete next;
+               throw;
+       }
 }
 
 TimeStamp Timer::get_next_timeout() const
@@ -107,7 +136,7 @@ Timer::SlotProxy::SlotProxy(Slot *s):
 
 bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
 {
-       return slot->get_timeout()<sp.slot->get_timeout();
+       return slot->get_timeout()>sp.slot->get_timeout();
 }
 
 } // namespace Time