2 * The mrouted program is covered by the license in the accompanying file
3 * named "LICENSE.mrouted". Use of the mrouted program represents acceptance
4 * of 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.5 1997/05/16 20:18:25 fenner Exp
17 /* the code below implements a callout queue */
19 static struct timeout_q *Q = 0; /* 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 __P((void));
39 log(LOG_ERR, 0, "timer used before callout_init()");
42 Q = (struct timeout_q *) 0;
59 * elapsed_time seconds have passed; perform all the events that should
63 age_callout_queue(elapsed_time)
66 struct timeout_q *ptr, *expQ;
69 IF_DEBUG(DEBUG_TIMEOUT)
70 log(LOG_DEBUG, 0, "aging queue (elapsed time %d):", elapsed_time);
78 if (Q->time > elapsed_time) {
79 Q->time -= elapsed_time;
86 elapsed_time -= Q->time;
92 /* handle queue of expired timers */
103 * Return in how many seconds age_callout_queue() would like to be called.
104 * Return -1 if there are no events pending.
111 log(LOG_WARNING, 0, "timer_nextTimer top of queue says %d",
124 timer_setTimer(delay, action, data)
125 int delay; /* number of units for timeout */
126 cfunc_t action; /* function to be called on timeout */
127 void *data; /* what to call the timeout function with */
129 struct timeout_q *ptr, *node, *prev;
132 IF_DEBUG(DEBUG_TIMEOUT)
133 log(LOG_DEBUG, 0, "setting timer:");
138 node = (struct timeout_q *)malloc(sizeof(struct timeout_q));
140 log(LOG_WARNING, 0, "Malloc Failed in timer_settimer\n");
151 /* insert node in the queue */
153 /* if the queue is empty, insert the node and return */
157 /* chase the pointer looking for the right place */
160 if (delay < ptr->time) {
168 ptr->time -= node->time;
173 delay -= ptr->time; node->time = delay;
183 /* returns the time until the timer is scheduled */
185 timer_leftTimer(timer_id)
188 struct timeout_q *ptr;
194 for (ptr = Q; ptr; ptr = ptr->next) {
196 if (ptr->id == timer_id)
202 /* clears the associated timer */
204 timer_clearTimer(timer_id)
207 struct timeout_q *ptr, *prev;
215 * find the right node, delete it. the subsequent node's time
220 if (ptr->id == timer_id) {
221 /* got the right node */
223 /* unlink it from the queue */
227 prev->next = ptr->next;
229 /* increment next node if any */
231 (ptr->next)->time += ptr->time;
250 struct timeout_q *ptr;
252 IF_DEBUG(DEBUG_TIMEOUT)
253 for (ptr = Q; ptr; ptr = ptr->next)
254 log(LOG_DEBUG, 0, "(%d,%d) ", ptr->id, ptr->time);
256 #endif /* CALLOUT_DEBUG */