]> jfr.im git - solanum.git/blob - librb/src/event.c
Remove the rest of the SVN id tags
[solanum.git] / librb / src / event.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * event.c: Event functions.
4 *
5 * Copyright (C) 1998-2000 Regents of the University of California
6 * Copyright (C) 2001-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * Code borrowed from the squid web cache by Adrian Chadd.
10 * Original header:
11 *
12 * DEBUG: section 41 Event Processing
13 * AUTHOR: Henrik Nordstrom
14 *
15 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
16 * ----------------------------------------------------------
17 *
18 * Squid is the result of efforts by numerous individuals from the
19 * Internet community. Development is led by Duane Wessels of the
20 * National Laboratory for Applied Network Research and funded by the
21 * National Science Foundation. Squid is Copyrighted (C) 1998 by
22 * the Regents of the University of California. Please see the
23 * COPYRIGHT file for full details. Squid incorporates software
24 * developed and/or copyrighted by other sources. Please see the
25 * CREDITS file for full details.
26 *
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
40 * USA
41 *
42 */
43
44 #include <librb_config.h>
45 #include <rb_lib.h>
46 #include <commio-int.h>
47 #include <event-int.h>
48
49 #define EV_NAME_LEN 33
50 static char last_event_ran[EV_NAME_LEN];
51 static rb_dlink_list event_list;
52
53 static time_t event_time_min = -1;
54
55 /*
56 * struct ev_entry *
57 * rb_event_find(EVH *func, void *arg)
58 *
59 * Input: Event function and the argument passed to it
60 * Output: Index to the slow in the event_table
61 * Side Effects: None
62 */
63 static struct ev_entry *
64 rb_event_find(EVH * func, void *arg)
65 {
66 rb_dlink_node *ptr;
67 struct ev_entry *ev;
68 RB_DLINK_FOREACH(ptr, event_list.head)
69 {
70 ev = ptr->data;
71 if((ev->func == func) && (ev->arg == arg))
72 return ev;
73 }
74
75 return NULL;
76 }
77
78 static
79 struct ev_entry *
80 rb_event_add_common(const char *name, EVH * func, void *arg, time_t when, time_t frequency)
81 {
82 struct ev_entry *ev;
83 ev = rb_malloc(sizeof(struct ev_entry));
84 ev->func = func;
85 ev->name = rb_strndup(name, EV_NAME_LEN);
86 ev->arg = arg;
87 ev->when = rb_current_time() + when;
88 ev->next = when;
89 ev->frequency = frequency;
90
91 if((ev->when < event_time_min) || (event_time_min == -1))
92 event_time_min = ev->when;
93
94 rb_dlinkAdd(ev, &ev->node, &event_list);
95 rb_io_sched_event(ev, when);
96 return ev;
97 }
98
99 /*
100 * struct ev_entry *
101 * rb_event_add(const char *name, EVH *func, void *arg, time_t when)
102 *
103 * Input: Name of event, function to call, arguments to pass, and frequency
104 * of the event.
105 * Output: None
106 * Side Effects: Adds the event to the event list.
107 */
108 struct ev_entry *
109 rb_event_add(const char *name, EVH * func, void *arg, time_t when)
110 {
111 if (rb_unlikely(when <= 0)) {
112 rb_lib_log("rb_event_add: tried to schedule %s event with a delay of "
113 "%d seconds", name, (int) when);
114 when = 1;
115 }
116
117 return rb_event_add_common(name, func, arg, when, when);
118 }
119
120 struct ev_entry *
121 rb_event_addonce(const char *name, EVH * func, void *arg, time_t when)
122 {
123 if (rb_unlikely(when <= 0)) {
124 rb_lib_log("rb_event_addonce: tried to schedule %s event to run in "
125 "%d seconds", name, (int) when);
126 when = 1;
127 }
128
129 return rb_event_add_common(name, func, arg, when, 0);
130 }
131
132 /*
133 * void rb_event_delete(struct ev_entry *ev)
134 *
135 * Input: pointer to ev_entry for the event
136 * Output: None
137 * Side Effects: Removes the event from the event list
138 */
139 void
140 rb_event_delete(struct ev_entry *ev)
141 {
142 if(ev == NULL)
143 return;
144
145 rb_dlinkDelete(&ev->node, &event_list);
146 rb_io_unsched_event(ev);
147 rb_free(ev->name);
148 rb_free(ev);
149 }
150
151 /*
152 * void rb_event_find_delete(EVH *func, void *arg)
153 *
154 * Input: pointer to func and data
155 * Output: None
156 * Side Effects: Removes the event from the event list
157 */
158 void
159 rb_event_find_delete(EVH * func, void *arg)
160 {
161 rb_event_delete(rb_event_find(func, arg));
162 }
163
164 static time_t
165 rb_event_frequency(time_t frequency)
166 {
167 if(frequency < 0)
168 {
169 const time_t two_third = (2 * labs(frequency)) / 3;
170 frequency = two_third + ((rand() % 1000) * two_third) / 1000;
171 }
172 return frequency;
173 }
174
175 /*
176 * struct ev_entry *
177 * rb_event_addish(const char *name, EVH *func, void *arg, time_t delta_isa)
178 *
179 * Input: Name of event, function to call, arguments to pass, and frequency
180 * of the event.
181 * Output: None
182 * Side Effects: Adds the event to the event list within +- 1/3 of the
183 * specified frequency.
184 */
185 struct ev_entry *
186 rb_event_addish(const char *name, EVH * func, void *arg, time_t delta_ish)
187 {
188 delta_ish = labs(delta_ish);
189 if(delta_ish >= 3.0)
190 delta_ish = -delta_ish;
191 return rb_event_add_common(name, func, arg,
192 rb_event_frequency(delta_ish), delta_ish);
193 }
194
195
196 void
197 rb_run_event(struct ev_entry *ev)
198 {
199 rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran));
200 ev->func(ev->arg);
201 if(!ev->frequency)
202 {
203 rb_event_delete(ev);
204 return;
205 }
206 ev->when = rb_current_time() + rb_event_frequency(ev->frequency);
207 if((ev->when < event_time_min) || (event_time_min == -1))
208 event_time_min = ev->when;
209 }
210
211 /*
212 * void rb_event_run(void)
213 *
214 * Input: None
215 * Output: None
216 * Side Effects: Runs pending events in the event list
217 */
218 void
219 rb_event_run(void)
220 {
221 rb_dlink_node *ptr, *next;
222 struct ev_entry *ev;
223
224 if(rb_io_supports_event())
225 return;
226
227 event_time_min = -1;
228 RB_DLINK_FOREACH_SAFE(ptr, next, event_list.head)
229 {
230 ev = ptr->data;
231 if(ev->when <= rb_current_time())
232 {
233 rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran));
234 ev->func(ev->arg);
235
236 /* event is scheduled more than once */
237 if(ev->frequency)
238 {
239 ev->when = rb_current_time() + rb_event_frequency(ev->frequency);
240 if((ev->when < event_time_min) || (event_time_min == -1))
241 event_time_min = ev->when;
242 }
243 else
244 {
245 rb_dlinkDelete(&ev->node, &event_list);
246 rb_free(ev);
247 }
248 }
249 else
250 {
251 if((ev->when < event_time_min) || (event_time_min == -1))
252 event_time_min = ev->when;
253 }
254 }
255 }
256
257 void
258 rb_event_io_register_all(void)
259 {
260 rb_dlink_node *ptr;
261 struct ev_entry *ev;
262
263 if(!rb_io_supports_event())
264 return;
265
266 RB_DLINK_FOREACH(ptr, event_list.head)
267 {
268 ev = ptr->data;
269 rb_io_sched_event(ev, ev->next);
270 }
271 }
272
273 /*
274 * void rb_event_init(void)
275 *
276 * Input: None
277 * Output: None
278 * Side Effects: Initializes the event system.
279 */
280 void
281 rb_event_init(void)
282 {
283 rb_strlcpy(last_event_ran, "NONE", sizeof(last_event_ran));
284 }
285
286 void
287 rb_dump_events(void (*func) (char *, void *), void *ptr)
288 {
289 int len;
290 char buf[512];
291 rb_dlink_node *dptr;
292 struct ev_entry *ev;
293 len = sizeof(buf);
294
295 snprintf(buf, len, "Last event to run: %s", last_event_ran);
296 func(buf, ptr);
297
298 rb_strlcpy(buf, "Operation Next Execution", len);
299 func(buf, ptr);
300
301 RB_DLINK_FOREACH(dptr, event_list.head)
302 {
303 ev = dptr->data;
304 snprintf(buf, len, "%-28s %-4ld seconds (frequency=%d)", ev->name,
305 ev->when - (long)rb_current_time(), (int)ev->frequency);
306 func(buf, ptr);
307 }
308 }
309
310 /*
311 * void rb_set_back_events(time_t by)
312 * Input: Time to set back events by.
313 * Output: None.
314 * Side-effects: Sets back all events by "by" seconds.
315 */
316 void
317 rb_set_back_events(time_t by)
318 {
319 rb_dlink_node *ptr;
320 struct ev_entry *ev;
321 RB_DLINK_FOREACH(ptr, event_list.head)
322 {
323 ev = ptr->data;
324 if(ev->when > by)
325 ev->when -= by;
326 else
327 ev->when = 0;
328 }
329 }
330
331 void
332 rb_event_update(struct ev_entry *ev, time_t freq)
333 {
334 if(ev == NULL)
335 return;
336
337 ev->frequency = freq;
338
339 /* update when it's scheduled to run if it's higher
340 * than the new frequency
341 */
342 time_t next = rb_event_frequency(freq);
343 if((rb_current_time() + next) < ev->when)
344 ev->when = rb_current_time() + next;
345 return;
346 }
347
348 time_t
349 rb_event_next(void)
350 {
351 return event_time_min;
352 }