struct timeval *tv,
const int et_const_retry)
{
+ if (!et->defined)
+ {
+ return false;
+ }
+
bool ret = false;
- const time_t local_now = now;
+ time_t wakeup = event_timeout_remaining(et);
- if (et->defined)
+ if (wakeup <= 0)
{
- time_t wakeup = et->last - local_now + et->n;
- if (wakeup <= 0)
- {
#if INTERVAL_DEBUG
- dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n,
- et_const_retry);
+ dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n,
+ et_const_retry);
#endif
- if (et_const_retry < 0)
- {
- et->last = local_now;
- wakeup = et->n;
- ret = true;
- }
- else
- {
- wakeup = et_const_retry;
- }
+ if (et_const_retry < 0)
+ {
+ et->last = now;
+ wakeup = et->n;
+ ret = true;
}
-
- if (tv && wakeup < tv->tv_sec)
+ else
{
+ wakeup = et_const_retry;
+ }
+ }
+
+ if (tv && wakeup < tv->tv_sec)
+ {
#if INTERVAL_DEBUG
- dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d",
- (int) wakeup, et->n, et_const_retry);
+ dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d",
+ (int) wakeup, et->n, et_const_retry);
#endif
- tv->tv_sec = wakeup;
- tv->tv_usec = 0;
- }
+ tv->tv_sec = wakeup;
+ tv->tv_usec = 0;
}
return ret;
}
struct event_timeout
{
- bool defined;
- interval_t n;
- time_t last; /* time of last event */
+ bool defined; /**< This timeout is active */
+ interval_t n; /**< periodic interval for periodic timeouts */
+ time_t last; /**< time of last event */
};
static inline bool
{
return et->defined;
}
-
+/**
+ * Clears the timeout and reset all values to 0. Following timer checks will
+ * not trigger.
+ *
+ * @param et timer struct
+ */
static inline void
event_timeout_clear(struct event_timeout *et)
{
et->last = 0;
}
-static inline struct event_timeout
-event_timeout_clear_ret(void)
-{
- struct event_timeout ret;
- event_timeout_clear(&ret);
- return ret;
-}
+/**
+ * Initialises a timer struct. The timer will become true/trigger after
+ * last + n seconds.
+ *
+ *
+ * @param et Timer struct
+ * @param n Interval of the timer for periodic timer. A negative
+ * value for n will be interpreted as 0.
+ * @param last Sets the base time of the timer.
+ */
static inline void
-event_timeout_init(struct event_timeout *et, interval_t n, const time_t local_now)
+event_timeout_init(struct event_timeout *et, interval_t n, const time_t last)
{
et->defined = true;
et->n = (n >= 0) ? n : 0;
- et->last = local_now;
+ et->last = last;
}
+/**
+ * Resets a timer.
+ *
+ * Sets the last time the timer has been triggered for the calculation of the
+ * next event.
+ * @param et
+ */
static inline void
event_timeout_reset(struct event_timeout *et)
{
}
}
+/**
+ * Sets the interval n of a timeout.
+ * @param et
+ * @param n Interval value of the timer, negative values
+ * will be interpreted as 0
+ *
+ * @note you might need to call reset_coarse_timers after this
+ */
static inline void
event_timeout_modify_wakeup(struct event_timeout *et, interval_t n)
{
- /* note that you might need to call reset_coarse_timers after this */
if (et->defined)
{
et->n = (n >= 0) ? n : 0;
}
}
-/*
- * Will return the time left for a timeout, this function does not check
- * if the timeout is actually valid
+/**
+ * Returns the time until the timeout should triggered, from \c now.
+ * This function does not check if the timeout is actually valid.
*/
static inline interval_t
event_timeout_remaining(struct event_timeout *et)
{
- return (interval_t) (et->last - now + et->n);
+ return (interval_t) ((et->last + et->n) - now);
}
-/*
- * This is the principal function for testing and triggering recurring
- * timers and will return true on a timer signal event.
- * If et_const_retry == ETT_DEFAULT and a signal occurs,
- * the function will return true and *et will be armed for the
- * next event. If et_const_retry >= 0 and a signal occurs,
- * *et will not be touched, but *tv will be set to
- * minimum (*tv, et_const_retry) for a future re-test,
- * and the function will return true.
- */
-
#define ETT_DEFAULT (-1)
+/**
+ * This is the principal function for testing and triggering recurring
+ * timers.
+ *
+ * If *et is not triggered, *tv is set to remaining time until the timeout if
+ * not already lower:
+ *
+ * *tv = minimum(*tv, event_timeout_remaining(*et))
+ *
+ * If *et triggers and et_const_retry is negative (ETT_DEFAULT is -1):
+ * - the function will return true
+ * - *et will be armed for the next event (et->last set to now).
+ * - *tv will be lowered to the event period (n) if larger than the
+ * period of the event (set to *et's next timeout)
+ * - *et will not changed (ie also not rearmed, stays armed)
+ *
+ * If *et triggers and et_const_retry >= 0, *tv will be lowered to et_const_try
+ * if larger:
+ *
+ * *tv = *minimum(*tv, et_const_retry)
+ *
+ * This is mainly useful if the trigger cannot yet be triggered for other
+ * reasons and a backoff timeout should be returned if the timer is ready
+ * to trigger.
+ *
+ *
+ * @param et the timeout to check
+ * @param tv will be set to timeout for next check for this
+ * timeout unless already smaller.
+ * @param et_const_retry see above
+ * @return if the timeout has triggered and event has been reset
+ */
bool event_timeout_trigger(struct event_timeout *et,
struct timeval *tv,
- const int et_const_retry);
+ int et_const_retry);
/*
* Measure time intervals in microseconds