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
14 static const char rcsid[] =
15 "$FreeBSD: src/usr.sbin/mrouted/callout.c,v 1.12 1999/08/28 01:17:03 peter Exp $";
20 /* the code below implements a callout queue */
22 static struct timeout_q *Q = 0; /* pointer to the beginning of timeout queue */
25 struct timeout_q *next; /* next event */
27 cfunc_t func; /* function to call */
28 void *data; /* func's data */
29 int time; /* time offset to next event*/
33 static void print_Q __P((void));
41 Q = (struct timeout_q *) 0;
58 * elapsed_time seconds have passed; perform all the events that should
62 age_callout_queue(elapsed_time)
65 struct timeout_q *ptr;
68 for (ptr = Q; ptr; ptr = Q, i++) {
69 if (ptr->time > elapsed_time) {
70 ptr->time -= elapsed_time;
73 elapsed_time -= ptr->time;
75 IF_DEBUG(DEBUG_TIMEOUT)
76 log(LOG_DEBUG, 0, "about to call timeout %d (#%d)", ptr->id, i);
85 * Return in how many seconds age_callout_queue() would like to be called.
86 * Return -1 if there are no events pending.
93 log(LOG_WARNING, 0, "timer_nextTimer top of queue says %d",
106 timer_setTimer(delay, action, data)
107 int delay; /* number of units for timeout */
108 cfunc_t action; /* function to be called on timeout */
109 void *data; /* what to call the timeout function with */
111 struct timeout_q *ptr, *node, *prev;
115 node = (struct timeout_q *)malloc(sizeof(struct timeout_q));
117 log(LOG_WARNING, 0, "Malloc Failed in timer_settimer\n");
128 /* insert node in the queue */
130 /* if the queue is empty, insert the node and return */
134 /* chase the pointer looking for the right place */
137 if (delay < ptr->time) {
145 ptr->time -= node->time;
147 IF_DEBUG(DEBUG_TIMEOUT)
148 log(LOG_DEBUG, 0, "created timeout %d (#%d)", node->id, i);
153 delay -= ptr->time; node->time = delay;
162 IF_DEBUG(DEBUG_TIMEOUT)
163 log(LOG_DEBUG, 0, "created timeout %d (#%d)", node->id, i);
167 /* returns the time until the timer is scheduled */
169 timer_leftTimer(timer_id)
172 struct timeout_q *ptr;
178 for (ptr = Q; ptr; ptr = ptr->next) {
180 if (ptr->id == timer_id)
186 /* clears the associated timer. Returns 1 if succeeded. */
188 timer_clearTimer(timer_id)
191 struct timeout_q *ptr, *prev;
200 * find the right node, delete it. the subsequent node's time
206 if (ptr->id == timer_id) {
207 /* got the right node */
209 /* unlink it from the queue */
213 prev->next = ptr->next;
215 /* increment next node if any */
217 (ptr->next)->time += ptr->time;
221 IF_DEBUG(DEBUG_TIMEOUT)
222 log(LOG_DEBUG, 0, "deleted timer %d (#%d)", ptr->id, i);
231 IF_DEBUG(DEBUG_TIMEOUT)
232 log(LOG_DEBUG, 0, "failed to delete timer %d (#%d)", timer_id, i);
244 struct timeout_q *ptr;
246 IF_DEBUG(DEBUG_TIMEOUT)
247 for (ptr = Q; ptr; ptr = ptr->next)
248 log(LOG_DEBUG, 0, "(%d,%d) ", ptr->id, ptr->time);
250 #endif /* IGMP_DEBUG */