phunix/minix/lib/libtimers/tmrs_clr.c
David van Moolenbroek cfd712b424 Various timer improvements
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
2016-08-05 11:12:44 +00:00

45 lines
1.2 KiB
C

#include <minix/timers.h>
/*
* Deactivate a timer and remove it from the timers queue. 'tmrs' is a pointer
* to the timers queue. 'tp' is a pointer to the timer to be removed, which
* generally should be on the queue (but this is not a requirement, and the
* kernel abuses this). If 'prev_time' is non-NULL, it is filled with the
* previous timer head time, which always exists since at least 'tp' is on it.
* The function returns TRUE if there is still at least one timer on the queue
* after this function is done, in which case 'next_time' (if non-NULL) is
* filled with the absolute expiry time of the new head timer.
*/
int
tmrs_clrtimer(minix_timer_t ** tmrs, minix_timer_t * tp, clock_t * prev_time,
clock_t * next_time)
{
minix_timer_t **atp;
int r;
if (*tmrs != NULL) {
if (prev_time != NULL)
*prev_time = (*tmrs)->tmr_exp_time;
r = TRUE;
} else
r = FALSE;
tp->tmr_func = NULL; /* clear the timer object */
for (atp = tmrs; *atp != NULL; atp = &(*atp)->tmr_next) {
if (*atp == tp) {
*atp = tp->tmr_next;
break;
}
}
if (next_time != NULL) {
if (*tmrs != NULL)
*next_time = (*tmrs)->tmr_exp_time;
else
*next_time = 0;
}
return r;
}