]> jfr.im git - irc/rqf/shadowircd.git/blame - doc/technical/event.txt
Fix another typo.
[irc/rqf/shadowircd.git] / doc / technical / event.txt
CommitLineData
212380e3 1Overview of the event subsystem
2Adrian Chadd <adrian@creative.net.au>
3
4$Id: event.txt 6 2005-09-10 01:02:21Z nenolod $
5
6
7One of the things that immediately struck me whilst first looking at the
8code was that the ircd periodically scheduled things in io_loop() but
9it did them manually. This is very wasteful and very tedious.
10
11Therefore, an event system was added to hybrid. src/event.c contains an
12event system ported from the squid web cache. It is pretty self contained,
13and only a few things (debugging, time resolution) needed changing.
14
15An event is scheduled through eventAdd() or eventAddIsh() :
16
17eventAdd(const char *name, EVH * func, void *arg, time_t when, int weight)
18eventAddIsh(const char *name, EVH * func, void *arg, time_t delta_ish,
19 int weight)
20
21after 'when' (or delta_ish) seconds has elapsed from the time the above
22functions are called, the 'func' is called with the given data 'arg'. The
23event is then deleted.
24
25To delete an event, use eventDelete() :
26
27eventDelete(EVH * func, void *arg)
28
29An event is identified by its callback function and data pair.
30
31Events are run through eventRun(). This is designed to be called *BEFORE*
32your IO handlers, to let events scheduled immediately (ie a time of 0)
33to initiate IO before the IO handlers are called.
34
35(Believe me, its useful.)
36
37
38
39Example:
40
41Say you have something which must be called every 15 seconds.
42
43* You would first define the callback in your module:
44
45static EVH foo_periodic_event;
46static int initialised = 0;
47
48* You would then add the event in your initialization function:
49
50void foo_init(void)
51{
52 if (!initialised) {
53 eventAdd("foo_periodic_event", foo_periodic_event, NULL, 0, 0);
54 initialised = 1;
55 }
56}
57
58 This will force the event to be called the next time eventRun() is called,
59 rather than waiting 15 seconds.
60
61* You then define your event callback:
62
63static void
64foo_periodic_event(void *data)
65{
66 /* We'd do our work here */
67
68 /* Then we'd finish */
69 eventAdd("foo_periodic_event", foo_periodic_event, NULL, 15, 0);
70}
71
72
73Notes:
74
75* I really should change the timeout value to be in milliseconds. Squid used
76 a double, but Dianora had something against floating point code in the main
77 loop (which is understandable). If someone wants a fun task .. :-)
78
79* Note that the 'name' parameter to eventAdd() / eventAddIsh() is a const
80 char *, and is *not copied* but *referenced*. Therefore, it is in your
81 best interest to use string constants.
82
83* /stats E for an oper shows pending events. Thanks Diane!
84