1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
17 for(const SlotProxy &s: slots)
21 Timer::Slot &Timer::add(const TimeDelta &td)
23 Slot *s = new Slot(td);
25 slots.push_back({ s });
26 push_heap(slots.begin(), slots.end());
32 Timer::Slot &Timer::add(const TimeStamp &ts)
34 Slot *s = new Slot(ts);
36 slots.push_back({ s });
37 push_heap(slots.begin(), slots.end());
43 void Timer::cancel(Slot &slot)
46 auto i = find_member(slots, &slot, &SlotProxy::slot);
51 make_heap(slots.begin(), slots.end());
60 void Timer::tick(const TimeDelta &timeout)
63 throw invalid_argument("Timer::tick");
68 void Timer::do_tick(const TimeDelta &timeout)
72 deadline = now()+timeout;
83 next = slots.begin()->slot;
84 stamp = next->get_timeout();
89 if(timeout && (!deadline || t<deadline))
91 SetFlag setf(blocking);
93 if(stamp && (!deadline || stamp<deadline))
100 // The slots may have changed while waiting so check again
107 pop_heap(slots.begin(), slots.end());
113 if(next->signal_timeout.emit() && next->increment())
116 slots.push_back({ next });
117 push_heap(slots.begin(), slots.end());
129 TimeStamp Timer::get_next_timeout() const
133 return slots.begin()->slot->get_timeout();
137 Timer::Slot::Slot(const TimeDelta &td):
139 timeout(now()+interval)
142 Timer::Slot::Slot(const TimeStamp &ts):
146 bool Timer::Slot::increment()
155 bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
157 return slot->get_timeout()>sp.slot->get_timeout();