1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
18 for(const SlotProxy &s: slots)
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 auto i = find_member(slots, &slot, &SlotProxy::slot);
52 make_heap(slots.begin(), slots.end());
61 void Timer::tick(const TimeDelta &timeout)
64 throw invalid_argument("Timer::tick");
69 void Timer::do_tick(const TimeDelta &timeout)
73 deadline = now()+timeout;
84 next = slots.begin()->slot;
85 stamp = next->get_timeout();
90 if(timeout && (!deadline || t<deadline))
92 SetFlag setf(blocking);
94 if(stamp && (!deadline || stamp<deadline))
101 // The slots may have changed while waiting so check again
108 pop_heap(slots.begin(), slots.end());
114 if(next->signal_timeout.emit() && next->increment())
117 slots.push_back(next);
118 push_heap(slots.begin(), slots.end());
130 TimeStamp Timer::get_next_timeout() const
134 return slots.begin()->slot->get_timeout();
138 Timer::Slot::Slot(const TimeDelta &td):
140 timeout(now()+interval)
143 Timer::Slot::Slot(const TimeStamp &ts):
147 bool Timer::Slot::increment()
156 Timer::SlotProxy::SlotProxy(Slot *s):
160 bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
162 return slot->get_timeout()>sp.slot->get_timeout();