aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2012-07-13 12:21:04 +0200
committerHarald Welte <laforge@gnumonks.org>2012-07-13 12:22:20 +0200
commite30b6ac5d1127c368184232dc749cef28d0cbb6c (patch)
tree2afac8b934cc4510981cfcf00432ebabf07fd12c /src
parent3c16de295480f269a1610c6d2cc559718c39c202 (diff)
timer: Add function osmo_timer_remaining() to determine remainign time
Diffstat (limited to 'src')
-rw-r--r--src/timer.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/timer.c b/src/timer.c
index cca2a239..6d4abc26 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -126,6 +126,34 @@ int osmo_timer_pending(struct osmo_timer_list *timer)
return timer->active;
}
+/*! \brief compute the remaining time of a timer
+ * \param[in] timer the to-be-checked timer
+ * \param[in] the current time (NULL if not known)
+ * \param[out] remaining remaining time until timer fires
+ * \return 0 if timer has not expired yet, -1 if it has
+ *
+ * This function can be used to determine the amount of time
+ * remaining until the expiration of the timer.
+ */
+int osmo_timer_remaining(const struct osmo_timer_list *timer,
+ const struct timeval *now,
+ struct timeval *remaining)
+{
+ struct timeval current_time;
+
+ if (!now) {
+ gettimeofday(&current_time, NULL);
+ now = &current_time;
+ }
+
+ timersub(&timer->timeout, &current_time, remaining);
+
+ if (remaining->tv_sec < 0)
+ return -1;
+
+ return 0;
+}
+
/*
* if we have a nearest time return the delta between the current
* time and the time of the nearest timer.