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