Lines Matching refs:timer

29 #include "utils/signals/timer.hpp"
68 /// \param delta The time to the first activation of the programmed timer.
70 /// existing system timer.
83 throw signals::system_error("Failed to program system's interval timer",
89 /// Deadline scheduler for all user timers on top of the unique system timer.
95 typedef std::set< signals::timer* > timers_set;
98 typedef std::vector< signals::timer* > timers_vector;
106 /// The original timer before any timer was programmed.
112 /// Time of the current activation of the timer.
118 /// Adds a timer to the _all_timers map.
120 /// \param timer The timer to add.
122 add_to_all_timers(signals::timer* timer)
124 timers_set& timers = _all_timers[timer->when()];
125 INV(timers.find(timer) == timers.end());
126 timers.insert(timer);
129 /// Removes a timer from the _all_timers map.
132 /// removal of the timer causes its bucket to be emptied.
134 /// \param timer The timer to remove.
136 remove_from_all_timers(signals::timer* timer)
138 // We may not find the timer in _all_timers if the timer has fired,
141 timer->when());
144 INV(timers.find(timer) != timers.end());
145 timers.erase(timer);
185 /// Adjusts the global system timer to point to the next activation.
198 // leave the global timer as is.
225 LD(F("Reprogramming timer; firing on %s; now is %s") % next % now);
232 /// Programs the first timer.
234 /// The programming of the first timer involves setting up the SIGALRM
235 /// handler and installing a timer handler for the first time, which in turn
238 /// \param timer The timer being programmed.
242 global_state(signals::timer* timer, const datetime::timestamp& now) :
243 _timer_activation(timer->when())
245 PRE(now < timer->when());
249 const datetime::delta delta = timer->when() - now;
250 LD(F("Installing first timer; firing on %s; now is %s") %
251 timer->when() % now);
257 _timer_activation = timer->when();
258 add_to_all_timers(timer);
267 /// This clears the global system timer and unsets the SIGALRM handler.
275 UNREACHABLE_MSG("Failed to restore original timer");
282 /// Programs a new timer, possibly adjusting the global system timer.
284 /// Programming any timer other than the first one only involves reloading
285 /// the existing timer, not backing up the previous handler nor installing a
288 /// \param timer The timer being programmed.
293 program_new(signals::timer* timer, const datetime::timestamp& now)
297 add_to_all_timers(timer);
301 /// Unprograms a timer.
303 /// This removes the timer from the global state and reprograms the global
304 /// system timer if necessary.
306 /// \param timer The timer to unprogram.
308 /// \return True if the system interval timer has been reprogrammed to
309 /// another future timer; false if there are no more active timers.
311 unprogram(signals::timer* timer)
315 LD(F("Unprogramming timer; previously firing on %s") % timer->when());
317 remove_from_all_timers(timer);
353 /// SIGALRM handler for the timer implementation.
367 /// Indirection to invoke the private do_fired() method of a timer.
369 /// \param timer The timer on which to run do_fired().
371 utils::signals::detail::invoke_do_fired(timer* timer)
373 timer->do_fired();
377 /// Internal implementation for the timer.
379 /// We assume that there is a 1-1 mapping between timer objects and impl
381 /// module breaks as well because we use pointers to the parent timer as the
382 /// identifier of the timer.
383 struct utils::signals::timer::impl : utils::noncopyable {
384 /// Timestamp when this timer is expected to fire.
386 /// Note that the timer might be processed after this timestamp, so users of
394 /// Whether this timer has fired already or not.
402 /// \param when_ Timestamp when this timer is expected to fire.
414 /// Constructor; programs a run-once timer.
416 /// This programs the global timer and signal handler if this is the first timer
417 /// being installed. Otherwise, reprograms the global timer if this timer
420 /// \param delta The time until the timer fires.
421 signals::timer::timer(const datetime::delta& delta)
435 /// Destructor; unprograms the timer if still programmed.
440 signals::timer::~timer(void)
445 LW("Auto-destroying still-programmed signals::timer object");
456 LW("Expired timer never fired; the code never called unprogram()!");
462 /// Returns the time of the timer activation.
465 /// the future or in the past) nor the timer's activation status.
467 signals::timer::when(void) const
473 /// Callback for the SIGALRM handler when this timer expires.
478 signals::timer::do_fired(void)
486 /// User-provided callback to run when the timer expires.
488 /// The default callback does nothing. We record the activation of the timer
494 signals::timer::callback(void)
500 /// Checks whether the timer has fired already or not.
502 /// \return Returns true if the timer has fired.
504 signals::timer::fired(void) const
510 /// Unprograms the timer.
512 /// \pre The timer is programmed (i.e. this can only be called once).
514 /// \post If the timer never fired asynchronously because the signal delivery
515 /// did not arrive on time, make sure we invoke the timer's callback here.
517 /// \throw system_error If unprogramming the timer failed.
519 signals::timer::unprogram(void)
524 // We cannot assert that the timer is not programmed because it might
527 LD("Called unprogram on already-unprogrammed timer; possibly just "
537 // Handle the case where the timer has expired before we ever got its
542 LW(F("Firing expired timer on destruction (was to fire on %s)") %