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