]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | Overview of the event subsystem |
2 | Adrian Chadd <adrian@creative.net.au> | |
3 | ||
4 | $Id: event.txt 6 2005-09-10 01:02:21Z nenolod $ | |
5 | ||
6 | ||
7 | One of the things that immediately struck me whilst first looking at the | |
8 | code was that the ircd periodically scheduled things in io_loop() but | |
9 | it did them manually. This is very wasteful and very tedious. | |
10 | ||
11 | Therefore, an event system was added to hybrid. src/event.c contains an | |
12 | event system ported from the squid web cache. It is pretty self contained, | |
13 | and only a few things (debugging, time resolution) needed changing. | |
14 | ||
15 | An event is scheduled through eventAdd() or eventAddIsh() : | |
16 | ||
17 | eventAdd(const char *name, EVH * func, void *arg, time_t when, int weight) | |
18 | eventAddIsh(const char *name, EVH * func, void *arg, time_t delta_ish, | |
19 | int weight) | |
20 | ||
21 | after 'when' (or delta_ish) seconds has elapsed from the time the above | |
22 | functions are called, the 'func' is called with the given data 'arg'. The | |
23 | event is then deleted. | |
24 | ||
25 | To delete an event, use eventDelete() : | |
26 | ||
27 | eventDelete(EVH * func, void *arg) | |
28 | ||
29 | An event is identified by its callback function and data pair. | |
30 | ||
31 | Events are run through eventRun(). This is designed to be called *BEFORE* | |
32 | your IO handlers, to let events scheduled immediately (ie a time of 0) | |
33 | to initiate IO before the IO handlers are called. | |
34 | ||
35 | (Believe me, its useful.) | |
36 | ||
37 | ||
38 | ||
39 | Example: | |
40 | ||
41 | Say you have something which must be called every 15 seconds. | |
42 | ||
43 | * You would first define the callback in your module: | |
44 | ||
45 | static EVH foo_periodic_event; | |
46 | static int initialised = 0; | |
47 | ||
48 | * You would then add the event in your initialization function: | |
49 | ||
50 | void 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 | ||
63 | static void | |
64 | foo_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 | ||
73 | Notes: | |
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 |