]> jfr.im git - solanum.git/blob - librb/src/event.c
Merge pull request #278 from edk0/override
[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 ev->dead = 0;
91
92 if((ev->when < event_time_min) || (event_time_min == -1))
93 event_time_min = ev->when;
94
95 rb_dlinkAdd(ev, &ev->node, &event_list);
96 rb_io_sched_event(ev, when);
97 return ev;
98 }
99
100 /*
101 * struct ev_entry *
102 * rb_event_add(const char *name, EVH *func, void *arg, time_t when)
103 *
104 * Input: Name of event, function to call, arguments to pass, and frequency
105 * of the event.
106 * Output: None
107 * Side Effects: Adds the event to the event list.
108 */
109 struct ev_entry *
110 rb_event_add(const char *name, EVH * func, void *arg, time_t when)
111 {
112 if (rb_unlikely(when <= 0)) {
113 rb_lib_log("rb_event_add: tried to schedule %s event with a delay of "
114 "%d seconds", name, (int) when);
115 when = 1;
116 }
117
118 return rb_event_add_common(name, func, arg, when, when);
119 }
120
121 struct ev_entry *
122 rb_event_addonce(const char *name, EVH * func, void *arg, time_t when)
123 {
124 if (rb_unlikely(when <= 0)) {
125 rb_lib_log("rb_event_addonce: tried to schedule %s event to run in "
126 "%d seconds", name, (int) when);
127 when = 1;
128 }
129
130 return rb_event_add_common(name, func, arg, when, 0);
131 }
132
133 /*
134 * void rb_event_delete(struct ev_entry *ev)
135 *
136 * Input: pointer to ev_entry for the event
137 * Output: None
138 * Side Effects: Removes the event from the event list
139 */
140 void
141 rb_event_delete(struct ev_entry *ev)
142 {
143 if(ev == NULL)
144 return;
145
146 ev->dead = 1;
147
148 rb_io_unsched_event(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_one_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->dead)
232 {
233 rb_dlinkDelete(&ev->node, &event_list);
234 rb_free(ev->name);
235 rb_free(ev);
236 continue;
237 }
238 if(ev->when <= rb_current_time())
239 {
240 rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran));
241 ev->func(ev->arg);
242
243 /* event is scheduled more than once */
244 if(ev->frequency)
245 {
246 ev->when = rb_current_time() + rb_event_frequency(ev->frequency);
247 if((ev->when < event_time_min) || (event_time_min == -1))
248 event_time_min = ev->when;
249 }
250 else
251 {
252 rb_dlinkDelete(&ev->node, &event_list);
253 rb_free(ev);
254 }
255 }
256 else
257 {
258 if((ev->when < event_time_min) || (event_time_min == -1))
259 event_time_min = ev->when;
260 }
261 }
262 }
263
264 void
265 rb_event_io_register_all(void)
266 {
267 rb_dlink_node *ptr;
268 struct ev_entry *ev;
269
270 if(!rb_io_supports_event())
271 return;
272
273 RB_DLINK_FOREACH(ptr, event_list.head)
274 {
275 ev = ptr->data;
276 rb_io_sched_event(ev, ev->next);
277 }
278 }
279
280 /*
281 * void rb_event_init(void)
282 *
283 * Input: None
284 * Output: None
285 * Side Effects: Initializes the event system.
286 */
287 void
288 rb_event_init(void)
289 {
290 rb_strlcpy(last_event_ran, "NONE", sizeof(last_event_ran));
291 }
292
293 void
294 rb_dump_events(void (*func) (char *, void *), void *ptr)
295 {
296 int len;
297 char buf[512];
298 rb_dlink_node *dptr;
299 struct ev_entry *ev;
300 len = sizeof(buf);
301
302 snprintf(buf, len, "Last event to run: %s", last_event_ran);
303 func(buf, ptr);
304
305 rb_strlcpy(buf, "Operation Next Execution", len);
306 func(buf, ptr);
307
308 RB_DLINK_FOREACH(dptr, event_list.head)
309 {
310 ev = dptr->data;
311 snprintf(buf, len, "%-28s %-4ld seconds (frequency=%d)", ev->name,
312 ev->when - (long)rb_current_time(), (int)ev->frequency);
313 func(buf, ptr);
314 }
315 }
316
317 /*
318 * void rb_set_back_events(time_t by)
319 * Input: Time to set back events by.
320 * Output: None.
321 * Side-effects: Sets back all events by "by" seconds.
322 */
323 void
324 rb_set_back_events(time_t by)
325 {
326 rb_dlink_node *ptr;
327 struct ev_entry *ev;
328 RB_DLINK_FOREACH(ptr, event_list.head)
329 {
330 ev = ptr->data;
331 if(ev->when > by)
332 ev->when -= by;
333 else
334 ev->when = 0;
335 }
336 }
337
338 void
339 rb_event_update(struct ev_entry *ev, time_t freq)
340 {
341 if(ev == NULL)
342 return;
343
344 ev->frequency = freq;
345
346 /* update when it's scheduled to run if it's higher
347 * than the new frequency
348 */
349 time_t next = rb_event_frequency(freq);
350 if((rb_current_time() + next) < ev->when)
351 ev->when = rb_current_time() + next;
352 return;
353 }
354
355 time_t
356 rb_event_next(void)
357 {
358 return event_time_min;
359 }