1 /* Watchdog timer management. These functions in this file provide a
2 * convenient interface to the timers library that manages a list of
3 * watchdog timers. All details of scheduling an alarm at the CLOCK task
4 * are hidden behind this interface.
6 * The entry points into this file are:
7 * init_timer: initialize a timer structure
8 * set_timer: reset and existing or set a new watchdog timer
9 * cancel_timer: remove a timer from the list of timers
10 * expire_timers: check for expired timers and run watchdog functions
16 #include <minix/sysutil.h>
18 static timer_t
*timers
= NULL
;
19 static int expiring
= 0;
21 /*===========================================================================*
23 *===========================================================================*/
24 void init_timer(timer_t
*tp
)
29 /*===========================================================================*
31 *===========================================================================*/
32 void set_timer(timer_t
*tp
, int ticks
, tmr_func_t watchdog
, int arg
)
35 clock_t now
, prev_time
= 0, next_time
;
37 if ((r
= getuptime(&now
)) != OK
)
38 panic("set_timer: couldn't get uptime");
40 /* Set timer argument and add timer to the list. */
41 tmr_arg(tp
)->ta_int
= arg
;
42 prev_time
= tmrs_settimer(&timers
, tp
, now
+ticks
, watchdog
, &next_time
);
44 /* Reschedule our synchronous alarm if necessary. */
45 if (expiring
== 0 && (! prev_time
|| prev_time
> next_time
)) {
46 if (sys_setalarm(next_time
, 1) != OK
)
47 panic("set_timer: couldn't set alarm");
51 /*===========================================================================*
53 *===========================================================================*/
54 void cancel_timer(timer_t
*tp
)
56 clock_t next_time
, prev_time
;
57 prev_time
= tmrs_clrtimer(&timers
, tp
, &next_time
);
59 /* If the earliest timer has been removed, we have to set the alarm to
60 * the next timer, or cancel the alarm altogether if the last timer
61 * has been cancelled (next_time will be 0 then).
63 if (expiring
== 0 && (prev_time
< next_time
|| ! next_time
)) {
64 if (sys_setalarm(next_time
, 1) != OK
)
65 panic("cancel_timer: couldn't set alarm");
69 /*===========================================================================*
71 *===========================================================================*/
72 void expire_timers(clock_t now
)
76 /* Check for expired timers. Use a global variable to indicate that
77 * watchdog functions are called, so that sys_setalarm() isn't called
78 * more often than necessary when set_timer or cancel_timer are called
79 * from these watchdog functions. */
81 tmrs_exptimers(&timers
, now
, &next_time
);
84 /* Reschedule an alarm if necessary. */
86 if (sys_setalarm(next_time
, 1) != OK
)
87 panic("expire_timers: couldn't set alarm");