1 /* $NetBSD: callout.c,v 1.2 2002/12/06 16:02:55 thorpej Exp $ */
4 * The mrouted program is covered by the license in the accompanying file
5 * named "LICENSE". Use of the mrouted program represents acceptance of
6 * the terms and conditions listed in that file.
8 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9 * Leland Stanford Junior University.
14 /* the code below implements a callout queue */
16 static struct timeout_q
*Q
= 0; /* pointer to the beginning of timeout queue */
18 static int in_callout
= 0;
21 struct timeout_q
*next
; /* next event */
23 cfunc_t func
; /* function to call */
24 char *data
; /* func's data */
25 int time
; /* time offset to next event*/
29 static void print_Q(void);
34 int secs_remaining(int);
39 Q
= (struct timeout_q
*) 0;
44 * signal handler for SIGALARM that is called once every second
47 age_callout_queue(void)
49 struct timeout_q
*ptr
;
59 /* timeout has happened */
73 logit(LOG_DEBUG
,0,"[callout, age_callout_queue] -- time (%d)", ptr
->time
);
74 #endif /* IGMP_DEBUG */
75 in_callout
= 0; return;
86 * delay: number of units for timeout
87 * action: function to be called on timeout
88 * data: what to call the timeout function with
91 timer_setTimer(int delay
, cfunc_t action
, char *data
)
93 struct timeout_q
*ptr
, *node
, *prev
;
101 node
= (struct timeout_q
*)malloc(sizeof(struct timeout_q
));
103 logit(LOG_WARNING
, 0, "Malloc Failed in timer_settimer\n");
115 /* insert node in the queue */
117 /* if the queue is empty, insert the node and return */
121 /* chase the pointer looking for the right place */
124 if (delay
< ptr
->time
) {
132 ptr
->time
-= node
->time
;
139 delay
-= ptr
->time
; node
->time
= delay
;
152 /* clears the associated timer */
154 timer_clearTimer(int timer_id
)
156 struct timeout_q
*ptr
, *prev
;
168 * find the right node, delete it. the subsequent node's time
174 if (ptr
->id
== timer_id
) {
175 /* got the right node */
177 /* unlink it from the queue */
181 prev
->next
= ptr
->next
;
183 /* increment next node if any */
185 (ptr
->next
)->time
+= ptr
->time
;
207 struct timeout_q
*ptr
;
209 for(ptr
= Q
; ptr
; ptr
= ptr
->next
)
210 logit(LOG_DEBUG
,0,"(%d,%d) ", ptr
->id
, ptr
->time
);
212 #endif /* IGMP_DEBUG */
215 secs_remaining(int timer_id
)
217 struct timeout_q
*ptr
;
220 for (ptr
= Q
; ptr
&& ptr
->id
!= timer_id
; ptr
= ptr
->next
)
223 if (!ptr
) /* not found */
226 return left
+ ptr
->time
;