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 $
13 * $DragonFly: src/usr.sbin/mrouted/callout.c,v 1.3 2003/11/03 19:31:38 eirikn Exp $
18 /* the code below implements a callout queue */
20 static struct timeout_q *Q = 0; /* pointer to the beginning of timeout queue */
23 struct timeout_q *next; /* next event */
25 cfunc_t func; /* function to call */
26 void *data; /* func's data */
27 int time; /* time offset to next event*/
31 static void print_Q(void);
39 Q = (struct timeout_q *) 0;
56 * elapsed_time seconds have passed; perform all the events that should
60 age_callout_queue(elapsed_time)
63 struct timeout_q *ptr;
66 for (ptr = Q; ptr; ptr = Q, i++) {
67 if (ptr->time > elapsed_time) {
68 ptr->time -= elapsed_time;
71 elapsed_time -= ptr->time;
73 IF_DEBUG(DEBUG_TIMEOUT)
74 log(LOG_DEBUG, 0, "about to call timeout %d (#%d)", ptr->id, i);
83 * Return in how many seconds age_callout_queue() would like to be called.
84 * Return -1 if there are no events pending.
91 log(LOG_WARNING, 0, "timer_nextTimer top of queue says %d",
104 timer_setTimer(delay, action, data)
105 int delay; /* number of units for timeout */
106 cfunc_t action; /* function to be called on timeout */
107 void *data; /* what to call the timeout function with */
109 struct timeout_q *ptr, *node, *prev;
113 node = (struct timeout_q *)malloc(sizeof(struct timeout_q));
115 log(LOG_WARNING, 0, "Malloc Failed in timer_settimer\n");
126 /* insert node in the queue */
128 /* if the queue is empty, insert the node and return */
132 /* chase the pointer looking for the right place */
135 if (delay < ptr->time) {
143 ptr->time -= node->time;
145 IF_DEBUG(DEBUG_TIMEOUT)
146 log(LOG_DEBUG, 0, "created timeout %d (#%d)", node->id, i);
151 delay -= ptr->time; node->time = delay;
160 IF_DEBUG(DEBUG_TIMEOUT)
161 log(LOG_DEBUG, 0, "created timeout %d (#%d)", node->id, i);
165 /* returns the time until the timer is scheduled */
167 timer_leftTimer(timer_id)
170 struct timeout_q *ptr;
176 for (ptr = Q; ptr; ptr = ptr->next) {
178 if (ptr->id == timer_id)
184 /* clears the associated timer. Returns 1 if succeeded. */
186 timer_clearTimer(timer_id)
189 struct timeout_q *ptr, *prev;
198 * find the right node, delete it. the subsequent node's time
204 if (ptr->id == timer_id) {
205 /* got the right node */
207 /* unlink it from the queue */
211 prev->next = ptr->next;
213 /* increment next node if any */
215 (ptr->next)->time += ptr->time;
219 IF_DEBUG(DEBUG_TIMEOUT)
220 log(LOG_DEBUG, 0, "deleted timer %d (#%d)", ptr->id, i);
229 IF_DEBUG(DEBUG_TIMEOUT)
230 log(LOG_DEBUG, 0, "failed to delete timer %d (#%d)", timer_id, i);
242 struct timeout_q *ptr;
244 IF_DEBUG(DEBUG_TIMEOUT)
245 for (ptr = Q; ptr; ptr = ptr->next)
246 log(LOG_DEBUG, 0, "(%d,%d) ", ptr->id, ptr->time);
248 #endif /* IGMP_DEBUG */