2
* alarms.c -- $Id: alarms.c 685 2003-03-08 15:26:51Z travo $
3
* alarm event functions, implemented using play interface
5
* Copyright (c) 1998. See accompanying LEGAL file for details.
12
typedef struct p_alarm p_alarm;
16
void (*on_alarm)(void *c);
20
static p_alarm *alarm_next = 0;
21
static p_alarm *alarm_free = 0;
22
static double alarm_query(void);
23
static int idle_eligible = 1;
25
static int p_dflt_idle(void);
26
static int (*p_app_idle)(void)= &p_dflt_idle;
29
p_idler(int (*on_idle)(void))
44
if (alarm_next && !alarm_query()) {
45
/* alarm has rung - unlink it and call its on_alarm */
46
p_alarm *next = alarm_next;
47
alarm_next = next->next;
48
next->next = alarm_free;
50
next->on_alarm(next->context);
53
idle_eligible = p_app_idle();
63
int eligible = idle_eligible;
65
return eligible? 0.0 : (alarm_next? alarm_query() : -1.0);
69
p_set_alarm(double secs, void (*on_alarm)(void *c), void *context)
72
p_alarm *next = alarm_next;
73
p_alarm **prev = &alarm_next;
77
alarm_free = p_malloc(sizeof(p_alarm)*n);
78
alarm_free[--n].next = 0;
79
while (n--) alarm_free[n].next = &alarm_free[n+1];
82
me->time = time = p_wall_secs() + secs;
83
me->on_alarm = on_alarm;
84
me->context = context;
85
/* insert me into alarm_next list, kept in order of time */
86
while (next && next->time<=time) {
90
alarm_free = alarm_free->next;
96
p_clr_alarm(void (*on_alarm)(void *c), void *context)
98
p_alarm *next, **prev = &alarm_next;
99
for (next=alarm_next ; next ; next=*prev) {
100
if ((!on_alarm || on_alarm==next->on_alarm) &&
101
(!context || context==next->context)) {
103
next->next = alarm_free;
114
if (alarm_next->time != -1.e35) {
115
double time = p_wall_secs();
116
p_alarm *next = alarm_next;
117
/* if no alarms need to ring yet, return earliest */
118
if (next->time > time)
119
return next->time - time;
121
next->time = -1.e35; /* mark all alarms that need to ring */
123
} while (next && next->time<=time);