
Now that clock_t is an unsigned value, we can also allow the system uptime to wrap. Essentially, instead of using (a <= b) to see if time a occurs no later than time b, we use (b - a <= CLOCK_MAX / 2). The latter value does not exist, so instead we add TMRDIFF_MAX for that purpose. We must therefore also avoid using values like 0 and LONG_MAX as special values for absolute times. This patch extends the libtimers interface so that it no longer uses 0 to indicate "no timeout". Similarly, TMR_NEVER is now used as special value only when otherwise a relative time difference would be used. A minix_timer structure is now considered in use when it has a watchdog function set, rather than when the absolute expiry time is not TMR_NEVER. A few new macros in <minix/timers.h> help with timer comparison and obtaining properties from a minix_timer structure. This patch also eliminates the union of timer arguments, instead using the only union element that is only used (the integer). This prevents potential problems with e.g. live update. The watchdog function prototype is changed to pass in the argument value rather than a pointer to the timer structure, since obtaining the argument value was the only current use of the timer structure anyway. The result is a somewhat friendlier timers API. The VFS select code required a few more invasive changes to restrict the timer value to the new maximum, effectively matching the timer code in PM. As a side effect, select(2) has been changed to reject invalid timeout values. That required a change to the test set, which relied on the previous, erroneous behavior. Finally, while we're rewriting significant chunks of the timer code anyway, also covert it to KNF and add a few more explanatory comments. Change-Id: Id43165c3fbb140b32b90be2cca7f68dd646ea72e
49 lines
1.5 KiB
C
49 lines
1.5 KiB
C
#include <minix/timers.h>
|
|
|
|
/*
|
|
* Activate a timer to run function 'watchdog' at absolute time 'exp_time', as
|
|
* part of timers queue 'tmrs'. If the timer is already in use, it is first
|
|
* removed from the timers queue. Then, it is put in the list of active timers
|
|
* with the first to expire in front. The caller responsible for scheduling a
|
|
* new alarm for the timer if needed. To that end, the function returns three
|
|
* values: its return value (TRUE or FALSE) indicates whether there was an old
|
|
* head timer; if TRUE, 'old_head' (if non-NULL) is filled with the absolute
|
|
* expiry time of the old head timer. If 'new_head' is non-NULL, it is filled
|
|
* with the absolute expiry time of the new head timer.
|
|
*/
|
|
int
|
|
tmrs_settimer(minix_timer_t ** tmrs, minix_timer_t * tp, clock_t exp_time,
|
|
tmr_func_t watchdog, int arg, clock_t * old_head, clock_t * new_head)
|
|
{
|
|
minix_timer_t **atp;
|
|
int r;
|
|
|
|
if (*tmrs != NULL) {
|
|
if (old_head != NULL)
|
|
*old_head = (*tmrs)->tmr_exp_time;
|
|
r = TRUE;
|
|
} else
|
|
r = FALSE;
|
|
|
|
/* Set the timer's variables. */
|
|
if (tmr_is_set(tp))
|
|
(void)tmrs_clrtimer(tmrs, tp, NULL, NULL);
|
|
tp->tmr_exp_time = exp_time;
|
|
tp->tmr_func = watchdog; /* set the timer object */
|
|
tp->tmr_arg = arg;
|
|
|
|
/*
|
|
* Add the timer to the active timers. The next timer due is in front.
|
|
*/
|
|
for (atp = tmrs; *atp != NULL; atp = &(*atp)->tmr_next) {
|
|
if (tmr_is_first(exp_time, (*atp)->tmr_exp_time))
|
|
break;
|
|
}
|
|
tp->tmr_next = *atp;
|
|
*atp = tp;
|
|
|
|
if (new_head != NULL)
|
|
*new_head = (*tmrs)->tmr_exp_time;
|
|
return r;
|
|
}
|