tty: don't use custom kputc; this fixes tty printf()s.
[minix.git] / servers / pm / timers.c
blobdf6d4addbc8b5ef1032d4eeaf06b571743904082
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
16 #include "pm.h"
18 #include <timers.h>
19 #include <minix/syslib.h>
20 #include <minix/com.h>
22 PRIVATE timer_t *pm_timers = NULL;
23 PRIVATE int pm_expiring = 0;
25 /*===========================================================================*
26 * pm_set_timer *
27 *===========================================================================*/
28 PUBLIC void pm_set_timer(timer_t *tp, int ticks, tmr_func_t watchdog, int arg)
30 int r;
31 clock_t now, prev_time = 0, next_time;
33 if ((r = getuptime(&now)) != OK)
34 panic("PM couldn't get uptime");
36 /* Set timer argument and add timer to the list. */
37 tmr_arg(tp)->ta_int = arg;
38 prev_time = tmrs_settimer(&pm_timers,tp,now+ticks,watchdog,&next_time);
40 /* Reschedule our synchronous alarm if necessary. */
41 if (pm_expiring == 0 && (! prev_time || prev_time > next_time)) {
42 if (sys_setalarm(next_time, 1) != OK)
43 panic("PM set timer couldn't set alarm");
46 return;
49 /*===========================================================================*
50 * pm_expire_timers *
51 *===========================================================================*/
52 PUBLIC void pm_expire_timers(clock_t now)
54 clock_t next_time;
56 /* Check for expired timers. Use a global variable to indicate that
57 * watchdog functions are called, so that sys_setalarm() isn't called
58 * more often than necessary when pm_set_timer or pm_cancel_timer are
59 * called from these watchdog functions. */
60 pm_expiring = 1;
61 tmrs_exptimers(&pm_timers, now, &next_time);
62 pm_expiring = 0;
64 /* Reschedule an alarm if necessary. */
65 if (next_time > 0) {
66 if (sys_setalarm(next_time, 1) != OK)
67 panic("PM expire timer couldn't set alarm");
71 /*===========================================================================*
72 * pm_cancel_timer *
73 *===========================================================================*/
74 PUBLIC void pm_cancel_timer(timer_t *tp)
76 clock_t next_time, prev_time;
77 prev_time = tmrs_clrtimer(&pm_timers, tp, &next_time);
79 /* If the earliest timer has been removed, we have to set the alarm to
80 * the next timer, or cancel the alarm altogether if the last timer has
81 * been cancelled (next_time will be 0 then).
83 if (pm_expiring == 0 && (prev_time < next_time || ! next_time)) {
84 if (sys_setalarm(next_time, 1) != OK)
85 panic("PM expire timer couldn't set alarm");