2 * The mrouted program is covered by the license in the accompanying file
3 * named "LICENSE". Use of the mrouted program represents acceptance of
4 * the terms and conditions listed in that file.
6 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
7 * Leland Stanford Junior University.
10 * callout.c,v 3.8.4.8 1998/01/06 01:58:45 fenner Exp
12 * $FreeBSD: src/usr.sbin/mrouted/callout.c,v 1.12 1999/08/28 01:17:03 peter Exp $
17 /* the code below implements a callout queue */
19 static struct timeout_q
*Q
= NULL
; /* pointer to the beginning of timeout queue */
22 struct timeout_q
*next
; /* next event */
24 cfunc_t func
; /* function to call */
25 void *data
; /* func's data */
26 int time
; /* time offset to next event*/
30 static void print_Q(void);
42 free_all_callouts(void)
55 * elapsed_time seconds have passed; perform all the events that should
59 age_callout_queue(int elapsed_time
)
61 struct timeout_q
*ptr
;
64 for (ptr
= Q
; ptr
; ptr
= Q
, i
++) {
65 if (ptr
->time
> elapsed_time
) {
66 ptr
->time
-= elapsed_time
;
69 elapsed_time
-= ptr
->time
;
71 IF_DEBUG(DEBUG_TIMEOUT
)
72 dolog(LOG_DEBUG
, 0, "about to call timeout %d (#%d)", ptr
->id
, i
);
81 * Return in how many seconds age_callout_queue() would like to be called.
82 * Return -1 if there are no events pending.
89 dolog(LOG_WARNING
, 0, "timer_nextTimer top of queue says %d",
101 * delay - number of units for timeout
102 * action - function to be called on timeout
103 * data - what to call the timeout function with
106 timer_setTimer(int delay
, cfunc_t action
, void *data
)
108 struct timeout_q
*ptr
, *node
, *prev
;
112 node
= (struct timeout_q
*)malloc(sizeof(struct timeout_q
));
114 dolog(LOG_WARNING
, 0, "Malloc Failed in timer_setTimer\n");
125 /* insert node in the queue */
127 /* if the queue is empty, insert the node and return */
131 /* chase the pointer looking for the right place */
134 if (delay
< ptr
->time
) {
142 ptr
->time
-= node
->time
;
144 IF_DEBUG(DEBUG_TIMEOUT
)
145 dolog(LOG_DEBUG
, 0, "created timeout %d (#%d)", node
->id
, i
);
150 delay
-= ptr
->time
; node
->time
= delay
;
159 IF_DEBUG(DEBUG_TIMEOUT
)
160 dolog(LOG_DEBUG
, 0, "created timeout %d (#%d)", node
->id
, i
);
164 /* returns the time until the timer is scheduled */
166 timer_leftTimer(int timer_id
)
168 struct timeout_q
*ptr
;
174 for (ptr
= Q
; ptr
; ptr
= ptr
->next
) {
176 if (ptr
->id
== timer_id
)
182 /* clears the associated timer. Returns 1 if succeeded. */
184 timer_clearTimer(int timer_id
)
186 struct timeout_q
*ptr
, *prev
;
195 * find the right node, delete it. the subsequent node's time
201 if (ptr
->id
== timer_id
) {
202 /* got the right node */
204 /* unlink it from the queue */
208 prev
->next
= ptr
->next
;
210 /* increment next node if any */
211 if (ptr
->next
!= NULL
)
212 (ptr
->next
)->time
+= ptr
->time
;
216 IF_DEBUG(DEBUG_TIMEOUT
)
217 dolog(LOG_DEBUG
, 0, "deleted timer %d (#%d)", ptr
->id
, i
);
226 IF_DEBUG(DEBUG_TIMEOUT
)
227 dolog(LOG_DEBUG
, 0, "failed to delete timer %d (#%d)", timer_id
, i
);
239 struct timeout_q
*ptr
;
241 IF_DEBUG(DEBUG_TIMEOUT
)
242 for (ptr
= Q
; ptr
; ptr
= ptr
->next
)
243 dolog(LOG_DEBUG
, 0, "(%d,%d) ", ptr
->id
, ptr
->time
);
245 #endif /* IGMP_DEBUG */