]>
jfr.im git - solanum.git/blob - librb/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
44 #include <librb_config.h>
46 #include <commio-int.h>
47 #include <event-int.h>
49 #define EV_NAME_LEN 33
50 static char last_event_ran
[EV_NAME_LEN
];
51 static rb_dlink_list event_list
;
53 static time_t event_time_min
= -1;
57 * rb_event_find(EVH *func, void *arg)
59 * Input: Event function and the argument passed to it
60 * Output: Index to the slow in the event_table
63 static struct ev_entry
*
64 rb_event_find(EVH
* func
, void *arg
)
68 RB_DLINK_FOREACH(ptr
, event_list
.head
)
71 if((ev
->func
== func
) && (ev
->arg
== arg
))
80 rb_event_add_common(const char *name
, EVH
* func
, void *arg
, time_t when
, time_t frequency
)
83 ev
= rb_malloc(sizeof(struct ev_entry
));
85 ev
->name
= rb_strndup(name
, EV_NAME_LEN
);
87 ev
->when
= rb_current_time() + when
;
89 ev
->frequency
= frequency
;
91 if((ev
->when
< event_time_min
) || (event_time_min
== -1))
92 event_time_min
= ev
->when
;
94 rb_dlinkAdd(ev
, &ev
->node
, &event_list
);
95 rb_io_sched_event(ev
, when
);
101 * rb_event_add(const char *name, EVH *func, void *arg, time_t when)
103 * Input: Name of event, function to call, arguments to pass, and frequency
106 * Side Effects: Adds the event to the event list.
109 rb_event_add(const char *name
, EVH
* func
, void *arg
, time_t when
)
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
);
117 return rb_event_add_common(name
, func
, arg
, when
, when
);
121 rb_event_addonce(const char *name
, EVH
* func
, void *arg
, time_t when
)
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
);
129 return rb_event_add_common(name
, func
, arg
, when
, 0);
133 * void rb_event_delete(struct ev_entry *ev)
135 * Input: pointer to ev_entry for the event
137 * Side Effects: Removes the event from the event list
140 rb_event_delete(struct ev_entry
*ev
)
145 rb_dlinkDelete(&ev
->node
, &event_list
);
146 rb_io_unsched_event(ev
);
152 * void rb_event_find_delete(EVH *func, void *arg)
154 * Input: pointer to func and data
156 * Side Effects: Removes the event from the event list
159 rb_event_find_delete(EVH
* func
, void *arg
)
161 rb_event_delete(rb_event_find(func
, arg
));
165 rb_event_frequency(time_t frequency
)
169 const time_t two_third
= (2 * labs(frequency
)) / 3;
170 frequency
= two_third
+ ((rand() % 1000) * two_third
) / 1000;
177 * rb_event_addish(const char *name, EVH *func, void *arg, time_t delta_isa)
179 * Input: Name of event, function to call, arguments to pass, and frequency
182 * Side Effects: Adds the event to the event list within +- 1/3 of the
183 * specified frequency.
186 rb_event_addish(const char *name
, EVH
* func
, void *arg
, time_t delta_ish
)
188 delta_ish
= labs(delta_ish
);
190 delta_ish
= -delta_ish
;
191 return rb_event_add_common(name
, func
, arg
,
192 rb_event_frequency(delta_ish
), delta_ish
);
197 rb_run_one_event(struct ev_entry
*ev
)
199 rb_strlcpy(last_event_ran
, ev
->name
, sizeof(last_event_ran
));
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
;
212 * void rb_event_run(void)
216 * Side Effects: Runs pending events in the event list
221 rb_dlink_node
*ptr
, *next
;
224 if(rb_io_supports_event())
228 RB_DLINK_FOREACH_SAFE(ptr
, next
, event_list
.head
)
231 if(ev
->when
<= rb_current_time())
233 rb_strlcpy(last_event_ran
, ev
->name
, sizeof(last_event_ran
));
236 /* event is scheduled more than once */
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
;
245 rb_dlinkDelete(&ev
->node
, &event_list
);
251 if((ev
->when
< event_time_min
) || (event_time_min
== -1))
252 event_time_min
= ev
->when
;
258 rb_event_io_register_all(void)
263 if(!rb_io_supports_event())
266 RB_DLINK_FOREACH(ptr
, event_list
.head
)
269 rb_io_sched_event(ev
, ev
->next
);
274 * void rb_event_init(void)
278 * Side Effects: Initializes the event system.
283 rb_strlcpy(last_event_ran
, "NONE", sizeof(last_event_ran
));
287 rb_dump_events(void (*func
) (char *, void *), void *ptr
)
295 snprintf(buf
, len
, "Last event to run: %s", last_event_ran
);
298 rb_strlcpy(buf
, "Operation Next Execution", len
);
301 RB_DLINK_FOREACH(dptr
, event_list
.head
)
304 snprintf(buf
, len
, "%-28s %-4ld seconds (frequency=%d)", ev
->name
,
305 ev
->when
- (long)rb_current_time(), (int)ev
->frequency
);
311 * void rb_set_back_events(time_t by)
312 * Input: Time to set back events by.
314 * Side-effects: Sets back all events by "by" seconds.
317 rb_set_back_events(time_t by
)
321 RB_DLINK_FOREACH(ptr
, event_list
.head
)
332 rb_event_update(struct ev_entry
*ev
, time_t freq
)
337 ev
->frequency
= freq
;
339 /* update when it's scheduled to run if it's higher
340 * than the new frequency
342 time_t next
= rb_event_frequency(freq
);
343 if((rb_current_time() + next
) < ev
->when
)
344 ev
->when
= rb_current_time() + next
;
351 return event_time_min
;