callout.c revision 1.3 1 1.3 mycroft /* $NetBSD: callout.c,v 1.3 1995/12/10 10:06:56 mycroft Exp $ */
2 1.2 thorpej
3 1.1 mycroft /*
4 1.1 mycroft * The mrouted program is covered by the license in the accompanying file
5 1.1 mycroft * named "LICENSE". Use of the mrouted program represents acceptance of
6 1.1 mycroft * the terms and conditions listed in that file.
7 1.1 mycroft *
8 1.1 mycroft * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
9 1.1 mycroft * Leland Stanford Junior University.
10 1.1 mycroft */
11 1.1 mycroft
12 1.1 mycroft #include "defs.h"
13 1.1 mycroft
14 1.1 mycroft /* the code below implements a callout queue */
15 1.1 mycroft static int id = 0;
16 1.1 mycroft static struct timeout_q *Q = 0; /* pointer to the beginning of timeout queue */
17 1.1 mycroft
18 1.3 mycroft static int in_callout = 0;
19 1.1 mycroft
20 1.1 mycroft struct timeout_q {
21 1.1 mycroft struct timeout_q *next; /* next event */
22 1.1 mycroft int id;
23 1.3 mycroft cfunc_t func; /* function to call */
24 1.1 mycroft char *data; /* func's data */
25 1.1 mycroft int time; /* time offset to next event*/
26 1.1 mycroft };
27 1.1 mycroft
28 1.3 mycroft #ifdef IGMP_DEBUG
29 1.3 mycroft static void print_Q __P((void));
30 1.3 mycroft #else
31 1.3 mycroft #define print_Q()
32 1.3 mycroft #endif
33 1.1 mycroft
34 1.3 mycroft void
35 1.3 mycroft callout_init()
36 1.1 mycroft {
37 1.1 mycroft Q = (struct timeout_q *) 0;
38 1.1 mycroft }
39 1.1 mycroft
40 1.1 mycroft
41 1.1 mycroft /*
42 1.1 mycroft * signal handler for SIGALARM that is called once every second
43 1.1 mycroft */
44 1.3 mycroft void
45 1.3 mycroft age_callout_queue()
46 1.1 mycroft {
47 1.1 mycroft struct timeout_q *ptr;
48 1.1 mycroft
49 1.1 mycroft if (in_callout)
50 1.1 mycroft return;
51 1.1 mycroft
52 1.1 mycroft in_callout = 1;
53 1.1 mycroft ptr = Q;
54 1.1 mycroft
55 1.3 mycroft while (ptr) {
56 1.3 mycroft if (!ptr->time) {
57 1.1 mycroft /* timeout has happened */
58 1.3 mycroft Q = Q->next;
59 1.3 mycroft
60 1.3 mycroft in_callout = 0;
61 1.3 mycroft if (ptr->func)
62 1.1 mycroft ptr->func(ptr->data);
63 1.3 mycroft in_callout = 1;
64 1.1 mycroft
65 1.1 mycroft free(ptr);
66 1.1 mycroft ptr = Q;
67 1.1 mycroft }
68 1.1 mycroft else {
69 1.1 mycroft ptr->time --;
70 1.1 mycroft #ifdef IGMP_DEBUG
71 1.1 mycroft log(LOG_DEBUG,0,"[callout, age_callout_queue] -- time (%d)", ptr->time);
72 1.3 mycroft #endif /* IGMP_DEBUG */
73 1.1 mycroft in_callout = 0; return;
74 1.1 mycroft }
75 1.1 mycroft }
76 1.1 mycroft in_callout = 0;
77 1.1 mycroft return;
78 1.1 mycroft }
79 1.1 mycroft
80 1.1 mycroft
81 1.1 mycroft /*
82 1.1 mycroft * sets the timer
83 1.1 mycroft */
84 1.3 mycroft int
85 1.3 mycroft timer_setTimer(delay, action, data)
86 1.1 mycroft int delay; /* number of units for timeout */
87 1.1 mycroft cfunc_t action; /* function to be called on timeout */
88 1.1 mycroft char *data; /* what to call the timeout function with */
89 1.1 mycroft {
90 1.1 mycroft struct timeout_q *ptr, *node, *prev;
91 1.1 mycroft
92 1.1 mycroft if (in_callout)
93 1.1 mycroft return -1;
94 1.1 mycroft
95 1.1 mycroft in_callout = 1;
96 1.1 mycroft
97 1.1 mycroft /* create a node */
98 1.3 mycroft node = (struct timeout_q *)malloc(sizeof(struct timeout_q));
99 1.1 mycroft if (node == 0) {
100 1.1 mycroft log(LOG_WARNING, 0, "Malloc Failed in timer_settimer\n");
101 1.1 mycroft in_callout = 0;
102 1.1 mycroft return -1;
103 1.1 mycroft }
104 1.1 mycroft node->func = action;
105 1.1 mycroft node->data = data;
106 1.1 mycroft node->time = delay;
107 1.1 mycroft node->next = 0;
108 1.1 mycroft node->id = ++id;
109 1.1 mycroft
110 1.1 mycroft prev = ptr = Q;
111 1.1 mycroft
112 1.1 mycroft /* insert node in the queue */
113 1.1 mycroft
114 1.1 mycroft /* if the queue is empty, insert the node and return */
115 1.1 mycroft if (!Q)
116 1.1 mycroft Q = node;
117 1.1 mycroft else {
118 1.1 mycroft /* chase the pointer looking for the right place */
119 1.3 mycroft while (ptr) {
120 1.1 mycroft
121 1.3 mycroft if (delay < ptr->time) {
122 1.1 mycroft /* right place */
123 1.1 mycroft
124 1.1 mycroft node->next = ptr;
125 1.1 mycroft if (ptr == Q)
126 1.1 mycroft Q = node;
127 1.1 mycroft else
128 1.1 mycroft prev->next = node;
129 1.1 mycroft ptr->time -= node->time;
130 1.1 mycroft print_Q();
131 1.1 mycroft in_callout = 0;
132 1.1 mycroft return node->id;
133 1.3 mycroft } else {
134 1.1 mycroft /* keep moving */
135 1.1 mycroft
136 1.1 mycroft delay -= ptr->time; node->time = delay;
137 1.1 mycroft prev = ptr;
138 1.1 mycroft ptr = ptr->next;
139 1.1 mycroft }
140 1.1 mycroft }
141 1.1 mycroft prev->next = node;
142 1.1 mycroft }
143 1.1 mycroft print_Q();
144 1.1 mycroft in_callout = 0;
145 1.1 mycroft return node->id;
146 1.1 mycroft }
147 1.1 mycroft
148 1.1 mycroft
149 1.1 mycroft /* clears the associated timer */
150 1.3 mycroft void
151 1.3 mycroft timer_clearTimer(timer_id)
152 1.1 mycroft int timer_id;
153 1.1 mycroft {
154 1.1 mycroft struct timeout_q *ptr, *prev;
155 1.1 mycroft
156 1.3 mycroft if (in_callout)
157 1.3 mycroft return;
158 1.3 mycroft if (!timer_id)
159 1.3 mycroft return;
160 1.3 mycroft
161 1.1 mycroft in_callout = 1;
162 1.1 mycroft
163 1.1 mycroft prev = ptr = Q;
164 1.1 mycroft
165 1.1 mycroft /*
166 1.1 mycroft * find the right node, delete it. the subsequent node's time
167 1.1 mycroft * gets bumped up
168 1.1 mycroft */
169 1.1 mycroft
170 1.1 mycroft print_Q();
171 1.3 mycroft while (ptr) {
172 1.3 mycroft if (ptr->id == timer_id) {
173 1.1 mycroft /* got the right node */
174 1.1 mycroft
175 1.1 mycroft /* unlink it from the queue */
176 1.3 mycroft if (ptr == Q)
177 1.1 mycroft Q = Q->next;
178 1.1 mycroft else
179 1.1 mycroft prev->next = ptr->next;
180 1.1 mycroft
181 1.1 mycroft /* increment next node if any */
182 1.1 mycroft if (ptr->next != 0)
183 1.1 mycroft (ptr->next)->time += ptr->time;
184 1.1 mycroft
185 1.1 mycroft free(ptr->data);
186 1.1 mycroft free(ptr);
187 1.1 mycroft print_Q();
188 1.1 mycroft in_callout = 0;
189 1.1 mycroft return;
190 1.1 mycroft }
191 1.1 mycroft prev = ptr;
192 1.1 mycroft ptr = ptr->next;
193 1.1 mycroft }
194 1.1 mycroft print_Q();
195 1.1 mycroft in_callout = 0;
196 1.1 mycroft }
197 1.1 mycroft
198 1.3 mycroft #ifdef IGMP_DEBUG
199 1.1 mycroft /*
200 1.1 mycroft * debugging utility
201 1.1 mycroft */
202 1.3 mycroft static void
203 1.3 mycroft print_Q()
204 1.1 mycroft {
205 1.1 mycroft struct timeout_q *ptr;
206 1.1 mycroft
207 1.1 mycroft for(ptr = Q; ptr; ptr = ptr->next)
208 1.1 mycroft log(LOG_DEBUG,0,"(%d,%d) ", ptr->id, ptr->time);
209 1.1 mycroft }
210 1.3 mycroft #endif /* IGMP_DEBUG */
211 1.3 mycroft int
212 1.3 mycroft secs_remaining( timer_id)
213 1.3 mycroft int timer_id;
214 1.3 mycroft {
215 1.3 mycroft struct timeout_q *ptr;
216 1.3 mycroft int left=0;
217 1.3 mycroft
218 1.3 mycroft for (ptr = Q; ptr && ptr->id != timer_id; ptr = ptr->next)
219 1.3 mycroft left += ptr->time;
220 1.1 mycroft
221 1.3 mycroft if (!ptr) /* not found */
222 1.3 mycroft return 0;
223 1.3 mycroft
224 1.3 mycroft return left + ptr->time;
225 1.3 mycroft }
226