[9] | 1 | /* PM watchdog timer management. These functions in this file provide
|
---|
| 2 | * a 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.
|
---|
| 5 | * Only system processes are allowed to set an alarm timer at the kernel.
|
---|
| 6 | * Therefore, the PM maintains a local list of timers for user processes
|
---|
| 7 | * that requested an alarm signal.
|
---|
| 8 | *
|
---|
| 9 | * The entry points into this file are:
|
---|
| 10 | * pm_set_timer: reset and existing or set a new watchdog timer
|
---|
| 11 | * pm_expire_timers: check for expired timers and run watchdog functions
|
---|
| 12 | * pm_cancel_timer: remove a time from the list of timers
|
---|
| 13 | *
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | #include "pm.h"
|
---|
| 17 |
|
---|
| 18 | #include <timers.h>
|
---|
| 19 | #include <minix/syslib.h>
|
---|
| 20 | #include <minix/com.h>
|
---|
| 21 |
|
---|
| 22 | PRIVATE timer_t *pm_timers = NULL;
|
---|
| 23 |
|
---|
| 24 | /*===========================================================================*
|
---|
| 25 | * pm_set_timer *
|
---|
| 26 | *===========================================================================*/
|
---|
| 27 | PUBLIC void pm_set_timer(timer_t *tp, int ticks, tmr_func_t watchdog, int arg)
|
---|
| 28 | {
|
---|
| 29 | int r;
|
---|
| 30 | clock_t now, prev_time = 0, next_time;
|
---|
| 31 |
|
---|
| 32 | if ((r = getuptime(&now)) != OK)
|
---|
| 33 | panic(__FILE__, "PM couldn't get uptime", NO_NUM);
|
---|
| 34 |
|
---|
| 35 | /* Set timer argument and add timer to the list. */
|
---|
| 36 | tmr_arg(tp)->ta_int = arg;
|
---|
| 37 | prev_time = tmrs_settimer(&pm_timers,tp,now+ticks,watchdog,&next_time);
|
---|
| 38 |
|
---|
| 39 | /* Reschedule our synchronous alarm if necessary. */
|
---|
| 40 | if (! prev_time || prev_time > next_time) {
|
---|
| 41 | if (sys_setalarm(next_time, 1) != OK)
|
---|
| 42 | panic(__FILE__, "PM set timer couldn't set alarm.", NO_NUM);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | return;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /*===========================================================================*
|
---|
| 49 | * pm_expire_timers *
|
---|
| 50 | *===========================================================================*/
|
---|
| 51 | PUBLIC void pm_expire_timers(clock_t now)
|
---|
| 52 | {
|
---|
| 53 | clock_t next_time;
|
---|
| 54 |
|
---|
| 55 | /* Check for expired timers and possibly reschedule an alarm. */
|
---|
| 56 | tmrs_exptimers(&pm_timers, now, &next_time);
|
---|
| 57 | if (next_time > 0) {
|
---|
| 58 | if (sys_setalarm(next_time, 1) != OK)
|
---|
| 59 | panic(__FILE__, "PM expire timer couldn't set alarm.", NO_NUM);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /*===========================================================================*
|
---|
| 64 | * pm_cancel_timer *
|
---|
| 65 | *===========================================================================*/
|
---|
| 66 | PUBLIC void pm_cancel_timer(timer_t *tp)
|
---|
| 67 | {
|
---|
| 68 | clock_t next_time, prev_time;
|
---|
| 69 | prev_time = tmrs_clrtimer(&pm_timers, tp, &next_time);
|
---|
| 70 |
|
---|
| 71 | /* If the earliest timer has been removed, we have to set the alarm to
|
---|
| 72 | * the next timer, or cancel the alarm altogether if the last timer has
|
---|
| 73 | * been cancelled (next_time will be 0 then).
|
---|
| 74 | */
|
---|
| 75 | if (prev_time < next_time || ! next_time) {
|
---|
| 76 | if (sys_setalarm(next_time, 1) != OK)
|
---|
| 77 | panic(__FILE__, "PM expire timer couldn't set alarm.", NO_NUM);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|