1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/time/Time.hpp" 5 #include "hmbdc/Exception.hpp" 6 #include "hmbdc/Compile.hpp" 14 #include <boost/intrusive/set.hpp> 17 namespace hmbdc {
namespace time {
19 namespace timers_detail {
20 void noop(TimerManager&, SysTime
const&);
24 : boost::intrusive::set_base_hook<
25 boost::intrusive::link_mode<boost::intrusive::normal_link>
30 SysTime getFireAt()
const {
return fireAt_; }
33 using Callback = std::function<void (TimerManager&, SysTime const&)>;
34 Timer(Callback cb = timers_detail::noop)
38 void setCallback(Callback cb) {
41 bool operator < (
Timer const& other)
const 42 {
return fireAt_ < other.fireAt_; }
43 bool operator <= (
SysTime const& t)
const 44 {
return !(t < fireAt_); }
46 bool scheduled()
const {
63 namespace timers_detail {
80 timer.fireAt_ = fireAt;
81 timers_.insert(timer);
91 auto range = timers_.equal_range(timer);
92 for (
auto it = range.first; it != range.second; ++it) {
104 auto res = std::numeric_limits<Duration>::max();
105 if (timers_.begin() != timers_.end()) {
106 res = std::max(
Duration(0), timers_.begin()->fireAt_ - SysTime::now());
112 using Timers = boost::intrusive::multiset<Timer>;
120 , interval_(interval)
128 { tm.
schedule(now + interval_, *
this); };
132 DailyTimer(Callback callback = timers_detail::noop)
137 Duration day = Duration::seconds(86400);
138 SysTime newFireTime = getFireAt() + day;
139 while (newFireTime < now) newFireTime += day;
157 for (
auto it = timers_.begin(); it != timers_.end() && hmbdc_unlikely(*it <= now);) {
158 if (hmbdc_unlikely((*it).armed)) {
160 (*it).fired(*
this, now);
161 it = timers_.begin();
167 for (
auto it = timers_.begin(); it != timers_.end() && hmbdc_unlikely(*it <= now);) {
171 (*tmp).reschedule(*
this, now);
Definition: Timers.hpp:23
Definition: Timers.hpp:70
Definition: Timers.hpp:131
void cancel(Timer &timer)
cancel a timer previously scheduled with the TimerManager
Definition: Timers.hpp:90
Definition: Timers.hpp:144
void schedule(SysTime fireAt, Timer &timer)
schedule the timer to start at a specific time
Definition: Timers.hpp:79
Definition: Timers.hpp:117