mirror of https://gerrit.osmocom.org/libosmocore
timer: Add function osmo_timer_remaining() to determine remainign time
This commit is contained in:
parent
3c16de2954
commit
e30b6ac5d1
|
@ -73,7 +73,9 @@ void osmo_timer_del(struct osmo_timer_list *timer);
|
||||||
|
|
||||||
int osmo_timer_pending(struct osmo_timer_list *timer);
|
int osmo_timer_pending(struct osmo_timer_list *timer);
|
||||||
|
|
||||||
|
int osmo_timer_remaining(const struct osmo_timer_list *timer,
|
||||||
|
const struct timeval *now,
|
||||||
|
struct timeval *remaining);
|
||||||
/*
|
/*
|
||||||
* internal timer list management
|
* internal timer list management
|
||||||
*/
|
*/
|
||||||
|
|
28
src/timer.c
28
src/timer.c
|
@ -126,6 +126,34 @@ int osmo_timer_pending(struct osmo_timer_list *timer)
|
||||||
return timer->active;
|
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(¤t_time, NULL);
|
||||||
|
now = ¤t_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
timersub(&timer->timeout, ¤t_time, remaining);
|
||||||
|
|
||||||
|
if (remaining->tv_sec < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* if we have a nearest time return the delta between the current
|
* if we have a nearest time return the delta between the current
|
||||||
* time and the time of the nearest timer.
|
* time and the time of the nearest timer.
|
||||||
|
|
Loading…
Reference in New Issue