… | |
… | |
2 | |
2 | |
3 | libev - a high performance full-featured event loop written in C |
3 | libev - a high performance full-featured event loop written in C |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
|
|
7 | /* this is the only header you need */ |
7 | #include <ev.h> |
8 | #include <ev.h> |
|
|
9 | |
|
|
10 | /* what follows is a fully working example program */ |
|
|
11 | ev_io stdin_watcher; |
|
|
12 | ev_timer timeout_watcher; |
|
|
13 | |
|
|
14 | /* called when data readable on stdin */ |
|
|
15 | static void |
|
|
16 | stdin_cb (EV_P_ struct ev_io *w, int revents) |
|
|
17 | { |
|
|
18 | /* puts ("stdin ready"); */ |
|
|
19 | ev_io_stop (EV_A_ w); /* just a syntax example */ |
|
|
20 | ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ |
|
|
21 | } |
|
|
22 | |
|
|
23 | static void |
|
|
24 | timeout_cb (EV_P_ struct ev_timer *w, int revents) |
|
|
25 | { |
|
|
26 | /* puts ("timeout"); */ |
|
|
27 | ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ |
|
|
28 | } |
|
|
29 | |
|
|
30 | int |
|
|
31 | main (void) |
|
|
32 | { |
|
|
33 | struct ev_loop *loop = ev_default_loop (0); |
|
|
34 | |
|
|
35 | /* initialise an io watcher, then start it */ |
|
|
36 | ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
|
|
37 | ev_io_start (loop, &stdin_watcher); |
|
|
38 | |
|
|
39 | /* simple non-repeating 5.5 second timeout */ |
|
|
40 | ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
41 | ev_timer_start (loop, &timeout_watcher); |
|
|
42 | |
|
|
43 | /* loop till timeout or data ready */ |
|
|
44 | ev_loop (loop, 0); |
|
|
45 | |
|
|
46 | return 0; |
|
|
47 | } |
8 | |
48 | |
9 | =head1 DESCRIPTION |
49 | =head1 DESCRIPTION |
10 | |
50 | |
11 | Libev is an event loop: you register interest in certain events (such as a |
51 | Libev is an event loop: you register interest in certain events (such as a |
12 | file descriptor being readable or a timeout occuring), and it will manage |
52 | file descriptor being readable or a timeout occuring), and it will manage |
… | |
… | |
115 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
155 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
116 | recommended ones. |
156 | recommended ones. |
117 | |
157 | |
118 | See the description of C<ev_embed> watchers for more info. |
158 | See the description of C<ev_embed> watchers for more info. |
119 | |
159 | |
120 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
160 | =item ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
121 | |
161 | |
122 | Sets the allocation function to use (the prototype is similar to the |
162 | Sets the allocation function to use (the prototype and semantics are |
123 | realloc C function, the semantics are identical). It is used to allocate |
163 | identical to the realloc C function). It is used to allocate and free |
124 | and free memory (no surprises here). If it returns zero when memory |
164 | memory (no surprises here). If it returns zero when memory needs to be |
125 | needs to be allocated, the library might abort or take some potentially |
165 | allocated, the library might abort or take some potentially destructive |
126 | destructive action. The default is your system realloc function. |
166 | action. The default is your system realloc function. |
127 | |
167 | |
128 | You could override this function in high-availability programs to, say, |
168 | You could override this function in high-availability programs to, say, |
129 | free some memory if it cannot allocate memory, to use a special allocator, |
169 | free some memory if it cannot allocate memory, to use a special allocator, |
130 | or even to sleep a while and retry until some memory is available. |
170 | or even to sleep a while and retry until some memory is available. |
131 | |
171 | |
132 | Example: replace the libev allocator with one that waits a bit and then |
172 | Example: replace the libev allocator with one that waits a bit and then |
133 | retries: better than mine). |
173 | retries: better than mine). |
134 | |
174 | |
135 | static void * |
175 | static void * |
136 | persistent_realloc (void *ptr, long size) |
176 | persistent_realloc (void *ptr, size_t size) |
137 | { |
177 | { |
138 | for (;;) |
178 | for (;;) |
139 | { |
179 | { |
140 | void *newptr = realloc (ptr, size); |
180 | void *newptr = realloc (ptr, size); |
141 | |
181 | |