2 #include <msp/core/raii.h>
18 for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
22 Timer::Slot &Timer::add(const TimeDelta &td)
24 Slot *s = new Slot(td);
27 push_heap(slots.begin(), slots.end());
33 Timer::Slot &Timer::add(const TimeStamp &ts)
35 Slot *s = new Slot(ts);
38 push_heap(slots.begin(), slots.end());
44 void Timer::cancel(Slot &slot)
47 for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
52 make_heap(slots.begin(), slots.end());
57 void Timer::tick(bool block)
70 void Timer::tick(const TimeDelta &timeout)
73 throw invalid_argument("Timer::tick");
78 void Timer::do_tick(const TimeDelta &timeout)
82 deadline = now()+timeout;
93 next = slots.begin()->slot;
94 stamp = next->get_timeout();
99 if(timeout && (!deadline || t<deadline))
101 SetFlag setf(blocking);
103 if(stamp && (!deadline || stamp<deadline))
106 sem.wait(deadline-t);
110 // The slots may have changed while waiting so check again
117 pop_heap(slots.begin(), slots.end());
123 if(next->signal_timeout.emit() && next->increment())
126 slots.push_back(next);
127 push_heap(slots.begin(), slots.end());
139 TimeStamp Timer::get_next_timeout() const
143 return slots.begin()->slot->get_timeout();
147 Timer::Slot::Slot(const TimeDelta &td):
149 timeout(now()+interval)
152 Timer::Slot::Slot(const TimeStamp &ts):
156 bool Timer::Slot::increment()
165 Timer::SlotProxy::SlotProxy(Slot *s):
169 bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
171 return slot->get_timeout()>sp.slot->get_timeout();