]>
jfr.im git - solanum.git/blob - libratbox/src/event.c
2 * ircd-ratbox: A slightly useful ircd.
3 * event.c: Event functions.
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
9 * Code borrowed from the squid web cache by Adrian Chadd.
12 * DEBUG: section 41 Event Processing
13 * AUTHOR: Henrik Nordstrom
15 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
16 * ----------------------------------------------------------
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.
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.
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.
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
42 * $Id: event.c 26272 2008-12-10 05:55:10Z androsyn $
45 #include <libratbox_config.h>
46 #include <ratbox_lib.h>
47 #include <commio-int.h>
48 #include <event-int.h>
50 #define EV_NAME_LEN 33
51 static char last_event_ran
[EV_NAME_LEN
];
52 static rb_dlink_list event_list
;
54 static time_t event_time_min
= -1;
58 * rb_event_find(EVH *func, void *arg)
60 * Input: Event function and the argument passed to it
61 * Output: Index to the slow in the event_table
64 static struct ev_entry
*
65 rb_event_find(EVH
* func
, void *arg
)
69 RB_DLINK_FOREACH(ptr
, event_list
.head
)
72 if((ev
->func
== func
) && (ev
->arg
== arg
))
81 * rb_event_add(const char *name, EVH *func, void *arg, time_t when)
83 * Input: Name of event, function to call, arguments to pass, and frequency
86 * Side Effects: Adds the event to the event list.
89 rb_event_add(const char *name
, EVH
* func
, void *arg
, time_t when
)
92 ev
= rb_malloc(sizeof(struct ev_entry
));
94 ev
->name
= rb_strndup(name
, EV_NAME_LEN
);
96 ev
->when
= rb_current_time() + when
;
100 if((ev
->when
< event_time_min
) || (event_time_min
== -1))
102 event_time_min
= ev
->when
;
104 rb_dlinkAdd(ev
, &ev
->node
, &event_list
);
105 rb_io_sched_event(ev
, when
);
110 rb_event_addonce(const char *name
, EVH
* func
, void *arg
, time_t when
)
113 ev
= rb_malloc(sizeof(struct ev_entry
));
115 ev
->name
= rb_strndup(name
, EV_NAME_LEN
);
117 ev
->when
= rb_current_time() + when
;
121 if((ev
->when
< event_time_min
) || (event_time_min
== -1))
122 event_time_min
= ev
->when
;
124 rb_dlinkAdd(ev
, &ev
->node
, &event_list
);
125 rb_io_sched_event(ev
, when
);
130 * void rb_event_delete(struct ev_entry *ev)
132 * Input: pointer to ev_entry for the event
134 * Side Effects: Removes the event from the event list
137 rb_event_delete(struct ev_entry
*ev
)
142 rb_dlinkDelete(&ev
->node
, &event_list
);
143 rb_io_unsched_event(ev
);
149 * void rb_event_find_delete(EVH *func, void *arg)
151 * Input: pointer to func and data
153 * Side Effects: Removes the event from the event list
156 rb_event_find_delete(EVH
* func
, void *arg
)
158 rb_event_delete(rb_event_find(func
, arg
));
163 * rb_event_addish(const char *name, EVH *func, void *arg, time_t delta_isa)
165 * Input: Name of event, function to call, arguments to pass, and frequency
168 * Side Effects: Adds the event to the event list within +- 1/3 of the
169 * specified frequency.
172 rb_event_addish(const char *name
, EVH
* func
, void *arg
, time_t delta_ish
)
176 const time_t two_third
= (2 * delta_ish
) / 3;
177 delta_ish
= two_third
+ ((rand() % 1000) * two_third
) / 1000;
179 * XXX I hate the above magic, I don't even know if its right.
183 return rb_event_add(name
, func
, arg
, delta_ish
);
188 rb_run_event(struct ev_entry
*ev
)
190 rb_strlcpy(last_event_ran
, ev
->name
, sizeof(last_event_ran
));
194 rb_io_unsched_event(ev
);
195 rb_dlinkDelete(&ev
->node
, &event_list
);
199 ev
->when
= rb_current_time() + ev
->frequency
;
200 if((ev
->when
< event_time_min
) || (event_time_min
== -1))
201 event_time_min
= ev
->when
;
205 * void rb_event_run(void)
209 * Side Effects: Runs pending events in the event list
214 rb_dlink_node
*ptr
, *next
;
217 if(rb_io_supports_event())
221 RB_DLINK_FOREACH_SAFE(ptr
, next
, event_list
.head
)
224 if(ev
->when
<= rb_current_time())
226 rb_strlcpy(last_event_ran
, ev
->name
, sizeof(last_event_ran
));
229 /* event is scheduled more than once */
232 ev
->when
= rb_current_time() + ev
->frequency
;
233 if((ev
->when
< event_time_min
) || (event_time_min
== -1))
234 event_time_min
= ev
->when
;
238 rb_dlinkDelete(&ev
->node
, &event_list
);
244 if((ev
->when
< event_time_min
) || (event_time_min
== -1))
245 event_time_min
= ev
->when
;
251 rb_event_io_register_all(void)
256 if(!rb_io_supports_event())
259 RB_DLINK_FOREACH(ptr
, event_list
.head
)
262 rb_io_sched_event(ev
, ev
->next
);
267 * void rb_event_init(void)
271 * Side Effects: Initializes the event system.
276 rb_strlcpy(last_event_ran
, "NONE", sizeof(last_event_ran
));
280 rb_dump_events(void (*func
) (char *, void *), void *ptr
)
288 rb_snprintf(buf
, len
, "Last event to run: %s", last_event_ran
);
291 rb_strlcpy(buf
, "Operation Next Execution", len
);
294 RB_DLINK_FOREACH(dptr
, event_list
.head
)
297 rb_snprintf(buf
, len
, "%-28s %-4ld seconds", ev
->name
,
298 ev
->when
- (long)rb_current_time());
304 * void rb_set_back_events(time_t by)
305 * Input: Time to set back events by.
307 * Side-effects: Sets back all events by "by" seconds.
310 rb_set_back_events(time_t by
)
314 RB_DLINK_FOREACH(ptr
, event_list
.head
)
325 rb_event_update(struct ev_entry
*ev
, time_t freq
)
330 ev
->frequency
= freq
;
332 /* update when its scheduled to run if its higher
333 * than the new frequency
335 if((rb_current_time() + freq
) < ev
->when
)
336 ev
->when
= rb_current_time() + freq
;
343 return event_time_min
;