-/*
-This file is part of libmspcore
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <algorithm>
+#include <msp/core/raii.h>
#include "timer.h"
#include "utils.h"
namespace Msp {
namespace Time {
+Timer::Timer():
+ sem(1),
+ blocking(false)
+{ }
+
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;
}
Timer::Slot &Timer::add(const TimeDelta &td)
{
- Slot *s=new Slot(td);
- mutex.lock();
- slots.insert(s);
- mutex.unlock();
- sem.signal();
+ Slot *s = new Slot(td);
+ MutexLock l(mutex);
+ slots.push_back(s);
+ push_heap(slots.begin(), slots.end());
+ if(blocking)
+ sem.signal();
return *s;
}
Timer::Slot &Timer::add(const TimeStamp &ts)
{
- Slot *s=new Slot(ts);
- {
- MutexLock l(mutex);
- slots.insert(s);
- }
- 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;
}
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;
- }
+ if(block)
+ tick();
+ else
+ tick(zero);
+}
+
+void Timer::tick()
+{
+ do_tick(-sec);
+}
+
+void Timer::tick(const TimeDelta &timeout)
+{
+ if(timeout<zero)
+ throw invalid_argument("Timer::tick");
- Slot *next;
+ do_tick(timeout);
+}
+
+void Timer::do_tick(const TimeDelta &timeout)
+{
+ TimeStamp deadline;
+ if(timeout>=zero)
+ deadline = now()+timeout;
+
+ Slot *next = 0;
{
MutexLock l(mutex);
- next=slots.begin()->slot;
+ while(1)
+ {
+ TimeStamp stamp;
+ TimeStamp t = now();
+ if(!slots.empty())
+ {
+ next = slots.begin()->slot;
+ stamp = next->get_timeout();
+ if(stamp<=t)
+ break;
+ }
+
+ if(timeout && (!deadline || t<deadline))
+ {
+ SetFlag setf(blocking);
+ mutex.unlock();
+ if(stamp && (!deadline || stamp<deadline))
+ sem.wait(stamp-t);
+ else if(deadline)
+ sem.wait(deadline-t);
+ else
+ sem.wait();
+ mutex.lock();
+ // The slots may have changed while waiting so check again
+ continue;
+ }
+ 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
{
if(!interval)
return false;
- timeout+=interval;
+ timeout += interval;
return true;
}
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