2005-07-14 17:30:12 +02:00
|
|
|
/* PM watchdog timer management.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pm.h"
|
|
|
|
|
|
|
|
#define VERBOSE 0
|
|
|
|
|
|
|
|
#include <timers.h>
|
|
|
|
#include <minix/syslib.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
|
|
|
|
PRIVATE timer_t *pm_timers = NULL;
|
|
|
|
|
|
|
|
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)
|
|
|
|
panic(__FILE__, "PM couldn't get uptime from system task.", NO_NUM);
|
|
|
|
|
2005-07-20 17:27:42 +02:00
|
|
|
/* Set timer argument. */
|
2005-07-14 17:30:12 +02:00
|
|
|
tmr_arg(tp)->ta_int = arg;
|
|
|
|
|
2005-07-20 17:27:42 +02:00
|
|
|
prev_time = tmrs_settimer(&pm_timers, tp, now+ticks, watchdog, &next_time);
|
2005-07-14 17:30:12 +02:00
|
|
|
|
|
|
|
/* reschedule our synchronous alarm if necessary */
|
2005-07-20 17:27:42 +02:00
|
|
|
if(! prev_time || prev_time > next_time) {
|
|
|
|
if(sys_syncalrm(SELF, next_time, 1) != OK)
|
2005-07-14 17:30:12 +02:00
|
|
|
panic(__FILE__, "PM set timer couldn't set synchronous alarm.", NO_NUM);
|
|
|
|
#if VERBOSE
|
|
|
|
else
|
2005-07-20 17:27:42 +02:00
|
|
|
printf("timers: after setting, set synalarm to %d -> %d\n", prev_time, next_time);
|
2005-07-14 17:30:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PUBLIC void pm_expire_timers(clock_t now)
|
|
|
|
{
|
2005-07-20 17:27:42 +02:00
|
|
|
clock_t next_time;
|
|
|
|
tmrs_exptimers(&pm_timers, now, &next_time);
|
|
|
|
if(next_time > 0) {
|
|
|
|
if(sys_syncalrm(SELF, next_time, 1) != OK)
|
2005-07-14 17:30:12 +02:00
|
|
|
panic(__FILE__, "PM expire timer couldn't set synchronous alarm.", NO_NUM);
|
|
|
|
#if VERBOSE
|
|
|
|
else
|
2005-07-20 17:27:42 +02:00
|
|
|
printf("timers: after expiry, set synalarm to %d\n", next_time);
|
2005-07-14 17:30:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if VERBOSE
|
|
|
|
else printf("after expiry, no new timer set\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/* if the earliest timer has been removed, we have to set
|
|
|
|
* the synalarm to the next timer, or cancel the synalarm
|
2005-07-20 17:27:42 +02:00
|
|
|
* altogether if th last time has been cancelled (next_time
|
2005-07-14 17:30:12 +02:00
|
|
|
* will be 0 then).
|
|
|
|
*/
|
2005-07-20 17:27:42 +02:00
|
|
|
if(prev_time < next_time || ! next_time) {
|
|
|
|
if(sys_syncalrm(SELF, next_time, 1) != OK)
|
2005-07-14 17:30:12 +02:00
|
|
|
panic(__FILE__, "PM expire timer couldn't set synchronous alarm.", NO_NUM);
|
|
|
|
#if VERBOSE
|
2005-07-20 17:27:42 +02:00
|
|
|
printf("timers: after cancelling, set synalarm to %d -> %d\n", prev_time, next_time);
|
2005-07-14 17:30:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#if VERBOSE
|
|
|
|
else printf("timers: after cancelling no new timer\n");
|
|
|
|
#endif
|
|
|
|
}
|