2005-08-05 12:45:54 +02:00
|
|
|
/* PM 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.
|
|
|
|
* Only system processes are allowed to set an alarm timer at the kernel.
|
|
|
|
* Therefore, the PM maintains a local list of timers for user processes
|
|
|
|
* that requested an alarm signal.
|
|
|
|
*
|
|
|
|
* The entry points into this file are:
|
|
|
|
* pm_set_timer: reset and existing or set a new watchdog timer
|
|
|
|
* pm_expire_timers: check for expired timers and run watchdog functions
|
|
|
|
* pm_cancel_timer: remove a time from the list of timers
|
|
|
|
*
|
2005-07-14 17:30:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pm.h"
|
|
|
|
|
|
|
|
#include <timers.h>
|
|
|
|
#include <minix/syslib.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
|
|
|
|
PRIVATE timer_t *pm_timers = NULL;
|
|
|
|
|
2005-08-05 12:45:54 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* pm_set_timer *
|
|
|
|
*===========================================================================*/
|
2005-07-14 17:30:12 +02:00
|
|
|
PUBLIC void pm_set_timer(timer_t *tp, int ticks, tmr_func_t watchdog, int arg)
|
|
|
|
{
|
|
|
|
int r;
|
2005-07-20 17:27:42 +02:00
|
|
|
clock_t now, prev_time = 0, next_time;
|
2005-07-14 17:30:12 +02:00
|
|
|
|
|
|
|
if((r = getuptime(&now)) != OK)
|
2005-08-05 12:45:54 +02:00
|
|
|
panic(__FILE__, "PM couldn't get uptime", NO_NUM);
|
2005-07-14 17:30:12 +02:00
|
|
|
|
2005-08-05 12:45:54 +02:00
|
|
|
/* Set timer argument and add timer to the list. */
|
2005-07-14 17:30:12 +02:00
|
|
|
tmr_arg(tp)->ta_int = arg;
|
2005-08-05 12:45:54 +02:00
|
|
|
prev_time = tmrs_settimer(&pm_timers,tp,now+ticks,watchdog,&next_time);
|
2005-07-14 17:30:12 +02:00
|
|
|
|
2005-08-05 12:45:54 +02:00
|
|
|
/* Reschedule our synchronous alarm if necessary. */
|
2005-07-20 17:27:42 +02:00
|
|
|
if(! prev_time || prev_time > next_time) {
|
2005-07-29 17:00:49 +02:00
|
|
|
if(sys_setalarm(next_time, 1) != OK)
|
2005-08-05 12:45:54 +02:00
|
|
|
panic(__FILE__, "PM set timer couldn't set alarm.", NO_NUM);
|
2005-07-14 17:30:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-08-05 12:45:54 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* pm_expire_timers *
|
|
|
|
*===========================================================================*/
|
2005-07-14 17:30:12 +02:00
|
|
|
PUBLIC void pm_expire_timers(clock_t now)
|
|
|
|
{
|
2005-07-20 17:27:42 +02:00
|
|
|
clock_t next_time;
|
2005-08-05 12:45:54 +02:00
|
|
|
|
|
|
|
/* Check for expired timers and possibly reschedule an alarm. */
|
2005-07-20 17:27:42 +02:00
|
|
|
tmrs_exptimers(&pm_timers, now, &next_time);
|
|
|
|
if(next_time > 0) {
|
2005-07-29 17:00:49 +02:00
|
|
|
if(sys_setalarm(next_time, 1) != OK)
|
2005-08-05 12:45:54 +02:00
|
|
|
panic(__FILE__, "PM expire timer couldn't set alarm.", NO_NUM);
|
2005-07-14 17:30:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-05 12:45:54 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* pm_cancel_timer *
|
|
|
|
*===========================================================================*/
|
2005-07-14 17:30:12 +02:00
|
|
|
PUBLIC void pm_cancel_timer(timer_t *tp)
|
|
|
|
{
|
2005-07-20 17:27:42 +02:00
|
|
|
clock_t next_time, prev_time;
|
|
|
|
prev_time = tmrs_clrtimer(&pm_timers, tp, &next_time);
|
2005-07-14 17:30:12 +02:00
|
|
|
|
2005-08-05 12:45:54 +02:00
|
|
|
/* 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 cancelled (next_time will be 0 then).
|
2005-07-14 17:30:12 +02:00
|
|
|
*/
|
2005-07-20 17:27:42 +02:00
|
|
|
if(prev_time < next_time || ! next_time) {
|
2005-07-29 17:00:49 +02:00
|
|
|
if(sys_setalarm(next_time, 1) != OK)
|
2005-08-05 12:45:54 +02:00
|
|
|
panic(__FILE__, "PM expire timer couldn't set alarm.", NO_NUM);
|
2005-07-14 17:30:12 +02:00
|
|
|
}
|
|
|
|
}
|