
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
118 lines
3.3 KiB
C
118 lines
3.3 KiB
C
/*
|
|
* Watchdog timer management. These functions in this file provide a
|
|
* convenient interface to the timers library that manages a list of
|
|
* watchdog timers. All details of scheduling an alarm at the CLOCK task
|
|
* are hidden behind this interface.
|
|
*
|
|
* The entry points into this file are:
|
|
* init_timer: initialize a timer structure
|
|
* set_timer: reset and existing or set a new watchdog timer
|
|
* cancel_timer: remove a timer from the list of timers
|
|
* expire_timers: check for expired timers and run watchdog functions
|
|
*
|
|
*/
|
|
|
|
#include "syslib.h"
|
|
#include <minix/timers.h>
|
|
#include <minix/sysutil.h>
|
|
|
|
static minix_timer_t *timers = NULL;
|
|
static int expiring = FALSE;
|
|
|
|
/*
|
|
* Initialize the timer 'tp'.
|
|
*/
|
|
void
|
|
init_timer(minix_timer_t * tp)
|
|
{
|
|
|
|
tmr_inittimer(tp);
|
|
}
|
|
|
|
/*
|
|
* Set the timer 'tp' to trigger 'ticks' clock ticks in the future. When it
|
|
* triggers, call function 'watchdog' with argument 'arg'. The given timer
|
|
* object must have been initialized with init_timer(3) already. The given
|
|
* number of ticks must be between 0 and TMRDIFF_MAX inclusive. A ticks value
|
|
* of zero will cause the alarm to trigger on the next clock tick. If the
|
|
* timer was already set, it will be canceled first.
|
|
*/
|
|
void
|
|
set_timer(minix_timer_t *tp, clock_t ticks, tmr_func_t watchdog, int arg)
|
|
{
|
|
clock_t prev_time, next_time;
|
|
int r, had_timers;
|
|
|
|
if (ticks > TMRDIFF_MAX)
|
|
panic("set_timer: ticks value too large: %u", (int)ticks);
|
|
|
|
/* Add the timer to the list. */
|
|
had_timers = tmrs_settimer(&timers, tp, getticks() + ticks, watchdog,
|
|
arg, &prev_time, &next_time);
|
|
|
|
/* Reschedule our synchronous alarm if necessary. */
|
|
if (!expiring && (!had_timers || next_time != prev_time)) {
|
|
if ((r = sys_setalarm(next_time, TRUE /*abs_time*/)) != OK)
|
|
panic("set_timer: couldn't set alarm: %d", r);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Cancel the timer 'tp'. The timer object must have been initialized with
|
|
* init_timer(3) first. If the timer was not set before, the call is a no-op.
|
|
*/
|
|
void
|
|
cancel_timer(minix_timer_t * tp)
|
|
{
|
|
clock_t next_time, prev_time;
|
|
int r, have_timers;
|
|
|
|
if (!tmr_is_set(tp))
|
|
return;
|
|
|
|
have_timers = tmrs_clrtimer(&timers, tp, &prev_time, &next_time);
|
|
|
|
/*
|
|
* If the earliest timer has been removed, we have to set the alarm to
|
|
* the next timer, or cancel the alarm altogether if the last timer
|
|
* has been canceled.
|
|
*/
|
|
if (!expiring) {
|
|
if (!have_timers)
|
|
r = sys_setalarm(0, FALSE /*abs_time*/);
|
|
else if (prev_time != next_time)
|
|
r = sys_setalarm(next_time, TRUE /*abs_time*/);
|
|
else
|
|
r = OK;
|
|
|
|
if (r != OK)
|
|
panic("cancel_timer: couldn't set alarm: %d", r);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Expire all timers that were set to expire before/at the given current time.
|
|
*/
|
|
void
|
|
expire_timers(clock_t now)
|
|
{
|
|
clock_t next_time;
|
|
int r, have_timers;
|
|
|
|
/*
|
|
* Check for expired timers. Use a global variable to indicate that
|
|
* watchdog functions are called, so that sys_setalarm() isn't called
|
|
* more often than necessary when set_timer or cancel_timer are called
|
|
* from these watchdog functions.
|
|
*/
|
|
expiring = TRUE;
|
|
have_timers = tmrs_exptimers(&timers, now, &next_time);
|
|
expiring = FALSE;
|
|
|
|
/* Reschedule an alarm if necessary. */
|
|
if (have_timers) {
|
|
if ((r = sys_setalarm(next_time, TRUE /*abs_time*/)) != OK)
|
|
panic("expire_timers: couldn't set alarm: %d", r);
|
|
}
|
|
}
|