… | |
… | |
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 | #include <ev.h> |
7 | #include <ev.h> |
8 | |
8 | |
9 | =head1 EXAMPLE PROGRAM |
9 | =head2 EXAMPLE PROGRAM |
10 | |
10 | |
|
|
11 | // a single header file is required |
11 | #include <ev.h> |
12 | #include <ev.h> |
12 | |
13 | |
|
|
14 | // every watcher type has its own typedef'd struct |
|
|
15 | // with the name ev_<type> |
13 | ev_io stdin_watcher; |
16 | ev_io stdin_watcher; |
14 | ev_timer timeout_watcher; |
17 | ev_timer timeout_watcher; |
15 | |
18 | |
16 | /* called when data readable on stdin */ |
19 | // all watcher callbacks have a similar signature |
|
|
20 | // this callback is called when data is readable on stdin |
17 | static void |
21 | static void |
18 | stdin_cb (EV_P_ struct ev_io *w, int revents) |
22 | stdin_cb (EV_P_ struct ev_io *w, int revents) |
19 | { |
23 | { |
20 | /* puts ("stdin ready"); */ |
24 | puts ("stdin ready"); |
21 | ev_io_stop (EV_A_ w); /* just a syntax example */ |
25 | // for one-shot events, one must manually stop the watcher |
22 | ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ |
26 | // with its corresponding stop function. |
|
|
27 | ev_io_stop (EV_A_ w); |
|
|
28 | |
|
|
29 | // this causes all nested ev_loop's to stop iterating |
|
|
30 | ev_unloop (EV_A_ EVUNLOOP_ALL); |
23 | } |
31 | } |
24 | |
32 | |
|
|
33 | // another callback, this time for a time-out |
25 | static void |
34 | static void |
26 | timeout_cb (EV_P_ struct ev_timer *w, int revents) |
35 | timeout_cb (EV_P_ struct ev_timer *w, int revents) |
27 | { |
36 | { |
28 | /* puts ("timeout"); */ |
37 | puts ("timeout"); |
29 | ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ |
38 | // this causes the innermost ev_loop to stop iterating |
|
|
39 | ev_unloop (EV_A_ EVUNLOOP_ONE); |
30 | } |
40 | } |
31 | |
41 | |
32 | int |
42 | int |
33 | main (void) |
43 | main (void) |
34 | { |
44 | { |
|
|
45 | // use the default event loop unless you have special needs |
35 | struct ev_loop *loop = ev_default_loop (0); |
46 | struct ev_loop *loop = ev_default_loop (0); |
36 | |
47 | |
37 | /* initialise an io watcher, then start it */ |
48 | // initialise an io watcher, then start it |
|
|
49 | // this one will watch for stdin to become readable |
38 | ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
50 | ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
39 | ev_io_start (loop, &stdin_watcher); |
51 | ev_io_start (loop, &stdin_watcher); |
40 | |
52 | |
|
|
53 | // initialise a timer watcher, then start it |
41 | /* simple non-repeating 5.5 second timeout */ |
54 | // simple non-repeating 5.5 second timeout |
42 | ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
55 | ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
43 | ev_timer_start (loop, &timeout_watcher); |
56 | ev_timer_start (loop, &timeout_watcher); |
44 | |
57 | |
45 | /* loop till timeout or data ready */ |
58 | // now wait for events to arrive |
46 | ev_loop (loop, 0); |
59 | ev_loop (loop, 0); |
47 | |
60 | |
|
|
61 | // unloop was called, so exit |
48 | return 0; |
62 | return 0; |
49 | } |
63 | } |
50 | |
64 | |
51 | =head1 DESCRIPTION |
65 | =head1 DESCRIPTION |
52 | |
66 | |
53 | The newest version of this document is also available as a html-formatted |
67 | The newest version of this document is also available as an html-formatted |
54 | web page you might find easier to navigate when reading it for the first |
68 | web page you might find easier to navigate when reading it for the first |
55 | time: L<http://cvs.schmorp.de/libev/ev.html>. |
69 | time: L<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>. |
56 | |
70 | |
57 | Libev is an event loop: you register interest in certain events (such as a |
71 | Libev is an event loop: you register interest in certain events (such as a |
58 | file descriptor being readable or a timeout occurring), and it will manage |
72 | file descriptor being readable or a timeout occurring), and it will manage |
59 | these event sources and provide your program with events. |
73 | these event sources and provide your program with events. |
60 | |
74 | |
… | |
… | |
65 | You register interest in certain events by registering so-called I<event |
79 | You register interest in certain events by registering so-called I<event |
66 | watchers>, which are relatively small C structures you initialise with the |
80 | watchers>, which are relatively small C structures you initialise with the |
67 | details of the event, and then hand it over to libev by I<starting> the |
81 | details of the event, and then hand it over to libev by I<starting> the |
68 | watcher. |
82 | watcher. |
69 | |
83 | |
70 | =head1 FEATURES |
84 | =head2 FEATURES |
71 | |
85 | |
72 | Libev supports C<select>, C<poll>, the Linux-specific C<epoll>, the |
86 | Libev supports C<select>, C<poll>, the Linux-specific C<epoll>, the |
73 | BSD-specific C<kqueue> and the Solaris-specific event port mechanisms |
87 | BSD-specific C<kqueue> and the Solaris-specific event port mechanisms |
74 | for file descriptor events (C<ev_io>), the Linux C<inotify> interface |
88 | for file descriptor events (C<ev_io>), the Linux C<inotify> interface |
75 | (for C<ev_stat>), relative timers (C<ev_timer>), absolute timers |
89 | (for C<ev_stat>), relative timers (C<ev_timer>), absolute timers |
… | |
… | |
82 | |
96 | |
83 | It also is quite fast (see this |
97 | It also is quite fast (see this |
84 | L<benchmark|http://libev.schmorp.de/bench.html> comparing it to libevent |
98 | L<benchmark|http://libev.schmorp.de/bench.html> comparing it to libevent |
85 | for example). |
99 | for example). |
86 | |
100 | |
87 | =head1 CONVENTIONS |
101 | =head2 CONVENTIONS |
88 | |
102 | |
89 | Libev is very configurable. In this manual the default configuration will |
103 | Libev is very configurable. In this manual the default (and most common) |
90 | be described, which supports multiple event loops. For more info about |
104 | configuration will be described, which supports multiple event loops. For |
91 | various configuration options please have a look at B<EMBED> section in |
105 | more info about various configuration options please have a look at |
92 | this manual. If libev was configured without support for multiple event |
106 | B<EMBED> section in this manual. If libev was configured without support |
93 | loops, then all functions taking an initial argument of name C<loop> |
107 | for multiple event loops, then all functions taking an initial argument of |
94 | (which is always of type C<struct ev_loop *>) will not have this argument. |
108 | name C<loop> (which is always of type C<struct ev_loop *>) will not have |
|
|
109 | this argument. |
95 | |
110 | |
96 | =head1 TIME REPRESENTATION |
111 | =head2 TIME REPRESENTATION |
97 | |
112 | |
98 | Libev represents time as a single floating point number, representing the |
113 | Libev represents time as a single floating point number, representing the |
99 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
114 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
100 | the beginning of 1970, details are complicated, don't ask). This type is |
115 | the beginning of 1970, details are complicated, don't ask). This type is |
101 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
116 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
102 | to the C<double> type in C, and when you need to do any calculations on |
117 | to the C<double> type in C, and when you need to do any calculations on |
103 | it, you should treat it as some floatingpoint value. Unlike the name |
118 | it, you should treat it as some floating point value. Unlike the name |
104 | component C<stamp> might indicate, it is also used for time differences |
119 | component C<stamp> might indicate, it is also used for time differences |
105 | throughout libev. |
120 | throughout libev. |
|
|
121 | |
|
|
122 | =head1 ERROR HANDLING |
|
|
123 | |
|
|
124 | Libev knows three classes of errors: operating system errors, usage errors |
|
|
125 | and internal errors (bugs). |
|
|
126 | |
|
|
127 | When libev catches an operating system error it cannot handle (for example |
|
|
128 | a system call indicating a condition libev cannot fix), it calls the callback |
|
|
129 | set via C<ev_set_syserr_cb>, which is supposed to fix the problem or |
|
|
130 | abort. The default is to print a diagnostic message and to call C<abort |
|
|
131 | ()>. |
|
|
132 | |
|
|
133 | When libev detects a usage error such as a negative timer interval, then |
|
|
134 | it will print a diagnostic message and abort (via the C<assert> mechanism, |
|
|
135 | so C<NDEBUG> will disable this checking): these are programming errors in |
|
|
136 | the libev caller and need to be fixed there. |
|
|
137 | |
|
|
138 | Libev also has a few internal error-checking C<assert>ions, and also has |
|
|
139 | extensive consistency checking code. These do not trigger under normal |
|
|
140 | circumstances, as they indicate either a bug in libev or worse. |
|
|
141 | |
106 | |
142 | |
107 | =head1 GLOBAL FUNCTIONS |
143 | =head1 GLOBAL FUNCTIONS |
108 | |
144 | |
109 | These functions can be called anytime, even before initialising the |
145 | These functions can be called anytime, even before initialising the |
110 | library in any way. |
146 | library in any way. |
… | |
… | |
119 | |
155 | |
120 | =item ev_sleep (ev_tstamp interval) |
156 | =item ev_sleep (ev_tstamp interval) |
121 | |
157 | |
122 | Sleep for the given interval: The current thread will be blocked until |
158 | Sleep for the given interval: The current thread will be blocked until |
123 | either it is interrupted or the given time interval has passed. Basically |
159 | either it is interrupted or the given time interval has passed. Basically |
124 | this is a subsecond-resolution C<sleep ()>. |
160 | this is a sub-second-resolution C<sleep ()>. |
125 | |
161 | |
126 | =item int ev_version_major () |
162 | =item int ev_version_major () |
127 | |
163 | |
128 | =item int ev_version_minor () |
164 | =item int ev_version_minor () |
129 | |
165 | |
… | |
… | |
142 | not a problem. |
178 | not a problem. |
143 | |
179 | |
144 | Example: Make sure we haven't accidentally been linked against the wrong |
180 | Example: Make sure we haven't accidentally been linked against the wrong |
145 | version. |
181 | version. |
146 | |
182 | |
147 | assert (("libev version mismatch", |
183 | assert (("libev version mismatch", |
148 | ev_version_major () == EV_VERSION_MAJOR |
184 | ev_version_major () == EV_VERSION_MAJOR |
149 | && ev_version_minor () >= EV_VERSION_MINOR)); |
185 | && ev_version_minor () >= EV_VERSION_MINOR)); |
150 | |
186 | |
151 | =item unsigned int ev_supported_backends () |
187 | =item unsigned int ev_supported_backends () |
152 | |
188 | |
153 | Return the set of all backends (i.e. their corresponding C<EV_BACKEND_*> |
189 | Return the set of all backends (i.e. their corresponding C<EV_BACKEND_*> |
154 | value) compiled into this binary of libev (independent of their |
190 | value) compiled into this binary of libev (independent of their |
… | |
… | |
156 | a description of the set values. |
192 | a description of the set values. |
157 | |
193 | |
158 | Example: make sure we have the epoll method, because yeah this is cool and |
194 | Example: make sure we have the epoll method, because yeah this is cool and |
159 | a must have and can we have a torrent of it please!!!11 |
195 | a must have and can we have a torrent of it please!!!11 |
160 | |
196 | |
161 | assert (("sorry, no epoll, no sex", |
197 | assert (("sorry, no epoll, no sex", |
162 | ev_supported_backends () & EVBACKEND_EPOLL)); |
198 | ev_supported_backends () & EVBACKEND_EPOLL)); |
163 | |
199 | |
164 | =item unsigned int ev_recommended_backends () |
200 | =item unsigned int ev_recommended_backends () |
165 | |
201 | |
166 | Return the set of all backends compiled into this binary of libev and also |
202 | Return the set of all backends compiled into this binary of libev and also |
167 | recommended for this platform. This set is often smaller than the one |
203 | recommended for this platform. This set is often smaller than the one |
168 | returned by C<ev_supported_backends>, as for example kqueue is broken on |
204 | returned by C<ev_supported_backends>, as for example kqueue is broken on |
169 | most BSDs and will not be autodetected unless you explicitly request it |
205 | most BSDs and will not be auto-detected unless you explicitly request it |
170 | (assuming you know what you are doing). This is the set of backends that |
206 | (assuming you know what you are doing). This is the set of backends that |
171 | libev will probe for if you specify no backends explicitly. |
207 | libev will probe for if you specify no backends explicitly. |
172 | |
208 | |
173 | =item unsigned int ev_embeddable_backends () |
209 | =item unsigned int ev_embeddable_backends () |
174 | |
210 | |
… | |
… | |
178 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
214 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
179 | recommended ones. |
215 | recommended ones. |
180 | |
216 | |
181 | See the description of C<ev_embed> watchers for more info. |
217 | See the description of C<ev_embed> watchers for more info. |
182 | |
218 | |
183 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
219 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) [NOT REENTRANT] |
184 | |
220 | |
185 | Sets the allocation function to use (the prototype is similar - the |
221 | Sets the allocation function to use (the prototype is similar - the |
186 | semantics is identical - to the realloc C function). It is used to |
222 | semantics are identical to the C<realloc> C89/SuS/POSIX function). It is |
187 | allocate and free memory (no surprises here). If it returns zero when |
223 | used to allocate and free memory (no surprises here). If it returns zero |
188 | memory needs to be allocated, the library might abort or take some |
224 | when memory needs to be allocated (C<size != 0>), the library might abort |
189 | potentially destructive action. The default is your system realloc |
225 | or take some potentially destructive action. |
190 | function. |
226 | |
|
|
227 | Since some systems (at least OpenBSD and Darwin) fail to implement |
|
|
228 | correct C<realloc> semantics, libev will use a wrapper around the system |
|
|
229 | C<realloc> and C<free> functions by default. |
191 | |
230 | |
192 | You could override this function in high-availability programs to, say, |
231 | You could override this function in high-availability programs to, say, |
193 | free some memory if it cannot allocate memory, to use a special allocator, |
232 | free some memory if it cannot allocate memory, to use a special allocator, |
194 | or even to sleep a while and retry until some memory is available. |
233 | or even to sleep a while and retry until some memory is available. |
195 | |
234 | |
196 | Example: Replace the libev allocator with one that waits a bit and then |
235 | Example: Replace the libev allocator with one that waits a bit and then |
197 | retries). |
236 | retries (example requires a standards-compliant C<realloc>). |
198 | |
237 | |
199 | static void * |
238 | static void * |
200 | persistent_realloc (void *ptr, size_t size) |
239 | persistent_realloc (void *ptr, size_t size) |
201 | { |
240 | { |
202 | for (;;) |
241 | for (;;) |
… | |
… | |
211 | } |
250 | } |
212 | |
251 | |
213 | ... |
252 | ... |
214 | ev_set_allocator (persistent_realloc); |
253 | ev_set_allocator (persistent_realloc); |
215 | |
254 | |
216 | =item ev_set_syserr_cb (void (*cb)(const char *msg)); |
255 | =item ev_set_syserr_cb (void (*cb)(const char *msg)); [NOT REENTRANT] |
217 | |
256 | |
218 | Set the callback function to call on a retryable syscall error (such |
257 | Set the callback function to call on a retryable system call error (such |
219 | as failed select, poll, epoll_wait). The message is a printable string |
258 | as failed select, poll, epoll_wait). The message is a printable string |
220 | indicating the system call or subsystem causing the problem. If this |
259 | indicating the system call or subsystem causing the problem. If this |
221 | callback is set, then libev will expect it to remedy the sitution, no |
260 | callback is set, then libev will expect it to remedy the situation, no |
222 | matter what, when it returns. That is, libev will generally retry the |
261 | matter what, when it returns. That is, libev will generally retry the |
223 | requested operation, or, if the condition doesn't go away, do bad stuff |
262 | requested operation, or, if the condition doesn't go away, do bad stuff |
224 | (such as abort). |
263 | (such as abort). |
225 | |
264 | |
226 | Example: This is basically the same thing that libev does internally, too. |
265 | Example: This is basically the same thing that libev does internally, too. |
… | |
… | |
241 | |
280 | |
242 | An event loop is described by a C<struct ev_loop *>. The library knows two |
281 | An event loop is described by a C<struct ev_loop *>. The library knows two |
243 | types of such loops, the I<default> loop, which supports signals and child |
282 | types of such loops, the I<default> loop, which supports signals and child |
244 | events, and dynamically created loops which do not. |
283 | events, and dynamically created loops which do not. |
245 | |
284 | |
246 | If you use threads, a common model is to run the default event loop |
|
|
247 | in your main thread (or in a separate thread) and for each thread you |
|
|
248 | create, you also create another event loop. Libev itself does no locking |
|
|
249 | whatsoever, so if you mix calls to the same event loop in different |
|
|
250 | threads, make sure you lock (this is usually a bad idea, though, even if |
|
|
251 | done correctly, because it's hideous and inefficient). |
|
|
252 | |
|
|
253 | =over 4 |
285 | =over 4 |
254 | |
286 | |
255 | =item struct ev_loop *ev_default_loop (unsigned int flags) |
287 | =item struct ev_loop *ev_default_loop (unsigned int flags) |
256 | |
288 | |
257 | This will initialise the default event loop if it hasn't been initialised |
289 | This will initialise the default event loop if it hasn't been initialised |
… | |
… | |
260 | flags. If that is troubling you, check C<ev_backend ()> afterwards). |
292 | flags. If that is troubling you, check C<ev_backend ()> afterwards). |
261 | |
293 | |
262 | If you don't know what event loop to use, use the one returned from this |
294 | If you don't know what event loop to use, use the one returned from this |
263 | function. |
295 | function. |
264 | |
296 | |
|
|
297 | Note that this function is I<not> thread-safe, so if you want to use it |
|
|
298 | from multiple threads, you have to lock (note also that this is unlikely, |
|
|
299 | as loops cannot bes hared easily between threads anyway). |
|
|
300 | |
|
|
301 | The default loop is the only loop that can handle C<ev_signal> and |
|
|
302 | C<ev_child> watchers, and to do this, it always registers a handler |
|
|
303 | for C<SIGCHLD>. If this is a problem for your application you can either |
|
|
304 | create a dynamic loop with C<ev_loop_new> that doesn't do that, or you |
|
|
305 | can simply overwrite the C<SIGCHLD> signal handler I<after> calling |
|
|
306 | C<ev_default_init>. |
|
|
307 | |
265 | The flags argument can be used to specify special behaviour or specific |
308 | The flags argument can be used to specify special behaviour or specific |
266 | backends to use, and is usually specified as C<0> (or C<EVFLAG_AUTO>). |
309 | backends to use, and is usually specified as C<0> (or C<EVFLAG_AUTO>). |
267 | |
310 | |
268 | The following flags are supported: |
311 | The following flags are supported: |
269 | |
312 | |
… | |
… | |
274 | The default flags value. Use this if you have no clue (it's the right |
317 | The default flags value. Use this if you have no clue (it's the right |
275 | thing, believe me). |
318 | thing, believe me). |
276 | |
319 | |
277 | =item C<EVFLAG_NOENV> |
320 | =item C<EVFLAG_NOENV> |
278 | |
321 | |
279 | If this flag bit is ored into the flag value (or the program runs setuid |
322 | If this flag bit is or'ed into the flag value (or the program runs setuid |
280 | or setgid) then libev will I<not> look at the environment variable |
323 | or setgid) then libev will I<not> look at the environment variable |
281 | C<LIBEV_FLAGS>. Otherwise (the default), this environment variable will |
324 | C<LIBEV_FLAGS>. Otherwise (the default), this environment variable will |
282 | override the flags completely if it is found in the environment. This is |
325 | override the flags completely if it is found in the environment. This is |
283 | useful to try out specific backends to test their performance, or to work |
326 | useful to try out specific backends to test their performance, or to work |
284 | around bugs. |
327 | around bugs. |
… | |
… | |
290 | enabling this flag. |
333 | enabling this flag. |
291 | |
334 | |
292 | This works by calling C<getpid ()> on every iteration of the loop, |
335 | This works by calling C<getpid ()> on every iteration of the loop, |
293 | and thus this might slow down your event loop if you do a lot of loop |
336 | and thus this might slow down your event loop if you do a lot of loop |
294 | iterations and little real work, but is usually not noticeable (on my |
337 | iterations and little real work, but is usually not noticeable (on my |
295 | Linux system for example, C<getpid> is actually a simple 5-insn sequence |
338 | GNU/Linux system for example, C<getpid> is actually a simple 5-insn sequence |
296 | without a syscall and thus I<very> fast, but my Linux system also has |
339 | without a system call and thus I<very> fast, but my GNU/Linux system also has |
297 | C<pthread_atfork> which is even faster). |
340 | C<pthread_atfork> which is even faster). |
298 | |
341 | |
299 | The big advantage of this flag is that you can forget about fork (and |
342 | The big advantage of this flag is that you can forget about fork (and |
300 | forget about forgetting to tell libev about forking) when you use this |
343 | forget about forgetting to tell libev about forking) when you use this |
301 | flag. |
344 | flag. |
302 | |
345 | |
303 | This flag setting cannot be overriden or specified in the C<LIBEV_FLAGS> |
346 | This flag setting cannot be overridden or specified in the C<LIBEV_FLAGS> |
304 | environment variable. |
347 | environment variable. |
305 | |
348 | |
306 | =item C<EVBACKEND_SELECT> (value 1, portable select backend) |
349 | =item C<EVBACKEND_SELECT> (value 1, portable select backend) |
307 | |
350 | |
308 | This is your standard select(2) backend. Not I<completely> standard, as |
351 | This is your standard select(2) backend. Not I<completely> standard, as |
309 | libev tries to roll its own fd_set with no limits on the number of fds, |
352 | libev tries to roll its own fd_set with no limits on the number of fds, |
310 | but if that fails, expect a fairly low limit on the number of fds when |
353 | but if that fails, expect a fairly low limit on the number of fds when |
311 | using this backend. It doesn't scale too well (O(highest_fd)), but its usually |
354 | using this backend. It doesn't scale too well (O(highest_fd)), but its |
312 | the fastest backend for a low number of fds. |
355 | usually the fastest backend for a low number of (low-numbered :) fds. |
|
|
356 | |
|
|
357 | To get good performance out of this backend you need a high amount of |
|
|
358 | parallelism (most of the file descriptors should be busy). If you are |
|
|
359 | writing a server, you should C<accept ()> in a loop to accept as many |
|
|
360 | connections as possible during one iteration. You might also want to have |
|
|
361 | a look at C<ev_set_io_collect_interval ()> to increase the amount of |
|
|
362 | readiness notifications you get per iteration. |
|
|
363 | |
|
|
364 | This backend maps C<EV_READ> to the C<readfds> set and C<EV_WRITE> to the |
|
|
365 | C<writefds> set (and to work around Microsoft Windows bugs, also onto the |
|
|
366 | C<exceptfds> set on that platform). |
313 | |
367 | |
314 | =item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows) |
368 | =item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows) |
315 | |
369 | |
316 | And this is your standard poll(2) backend. It's more complicated than |
370 | And this is your standard poll(2) backend. It's more complicated |
317 | select, but handles sparse fds better and has no artificial limit on the |
371 | than select, but handles sparse fds better and has no artificial |
318 | number of fds you can use (except it will slow down considerably with a |
372 | limit on the number of fds you can use (except it will slow down |
319 | lot of inactive fds). It scales similarly to select, i.e. O(total_fds). |
373 | considerably with a lot of inactive fds). It scales similarly to select, |
|
|
374 | i.e. O(total_fds). See the entry for C<EVBACKEND_SELECT>, above, for |
|
|
375 | performance tips. |
|
|
376 | |
|
|
377 | This backend maps C<EV_READ> to C<POLLIN | POLLERR | POLLHUP>, and |
|
|
378 | C<EV_WRITE> to C<POLLOUT | POLLERR | POLLHUP>. |
320 | |
379 | |
321 | =item C<EVBACKEND_EPOLL> (value 4, Linux) |
380 | =item C<EVBACKEND_EPOLL> (value 4, Linux) |
322 | |
381 | |
323 | For few fds, this backend is a bit little slower than poll and select, |
382 | For few fds, this backend is a bit little slower than poll and select, |
324 | but it scales phenomenally better. While poll and select usually scale |
383 | but it scales phenomenally better. While poll and select usually scale |
325 | like O(total_fds) where n is the total number of fds (or the highest fd), |
384 | like O(total_fds) where n is the total number of fds (or the highest fd), |
326 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
385 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
327 | of shortcomings, such as silently dropping events in some hard-to-detect |
386 | of shortcomings, such as silently dropping events in some hard-to-detect |
328 | cases and rewiring a syscall per fd change, no fork support and bad |
387 | cases and requiring a system call per fd change, no fork support and bad |
329 | support for dup: |
388 | support for dup. |
330 | |
389 | |
331 | While stopping, setting and starting an I/O watcher in the same iteration |
390 | While stopping, setting and starting an I/O watcher in the same iteration |
332 | will result in some caching, there is still a syscall per such incident |
391 | will result in some caching, there is still a system call per such incident |
333 | (because the fd could point to a different file description now), so its |
392 | (because the fd could point to a different file description now), so its |
334 | best to avoid that. Also, C<dup ()>'ed file descriptors might not work |
393 | best to avoid that. Also, C<dup ()>'ed file descriptors might not work |
335 | very well if you register events for both fds. |
394 | very well if you register events for both fds. |
336 | |
395 | |
337 | Please note that epoll sometimes generates spurious notifications, so you |
396 | Please note that epoll sometimes generates spurious notifications, so you |
338 | need to use non-blocking I/O or other means to avoid blocking when no data |
397 | need to use non-blocking I/O or other means to avoid blocking when no data |
339 | (or space) is available. |
398 | (or space) is available. |
340 | |
399 | |
|
|
400 | Best performance from this backend is achieved by not unregistering all |
|
|
401 | watchers for a file descriptor until it has been closed, if possible, |
|
|
402 | i.e. keep at least one watcher active per fd at all times. Stopping and |
|
|
403 | starting a watcher (without re-setting it) also usually doesn't cause |
|
|
404 | extra overhead. |
|
|
405 | |
|
|
406 | While nominally embeddable in other event loops, this feature is broken in |
|
|
407 | all kernel versions tested so far. |
|
|
408 | |
|
|
409 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
|
|
410 | C<EVBACKEND_POLL>. |
|
|
411 | |
341 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
412 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
342 | |
413 | |
343 | Kqueue deserves special mention, as at the time of this writing, it |
414 | Kqueue deserves special mention, as at the time of this writing, it was |
344 | was broken on all BSDs except NetBSD (usually it doesn't work reliably |
415 | broken on all BSDs except NetBSD (usually it doesn't work reliably with |
345 | with anything but sockets and pipes, except on Darwin, where of course |
416 | anything but sockets and pipes, except on Darwin, where of course it's |
346 | it's completely useless). For this reason it's not being "autodetected" |
417 | completely useless). For this reason it's not being "auto-detected" unless |
347 | unless you explicitly specify it explicitly in the flags (i.e. using |
418 | you explicitly specify it in the flags (i.e. using C<EVBACKEND_KQUEUE>) or |
348 | C<EVBACKEND_KQUEUE>) or libev was compiled on a known-to-be-good (-enough) |
419 | libev was compiled on a known-to-be-good (-enough) system like NetBSD. |
349 | system like NetBSD. |
|
|
350 | |
420 | |
351 | You still can embed kqueue into a normal poll or select backend and use it |
421 | You still can embed kqueue into a normal poll or select backend and use it |
352 | only for sockets (after having made sure that sockets work with kqueue on |
422 | only for sockets (after having made sure that sockets work with kqueue on |
353 | the target platform). See C<ev_embed> watchers for more info. |
423 | the target platform). See C<ev_embed> watchers for more info. |
354 | |
424 | |
355 | It scales in the same way as the epoll backend, but the interface to the |
425 | It scales in the same way as the epoll backend, but the interface to the |
356 | kernel is more efficient (which says nothing about its actual speed, of |
426 | kernel is more efficient (which says nothing about its actual speed, of |
357 | course). While stopping, setting and starting an I/O watcher does never |
427 | course). While stopping, setting and starting an I/O watcher does never |
358 | cause an extra syscall as with C<EVBACKEND_EPOLL>, it still adds up to |
428 | cause an extra system call as with C<EVBACKEND_EPOLL>, it still adds up to |
359 | two event changes per incident, support for C<fork ()> is very bad and it |
429 | two event changes per incident. Support for C<fork ()> is very bad and it |
360 | drops fds silently in similarly hard-to-detect cases. |
430 | drops fds silently in similarly hard-to-detect cases. |
361 | |
431 | |
|
|
432 | This backend usually performs well under most conditions. |
|
|
433 | |
|
|
434 | While nominally embeddable in other event loops, this doesn't work |
|
|
435 | everywhere, so you might need to test for this. And since it is broken |
|
|
436 | almost everywhere, you should only use it when you have a lot of sockets |
|
|
437 | (for which it usually works), by embedding it into another event loop |
|
|
438 | (e.g. C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>) and, did I mention it, |
|
|
439 | using it only for sockets. |
|
|
440 | |
|
|
441 | This backend maps C<EV_READ> into an C<EVFILT_READ> kevent with |
|
|
442 | C<NOTE_EOF>, and C<EV_WRITE> into an C<EVFILT_WRITE> kevent with |
|
|
443 | C<NOTE_EOF>. |
|
|
444 | |
362 | =item C<EVBACKEND_DEVPOLL> (value 16, Solaris 8) |
445 | =item C<EVBACKEND_DEVPOLL> (value 16, Solaris 8) |
363 | |
446 | |
364 | This is not implemented yet (and might never be). |
447 | This is not implemented yet (and might never be, unless you send me an |
|
|
448 | implementation). According to reports, C</dev/poll> only supports sockets |
|
|
449 | and is not embeddable, which would limit the usefulness of this backend |
|
|
450 | immensely. |
365 | |
451 | |
366 | =item C<EVBACKEND_PORT> (value 32, Solaris 10) |
452 | =item C<EVBACKEND_PORT> (value 32, Solaris 10) |
367 | |
453 | |
368 | This uses the Solaris 10 event port mechanism. As with everything on Solaris, |
454 | This uses the Solaris 10 event port mechanism. As with everything on Solaris, |
369 | it's really slow, but it still scales very well (O(active_fds)). |
455 | it's really slow, but it still scales very well (O(active_fds)). |
370 | |
456 | |
371 | Please note that solaris event ports can deliver a lot of spurious |
457 | Please note that Solaris event ports can deliver a lot of spurious |
372 | notifications, so you need to use non-blocking I/O or other means to avoid |
458 | notifications, so you need to use non-blocking I/O or other means to avoid |
373 | blocking when no data (or space) is available. |
459 | blocking when no data (or space) is available. |
|
|
460 | |
|
|
461 | While this backend scales well, it requires one system call per active |
|
|
462 | file descriptor per loop iteration. For small and medium numbers of file |
|
|
463 | descriptors a "slow" C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> backend |
|
|
464 | might perform better. |
|
|
465 | |
|
|
466 | On the positive side, with the exception of the spurious readiness |
|
|
467 | notifications, this backend actually performed fully to specification |
|
|
468 | in all tests and is fully embeddable, which is a rare feat among the |
|
|
469 | OS-specific backends. |
|
|
470 | |
|
|
471 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
|
|
472 | C<EVBACKEND_POLL>. |
374 | |
473 | |
375 | =item C<EVBACKEND_ALL> |
474 | =item C<EVBACKEND_ALL> |
376 | |
475 | |
377 | Try all backends (even potentially broken ones that wouldn't be tried |
476 | Try all backends (even potentially broken ones that wouldn't be tried |
378 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
477 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
379 | C<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>. |
478 | C<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>. |
380 | |
479 | |
|
|
480 | It is definitely not recommended to use this flag. |
|
|
481 | |
381 | =back |
482 | =back |
382 | |
483 | |
383 | If one or more of these are ored into the flags value, then only these |
484 | If one or more of these are or'ed into the flags value, then only these |
384 | backends will be tried (in the reverse order as given here). If none are |
485 | backends will be tried (in the reverse order as listed here). If none are |
385 | specified, most compiled-in backend will be tried, usually in reverse |
486 | specified, all backends in C<ev_recommended_backends ()> will be tried. |
386 | order of their flag values :) |
|
|
387 | |
487 | |
388 | The most typical usage is like this: |
488 | Example: This is the most typical usage. |
389 | |
489 | |
390 | if (!ev_default_loop (0)) |
490 | if (!ev_default_loop (0)) |
391 | fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); |
491 | fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); |
392 | |
492 | |
393 | Restrict libev to the select and poll backends, and do not allow |
493 | Example: Restrict libev to the select and poll backends, and do not allow |
394 | environment settings to be taken into account: |
494 | environment settings to be taken into account: |
395 | |
495 | |
396 | ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); |
496 | ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); |
397 | |
497 | |
398 | Use whatever libev has to offer, but make sure that kqueue is used if |
498 | Example: Use whatever libev has to offer, but make sure that kqueue is |
399 | available (warning, breaks stuff, best use only with your own private |
499 | used if available (warning, breaks stuff, best use only with your own |
400 | event loop and only if you know the OS supports your types of fds): |
500 | private event loop and only if you know the OS supports your types of |
|
|
501 | fds): |
401 | |
502 | |
402 | ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); |
503 | ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); |
403 | |
504 | |
404 | =item struct ev_loop *ev_loop_new (unsigned int flags) |
505 | =item struct ev_loop *ev_loop_new (unsigned int flags) |
405 | |
506 | |
406 | Similar to C<ev_default_loop>, but always creates a new event loop that is |
507 | Similar to C<ev_default_loop>, but always creates a new event loop that is |
407 | always distinct from the default loop. Unlike the default loop, it cannot |
508 | always distinct from the default loop. Unlike the default loop, it cannot |
408 | handle signal and child watchers, and attempts to do so will be greeted by |
509 | handle signal and child watchers, and attempts to do so will be greeted by |
409 | undefined behaviour (or a failed assertion if assertions are enabled). |
510 | undefined behaviour (or a failed assertion if assertions are enabled). |
410 | |
511 | |
|
|
512 | Note that this function I<is> thread-safe, and the recommended way to use |
|
|
513 | libev with threads is indeed to create one loop per thread, and using the |
|
|
514 | default loop in the "main" or "initial" thread. |
|
|
515 | |
411 | Example: Try to create a event loop that uses epoll and nothing else. |
516 | Example: Try to create a event loop that uses epoll and nothing else. |
412 | |
517 | |
413 | struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); |
518 | struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); |
414 | if (!epoller) |
519 | if (!epoller) |
415 | fatal ("no epoll found here, maybe it hides under your chair"); |
520 | fatal ("no epoll found here, maybe it hides under your chair"); |
416 | |
521 | |
417 | =item ev_default_destroy () |
522 | =item ev_default_destroy () |
418 | |
523 | |
419 | Destroys the default loop again (frees all memory and kernel state |
524 | Destroys the default loop again (frees all memory and kernel state |
420 | etc.). None of the active event watchers will be stopped in the normal |
525 | etc.). None of the active event watchers will be stopped in the normal |
421 | sense, so e.g. C<ev_is_active> might still return true. It is your |
526 | sense, so e.g. C<ev_is_active> might still return true. It is your |
422 | responsibility to either stop all watchers cleanly yoursef I<before> |
527 | responsibility to either stop all watchers cleanly yourself I<before> |
423 | calling this function, or cope with the fact afterwards (which is usually |
528 | calling this function, or cope with the fact afterwards (which is usually |
424 | the easiest thing, you can just ignore the watchers and/or C<free ()> them |
529 | the easiest thing, you can just ignore the watchers and/or C<free ()> them |
425 | for example). |
530 | for example). |
426 | |
531 | |
427 | Note that certain global state, such as signal state, will not be freed by |
532 | Note that certain global state, such as signal state, will not be freed by |
… | |
… | |
438 | Like C<ev_default_destroy>, but destroys an event loop created by an |
543 | Like C<ev_default_destroy>, but destroys an event loop created by an |
439 | earlier call to C<ev_loop_new>. |
544 | earlier call to C<ev_loop_new>. |
440 | |
545 | |
441 | =item ev_default_fork () |
546 | =item ev_default_fork () |
442 | |
547 | |
|
|
548 | This function sets a flag that causes subsequent C<ev_loop> iterations |
443 | This function reinitialises the kernel state for backends that have |
549 | to reinitialise the kernel state for backends that have one. Despite the |
444 | one. Despite the name, you can call it anytime, but it makes most sense |
550 | name, you can call it anytime, but it makes most sense after forking, in |
445 | after forking, in either the parent or child process (or both, but that |
551 | the child process (or both child and parent, but that again makes little |
446 | again makes little sense). |
552 | sense). You I<must> call it in the child before using any of the libev |
|
|
553 | functions, and it will only take effect at the next C<ev_loop> iteration. |
447 | |
554 | |
448 | You I<must> call this function in the child process after forking if and |
555 | On the other hand, you only need to call this function in the child |
449 | only if you want to use the event library in both processes. If you just |
556 | process if and only if you want to use the event library in the child. If |
450 | fork+exec, you don't have to call it. |
557 | you just fork+exec, you don't have to call it at all. |
451 | |
558 | |
452 | The function itself is quite fast and it's usually not a problem to call |
559 | The function itself is quite fast and it's usually not a problem to call |
453 | it just in case after a fork. To make this easy, the function will fit in |
560 | it just in case after a fork. To make this easy, the function will fit in |
454 | quite nicely into a call to C<pthread_atfork>: |
561 | quite nicely into a call to C<pthread_atfork>: |
455 | |
562 | |
456 | pthread_atfork (0, 0, ev_default_fork); |
563 | pthread_atfork (0, 0, ev_default_fork); |
457 | |
564 | |
458 | At the moment, C<EVBACKEND_SELECT> and C<EVBACKEND_POLL> are safe to use |
|
|
459 | without calling this function, so if you force one of those backends you |
|
|
460 | do not need to care. |
|
|
461 | |
|
|
462 | =item ev_loop_fork (loop) |
565 | =item ev_loop_fork (loop) |
463 | |
566 | |
464 | Like C<ev_default_fork>, but acts on an event loop created by |
567 | Like C<ev_default_fork>, but acts on an event loop created by |
465 | C<ev_loop_new>. Yes, you have to call this on every allocated event loop |
568 | C<ev_loop_new>. Yes, you have to call this on every allocated event loop |
466 | after fork, and how you do this is entirely your own problem. |
569 | after fork that you want to re-use in the child, and how you do this is |
|
|
570 | entirely your own problem. |
|
|
571 | |
|
|
572 | =item int ev_is_default_loop (loop) |
|
|
573 | |
|
|
574 | Returns true when the given loop is, in fact, the default loop, and false |
|
|
575 | otherwise. |
467 | |
576 | |
468 | =item unsigned int ev_loop_count (loop) |
577 | =item unsigned int ev_loop_count (loop) |
469 | |
578 | |
470 | Returns the count of loop iterations for the loop, which is identical to |
579 | Returns the count of loop iterations for the loop, which is identical to |
471 | the number of times libev did poll for new events. It starts at C<0> and |
580 | the number of times libev did poll for new events. It starts at C<0> and |
… | |
… | |
486 | received events and started processing them. This timestamp does not |
595 | received events and started processing them. This timestamp does not |
487 | change as long as callbacks are being processed, and this is also the base |
596 | change as long as callbacks are being processed, and this is also the base |
488 | time used for relative timers. You can treat it as the timestamp of the |
597 | time used for relative timers. You can treat it as the timestamp of the |
489 | event occurring (or more correctly, libev finding out about it). |
598 | event occurring (or more correctly, libev finding out about it). |
490 | |
599 | |
|
|
600 | =item ev_now_update (loop) |
|
|
601 | |
|
|
602 | Establishes the current time by querying the kernel, updating the time |
|
|
603 | returned by C<ev_now ()> in the progress. This is a costly operation and |
|
|
604 | is usually done automatically within C<ev_loop ()>. |
|
|
605 | |
|
|
606 | This function is rarely useful, but when some event callback runs for a |
|
|
607 | very long time without entering the event loop, updating libev's idea of |
|
|
608 | the current time is a good idea. |
|
|
609 | |
|
|
610 | See also "The special problem of time updates" in the C<ev_timer> section. |
|
|
611 | |
491 | =item ev_loop (loop, int flags) |
612 | =item ev_loop (loop, int flags) |
492 | |
613 | |
493 | Finally, this is it, the event handler. This function usually is called |
614 | Finally, this is it, the event handler. This function usually is called |
494 | after you initialised all your watchers and you want to start handling |
615 | after you initialised all your watchers and you want to start handling |
495 | events. |
616 | events. |
… | |
… | |
497 | If the flags argument is specified as C<0>, it will not return until |
618 | If the flags argument is specified as C<0>, it will not return until |
498 | either no event watchers are active anymore or C<ev_unloop> was called. |
619 | either no event watchers are active anymore or C<ev_unloop> was called. |
499 | |
620 | |
500 | Please note that an explicit C<ev_unloop> is usually better than |
621 | Please note that an explicit C<ev_unloop> is usually better than |
501 | relying on all watchers to be stopped when deciding when a program has |
622 | relying on all watchers to be stopped when deciding when a program has |
502 | finished (especially in interactive programs), but having a program that |
623 | finished (especially in interactive programs), but having a program |
503 | automatically loops as long as it has to and no longer by virtue of |
624 | that automatically loops as long as it has to and no longer by virtue |
504 | relying on its watchers stopping correctly is a thing of beauty. |
625 | of relying on its watchers stopping correctly, that is truly a thing of |
|
|
626 | beauty. |
505 | |
627 | |
506 | A flags value of C<EVLOOP_NONBLOCK> will look for new events, will handle |
628 | A flags value of C<EVLOOP_NONBLOCK> will look for new events, will handle |
507 | those events and any outstanding ones, but will not block your process in |
629 | those events and any already outstanding ones, but will not block your |
508 | case there are no events and will return after one iteration of the loop. |
630 | process in case there are no events and will return after one iteration of |
|
|
631 | the loop. |
509 | |
632 | |
510 | A flags value of C<EVLOOP_ONESHOT> will look for new events (waiting if |
633 | A flags value of C<EVLOOP_ONESHOT> will look for new events (waiting if |
511 | neccessary) and will handle those and any outstanding ones. It will block |
634 | necessary) and will handle those and any already outstanding ones. It |
512 | your process until at least one new event arrives, and will return after |
635 | will block your process until at least one new event arrives (which could |
513 | one iteration of the loop. This is useful if you are waiting for some |
636 | be an event internal to libev itself, so there is no guarentee that a |
514 | external event in conjunction with something not expressible using other |
637 | user-registered callback will be called), and will return after one |
|
|
638 | iteration of the loop. |
|
|
639 | |
|
|
640 | This is useful if you are waiting for some external event in conjunction |
|
|
641 | with something not expressible using other libev watchers (i.e. "roll your |
515 | libev watchers. However, a pair of C<ev_prepare>/C<ev_check> watchers is |
642 | own C<ev_loop>"). However, a pair of C<ev_prepare>/C<ev_check> watchers is |
516 | usually a better approach for this kind of thing. |
643 | usually a better approach for this kind of thing. |
517 | |
644 | |
518 | Here are the gory details of what C<ev_loop> does: |
645 | Here are the gory details of what C<ev_loop> does: |
519 | |
646 | |
520 | - Before the first iteration, call any pending watchers. |
647 | - Before the first iteration, call any pending watchers. |
521 | * If there are no active watchers (reference count is zero), return. |
648 | * If EVFLAG_FORKCHECK was used, check for a fork. |
522 | - Queue all prepare watchers and then call all outstanding watchers. |
649 | - If a fork was detected (by any means), queue and call all fork watchers. |
|
|
650 | - Queue and call all prepare watchers. |
523 | - If we have been forked, recreate the kernel state. |
651 | - If we have been forked, detach and recreate the kernel state |
|
|
652 | as to not disturb the other process. |
524 | - Update the kernel state with all outstanding changes. |
653 | - Update the kernel state with all outstanding changes. |
525 | - Update the "event loop time". |
654 | - Update the "event loop time" (ev_now ()). |
526 | - Calculate for how long to block. |
655 | - Calculate for how long to sleep or block, if at all |
|
|
656 | (active idle watchers, EVLOOP_NONBLOCK or not having |
|
|
657 | any active watchers at all will result in not sleeping). |
|
|
658 | - Sleep if the I/O and timer collect interval say so. |
527 | - Block the process, waiting for any events. |
659 | - Block the process, waiting for any events. |
528 | - Queue all outstanding I/O (fd) events. |
660 | - Queue all outstanding I/O (fd) events. |
529 | - Update the "event loop time" and do time jump handling. |
661 | - Update the "event loop time" (ev_now ()), and do time jump adjustments. |
530 | - Queue all outstanding timers. |
662 | - Queue all expired timers. |
531 | - Queue all outstanding periodics. |
663 | - Queue all expired periodics. |
532 | - If no events are pending now, queue all idle watchers. |
664 | - Unless any events are pending now, queue all idle watchers. |
533 | - Queue all check watchers. |
665 | - Queue all check watchers. |
534 | - Call all queued watchers in reverse order (i.e. check watchers first). |
666 | - Call all queued watchers in reverse order (i.e. check watchers first). |
535 | Signals and child watchers are implemented as I/O watchers, and will |
667 | Signals and child watchers are implemented as I/O watchers, and will |
536 | be handled here by queueing them when their watcher gets executed. |
668 | be handled here by queueing them when their watcher gets executed. |
537 | - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
669 | - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
538 | were used, return, otherwise continue with step *. |
670 | were used, or there are no active watchers, return, otherwise |
|
|
671 | continue with step *. |
539 | |
672 | |
540 | Example: Queue some jobs and then loop until no events are outsanding |
673 | Example: Queue some jobs and then loop until no events are outstanding |
541 | anymore. |
674 | anymore. |
542 | |
675 | |
543 | ... queue jobs here, make sure they register event watchers as long |
676 | ... queue jobs here, make sure they register event watchers as long |
544 | ... as they still have work to do (even an idle watcher will do..) |
677 | ... as they still have work to do (even an idle watcher will do..) |
545 | ev_loop (my_loop, 0); |
678 | ev_loop (my_loop, 0); |
546 | ... jobs done. yeah! |
679 | ... jobs done or somebody called unloop. yeah! |
547 | |
680 | |
548 | =item ev_unloop (loop, how) |
681 | =item ev_unloop (loop, how) |
549 | |
682 | |
550 | Can be used to make a call to C<ev_loop> return early (but only after it |
683 | Can be used to make a call to C<ev_loop> return early (but only after it |
551 | has processed all outstanding events). The C<how> argument must be either |
684 | has processed all outstanding events). The C<how> argument must be either |
552 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
685 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
553 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
686 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
554 | |
687 | |
|
|
688 | This "unloop state" will be cleared when entering C<ev_loop> again. |
|
|
689 | |
|
|
690 | It is safe to call C<ev_unloop> from otuside any C<ev_loop> calls. |
|
|
691 | |
555 | =item ev_ref (loop) |
692 | =item ev_ref (loop) |
556 | |
693 | |
557 | =item ev_unref (loop) |
694 | =item ev_unref (loop) |
558 | |
695 | |
559 | Ref/unref can be used to add or remove a reference count on the event |
696 | Ref/unref can be used to add or remove a reference count on the event |
560 | loop: Every watcher keeps one reference, and as long as the reference |
697 | loop: Every watcher keeps one reference, and as long as the reference |
561 | count is nonzero, C<ev_loop> will not return on its own. If you have |
698 | count is nonzero, C<ev_loop> will not return on its own. |
|
|
699 | |
562 | a watcher you never unregister that should not keep C<ev_loop> from |
700 | If you have a watcher you never unregister that should not keep C<ev_loop> |
563 | returning, ev_unref() after starting, and ev_ref() before stopping it. For |
701 | from returning, call ev_unref() after starting, and ev_ref() before |
|
|
702 | stopping it. |
|
|
703 | |
564 | example, libev itself uses this for its internal signal pipe: It is not |
704 | As an example, libev itself uses this for its internal signal pipe: It is |
565 | visible to the libev user and should not keep C<ev_loop> from exiting if |
705 | not visible to the libev user and should not keep C<ev_loop> from exiting |
566 | no event watchers registered by it are active. It is also an excellent |
706 | if no event watchers registered by it are active. It is also an excellent |
567 | way to do this for generic recurring timers or from within third-party |
707 | way to do this for generic recurring timers or from within third-party |
568 | libraries. Just remember to I<unref after start> and I<ref before stop>. |
708 | libraries. Just remember to I<unref after start> and I<ref before stop> |
|
|
709 | (but only if the watcher wasn't active before, or was active before, |
|
|
710 | respectively). |
569 | |
711 | |
570 | Example: Create a signal watcher, but keep it from keeping C<ev_loop> |
712 | Example: Create a signal watcher, but keep it from keeping C<ev_loop> |
571 | running when nothing else is active. |
713 | running when nothing else is active. |
572 | |
714 | |
573 | struct ev_signal exitsig; |
715 | struct ev_signal exitsig; |
574 | ev_signal_init (&exitsig, sig_cb, SIGINT); |
716 | ev_signal_init (&exitsig, sig_cb, SIGINT); |
575 | ev_signal_start (loop, &exitsig); |
717 | ev_signal_start (loop, &exitsig); |
576 | evf_unref (loop); |
718 | evf_unref (loop); |
577 | |
719 | |
578 | Example: For some weird reason, unregister the above signal handler again. |
720 | Example: For some weird reason, unregister the above signal handler again. |
579 | |
721 | |
580 | ev_ref (loop); |
722 | ev_ref (loop); |
581 | ev_signal_stop (loop, &exitsig); |
723 | ev_signal_stop (loop, &exitsig); |
582 | |
724 | |
583 | =item ev_set_io_collect_interval (loop, ev_tstamp interval) |
725 | =item ev_set_io_collect_interval (loop, ev_tstamp interval) |
584 | |
726 | |
585 | =item ev_set_timeout_collect_interval (loop, ev_tstamp interval) |
727 | =item ev_set_timeout_collect_interval (loop, ev_tstamp interval) |
586 | |
728 | |
587 | These advanced functions influence the time that libev will spend waiting |
729 | These advanced functions influence the time that libev will spend waiting |
588 | for events. Both are by default C<0>, meaning that libev will try to |
730 | for events. Both time intervals are by default C<0>, meaning that libev |
589 | invoke timer/periodic callbacks and I/O callbacks with minimum latency. |
731 | will try to invoke timer/periodic callbacks and I/O callbacks with minimum |
|
|
732 | latency. |
590 | |
733 | |
591 | Setting these to a higher value (the C<interval> I<must> be >= C<0>) |
734 | Setting these to a higher value (the C<interval> I<must> be >= C<0>) |
592 | allows libev to delay invocation of I/O and timer/periodic callbacks to |
735 | allows libev to delay invocation of I/O and timer/periodic callbacks |
593 | increase efficiency of loop iterations. |
736 | to increase efficiency of loop iterations (or to increase power-saving |
|
|
737 | opportunities). |
594 | |
738 | |
595 | The background is that sometimes your program runs just fast enough to |
739 | The idea is that sometimes your program runs just fast enough to handle |
596 | handle one (or very few) event(s) per loop iteration. While this makes |
740 | one (or very few) event(s) per loop iteration. While this makes the |
597 | the program responsive, it also wastes a lot of CPU time to poll for new |
741 | program responsive, it also wastes a lot of CPU time to poll for new |
598 | events, especially with backends like C<select ()> which have a high |
742 | events, especially with backends like C<select ()> which have a high |
599 | overhead for the actual polling but can deliver many events at once. |
743 | overhead for the actual polling but can deliver many events at once. |
600 | |
744 | |
601 | By setting a higher I<io collect interval> you allow libev to spend more |
745 | By setting a higher I<io collect interval> you allow libev to spend more |
602 | time collecting I/O events, so you can handle more events per iteration, |
746 | time collecting I/O events, so you can handle more events per iteration, |
… | |
… | |
604 | C<ev_timer>) will be not affected. Setting this to a non-null value will |
748 | C<ev_timer>) will be not affected. Setting this to a non-null value will |
605 | introduce an additional C<ev_sleep ()> call into most loop iterations. |
749 | introduce an additional C<ev_sleep ()> call into most loop iterations. |
606 | |
750 | |
607 | Likewise, by setting a higher I<timeout collect interval> you allow libev |
751 | Likewise, by setting a higher I<timeout collect interval> you allow libev |
608 | to spend more time collecting timeouts, at the expense of increased |
752 | to spend more time collecting timeouts, at the expense of increased |
609 | latency (the watcher callback will be called later). C<ev_io> watchers |
753 | latency/jitter/inexactness (the watcher callback will be called |
610 | will not be affected. Setting this to a non-null value will not introduce |
754 | later). C<ev_io> watchers will not be affected. Setting this to a non-null |
611 | any overhead in libev. |
755 | value will not introduce any overhead in libev. |
612 | |
756 | |
613 | Many (busy) programs can usually benefit by setting the io collect |
757 | Many (busy) programs can usually benefit by setting the I/O collect |
614 | interval to a value near C<0.1> or so, which is often enough for |
758 | interval to a value near C<0.1> or so, which is often enough for |
615 | interactive servers (of course not for games), likewise for timeouts. It |
759 | interactive servers (of course not for games), likewise for timeouts. It |
616 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
760 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
617 | as this approsaches the timing granularity of most systems. |
761 | as this approaches the timing granularity of most systems. |
|
|
762 | |
|
|
763 | Setting the I<timeout collect interval> can improve the opportunity for |
|
|
764 | saving power, as the program will "bundle" timer callback invocations that |
|
|
765 | are "near" in time together, by delaying some, thus reducing the number of |
|
|
766 | times the process sleeps and wakes up again. Another useful technique to |
|
|
767 | reduce iterations/wake-ups is to use C<ev_periodic> watchers and make sure |
|
|
768 | they fire on, say, one-second boundaries only. |
|
|
769 | |
|
|
770 | =item ev_loop_verify (loop) |
|
|
771 | |
|
|
772 | This function only does something when C<EV_VERIFY> support has been |
|
|
773 | compiled in. which is the default for non-minimal builds. It tries to go |
|
|
774 | through all internal structures and checks them for validity. If anything |
|
|
775 | is found to be inconsistent, it will print an error message to standard |
|
|
776 | error and call C<abort ()>. |
|
|
777 | |
|
|
778 | This can be used to catch bugs inside libev itself: under normal |
|
|
779 | circumstances, this function will never abort as of course libev keeps its |
|
|
780 | data structures consistent. |
618 | |
781 | |
619 | =back |
782 | =back |
620 | |
783 | |
621 | |
784 | |
622 | =head1 ANATOMY OF A WATCHER |
785 | =head1 ANATOMY OF A WATCHER |
623 | |
786 | |
624 | A watcher is a structure that you create and register to record your |
787 | A watcher is a structure that you create and register to record your |
625 | interest in some event. For instance, if you want to wait for STDIN to |
788 | interest in some event. For instance, if you want to wait for STDIN to |
626 | become readable, you would create an C<ev_io> watcher for that: |
789 | become readable, you would create an C<ev_io> watcher for that: |
627 | |
790 | |
628 | static void my_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
791 | static void my_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
629 | { |
792 | { |
630 | ev_io_stop (w); |
793 | ev_io_stop (w); |
631 | ev_unloop (loop, EVUNLOOP_ALL); |
794 | ev_unloop (loop, EVUNLOOP_ALL); |
632 | } |
795 | } |
633 | |
796 | |
634 | struct ev_loop *loop = ev_default_loop (0); |
797 | struct ev_loop *loop = ev_default_loop (0); |
635 | struct ev_io stdin_watcher; |
798 | struct ev_io stdin_watcher; |
636 | ev_init (&stdin_watcher, my_cb); |
799 | ev_init (&stdin_watcher, my_cb); |
637 | ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ); |
800 | ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ); |
638 | ev_io_start (loop, &stdin_watcher); |
801 | ev_io_start (loop, &stdin_watcher); |
639 | ev_loop (loop, 0); |
802 | ev_loop (loop, 0); |
640 | |
803 | |
641 | As you can see, you are responsible for allocating the memory for your |
804 | As you can see, you are responsible for allocating the memory for your |
642 | watcher structures (and it is usually a bad idea to do this on the stack, |
805 | watcher structures (and it is usually a bad idea to do this on the stack, |
643 | although this can sometimes be quite valid). |
806 | although this can sometimes be quite valid). |
644 | |
807 | |
645 | Each watcher structure must be initialised by a call to C<ev_init |
808 | Each watcher structure must be initialised by a call to C<ev_init |
646 | (watcher *, callback)>, which expects a callback to be provided. This |
809 | (watcher *, callback)>, which expects a callback to be provided. This |
647 | callback gets invoked each time the event occurs (or, in the case of io |
810 | callback gets invoked each time the event occurs (or, in the case of I/O |
648 | watchers, each time the event loop detects that the file descriptor given |
811 | watchers, each time the event loop detects that the file descriptor given |
649 | is readable and/or writable). |
812 | is readable and/or writable). |
650 | |
813 | |
651 | Each watcher type has its own C<< ev_<type>_set (watcher *, ...) >> macro |
814 | Each watcher type has its own C<< ev_<type>_set (watcher *, ...) >> macro |
652 | with arguments specific to this watcher type. There is also a macro |
815 | with arguments specific to this watcher type. There is also a macro |
… | |
… | |
722 | =item C<EV_FORK> |
885 | =item C<EV_FORK> |
723 | |
886 | |
724 | The event loop has been resumed in the child process after fork (see |
887 | The event loop has been resumed in the child process after fork (see |
725 | C<ev_fork>). |
888 | C<ev_fork>). |
726 | |
889 | |
|
|
890 | =item C<EV_ASYNC> |
|
|
891 | |
|
|
892 | The given async watcher has been asynchronously notified (see C<ev_async>). |
|
|
893 | |
727 | =item C<EV_ERROR> |
894 | =item C<EV_ERROR> |
728 | |
895 | |
729 | An unspecified error has occured, the watcher has been stopped. This might |
896 | An unspecified error has occurred, the watcher has been stopped. This might |
730 | happen because the watcher could not be properly started because libev |
897 | happen because the watcher could not be properly started because libev |
731 | ran out of memory, a file descriptor was found to be closed or any other |
898 | ran out of memory, a file descriptor was found to be closed or any other |
732 | problem. You best act on it by reporting the problem and somehow coping |
899 | problem. You best act on it by reporting the problem and somehow coping |
733 | with the watcher being stopped. |
900 | with the watcher being stopped. |
734 | |
901 | |
735 | Libev will usually signal a few "dummy" events together with an error, |
902 | Libev will usually signal a few "dummy" events together with an error, for |
736 | for example it might indicate that a fd is readable or writable, and if |
903 | example it might indicate that a fd is readable or writable, and if your |
737 | your callbacks is well-written it can just attempt the operation and cope |
904 | callbacks is well-written it can just attempt the operation and cope with |
738 | with the error from read() or write(). This will not work in multithreaded |
905 | the error from read() or write(). This will not work in multi-threaded |
739 | programs, though, so beware. |
906 | programs, though, as the fd could already be closed and reused for another |
|
|
907 | thing, so beware. |
740 | |
908 | |
741 | =back |
909 | =back |
742 | |
910 | |
743 | =head2 GENERIC WATCHER FUNCTIONS |
911 | =head2 GENERIC WATCHER FUNCTIONS |
744 | |
912 | |
… | |
… | |
760 | (or never started) and there are no pending events outstanding. |
928 | (or never started) and there are no pending events outstanding. |
761 | |
929 | |
762 | The callback is always of type C<void (*)(ev_loop *loop, ev_TYPE *watcher, |
930 | The callback is always of type C<void (*)(ev_loop *loop, ev_TYPE *watcher, |
763 | int revents)>. |
931 | int revents)>. |
764 | |
932 | |
|
|
933 | Example: Initialise an C<ev_io> watcher in two steps. |
|
|
934 | |
|
|
935 | ev_io w; |
|
|
936 | ev_init (&w, my_cb); |
|
|
937 | ev_io_set (&w, STDIN_FILENO, EV_READ); |
|
|
938 | |
765 | =item C<ev_TYPE_set> (ev_TYPE *, [args]) |
939 | =item C<ev_TYPE_set> (ev_TYPE *, [args]) |
766 | |
940 | |
767 | This macro initialises the type-specific parts of a watcher. You need to |
941 | This macro initialises the type-specific parts of a watcher. You need to |
768 | call C<ev_init> at least once before you call this macro, but you can |
942 | call C<ev_init> at least once before you call this macro, but you can |
769 | call C<ev_TYPE_set> any number of times. You must not, however, call this |
943 | call C<ev_TYPE_set> any number of times. You must not, however, call this |
… | |
… | |
771 | difference to the C<ev_init> macro). |
945 | difference to the C<ev_init> macro). |
772 | |
946 | |
773 | Although some watcher types do not have type-specific arguments |
947 | Although some watcher types do not have type-specific arguments |
774 | (e.g. C<ev_prepare>) you still need to call its C<set> macro. |
948 | (e.g. C<ev_prepare>) you still need to call its C<set> macro. |
775 | |
949 | |
|
|
950 | See C<ev_init>, above, for an example. |
|
|
951 | |
776 | =item C<ev_TYPE_init> (ev_TYPE *watcher, callback, [args]) |
952 | =item C<ev_TYPE_init> (ev_TYPE *watcher, callback, [args]) |
777 | |
953 | |
778 | This convinience macro rolls both C<ev_init> and C<ev_TYPE_set> macro |
954 | This convenience macro rolls both C<ev_init> and C<ev_TYPE_set> macro |
779 | calls into a single call. This is the most convinient method to initialise |
955 | calls into a single call. This is the most convenient method to initialise |
780 | a watcher. The same limitations apply, of course. |
956 | a watcher. The same limitations apply, of course. |
|
|
957 | |
|
|
958 | Example: Initialise and set an C<ev_io> watcher in one step. |
|
|
959 | |
|
|
960 | ev_io_init (&w, my_cb, STDIN_FILENO, EV_READ); |
781 | |
961 | |
782 | =item C<ev_TYPE_start> (loop *, ev_TYPE *watcher) |
962 | =item C<ev_TYPE_start> (loop *, ev_TYPE *watcher) |
783 | |
963 | |
784 | Starts (activates) the given watcher. Only active watchers will receive |
964 | Starts (activates) the given watcher. Only active watchers will receive |
785 | events. If the watcher is already active nothing will happen. |
965 | events. If the watcher is already active nothing will happen. |
786 | |
966 | |
|
|
967 | Example: Start the C<ev_io> watcher that is being abused as example in this |
|
|
968 | whole section. |
|
|
969 | |
|
|
970 | ev_io_start (EV_DEFAULT_UC, &w); |
|
|
971 | |
787 | =item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher) |
972 | =item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher) |
788 | |
973 | |
789 | Stops the given watcher again (if active) and clears the pending |
974 | Stops the given watcher if active, and clears the pending status (whether |
|
|
975 | the watcher was active or not). |
|
|
976 | |
790 | status. It is possible that stopped watchers are pending (for example, |
977 | It is possible that stopped watchers are pending - for example, |
791 | non-repeating timers are being stopped when they become pending), but |
978 | non-repeating timers are being stopped when they become pending - but |
792 | C<ev_TYPE_stop> ensures that the watcher is neither active nor pending. If |
979 | calling C<ev_TYPE_stop> ensures that the watcher is neither active nor |
793 | you want to free or reuse the memory used by the watcher it is therefore a |
980 | pending. If you want to free or reuse the memory used by the watcher it is |
794 | good idea to always call its C<ev_TYPE_stop> function. |
981 | therefore a good idea to always call its C<ev_TYPE_stop> function. |
795 | |
982 | |
796 | =item bool ev_is_active (ev_TYPE *watcher) |
983 | =item bool ev_is_active (ev_TYPE *watcher) |
797 | |
984 | |
798 | Returns a true value iff the watcher is active (i.e. it has been started |
985 | Returns a true value iff the watcher is active (i.e. it has been started |
799 | and not yet been stopped). As long as a watcher is active you must not modify |
986 | and not yet been stopped). As long as a watcher is active you must not modify |
… | |
… | |
847 | |
1034 | |
848 | =item ev_invoke (loop, ev_TYPE *watcher, int revents) |
1035 | =item ev_invoke (loop, ev_TYPE *watcher, int revents) |
849 | |
1036 | |
850 | Invoke the C<watcher> with the given C<loop> and C<revents>. Neither |
1037 | Invoke the C<watcher> with the given C<loop> and C<revents>. Neither |
851 | C<loop> nor C<revents> need to be valid as long as the watcher callback |
1038 | C<loop> nor C<revents> need to be valid as long as the watcher callback |
852 | can deal with that fact. |
1039 | can deal with that fact, as both are simply passed through to the |
|
|
1040 | callback. |
853 | |
1041 | |
854 | =item int ev_clear_pending (loop, ev_TYPE *watcher) |
1042 | =item int ev_clear_pending (loop, ev_TYPE *watcher) |
855 | |
1043 | |
856 | If the watcher is pending, this function returns clears its pending status |
1044 | If the watcher is pending, this function clears its pending status and |
857 | and returns its C<revents> bitset (as if its callback was invoked). If the |
1045 | returns its C<revents> bitset (as if its callback was invoked). If the |
858 | watcher isn't pending it does nothing and returns C<0>. |
1046 | watcher isn't pending it does nothing and returns C<0>. |
859 | |
1047 | |
|
|
1048 | Sometimes it can be useful to "poll" a watcher instead of waiting for its |
|
|
1049 | callback to be invoked, which can be accomplished with this function. |
|
|
1050 | |
860 | =back |
1051 | =back |
861 | |
1052 | |
862 | |
1053 | |
863 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
1054 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
864 | |
1055 | |
865 | Each watcher has, by default, a member C<void *data> that you can change |
1056 | Each watcher has, by default, a member C<void *data> that you can change |
866 | and read at any time, libev will completely ignore it. This can be used |
1057 | and read at any time: libev will completely ignore it. This can be used |
867 | to associate arbitrary data with your watcher. If you need more data and |
1058 | to associate arbitrary data with your watcher. If you need more data and |
868 | don't want to allocate memory and store a pointer to it in that data |
1059 | don't want to allocate memory and store a pointer to it in that data |
869 | member, you can also "subclass" the watcher type and provide your own |
1060 | member, you can also "subclass" the watcher type and provide your own |
870 | data: |
1061 | data: |
871 | |
1062 | |
872 | struct my_io |
1063 | struct my_io |
873 | { |
1064 | { |
874 | struct ev_io io; |
1065 | struct ev_io io; |
875 | int otherfd; |
1066 | int otherfd; |
876 | void *somedata; |
1067 | void *somedata; |
877 | struct whatever *mostinteresting; |
1068 | struct whatever *mostinteresting; |
878 | } |
1069 | }; |
|
|
1070 | |
|
|
1071 | ... |
|
|
1072 | struct my_io w; |
|
|
1073 | ev_io_init (&w.io, my_cb, fd, EV_READ); |
879 | |
1074 | |
880 | And since your callback will be called with a pointer to the watcher, you |
1075 | And since your callback will be called with a pointer to the watcher, you |
881 | can cast it back to your own type: |
1076 | can cast it back to your own type: |
882 | |
1077 | |
883 | static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) |
1078 | static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) |
884 | { |
1079 | { |
885 | struct my_io *w = (struct my_io *)w_; |
1080 | struct my_io *w = (struct my_io *)w_; |
886 | ... |
1081 | ... |
887 | } |
1082 | } |
888 | |
1083 | |
889 | More interesting and less C-conformant ways of casting your callback type |
1084 | More interesting and less C-conformant ways of casting your callback type |
890 | instead have been omitted. |
1085 | instead have been omitted. |
891 | |
1086 | |
892 | Another common scenario is having some data structure with multiple |
1087 | Another common scenario is to use some data structure with multiple |
893 | watchers: |
1088 | embedded watchers: |
894 | |
1089 | |
895 | struct my_biggy |
1090 | struct my_biggy |
896 | { |
1091 | { |
897 | int some_data; |
1092 | int some_data; |
898 | ev_timer t1; |
1093 | ev_timer t1; |
899 | ev_timer t2; |
1094 | ev_timer t2; |
900 | } |
1095 | } |
901 | |
1096 | |
902 | In this case getting the pointer to C<my_biggy> is a bit more complicated, |
1097 | In this case getting the pointer to C<my_biggy> is a bit more |
903 | you need to use C<offsetof>: |
1098 | complicated: Either you store the address of your C<my_biggy> struct |
|
|
1099 | in the C<data> member of the watcher (for woozies), or you need to use |
|
|
1100 | some pointer arithmetic using C<offsetof> inside your watchers (for real |
|
|
1101 | programmers): |
904 | |
1102 | |
905 | #include <stddef.h> |
1103 | #include <stddef.h> |
906 | |
1104 | |
907 | static void |
1105 | static void |
908 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
1106 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
909 | { |
1107 | { |
910 | struct my_biggy big = (struct my_biggy * |
1108 | struct my_biggy big = (struct my_biggy * |
911 | (((char *)w) - offsetof (struct my_biggy, t1)); |
1109 | (((char *)w) - offsetof (struct my_biggy, t1)); |
912 | } |
1110 | } |
913 | |
1111 | |
914 | static void |
1112 | static void |
915 | t2_cb (EV_P_ struct ev_timer *w, int revents) |
1113 | t2_cb (EV_P_ struct ev_timer *w, int revents) |
916 | { |
1114 | { |
917 | struct my_biggy big = (struct my_biggy * |
1115 | struct my_biggy big = (struct my_biggy * |
918 | (((char *)w) - offsetof (struct my_biggy, t2)); |
1116 | (((char *)w) - offsetof (struct my_biggy, t2)); |
919 | } |
1117 | } |
920 | |
1118 | |
921 | |
1119 | |
922 | =head1 WATCHER TYPES |
1120 | =head1 WATCHER TYPES |
923 | |
1121 | |
924 | This section describes each watcher in detail, but will not repeat |
1122 | This section describes each watcher in detail, but will not repeat |
… | |
… | |
948 | In general you can register as many read and/or write event watchers per |
1146 | In general you can register as many read and/or write event watchers per |
949 | fd as you want (as long as you don't confuse yourself). Setting all file |
1147 | fd as you want (as long as you don't confuse yourself). Setting all file |
950 | descriptors to non-blocking mode is also usually a good idea (but not |
1148 | descriptors to non-blocking mode is also usually a good idea (but not |
951 | required if you know what you are doing). |
1149 | required if you know what you are doing). |
952 | |
1150 | |
953 | You have to be careful with dup'ed file descriptors, though. Some backends |
1151 | If you cannot use non-blocking mode, then force the use of a |
954 | (the linux epoll backend is a notable example) cannot handle dup'ed file |
1152 | known-to-be-good backend (at the time of this writing, this includes only |
955 | descriptors correctly if you register interest in two or more fds pointing |
1153 | C<EVBACKEND_SELECT> and C<EVBACKEND_POLL>). |
956 | to the same underlying file/socket/etc. description (that is, they share |
|
|
957 | the same underlying "file open"). |
|
|
958 | |
|
|
959 | If you must do this, then force the use of a known-to-be-good backend |
|
|
960 | (at the time of this writing, this includes only C<EVBACKEND_SELECT> and |
|
|
961 | C<EVBACKEND_POLL>). |
|
|
962 | |
1154 | |
963 | Another thing you have to watch out for is that it is quite easy to |
1155 | Another thing you have to watch out for is that it is quite easy to |
964 | receive "spurious" readyness notifications, that is your callback might |
1156 | receive "spurious" readiness notifications, that is your callback might |
965 | be called with C<EV_READ> but a subsequent C<read>(2) will actually block |
1157 | be called with C<EV_READ> but a subsequent C<read>(2) will actually block |
966 | because there is no data. Not only are some backends known to create a |
1158 | because there is no data. Not only are some backends known to create a |
967 | lot of those (for example solaris ports), it is very easy to get into |
1159 | lot of those (for example Solaris ports), it is very easy to get into |
968 | this situation even with a relatively standard program structure. Thus |
1160 | this situation even with a relatively standard program structure. Thus |
969 | it is best to always use non-blocking I/O: An extra C<read>(2) returning |
1161 | it is best to always use non-blocking I/O: An extra C<read>(2) returning |
970 | C<EAGAIN> is far preferable to a program hanging until some data arrives. |
1162 | C<EAGAIN> is far preferable to a program hanging until some data arrives. |
971 | |
1163 | |
972 | If you cannot run the fd in non-blocking mode (for example you should not |
1164 | If you cannot run the fd in non-blocking mode (for example you should |
973 | play around with an Xlib connection), then you have to seperately re-test |
1165 | not play around with an Xlib connection), then you have to separately |
974 | whether a file descriptor is really ready with a known-to-be good interface |
1166 | re-test whether a file descriptor is really ready with a known-to-be good |
975 | such as poll (fortunately in our Xlib example, Xlib already does this on |
1167 | interface such as poll (fortunately in our Xlib example, Xlib already |
976 | its own, so its quite safe to use). |
1168 | does this on its own, so its quite safe to use). Some people additionally |
|
|
1169 | use C<SIGALRM> and an interval timer, just to be sure you won't block |
|
|
1170 | indefinitely. |
|
|
1171 | |
|
|
1172 | But really, best use non-blocking mode. |
977 | |
1173 | |
978 | =head3 The special problem of disappearing file descriptors |
1174 | =head3 The special problem of disappearing file descriptors |
979 | |
1175 | |
980 | Some backends (e.g. kqueue, epoll) need to be told about closing a file |
1176 | Some backends (e.g. kqueue, epoll) need to be told about closing a file |
981 | descriptor (either by calling C<close> explicitly or by any other means, |
1177 | descriptor (either due to calling C<close> explicitly or any other means, |
982 | such as C<dup>). The reason is that you register interest in some file |
1178 | such as C<dup2>). The reason is that you register interest in some file |
983 | descriptor, but when it goes away, the operating system will silently drop |
1179 | descriptor, but when it goes away, the operating system will silently drop |
984 | this interest. If another file descriptor with the same number then is |
1180 | this interest. If another file descriptor with the same number then is |
985 | registered with libev, there is no efficient way to see that this is, in |
1181 | registered with libev, there is no efficient way to see that this is, in |
986 | fact, a different file descriptor. |
1182 | fact, a different file descriptor. |
987 | |
1183 | |
… | |
… | |
997 | optimisations to libev. |
1193 | optimisations to libev. |
998 | |
1194 | |
999 | =head3 The special problem of dup'ed file descriptors |
1195 | =head3 The special problem of dup'ed file descriptors |
1000 | |
1196 | |
1001 | Some backends (e.g. epoll), cannot register events for file descriptors, |
1197 | Some backends (e.g. epoll), cannot register events for file descriptors, |
1002 | but only events for the underlying file descriptions. That menas when you |
1198 | but only events for the underlying file descriptions. That means when you |
1003 | have C<dup ()>'ed file descriptors and register events for them, only one |
1199 | have C<dup ()>'ed file descriptors or weirder constellations, and register |
1004 | file descriptor might actually receive events. |
1200 | events for them, only one file descriptor might actually receive events. |
1005 | |
1201 | |
1006 | There is no workaorund possible except not registering events |
1202 | There is no workaround possible except not registering events |
1007 | for potentially C<dup ()>'ed file descriptors or to resort to |
1203 | for potentially C<dup ()>'ed file descriptors, or to resort to |
1008 | C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>. |
1204 | C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>. |
1009 | |
1205 | |
1010 | =head3 The special problem of fork |
1206 | =head3 The special problem of fork |
1011 | |
1207 | |
1012 | Some backends (epoll, kqueue) do not support C<fork ()> at all or exhibit |
1208 | Some backends (epoll, kqueue) do not support C<fork ()> at all or exhibit |
… | |
… | |
1016 | To support fork in your programs, you either have to call |
1212 | To support fork in your programs, you either have to call |
1017 | C<ev_default_fork ()> or C<ev_loop_fork ()> after a fork in the child, |
1213 | C<ev_default_fork ()> or C<ev_loop_fork ()> after a fork in the child, |
1018 | enable C<EVFLAG_FORKCHECK>, or resort to C<EVBACKEND_SELECT> or |
1214 | enable C<EVFLAG_FORKCHECK>, or resort to C<EVBACKEND_SELECT> or |
1019 | C<EVBACKEND_POLL>. |
1215 | C<EVBACKEND_POLL>. |
1020 | |
1216 | |
|
|
1217 | =head3 The special problem of SIGPIPE |
|
|
1218 | |
|
|
1219 | While not really specific to libev, it is easy to forget about C<SIGPIPE>: |
|
|
1220 | when writing to a pipe whose other end has been closed, your program gets |
|
|
1221 | sent a SIGPIPE, which, by default, aborts your program. For most programs |
|
|
1222 | this is sensible behaviour, for daemons, this is usually undesirable. |
|
|
1223 | |
|
|
1224 | So when you encounter spurious, unexplained daemon exits, make sure you |
|
|
1225 | ignore SIGPIPE (and maybe make sure you log the exit status of your daemon |
|
|
1226 | somewhere, as that would have given you a big clue). |
|
|
1227 | |
1021 | |
1228 | |
1022 | =head3 Watcher-Specific Functions |
1229 | =head3 Watcher-Specific Functions |
1023 | |
1230 | |
1024 | =over 4 |
1231 | =over 4 |
1025 | |
1232 | |
1026 | =item ev_io_init (ev_io *, callback, int fd, int events) |
1233 | =item ev_io_init (ev_io *, callback, int fd, int events) |
1027 | |
1234 | |
1028 | =item ev_io_set (ev_io *, int fd, int events) |
1235 | =item ev_io_set (ev_io *, int fd, int events) |
1029 | |
1236 | |
1030 | Configures an C<ev_io> watcher. The C<fd> is the file descriptor to |
1237 | Configures an C<ev_io> watcher. The C<fd> is the file descriptor to |
1031 | rceeive events for and events is either C<EV_READ>, C<EV_WRITE> or |
1238 | receive events for and C<events> is either C<EV_READ>, C<EV_WRITE> or |
1032 | C<EV_READ | EV_WRITE> to receive the given events. |
1239 | C<EV_READ | EV_WRITE>, to express the desire to receive the given events. |
1033 | |
1240 | |
1034 | =item int fd [read-only] |
1241 | =item int fd [read-only] |
1035 | |
1242 | |
1036 | The file descriptor being watched. |
1243 | The file descriptor being watched. |
1037 | |
1244 | |
1038 | =item int events [read-only] |
1245 | =item int events [read-only] |
1039 | |
1246 | |
1040 | The events being watched. |
1247 | The events being watched. |
1041 | |
1248 | |
1042 | =back |
1249 | =back |
|
|
1250 | |
|
|
1251 | =head3 Examples |
1043 | |
1252 | |
1044 | Example: Call C<stdin_readable_cb> when STDIN_FILENO has become, well |
1253 | Example: Call C<stdin_readable_cb> when STDIN_FILENO has become, well |
1045 | readable, but only once. Since it is likely line-buffered, you could |
1254 | readable, but only once. Since it is likely line-buffered, you could |
1046 | attempt to read a whole line in the callback. |
1255 | attempt to read a whole line in the callback. |
1047 | |
1256 | |
1048 | static void |
1257 | static void |
1049 | stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
1258 | stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
1050 | { |
1259 | { |
1051 | ev_io_stop (loop, w); |
1260 | ev_io_stop (loop, w); |
1052 | .. read from stdin here (or from w->fd) and haqndle any I/O errors |
1261 | .. read from stdin here (or from w->fd) and handle any I/O errors |
1053 | } |
1262 | } |
1054 | |
1263 | |
1055 | ... |
1264 | ... |
1056 | struct ev_loop *loop = ev_default_init (0); |
1265 | struct ev_loop *loop = ev_default_init (0); |
1057 | struct ev_io stdin_readable; |
1266 | struct ev_io stdin_readable; |
1058 | ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ); |
1267 | ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ); |
1059 | ev_io_start (loop, &stdin_readable); |
1268 | ev_io_start (loop, &stdin_readable); |
1060 | ev_loop (loop, 0); |
1269 | ev_loop (loop, 0); |
1061 | |
1270 | |
1062 | |
1271 | |
1063 | =head2 C<ev_timer> - relative and optionally repeating timeouts |
1272 | =head2 C<ev_timer> - relative and optionally repeating timeouts |
1064 | |
1273 | |
1065 | Timer watchers are simple relative timers that generate an event after a |
1274 | Timer watchers are simple relative timers that generate an event after a |
1066 | given time, and optionally repeating in regular intervals after that. |
1275 | given time, and optionally repeating in regular intervals after that. |
1067 | |
1276 | |
1068 | The timers are based on real time, that is, if you register an event that |
1277 | The timers are based on real time, that is, if you register an event that |
1069 | times out after an hour and you reset your system clock to last years |
1278 | times out after an hour and you reset your system clock to January last |
1070 | time, it will still time out after (roughly) and hour. "Roughly" because |
1279 | year, it will still time out after (roughly) one hour. "Roughly" because |
1071 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1280 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1072 | monotonic clock option helps a lot here). |
1281 | monotonic clock option helps a lot here). |
|
|
1282 | |
|
|
1283 | The callback is guaranteed to be invoked only I<after> its timeout has |
|
|
1284 | passed, but if multiple timers become ready during the same loop iteration |
|
|
1285 | then order of execution is undefined. |
|
|
1286 | |
|
|
1287 | =head3 The special problem of time updates |
|
|
1288 | |
|
|
1289 | Establishing the current time is a costly operation (it usually takes at |
|
|
1290 | least two system calls): EV therefore updates its idea of the current |
|
|
1291 | time only before and after C<ev_loop> collects new events, which causes a |
|
|
1292 | growing difference between C<ev_now ()> and C<ev_time ()> when handling |
|
|
1293 | lots of events in one iteration. |
1073 | |
1294 | |
1074 | The relative timeouts are calculated relative to the C<ev_now ()> |
1295 | The relative timeouts are calculated relative to the C<ev_now ()> |
1075 | time. This is usually the right thing as this timestamp refers to the time |
1296 | time. This is usually the right thing as this timestamp refers to the time |
1076 | of the event triggering whatever timeout you are modifying/starting. If |
1297 | of the event triggering whatever timeout you are modifying/starting. If |
1077 | you suspect event processing to be delayed and you I<need> to base the timeout |
1298 | you suspect event processing to be delayed and you I<need> to base the |
1078 | on the current time, use something like this to adjust for this: |
1299 | timeout on the current time, use something like this to adjust for this: |
1079 | |
1300 | |
1080 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1301 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1081 | |
1302 | |
1082 | The callback is guarenteed to be invoked only when its timeout has passed, |
1303 | If the event loop is suspended for a long time, you can also force an |
1083 | but if multiple timers become ready during the same loop iteration then |
1304 | update of the time returned by C<ev_now ()> by calling C<ev_now_update |
1084 | order of execution is undefined. |
1305 | ()>. |
1085 | |
1306 | |
1086 | =head3 Watcher-Specific Functions and Data Members |
1307 | =head3 Watcher-Specific Functions and Data Members |
1087 | |
1308 | |
1088 | =over 4 |
1309 | =over 4 |
1089 | |
1310 | |
1090 | =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) |
1311 | =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) |
1091 | |
1312 | |
1092 | =item ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat) |
1313 | =item ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat) |
1093 | |
1314 | |
1094 | Configure the timer to trigger after C<after> seconds. If C<repeat> is |
1315 | Configure the timer to trigger after C<after> seconds. If C<repeat> |
1095 | C<0.>, then it will automatically be stopped. If it is positive, then the |
1316 | is C<0.>, then it will automatically be stopped once the timeout is |
1096 | timer will automatically be configured to trigger again C<repeat> seconds |
1317 | reached. If it is positive, then the timer will automatically be |
1097 | later, again, and again, until stopped manually. |
1318 | configured to trigger again C<repeat> seconds later, again, and again, |
|
|
1319 | until stopped manually. |
1098 | |
1320 | |
1099 | The timer itself will do a best-effort at avoiding drift, that is, if you |
1321 | The timer itself will do a best-effort at avoiding drift, that is, if |
1100 | configure a timer to trigger every 10 seconds, then it will trigger at |
1322 | you configure a timer to trigger every 10 seconds, then it will normally |
1101 | exactly 10 second intervals. If, however, your program cannot keep up with |
1323 | trigger at exactly 10 second intervals. If, however, your program cannot |
1102 | the timer (because it takes longer than those 10 seconds to do stuff) the |
1324 | keep up with the timer (because it takes longer than those 10 seconds to |
1103 | timer will not fire more than once per event loop iteration. |
1325 | do stuff) the timer will not fire more than once per event loop iteration. |
1104 | |
1326 | |
1105 | =item ev_timer_again (loop) |
1327 | =item ev_timer_again (loop, ev_timer *) |
1106 | |
1328 | |
1107 | This will act as if the timer timed out and restart it again if it is |
1329 | This will act as if the timer timed out and restart it again if it is |
1108 | repeating. The exact semantics are: |
1330 | repeating. The exact semantics are: |
1109 | |
1331 | |
1110 | If the timer is pending, its pending status is cleared. |
1332 | If the timer is pending, its pending status is cleared. |
1111 | |
1333 | |
1112 | If the timer is started but nonrepeating, stop it (as if it timed out). |
1334 | If the timer is started but non-repeating, stop it (as if it timed out). |
1113 | |
1335 | |
1114 | If the timer is repeating, either start it if necessary (with the |
1336 | If the timer is repeating, either start it if necessary (with the |
1115 | C<repeat> value), or reset the running timer to the C<repeat> value. |
1337 | C<repeat> value), or reset the running timer to the C<repeat> value. |
1116 | |
1338 | |
1117 | This sounds a bit complicated, but here is a useful and typical |
1339 | This sounds a bit complicated, but here is a useful and typical |
1118 | example: Imagine you have a tcp connection and you want a so-called idle |
1340 | example: Imagine you have a TCP connection and you want a so-called idle |
1119 | timeout, that is, you want to be called when there have been, say, 60 |
1341 | timeout, that is, you want to be called when there have been, say, 60 |
1120 | seconds of inactivity on the socket. The easiest way to do this is to |
1342 | seconds of inactivity on the socket. The easiest way to do this is to |
1121 | configure an C<ev_timer> with a C<repeat> value of C<60> and then call |
1343 | configure an C<ev_timer> with a C<repeat> value of C<60> and then call |
1122 | C<ev_timer_again> each time you successfully read or write some data. If |
1344 | C<ev_timer_again> each time you successfully read or write some data. If |
1123 | you go into an idle state where you do not expect data to travel on the |
1345 | you go into an idle state where you do not expect data to travel on the |
… | |
… | |
1137 | ev_timer_again (loop, timer); |
1359 | ev_timer_again (loop, timer); |
1138 | |
1360 | |
1139 | This is more slightly efficient then stopping/starting the timer each time |
1361 | This is more slightly efficient then stopping/starting the timer each time |
1140 | you want to modify its timeout value. |
1362 | you want to modify its timeout value. |
1141 | |
1363 | |
|
|
1364 | Note, however, that it is often even more efficient to remember the |
|
|
1365 | time of the last activity and let the timer time-out naturally. In the |
|
|
1366 | callback, you then check whether the time-out is real, or, if there was |
|
|
1367 | some activity, you reschedule the watcher to time-out in "last_activity + |
|
|
1368 | timeout - ev_now ()" seconds. |
|
|
1369 | |
1142 | =item ev_tstamp repeat [read-write] |
1370 | =item ev_tstamp repeat [read-write] |
1143 | |
1371 | |
1144 | The current C<repeat> value. Will be used each time the watcher times out |
1372 | The current C<repeat> value. Will be used each time the watcher times out |
1145 | or C<ev_timer_again> is called and determines the next timeout (if any), |
1373 | or C<ev_timer_again> is called, and determines the next timeout (if any), |
1146 | which is also when any modifications are taken into account. |
1374 | which is also when any modifications are taken into account. |
1147 | |
1375 | |
1148 | =back |
1376 | =back |
1149 | |
1377 | |
|
|
1378 | =head3 Examples |
|
|
1379 | |
1150 | Example: Create a timer that fires after 60 seconds. |
1380 | Example: Create a timer that fires after 60 seconds. |
1151 | |
1381 | |
1152 | static void |
1382 | static void |
1153 | one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
1383 | one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
1154 | { |
1384 | { |
1155 | .. one minute over, w is actually stopped right here |
1385 | .. one minute over, w is actually stopped right here |
1156 | } |
1386 | } |
1157 | |
1387 | |
1158 | struct ev_timer mytimer; |
1388 | struct ev_timer mytimer; |
1159 | ev_timer_init (&mytimer, one_minute_cb, 60., 0.); |
1389 | ev_timer_init (&mytimer, one_minute_cb, 60., 0.); |
1160 | ev_timer_start (loop, &mytimer); |
1390 | ev_timer_start (loop, &mytimer); |
1161 | |
1391 | |
1162 | Example: Create a timeout timer that times out after 10 seconds of |
1392 | Example: Create a timeout timer that times out after 10 seconds of |
1163 | inactivity. |
1393 | inactivity. |
1164 | |
1394 | |
1165 | static void |
1395 | static void |
1166 | timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
1396 | timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
1167 | { |
1397 | { |
1168 | .. ten seconds without any activity |
1398 | .. ten seconds without any activity |
1169 | } |
1399 | } |
1170 | |
1400 | |
1171 | struct ev_timer mytimer; |
1401 | struct ev_timer mytimer; |
1172 | ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */ |
1402 | ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */ |
1173 | ev_timer_again (&mytimer); /* start timer */ |
1403 | ev_timer_again (&mytimer); /* start timer */ |
1174 | ev_loop (loop, 0); |
1404 | ev_loop (loop, 0); |
1175 | |
1405 | |
1176 | // and in some piece of code that gets executed on any "activity": |
1406 | // and in some piece of code that gets executed on any "activity": |
1177 | // reset the timeout to start ticking again at 10 seconds |
1407 | // reset the timeout to start ticking again at 10 seconds |
1178 | ev_timer_again (&mytimer); |
1408 | ev_timer_again (&mytimer); |
1179 | |
1409 | |
1180 | |
1410 | |
1181 | =head2 C<ev_periodic> - to cron or not to cron? |
1411 | =head2 C<ev_periodic> - to cron or not to cron? |
1182 | |
1412 | |
1183 | Periodic watchers are also timers of a kind, but they are very versatile |
1413 | Periodic watchers are also timers of a kind, but they are very versatile |
1184 | (and unfortunately a bit complex). |
1414 | (and unfortunately a bit complex). |
1185 | |
1415 | |
1186 | Unlike C<ev_timer>'s, they are not based on real time (or relative time) |
1416 | Unlike C<ev_timer>'s, they are not based on real time (or relative time) |
1187 | but on wallclock time (absolute time). You can tell a periodic watcher |
1417 | but on wall clock time (absolute time). You can tell a periodic watcher |
1188 | to trigger "at" some specific point in time. For example, if you tell a |
1418 | to trigger after some specific point in time. For example, if you tell a |
1189 | periodic watcher to trigger in 10 seconds (by specifiying e.g. C<ev_now () |
1419 | periodic watcher to trigger in 10 seconds (by specifying e.g. C<ev_now () |
1190 | + 10.>) and then reset your system clock to the last year, then it will |
1420 | + 10.>, that is, an absolute time not a delay) and then reset your system |
|
|
1421 | clock to January of the previous year, then it will take more than year |
1191 | take a year to trigger the event (unlike an C<ev_timer>, which would trigger |
1422 | to trigger the event (unlike an C<ev_timer>, which would still trigger |
1192 | roughly 10 seconds later). |
1423 | roughly 10 seconds later as it uses a relative timeout). |
1193 | |
1424 | |
1194 | They can also be used to implement vastly more complex timers, such as |
1425 | C<ev_periodic>s can also be used to implement vastly more complex timers, |
1195 | triggering an event on each midnight, local time or other, complicated, |
1426 | such as triggering an event on each "midnight, local time", or other |
1196 | rules. |
1427 | complicated rules. |
1197 | |
1428 | |
1198 | As with timers, the callback is guarenteed to be invoked only when the |
1429 | As with timers, the callback is guaranteed to be invoked only when the |
1199 | time (C<at>) has been passed, but if multiple periodic timers become ready |
1430 | time (C<at>) has passed, but if multiple periodic timers become ready |
1200 | during the same loop iteration then order of execution is undefined. |
1431 | during the same loop iteration, then order of execution is undefined. |
1201 | |
1432 | |
1202 | =head3 Watcher-Specific Functions and Data Members |
1433 | =head3 Watcher-Specific Functions and Data Members |
1203 | |
1434 | |
1204 | =over 4 |
1435 | =over 4 |
1205 | |
1436 | |
1206 | =item ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb) |
1437 | =item ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb) |
1207 | |
1438 | |
1208 | =item ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb) |
1439 | =item ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb) |
1209 | |
1440 | |
1210 | Lots of arguments, lets sort it out... There are basically three modes of |
1441 | Lots of arguments, lets sort it out... There are basically three modes of |
1211 | operation, and we will explain them from simplest to complex: |
1442 | operation, and we will explain them from simplest to most complex: |
1212 | |
1443 | |
1213 | =over 4 |
1444 | =over 4 |
1214 | |
1445 | |
1215 | =item * absolute timer (at = time, interval = reschedule_cb = 0) |
1446 | =item * absolute timer (at = time, interval = reschedule_cb = 0) |
1216 | |
1447 | |
1217 | In this configuration the watcher triggers an event at the wallclock time |
1448 | In this configuration the watcher triggers an event after the wall clock |
1218 | C<at> and doesn't repeat. It will not adjust when a time jump occurs, |
1449 | time C<at> has passed. It will not repeat and will not adjust when a time |
1219 | that is, if it is to be run at January 1st 2011 then it will run when the |
1450 | jump occurs, that is, if it is to be run at January 1st 2011 then it will |
1220 | system time reaches or surpasses this time. |
1451 | only run when the system clock reaches or surpasses this time. |
1221 | |
1452 | |
1222 | =item * non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1453 | =item * repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1223 | |
1454 | |
1224 | In this mode the watcher will always be scheduled to time out at the next |
1455 | In this mode the watcher will always be scheduled to time out at the next |
1225 | C<at + N * interval> time (for some integer N, which can also be negative) |
1456 | C<at + N * interval> time (for some integer N, which can also be negative) |
1226 | and then repeat, regardless of any time jumps. |
1457 | and then repeat, regardless of any time jumps. |
1227 | |
1458 | |
1228 | This can be used to create timers that do not drift with respect to system |
1459 | This can be used to create timers that do not drift with respect to the |
1229 | time: |
1460 | system clock, for example, here is a C<ev_periodic> that triggers each |
|
|
1461 | hour, on the hour: |
1230 | |
1462 | |
1231 | ev_periodic_set (&periodic, 0., 3600., 0); |
1463 | ev_periodic_set (&periodic, 0., 3600., 0); |
1232 | |
1464 | |
1233 | This doesn't mean there will always be 3600 seconds in between triggers, |
1465 | This doesn't mean there will always be 3600 seconds in between triggers, |
1234 | but only that the the callback will be called when the system time shows a |
1466 | but only that the callback will be called when the system time shows a |
1235 | full hour (UTC), or more correctly, when the system time is evenly divisible |
1467 | full hour (UTC), or more correctly, when the system time is evenly divisible |
1236 | by 3600. |
1468 | by 3600. |
1237 | |
1469 | |
1238 | Another way to think about it (for the mathematically inclined) is that |
1470 | Another way to think about it (for the mathematically inclined) is that |
1239 | C<ev_periodic> will try to run the callback in this mode at the next possible |
1471 | C<ev_periodic> will try to run the callback in this mode at the next possible |
1240 | time where C<time = at (mod interval)>, regardless of any time jumps. |
1472 | time where C<time = at (mod interval)>, regardless of any time jumps. |
1241 | |
1473 | |
1242 | For numerical stability it is preferable that the C<at> value is near |
1474 | For numerical stability it is preferable that the C<at> value is near |
1243 | C<ev_now ()> (the current time), but there is no range requirement for |
1475 | C<ev_now ()> (the current time), but there is no range requirement for |
1244 | this value. |
1476 | this value, and in fact is often specified as zero. |
|
|
1477 | |
|
|
1478 | Note also that there is an upper limit to how often a timer can fire (CPU |
|
|
1479 | speed for example), so if C<interval> is very small then timing stability |
|
|
1480 | will of course deteriorate. Libev itself tries to be exact to be about one |
|
|
1481 | millisecond (if the OS supports it and the machine is fast enough). |
1245 | |
1482 | |
1246 | =item * manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1483 | =item * manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1247 | |
1484 | |
1248 | In this mode the values for C<interval> and C<at> are both being |
1485 | In this mode the values for C<interval> and C<at> are both being |
1249 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1486 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1250 | reschedule callback will be called with the watcher as first, and the |
1487 | reschedule callback will be called with the watcher as first, and the |
1251 | current time as second argument. |
1488 | current time as second argument. |
1252 | |
1489 | |
1253 | NOTE: I<This callback MUST NOT stop or destroy any periodic watcher, |
1490 | NOTE: I<This callback MUST NOT stop or destroy any periodic watcher, |
1254 | ever, or make any event loop modifications>. If you need to stop it, |
1491 | ever, or make ANY event loop modifications whatsoever>. |
1255 | return C<now + 1e30> (or so, fudge fudge) and stop it afterwards (e.g. by |
|
|
1256 | starting an C<ev_prepare> watcher, which is legal). |
|
|
1257 | |
1492 | |
|
|
1493 | If you need to stop it, return C<now + 1e30> (or so, fudge fudge) and stop |
|
|
1494 | it afterwards (e.g. by starting an C<ev_prepare> watcher, which is the |
|
|
1495 | only event loop modification you are allowed to do). |
|
|
1496 | |
1258 | Its prototype is C<ev_tstamp (*reschedule_cb)(struct ev_periodic *w, |
1497 | The callback prototype is C<ev_tstamp (*reschedule_cb)(struct ev_periodic |
1259 | ev_tstamp now)>, e.g.: |
1498 | *w, ev_tstamp now)>, e.g.: |
1260 | |
1499 | |
1261 | static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
1500 | static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
1262 | { |
1501 | { |
1263 | return now + 60.; |
1502 | return now + 60.; |
1264 | } |
1503 | } |
… | |
… | |
1266 | It must return the next time to trigger, based on the passed time value |
1505 | It must return the next time to trigger, based on the passed time value |
1267 | (that is, the lowest time value larger than to the second argument). It |
1506 | (that is, the lowest time value larger than to the second argument). It |
1268 | will usually be called just before the callback will be triggered, but |
1507 | will usually be called just before the callback will be triggered, but |
1269 | might be called at other times, too. |
1508 | might be called at other times, too. |
1270 | |
1509 | |
1271 | NOTE: I<< This callback must always return a time that is later than the |
1510 | NOTE: I<< This callback must always return a time that is higher than or |
1272 | passed C<now> value >>. Not even C<now> itself will do, it I<must> be larger. |
1511 | equal to the passed C<now> value >>. |
1273 | |
1512 | |
1274 | This can be used to create very complex timers, such as a timer that |
1513 | This can be used to create very complex timers, such as a timer that |
1275 | triggers on each midnight, local time. To do this, you would calculate the |
1514 | triggers on "next midnight, local time". To do this, you would calculate the |
1276 | next midnight after C<now> and return the timestamp value for this. How |
1515 | next midnight after C<now> and return the timestamp value for this. How |
1277 | you do this is, again, up to you (but it is not trivial, which is the main |
1516 | you do this is, again, up to you (but it is not trivial, which is the main |
1278 | reason I omitted it as an example). |
1517 | reason I omitted it as an example). |
1279 | |
1518 | |
1280 | =back |
1519 | =back |
… | |
… | |
1284 | Simply stops and restarts the periodic watcher again. This is only useful |
1523 | Simply stops and restarts the periodic watcher again. This is only useful |
1285 | when you changed some parameters or the reschedule callback would return |
1524 | when you changed some parameters or the reschedule callback would return |
1286 | a different time than the last time it was called (e.g. in a crond like |
1525 | a different time than the last time it was called (e.g. in a crond like |
1287 | program when the crontabs have changed). |
1526 | program when the crontabs have changed). |
1288 | |
1527 | |
|
|
1528 | =item ev_tstamp ev_periodic_at (ev_periodic *) |
|
|
1529 | |
|
|
1530 | When active, returns the absolute time that the watcher is supposed to |
|
|
1531 | trigger next. |
|
|
1532 | |
1289 | =item ev_tstamp offset [read-write] |
1533 | =item ev_tstamp offset [read-write] |
1290 | |
1534 | |
1291 | When repeating, this contains the offset value, otherwise this is the |
1535 | When repeating, this contains the offset value, otherwise this is the |
1292 | absolute point in time (the C<at> value passed to C<ev_periodic_set>). |
1536 | absolute point in time (the C<at> value passed to C<ev_periodic_set>). |
1293 | |
1537 | |
… | |
… | |
1304 | |
1548 | |
1305 | The current reschedule callback, or C<0>, if this functionality is |
1549 | The current reschedule callback, or C<0>, if this functionality is |
1306 | switched off. Can be changed any time, but changes only take effect when |
1550 | switched off. Can be changed any time, but changes only take effect when |
1307 | the periodic timer fires or C<ev_periodic_again> is being called. |
1551 | the periodic timer fires or C<ev_periodic_again> is being called. |
1308 | |
1552 | |
1309 | =item ev_tstamp at [read-only] |
|
|
1310 | |
|
|
1311 | When active, contains the absolute time that the watcher is supposed to |
|
|
1312 | trigger next. |
|
|
1313 | |
|
|
1314 | =back |
1553 | =back |
1315 | |
1554 | |
|
|
1555 | =head3 Examples |
|
|
1556 | |
1316 | Example: Call a callback every hour, or, more precisely, whenever the |
1557 | Example: Call a callback every hour, or, more precisely, whenever the |
1317 | system clock is divisible by 3600. The callback invocation times have |
1558 | system time is divisible by 3600. The callback invocation times have |
1318 | potentially a lot of jittering, but good long-term stability. |
1559 | potentially a lot of jitter, but good long-term stability. |
1319 | |
1560 | |
1320 | static void |
1561 | static void |
1321 | clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
1562 | clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
1322 | { |
1563 | { |
1323 | ... its now a full hour (UTC, or TAI or whatever your clock follows) |
1564 | ... its now a full hour (UTC, or TAI or whatever your clock follows) |
1324 | } |
1565 | } |
1325 | |
1566 | |
1326 | struct ev_periodic hourly_tick; |
1567 | struct ev_periodic hourly_tick; |
1327 | ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0); |
1568 | ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0); |
1328 | ev_periodic_start (loop, &hourly_tick); |
1569 | ev_periodic_start (loop, &hourly_tick); |
1329 | |
1570 | |
1330 | Example: The same as above, but use a reschedule callback to do it: |
1571 | Example: The same as above, but use a reschedule callback to do it: |
1331 | |
1572 | |
1332 | #include <math.h> |
1573 | #include <math.h> |
1333 | |
1574 | |
1334 | static ev_tstamp |
1575 | static ev_tstamp |
1335 | my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) |
1576 | my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) |
1336 | { |
1577 | { |
1337 | return fmod (now, 3600.) + 3600.; |
1578 | return now + (3600. - fmod (now, 3600.)); |
1338 | } |
1579 | } |
1339 | |
1580 | |
1340 | ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); |
1581 | ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); |
1341 | |
1582 | |
1342 | Example: Call a callback every hour, starting now: |
1583 | Example: Call a callback every hour, starting now: |
1343 | |
1584 | |
1344 | struct ev_periodic hourly_tick; |
1585 | struct ev_periodic hourly_tick; |
1345 | ev_periodic_init (&hourly_tick, clock_cb, |
1586 | ev_periodic_init (&hourly_tick, clock_cb, |
1346 | fmod (ev_now (loop), 3600.), 3600., 0); |
1587 | fmod (ev_now (loop), 3600.), 3600., 0); |
1347 | ev_periodic_start (loop, &hourly_tick); |
1588 | ev_periodic_start (loop, &hourly_tick); |
1348 | |
1589 | |
1349 | |
1590 | |
1350 | =head2 C<ev_signal> - signal me when a signal gets signalled! |
1591 | =head2 C<ev_signal> - signal me when a signal gets signalled! |
1351 | |
1592 | |
1352 | Signal watchers will trigger an event when the process receives a specific |
1593 | Signal watchers will trigger an event when the process receives a specific |
1353 | signal one or more times. Even though signals are very asynchronous, libev |
1594 | signal one or more times. Even though signals are very asynchronous, libev |
1354 | will try it's best to deliver signals synchronously, i.e. as part of the |
1595 | will try it's best to deliver signals synchronously, i.e. as part of the |
1355 | normal event processing, like any other event. |
1596 | normal event processing, like any other event. |
1356 | |
1597 | |
|
|
1598 | If you want signals asynchronously, just use C<sigaction> as you would |
|
|
1599 | do without libev and forget about sharing the signal. You can even use |
|
|
1600 | C<ev_async> from a signal handler to synchronously wake up an event loop. |
|
|
1601 | |
1357 | You can configure as many watchers as you like per signal. Only when the |
1602 | You can configure as many watchers as you like per signal. Only when the |
1358 | first watcher gets started will libev actually register a signal watcher |
1603 | first watcher gets started will libev actually register a signal handler |
1359 | with the kernel (thus it coexists with your own signal handlers as long |
1604 | with the kernel (thus it coexists with your own signal handlers as long as |
1360 | as you don't register any with libev). Similarly, when the last signal |
1605 | you don't register any with libev for the same signal). Similarly, when |
1361 | watcher for a signal is stopped libev will reset the signal handler to |
1606 | the last signal watcher for a signal is stopped, libev will reset the |
1362 | SIG_DFL (regardless of what it was set to before). |
1607 | signal handler to SIG_DFL (regardless of what it was set to before). |
|
|
1608 | |
|
|
1609 | If possible and supported, libev will install its handlers with |
|
|
1610 | C<SA_RESTART> behaviour enabled, so system calls should not be unduly |
|
|
1611 | interrupted. If you have a problem with system calls getting interrupted by |
|
|
1612 | signals you can block all signals in an C<ev_check> watcher and unblock |
|
|
1613 | them in an C<ev_prepare> watcher. |
1363 | |
1614 | |
1364 | =head3 Watcher-Specific Functions and Data Members |
1615 | =head3 Watcher-Specific Functions and Data Members |
1365 | |
1616 | |
1366 | =over 4 |
1617 | =over 4 |
1367 | |
1618 | |
… | |
… | |
1376 | |
1627 | |
1377 | The signal the watcher watches out for. |
1628 | The signal the watcher watches out for. |
1378 | |
1629 | |
1379 | =back |
1630 | =back |
1380 | |
1631 | |
|
|
1632 | =head3 Examples |
|
|
1633 | |
|
|
1634 | Example: Try to exit cleanly on SIGINT. |
|
|
1635 | |
|
|
1636 | static void |
|
|
1637 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
|
|
1638 | { |
|
|
1639 | ev_unloop (loop, EVUNLOOP_ALL); |
|
|
1640 | } |
|
|
1641 | |
|
|
1642 | struct ev_signal signal_watcher; |
|
|
1643 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
|
|
1644 | ev_signal_start (loop, &signal_watcher); |
|
|
1645 | |
1381 | |
1646 | |
1382 | =head2 C<ev_child> - watch out for process status changes |
1647 | =head2 C<ev_child> - watch out for process status changes |
1383 | |
1648 | |
1384 | Child watchers trigger when your process receives a SIGCHLD in response to |
1649 | Child watchers trigger when your process receives a SIGCHLD in response to |
1385 | some child status changes (most typically when a child of yours dies). |
1650 | some child status changes (most typically when a child of yours dies or |
|
|
1651 | exits). It is permissible to install a child watcher I<after> the child |
|
|
1652 | has been forked (which implies it might have already exited), as long |
|
|
1653 | as the event loop isn't entered (or is continued from a watcher), i.e., |
|
|
1654 | forking and then immediately registering a watcher for the child is fine, |
|
|
1655 | but forking and registering a watcher a few event loop iterations later is |
|
|
1656 | not. |
|
|
1657 | |
|
|
1658 | Only the default event loop is capable of handling signals, and therefore |
|
|
1659 | you can only register child watchers in the default event loop. |
|
|
1660 | |
|
|
1661 | =head3 Process Interaction |
|
|
1662 | |
|
|
1663 | Libev grabs C<SIGCHLD> as soon as the default event loop is |
|
|
1664 | initialised. This is necessary to guarantee proper behaviour even if |
|
|
1665 | the first child watcher is started after the child exits. The occurrence |
|
|
1666 | of C<SIGCHLD> is recorded asynchronously, but child reaping is done |
|
|
1667 | synchronously as part of the event loop processing. Libev always reaps all |
|
|
1668 | children, even ones not watched. |
|
|
1669 | |
|
|
1670 | =head3 Overriding the Built-In Processing |
|
|
1671 | |
|
|
1672 | Libev offers no special support for overriding the built-in child |
|
|
1673 | processing, but if your application collides with libev's default child |
|
|
1674 | handler, you can override it easily by installing your own handler for |
|
|
1675 | C<SIGCHLD> after initialising the default loop, and making sure the |
|
|
1676 | default loop never gets destroyed. You are encouraged, however, to use an |
|
|
1677 | event-based approach to child reaping and thus use libev's support for |
|
|
1678 | that, so other libev users can use C<ev_child> watchers freely. |
|
|
1679 | |
|
|
1680 | =head3 Stopping the Child Watcher |
|
|
1681 | |
|
|
1682 | Currently, the child watcher never gets stopped, even when the |
|
|
1683 | child terminates, so normally one needs to stop the watcher in the |
|
|
1684 | callback. Future versions of libev might stop the watcher automatically |
|
|
1685 | when a child exit is detected. |
1386 | |
1686 | |
1387 | =head3 Watcher-Specific Functions and Data Members |
1687 | =head3 Watcher-Specific Functions and Data Members |
1388 | |
1688 | |
1389 | =over 4 |
1689 | =over 4 |
1390 | |
1690 | |
1391 | =item ev_child_init (ev_child *, callback, int pid) |
1691 | =item ev_child_init (ev_child *, callback, int pid, int trace) |
1392 | |
1692 | |
1393 | =item ev_child_set (ev_child *, int pid) |
1693 | =item ev_child_set (ev_child *, int pid, int trace) |
1394 | |
1694 | |
1395 | Configures the watcher to wait for status changes of process C<pid> (or |
1695 | Configures the watcher to wait for status changes of process C<pid> (or |
1396 | I<any> process if C<pid> is specified as C<0>). The callback can look |
1696 | I<any> process if C<pid> is specified as C<0>). The callback can look |
1397 | at the C<rstatus> member of the C<ev_child> watcher structure to see |
1697 | at the C<rstatus> member of the C<ev_child> watcher structure to see |
1398 | the status word (use the macros from C<sys/wait.h> and see your systems |
1698 | the status word (use the macros from C<sys/wait.h> and see your systems |
1399 | C<waitpid> documentation). The C<rpid> member contains the pid of the |
1699 | C<waitpid> documentation). The C<rpid> member contains the pid of the |
1400 | process causing the status change. |
1700 | process causing the status change. C<trace> must be either C<0> (only |
|
|
1701 | activate the watcher when the process terminates) or C<1> (additionally |
|
|
1702 | activate the watcher when the process is stopped or continued). |
1401 | |
1703 | |
1402 | =item int pid [read-only] |
1704 | =item int pid [read-only] |
1403 | |
1705 | |
1404 | The process id this watcher watches out for, or C<0>, meaning any process id. |
1706 | The process id this watcher watches out for, or C<0>, meaning any process id. |
1405 | |
1707 | |
… | |
… | |
1412 | The process exit/trace status caused by C<rpid> (see your systems |
1714 | The process exit/trace status caused by C<rpid> (see your systems |
1413 | C<waitpid> and C<sys/wait.h> documentation for details). |
1715 | C<waitpid> and C<sys/wait.h> documentation for details). |
1414 | |
1716 | |
1415 | =back |
1717 | =back |
1416 | |
1718 | |
1417 | Example: Try to exit cleanly on SIGINT and SIGTERM. |
1719 | =head3 Examples |
1418 | |
1720 | |
|
|
1721 | Example: C<fork()> a new process and install a child handler to wait for |
|
|
1722 | its completion. |
|
|
1723 | |
|
|
1724 | ev_child cw; |
|
|
1725 | |
1419 | static void |
1726 | static void |
1420 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
1727 | child_cb (EV_P_ struct ev_child *w, int revents) |
1421 | { |
1728 | { |
1422 | ev_unloop (loop, EVUNLOOP_ALL); |
1729 | ev_child_stop (EV_A_ w); |
|
|
1730 | printf ("process %d exited with status %x\n", w->rpid, w->rstatus); |
1423 | } |
1731 | } |
1424 | |
1732 | |
1425 | struct ev_signal signal_watcher; |
1733 | pid_t pid = fork (); |
1426 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
1734 | |
1427 | ev_signal_start (loop, &sigint_cb); |
1735 | if (pid < 0) |
|
|
1736 | // error |
|
|
1737 | else if (pid == 0) |
|
|
1738 | { |
|
|
1739 | // the forked child executes here |
|
|
1740 | exit (1); |
|
|
1741 | } |
|
|
1742 | else |
|
|
1743 | { |
|
|
1744 | ev_child_init (&cw, child_cb, pid, 0); |
|
|
1745 | ev_child_start (EV_DEFAULT_ &cw); |
|
|
1746 | } |
1428 | |
1747 | |
1429 | |
1748 | |
1430 | =head2 C<ev_stat> - did the file attributes just change? |
1749 | =head2 C<ev_stat> - did the file attributes just change? |
1431 | |
1750 | |
1432 | This watches a filesystem path for attribute changes. That is, it calls |
1751 | This watches a file system path for attribute changes. That is, it calls |
1433 | C<stat> regularly (or when the OS says it changed) and sees if it changed |
1752 | C<stat> regularly (or when the OS says it changed) and sees if it changed |
1434 | compared to the last time, invoking the callback if it did. |
1753 | compared to the last time, invoking the callback if it did. |
1435 | |
1754 | |
1436 | The path does not need to exist: changing from "path exists" to "path does |
1755 | The path does not need to exist: changing from "path exists" to "path does |
1437 | not exist" is a status change like any other. The condition "path does |
1756 | not exist" is a status change like any other. The condition "path does |
… | |
… | |
1440 | the stat buffer having unspecified contents. |
1759 | the stat buffer having unspecified contents. |
1441 | |
1760 | |
1442 | The path I<should> be absolute and I<must not> end in a slash. If it is |
1761 | The path I<should> be absolute and I<must not> end in a slash. If it is |
1443 | relative and your working directory changes, the behaviour is undefined. |
1762 | relative and your working directory changes, the behaviour is undefined. |
1444 | |
1763 | |
1445 | Since there is no standard to do this, the portable implementation simply |
1764 | Since there is no standard kernel interface to do this, the portable |
1446 | calls C<stat (2)> regularly on the path to see if it changed somehow. You |
1765 | implementation simply calls C<stat (2)> regularly on the path to see if |
1447 | can specify a recommended polling interval for this case. If you specify |
1766 | it changed somehow. You can specify a recommended polling interval for |
1448 | a polling interval of C<0> (highly recommended!) then a I<suitable, |
1767 | this case. If you specify a polling interval of C<0> (highly recommended!) |
1449 | unspecified default> value will be used (which you can expect to be around |
1768 | then a I<suitable, unspecified default> value will be used (which |
1450 | five seconds, although this might change dynamically). Libev will also |
1769 | you can expect to be around five seconds, although this might change |
1451 | impose a minimum interval which is currently around C<0.1>, but thats |
1770 | dynamically). Libev will also impose a minimum interval which is currently |
1452 | usually overkill. |
1771 | around C<0.1>, but thats usually overkill. |
1453 | |
1772 | |
1454 | This watcher type is not meant for massive numbers of stat watchers, |
1773 | This watcher type is not meant for massive numbers of stat watchers, |
1455 | as even with OS-supported change notifications, this can be |
1774 | as even with OS-supported change notifications, this can be |
1456 | resource-intensive. |
1775 | resource-intensive. |
1457 | |
1776 | |
1458 | At the time of this writing, only the Linux inotify interface is |
1777 | At the time of this writing, the only OS-specific interface implemented |
1459 | implemented (implementing kqueue support is left as an exercise for the |
1778 | is the Linux inotify interface (implementing kqueue support is left as |
1460 | reader). Inotify will be used to give hints only and should not change the |
1779 | an exercise for the reader. Note, however, that the author sees no way |
1461 | semantics of C<ev_stat> watchers, which means that libev sometimes needs |
1780 | of implementing C<ev_stat> semantics with kqueue). |
1462 | to fall back to regular polling again even with inotify, but changes are |
1781 | |
1463 | usually detected immediately, and if the file exists there will be no |
1782 | =head3 ABI Issues (Largefile Support) |
1464 | polling. |
1783 | |
|
|
1784 | Libev by default (unless the user overrides this) uses the default |
|
|
1785 | compilation environment, which means that on systems with large file |
|
|
1786 | support disabled by default, you get the 32 bit version of the stat |
|
|
1787 | structure. When using the library from programs that change the ABI to |
|
|
1788 | use 64 bit file offsets the programs will fail. In that case you have to |
|
|
1789 | compile libev with the same flags to get binary compatibility. This is |
|
|
1790 | obviously the case with any flags that change the ABI, but the problem is |
|
|
1791 | most noticeably disabled with ev_stat and large file support. |
|
|
1792 | |
|
|
1793 | The solution for this is to lobby your distribution maker to make large |
|
|
1794 | file interfaces available by default (as e.g. FreeBSD does) and not |
|
|
1795 | optional. Libev cannot simply switch on large file support because it has |
|
|
1796 | to exchange stat structures with application programs compiled using the |
|
|
1797 | default compilation environment. |
|
|
1798 | |
|
|
1799 | =head3 Inotify and Kqueue |
|
|
1800 | |
|
|
1801 | When C<inotify (7)> support has been compiled into libev (generally |
|
|
1802 | only available with Linux 2.6.25 or above due to bugs in earlier |
|
|
1803 | implementations) and present at runtime, it will be used to speed up |
|
|
1804 | change detection where possible. The inotify descriptor will be created |
|
|
1805 | lazily when the first C<ev_stat> watcher is being started. |
|
|
1806 | |
|
|
1807 | Inotify presence does not change the semantics of C<ev_stat> watchers |
|
|
1808 | except that changes might be detected earlier, and in some cases, to avoid |
|
|
1809 | making regular C<stat> calls. Even in the presence of inotify support |
|
|
1810 | there are many cases where libev has to resort to regular C<stat> polling, |
|
|
1811 | but as long as the path exists, libev usually gets away without polling. |
|
|
1812 | |
|
|
1813 | There is no support for kqueue, as apparently it cannot be used to |
|
|
1814 | implement this functionality, due to the requirement of having a file |
|
|
1815 | descriptor open on the object at all times, and detecting renames, unlinks |
|
|
1816 | etc. is difficult. |
|
|
1817 | |
|
|
1818 | =head3 The special problem of stat time resolution |
|
|
1819 | |
|
|
1820 | The C<stat ()> system call only supports full-second resolution portably, and |
|
|
1821 | even on systems where the resolution is higher, most file systems still |
|
|
1822 | only support whole seconds. |
|
|
1823 | |
|
|
1824 | That means that, if the time is the only thing that changes, you can |
|
|
1825 | easily miss updates: on the first update, C<ev_stat> detects a change and |
|
|
1826 | calls your callback, which does something. When there is another update |
|
|
1827 | within the same second, C<ev_stat> will be unable to detect unless the |
|
|
1828 | stat data does change in other ways (e.g. file size). |
|
|
1829 | |
|
|
1830 | The solution to this is to delay acting on a change for slightly more |
|
|
1831 | than a second (or till slightly after the next full second boundary), using |
|
|
1832 | a roughly one-second-delay C<ev_timer> (e.g. C<ev_timer_set (w, 0., 1.02); |
|
|
1833 | ev_timer_again (loop, w)>). |
|
|
1834 | |
|
|
1835 | The C<.02> offset is added to work around small timing inconsistencies |
|
|
1836 | of some operating systems (where the second counter of the current time |
|
|
1837 | might be be delayed. One such system is the Linux kernel, where a call to |
|
|
1838 | C<gettimeofday> might return a timestamp with a full second later than |
|
|
1839 | a subsequent C<time> call - if the equivalent of C<time ()> is used to |
|
|
1840 | update file times then there will be a small window where the kernel uses |
|
|
1841 | the previous second to update file times but libev might already execute |
|
|
1842 | the timer callback). |
1465 | |
1843 | |
1466 | =head3 Watcher-Specific Functions and Data Members |
1844 | =head3 Watcher-Specific Functions and Data Members |
1467 | |
1845 | |
1468 | =over 4 |
1846 | =over 4 |
1469 | |
1847 | |
… | |
… | |
1475 | C<path>. The C<interval> is a hint on how quickly a change is expected to |
1853 | C<path>. The C<interval> is a hint on how quickly a change is expected to |
1476 | be detected and should normally be specified as C<0> to let libev choose |
1854 | be detected and should normally be specified as C<0> to let libev choose |
1477 | a suitable value. The memory pointed to by C<path> must point to the same |
1855 | a suitable value. The memory pointed to by C<path> must point to the same |
1478 | path for as long as the watcher is active. |
1856 | path for as long as the watcher is active. |
1479 | |
1857 | |
1480 | The callback will be receive C<EV_STAT> when a change was detected, |
1858 | The callback will receive an C<EV_STAT> event when a change was detected, |
1481 | relative to the attributes at the time the watcher was started (or the |
1859 | relative to the attributes at the time the watcher was started (or the |
1482 | last change was detected). |
1860 | last change was detected). |
1483 | |
1861 | |
1484 | =item ev_stat_stat (ev_stat *) |
1862 | =item ev_stat_stat (loop, ev_stat *) |
1485 | |
1863 | |
1486 | Updates the stat buffer immediately with new values. If you change the |
1864 | Updates the stat buffer immediately with new values. If you change the |
1487 | watched path in your callback, you could call this fucntion to avoid |
1865 | watched path in your callback, you could call this function to avoid |
1488 | detecting this change (while introducing a race condition). Can also be |
1866 | detecting this change (while introducing a race condition if you are not |
1489 | useful simply to find out the new values. |
1867 | the only one changing the path). Can also be useful simply to find out the |
|
|
1868 | new values. |
1490 | |
1869 | |
1491 | =item ev_statdata attr [read-only] |
1870 | =item ev_statdata attr [read-only] |
1492 | |
1871 | |
1493 | The most-recently detected attributes of the file. Although the type is of |
1872 | The most-recently detected attributes of the file. Although the type is |
1494 | C<ev_statdata>, this is usually the (or one of the) C<struct stat> types |
1873 | C<ev_statdata>, this is usually the (or one of the) C<struct stat> types |
1495 | suitable for your system. If the C<st_nlink> member is C<0>, then there |
1874 | suitable for your system, but you can only rely on the POSIX-standardised |
|
|
1875 | members to be present. If the C<st_nlink> member is C<0>, then there was |
1496 | was some error while C<stat>ing the file. |
1876 | some error while C<stat>ing the file. |
1497 | |
1877 | |
1498 | =item ev_statdata prev [read-only] |
1878 | =item ev_statdata prev [read-only] |
1499 | |
1879 | |
1500 | The previous attributes of the file. The callback gets invoked whenever |
1880 | The previous attributes of the file. The callback gets invoked whenever |
1501 | C<prev> != C<attr>. |
1881 | C<prev> != C<attr>, or, more precisely, one or more of these members |
|
|
1882 | differ: C<st_dev>, C<st_ino>, C<st_mode>, C<st_nlink>, C<st_uid>, |
|
|
1883 | C<st_gid>, C<st_rdev>, C<st_size>, C<st_atime>, C<st_mtime>, C<st_ctime>. |
1502 | |
1884 | |
1503 | =item ev_tstamp interval [read-only] |
1885 | =item ev_tstamp interval [read-only] |
1504 | |
1886 | |
1505 | The specified interval. |
1887 | The specified interval. |
1506 | |
1888 | |
1507 | =item const char *path [read-only] |
1889 | =item const char *path [read-only] |
1508 | |
1890 | |
1509 | The filesystem path that is being watched. |
1891 | The file system path that is being watched. |
1510 | |
1892 | |
1511 | =back |
1893 | =back |
1512 | |
1894 | |
|
|
1895 | =head3 Examples |
|
|
1896 | |
1513 | Example: Watch C</etc/passwd> for attribute changes. |
1897 | Example: Watch C</etc/passwd> for attribute changes. |
1514 | |
1898 | |
1515 | static void |
1899 | static void |
1516 | passwd_cb (struct ev_loop *loop, ev_stat *w, int revents) |
1900 | passwd_cb (struct ev_loop *loop, ev_stat *w, int revents) |
1517 | { |
1901 | { |
1518 | /* /etc/passwd changed in some way */ |
1902 | /* /etc/passwd changed in some way */ |
1519 | if (w->attr.st_nlink) |
1903 | if (w->attr.st_nlink) |
1520 | { |
1904 | { |
1521 | printf ("passwd current size %ld\n", (long)w->attr.st_size); |
1905 | printf ("passwd current size %ld\n", (long)w->attr.st_size); |
1522 | printf ("passwd current atime %ld\n", (long)w->attr.st_mtime); |
1906 | printf ("passwd current atime %ld\n", (long)w->attr.st_mtime); |
1523 | printf ("passwd current mtime %ld\n", (long)w->attr.st_mtime); |
1907 | printf ("passwd current mtime %ld\n", (long)w->attr.st_mtime); |
1524 | } |
1908 | } |
1525 | else |
1909 | else |
1526 | /* you shalt not abuse printf for puts */ |
1910 | /* you shalt not abuse printf for puts */ |
1527 | puts ("wow, /etc/passwd is not there, expect problems. " |
1911 | puts ("wow, /etc/passwd is not there, expect problems. " |
1528 | "if this is windows, they already arrived\n"); |
1912 | "if this is windows, they already arrived\n"); |
1529 | } |
1913 | } |
1530 | |
1914 | |
1531 | ... |
1915 | ... |
1532 | ev_stat passwd; |
1916 | ev_stat passwd; |
1533 | |
1917 | |
1534 | ev_stat_init (&passwd, passwd_cb, "/etc/passwd"); |
1918 | ev_stat_init (&passwd, passwd_cb, "/etc/passwd", 0.); |
1535 | ev_stat_start (loop, &passwd); |
1919 | ev_stat_start (loop, &passwd); |
|
|
1920 | |
|
|
1921 | Example: Like above, but additionally use a one-second delay so we do not |
|
|
1922 | miss updates (however, frequent updates will delay processing, too, so |
|
|
1923 | one might do the work both on C<ev_stat> callback invocation I<and> on |
|
|
1924 | C<ev_timer> callback invocation). |
|
|
1925 | |
|
|
1926 | static ev_stat passwd; |
|
|
1927 | static ev_timer timer; |
|
|
1928 | |
|
|
1929 | static void |
|
|
1930 | timer_cb (EV_P_ ev_timer *w, int revents) |
|
|
1931 | { |
|
|
1932 | ev_timer_stop (EV_A_ w); |
|
|
1933 | |
|
|
1934 | /* now it's one second after the most recent passwd change */ |
|
|
1935 | } |
|
|
1936 | |
|
|
1937 | static void |
|
|
1938 | stat_cb (EV_P_ ev_stat *w, int revents) |
|
|
1939 | { |
|
|
1940 | /* reset the one-second timer */ |
|
|
1941 | ev_timer_again (EV_A_ &timer); |
|
|
1942 | } |
|
|
1943 | |
|
|
1944 | ... |
|
|
1945 | ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.); |
|
|
1946 | ev_stat_start (loop, &passwd); |
|
|
1947 | ev_timer_init (&timer, timer_cb, 0., 1.02); |
1536 | |
1948 | |
1537 | |
1949 | |
1538 | =head2 C<ev_idle> - when you've got nothing better to do... |
1950 | =head2 C<ev_idle> - when you've got nothing better to do... |
1539 | |
1951 | |
1540 | Idle watchers trigger events when no other events of the same or higher |
1952 | Idle watchers trigger events when no other events of the same or higher |
1541 | priority are pending (prepare, check and other idle watchers do not |
1953 | priority are pending (prepare, check and other idle watchers do not count |
1542 | count). |
1954 | as receiving "events"). |
1543 | |
1955 | |
1544 | That is, as long as your process is busy handling sockets or timeouts |
1956 | That is, as long as your process is busy handling sockets or timeouts |
1545 | (or even signals, imagine) of the same or higher priority it will not be |
1957 | (or even signals, imagine) of the same or higher priority it will not be |
1546 | triggered. But when your process is idle (or only lower-priority watchers |
1958 | triggered. But when your process is idle (or only lower-priority watchers |
1547 | are pending), the idle watchers are being called once per event loop |
1959 | are pending), the idle watchers are being called once per event loop |
… | |
… | |
1566 | kind. There is a C<ev_idle_set> macro, but using it is utterly pointless, |
1978 | kind. There is a C<ev_idle_set> macro, but using it is utterly pointless, |
1567 | believe me. |
1979 | believe me. |
1568 | |
1980 | |
1569 | =back |
1981 | =back |
1570 | |
1982 | |
|
|
1983 | =head3 Examples |
|
|
1984 | |
1571 | Example: Dynamically allocate an C<ev_idle> watcher, start it, and in the |
1985 | Example: Dynamically allocate an C<ev_idle> watcher, start it, and in the |
1572 | callback, free it. Also, use no error checking, as usual. |
1986 | callback, free it. Also, use no error checking, as usual. |
1573 | |
1987 | |
1574 | static void |
1988 | static void |
1575 | idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) |
1989 | idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) |
1576 | { |
1990 | { |
1577 | free (w); |
1991 | free (w); |
1578 | // now do something you wanted to do when the program has |
1992 | // now do something you wanted to do when the program has |
1579 | // no longer asnything immediate to do. |
1993 | // no longer anything immediate to do. |
1580 | } |
1994 | } |
1581 | |
1995 | |
1582 | struct ev_idle *idle_watcher = malloc (sizeof (struct ev_idle)); |
1996 | struct ev_idle *idle_watcher = malloc (sizeof (struct ev_idle)); |
1583 | ev_idle_init (idle_watcher, idle_cb); |
1997 | ev_idle_init (idle_watcher, idle_cb); |
1584 | ev_idle_start (loop, idle_cb); |
1998 | ev_idle_start (loop, idle_cb); |
1585 | |
1999 | |
1586 | |
2000 | |
1587 | =head2 C<ev_prepare> and C<ev_check> - customise your event loop! |
2001 | =head2 C<ev_prepare> and C<ev_check> - customise your event loop! |
1588 | |
2002 | |
1589 | Prepare and check watchers are usually (but not always) used in tandem: |
2003 | Prepare and check watchers are usually (but not always) used in pairs: |
1590 | prepare watchers get invoked before the process blocks and check watchers |
2004 | prepare watchers get invoked before the process blocks and check watchers |
1591 | afterwards. |
2005 | afterwards. |
1592 | |
2006 | |
1593 | You I<must not> call C<ev_loop> or similar functions that enter |
2007 | You I<must not> call C<ev_loop> or similar functions that enter |
1594 | the current event loop from either C<ev_prepare> or C<ev_check> |
2008 | the current event loop from either C<ev_prepare> or C<ev_check> |
… | |
… | |
1597 | those watchers, i.e. the sequence will always be C<ev_prepare>, blocking, |
2011 | those watchers, i.e. the sequence will always be C<ev_prepare>, blocking, |
1598 | C<ev_check> so if you have one watcher of each kind they will always be |
2012 | C<ev_check> so if you have one watcher of each kind they will always be |
1599 | called in pairs bracketing the blocking call. |
2013 | called in pairs bracketing the blocking call. |
1600 | |
2014 | |
1601 | Their main purpose is to integrate other event mechanisms into libev and |
2015 | Their main purpose is to integrate other event mechanisms into libev and |
1602 | their use is somewhat advanced. This could be used, for example, to track |
2016 | their use is somewhat advanced. They could be used, for example, to track |
1603 | variable changes, implement your own watchers, integrate net-snmp or a |
2017 | variable changes, implement your own watchers, integrate net-snmp or a |
1604 | coroutine library and lots more. They are also occasionally useful if |
2018 | coroutine library and lots more. They are also occasionally useful if |
1605 | you cache some data and want to flush it before blocking (for example, |
2019 | you cache some data and want to flush it before blocking (for example, |
1606 | in X programs you might want to do an C<XFlush ()> in an C<ev_prepare> |
2020 | in X programs you might want to do an C<XFlush ()> in an C<ev_prepare> |
1607 | watcher). |
2021 | watcher). |
1608 | |
2022 | |
1609 | This is done by examining in each prepare call which file descriptors need |
2023 | This is done by examining in each prepare call which file descriptors |
1610 | to be watched by the other library, registering C<ev_io> watchers for |
2024 | need to be watched by the other library, registering C<ev_io> watchers |
1611 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
2025 | for them and starting an C<ev_timer> watcher for any timeouts (many |
1612 | provide just this functionality). Then, in the check watcher you check for |
2026 | libraries provide exactly this functionality). Then, in the check watcher, |
1613 | any events that occured (by checking the pending status of all watchers |
2027 | you check for any events that occurred (by checking the pending status |
1614 | and stopping them) and call back into the library. The I/O and timer |
2028 | of all watchers and stopping them) and call back into the library. The |
1615 | callbacks will never actually be called (but must be valid nevertheless, |
2029 | I/O and timer callbacks will never actually be called (but must be valid |
1616 | because you never know, you know?). |
2030 | nevertheless, because you never know, you know?). |
1617 | |
2031 | |
1618 | As another example, the Perl Coro module uses these hooks to integrate |
2032 | As another example, the Perl Coro module uses these hooks to integrate |
1619 | coroutines into libev programs, by yielding to other active coroutines |
2033 | coroutines into libev programs, by yielding to other active coroutines |
1620 | during each prepare and only letting the process block if no coroutines |
2034 | during each prepare and only letting the process block if no coroutines |
1621 | are ready to run (it's actually more complicated: it only runs coroutines |
2035 | are ready to run (it's actually more complicated: it only runs coroutines |
… | |
… | |
1624 | loop from blocking if lower-priority coroutines are active, thus mapping |
2038 | loop from blocking if lower-priority coroutines are active, thus mapping |
1625 | low-priority coroutines to idle/background tasks). |
2039 | low-priority coroutines to idle/background tasks). |
1626 | |
2040 | |
1627 | It is recommended to give C<ev_check> watchers highest (C<EV_MAXPRI>) |
2041 | It is recommended to give C<ev_check> watchers highest (C<EV_MAXPRI>) |
1628 | priority, to ensure that they are being run before any other watchers |
2042 | priority, to ensure that they are being run before any other watchers |
|
|
2043 | after the poll (this doesn't matter for C<ev_prepare> watchers). |
|
|
2044 | |
1629 | after the poll. Also, C<ev_check> watchers (and C<ev_prepare> watchers, |
2045 | Also, C<ev_check> watchers (and C<ev_prepare> watchers, too) should not |
1630 | too) should not activate ("feed") events into libev. While libev fully |
2046 | activate ("feed") events into libev. While libev fully supports this, they |
1631 | supports this, they will be called before other C<ev_check> watchers |
2047 | might get executed before other C<ev_check> watchers did their job. As |
1632 | did their job. As C<ev_check> watchers are often used to embed other |
2048 | C<ev_check> watchers are often used to embed other (non-libev) event |
1633 | (non-libev) event loops those other event loops might be in an unusable |
2049 | loops those other event loops might be in an unusable state until their |
1634 | state until their C<ev_check> watcher ran (always remind yourself to |
2050 | C<ev_check> watcher ran (always remind yourself to coexist peacefully with |
1635 | coexist peacefully with others). |
2051 | others). |
1636 | |
2052 | |
1637 | =head3 Watcher-Specific Functions and Data Members |
2053 | =head3 Watcher-Specific Functions and Data Members |
1638 | |
2054 | |
1639 | =over 4 |
2055 | =over 4 |
1640 | |
2056 | |
… | |
… | |
1642 | |
2058 | |
1643 | =item ev_check_init (ev_check *, callback) |
2059 | =item ev_check_init (ev_check *, callback) |
1644 | |
2060 | |
1645 | Initialises and configures the prepare or check watcher - they have no |
2061 | Initialises and configures the prepare or check watcher - they have no |
1646 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
2062 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
1647 | macros, but using them is utterly, utterly and completely pointless. |
2063 | macros, but using them is utterly, utterly, utterly and completely |
|
|
2064 | pointless. |
1648 | |
2065 | |
1649 | =back |
2066 | =back |
|
|
2067 | |
|
|
2068 | =head3 Examples |
1650 | |
2069 | |
1651 | There are a number of principal ways to embed other event loops or modules |
2070 | There are a number of principal ways to embed other event loops or modules |
1652 | into libev. Here are some ideas on how to include libadns into libev |
2071 | into libev. Here are some ideas on how to include libadns into libev |
1653 | (there is a Perl module named C<EV::ADNS> that does this, which you could |
2072 | (there is a Perl module named C<EV::ADNS> that does this, which you could |
1654 | use for an actually working example. Another Perl module named C<EV::Glib> |
2073 | use as a working example. Another Perl module named C<EV::Glib> embeds a |
1655 | embeds a Glib main context into libev, and finally, C<Glib::EV> embeds EV |
2074 | Glib main context into libev, and finally, C<Glib::EV> embeds EV into the |
1656 | into the Glib event loop). |
2075 | Glib event loop). |
1657 | |
2076 | |
1658 | Method 1: Add IO watchers and a timeout watcher in a prepare handler, |
2077 | Method 1: Add IO watchers and a timeout watcher in a prepare handler, |
1659 | and in a check watcher, destroy them and call into libadns. What follows |
2078 | and in a check watcher, destroy them and call into libadns. What follows |
1660 | is pseudo-code only of course. This requires you to either use a low |
2079 | is pseudo-code only of course. This requires you to either use a low |
1661 | priority for the check watcher or use C<ev_clear_pending> explicitly, as |
2080 | priority for the check watcher or use C<ev_clear_pending> explicitly, as |
1662 | the callbacks for the IO/timeout watchers might not have been called yet. |
2081 | the callbacks for the IO/timeout watchers might not have been called yet. |
1663 | |
2082 | |
1664 | static ev_io iow [nfd]; |
2083 | static ev_io iow [nfd]; |
1665 | static ev_timer tw; |
2084 | static ev_timer tw; |
1666 | |
2085 | |
1667 | static void |
2086 | static void |
1668 | io_cb (ev_loop *loop, ev_io *w, int revents) |
2087 | io_cb (ev_loop *loop, ev_io *w, int revents) |
1669 | { |
2088 | { |
1670 | } |
2089 | } |
1671 | |
2090 | |
1672 | // create io watchers for each fd and a timer before blocking |
2091 | // create io watchers for each fd and a timer before blocking |
1673 | static void |
2092 | static void |
1674 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
2093 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
1675 | { |
2094 | { |
1676 | int timeout = 3600000; |
2095 | int timeout = 3600000; |
1677 | struct pollfd fds [nfd]; |
2096 | struct pollfd fds [nfd]; |
1678 | // actual code will need to loop here and realloc etc. |
2097 | // actual code will need to loop here and realloc etc. |
1679 | adns_beforepoll (ads, fds, &nfd, &timeout, timeval_from (ev_time ())); |
2098 | adns_beforepoll (ads, fds, &nfd, &timeout, timeval_from (ev_time ())); |
1680 | |
2099 | |
1681 | /* the callback is illegal, but won't be called as we stop during check */ |
2100 | /* the callback is illegal, but won't be called as we stop during check */ |
1682 | ev_timer_init (&tw, 0, timeout * 1e-3); |
2101 | ev_timer_init (&tw, 0, timeout * 1e-3); |
1683 | ev_timer_start (loop, &tw); |
2102 | ev_timer_start (loop, &tw); |
1684 | |
2103 | |
1685 | // create one ev_io per pollfd |
2104 | // create one ev_io per pollfd |
1686 | for (int i = 0; i < nfd; ++i) |
2105 | for (int i = 0; i < nfd; ++i) |
1687 | { |
2106 | { |
1688 | ev_io_init (iow + i, io_cb, fds [i].fd, |
2107 | ev_io_init (iow + i, io_cb, fds [i].fd, |
1689 | ((fds [i].events & POLLIN ? EV_READ : 0) |
2108 | ((fds [i].events & POLLIN ? EV_READ : 0) |
1690 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
2109 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
1691 | |
2110 | |
1692 | fds [i].revents = 0; |
2111 | fds [i].revents = 0; |
1693 | ev_io_start (loop, iow + i); |
2112 | ev_io_start (loop, iow + i); |
1694 | } |
2113 | } |
1695 | } |
2114 | } |
1696 | |
2115 | |
1697 | // stop all watchers after blocking |
2116 | // stop all watchers after blocking |
1698 | static void |
2117 | static void |
1699 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
2118 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
1700 | { |
2119 | { |
1701 | ev_timer_stop (loop, &tw); |
2120 | ev_timer_stop (loop, &tw); |
1702 | |
2121 | |
1703 | for (int i = 0; i < nfd; ++i) |
2122 | for (int i = 0; i < nfd; ++i) |
1704 | { |
2123 | { |
1705 | // set the relevant poll flags |
2124 | // set the relevant poll flags |
1706 | // could also call adns_processreadable etc. here |
2125 | // could also call adns_processreadable etc. here |
1707 | struct pollfd *fd = fds + i; |
2126 | struct pollfd *fd = fds + i; |
1708 | int revents = ev_clear_pending (iow + i); |
2127 | int revents = ev_clear_pending (iow + i); |
1709 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
2128 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
1710 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
2129 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
1711 | |
2130 | |
1712 | // now stop the watcher |
2131 | // now stop the watcher |
1713 | ev_io_stop (loop, iow + i); |
2132 | ev_io_stop (loop, iow + i); |
1714 | } |
2133 | } |
1715 | |
2134 | |
1716 | adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
2135 | adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
1717 | } |
2136 | } |
1718 | |
2137 | |
1719 | Method 2: This would be just like method 1, but you run C<adns_afterpoll> |
2138 | Method 2: This would be just like method 1, but you run C<adns_afterpoll> |
1720 | in the prepare watcher and would dispose of the check watcher. |
2139 | in the prepare watcher and would dispose of the check watcher. |
1721 | |
2140 | |
1722 | Method 3: If the module to be embedded supports explicit event |
2141 | Method 3: If the module to be embedded supports explicit event |
1723 | notification (adns does), you can also make use of the actual watcher |
2142 | notification (libadns does), you can also make use of the actual watcher |
1724 | callbacks, and only destroy/create the watchers in the prepare watcher. |
2143 | callbacks, and only destroy/create the watchers in the prepare watcher. |
1725 | |
2144 | |
1726 | static void |
2145 | static void |
1727 | timer_cb (EV_P_ ev_timer *w, int revents) |
2146 | timer_cb (EV_P_ ev_timer *w, int revents) |
1728 | { |
2147 | { |
1729 | adns_state ads = (adns_state)w->data; |
2148 | adns_state ads = (adns_state)w->data; |
1730 | update_now (EV_A); |
2149 | update_now (EV_A); |
1731 | |
2150 | |
1732 | adns_processtimeouts (ads, &tv_now); |
2151 | adns_processtimeouts (ads, &tv_now); |
1733 | } |
2152 | } |
1734 | |
2153 | |
1735 | static void |
2154 | static void |
1736 | io_cb (EV_P_ ev_io *w, int revents) |
2155 | io_cb (EV_P_ ev_io *w, int revents) |
1737 | { |
2156 | { |
1738 | adns_state ads = (adns_state)w->data; |
2157 | adns_state ads = (adns_state)w->data; |
1739 | update_now (EV_A); |
2158 | update_now (EV_A); |
1740 | |
2159 | |
1741 | if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); |
2160 | if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); |
1742 | if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); |
2161 | if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); |
1743 | } |
2162 | } |
1744 | |
2163 | |
1745 | // do not ever call adns_afterpoll |
2164 | // do not ever call adns_afterpoll |
1746 | |
2165 | |
1747 | Method 4: Do not use a prepare or check watcher because the module you |
2166 | Method 4: Do not use a prepare or check watcher because the module you |
1748 | want to embed is too inflexible to support it. Instead, youc na override |
2167 | want to embed is not flexible enough to support it. Instead, you can |
1749 | their poll function. The drawback with this solution is that the main |
2168 | override their poll function. The drawback with this solution is that the |
1750 | loop is now no longer controllable by EV. The C<Glib::EV> module does |
2169 | main loop is now no longer controllable by EV. The C<Glib::EV> module uses |
1751 | this. |
2170 | this approach, effectively embedding EV as a client into the horrible |
|
|
2171 | libglib event loop. |
1752 | |
2172 | |
1753 | static gint |
2173 | static gint |
1754 | event_poll_func (GPollFD *fds, guint nfds, gint timeout) |
2174 | event_poll_func (GPollFD *fds, guint nfds, gint timeout) |
1755 | { |
2175 | { |
1756 | int got_events = 0; |
2176 | int got_events = 0; |
1757 | |
2177 | |
1758 | for (n = 0; n < nfds; ++n) |
2178 | for (n = 0; n < nfds; ++n) |
1759 | // create/start io watcher that sets the relevant bits in fds[n] and increment got_events |
2179 | // create/start io watcher that sets the relevant bits in fds[n] and increment got_events |
1760 | |
2180 | |
1761 | if (timeout >= 0) |
2181 | if (timeout >= 0) |
1762 | // create/start timer |
2182 | // create/start timer |
1763 | |
2183 | |
1764 | // poll |
2184 | // poll |
1765 | ev_loop (EV_A_ 0); |
2185 | ev_loop (EV_A_ 0); |
1766 | |
2186 | |
1767 | // stop timer again |
2187 | // stop timer again |
1768 | if (timeout >= 0) |
2188 | if (timeout >= 0) |
1769 | ev_timer_stop (EV_A_ &to); |
2189 | ev_timer_stop (EV_A_ &to); |
1770 | |
2190 | |
1771 | // stop io watchers again - their callbacks should have set |
2191 | // stop io watchers again - their callbacks should have set |
1772 | for (n = 0; n < nfds; ++n) |
2192 | for (n = 0; n < nfds; ++n) |
1773 | ev_io_stop (EV_A_ iow [n]); |
2193 | ev_io_stop (EV_A_ iow [n]); |
1774 | |
2194 | |
1775 | return got_events; |
2195 | return got_events; |
1776 | } |
2196 | } |
1777 | |
2197 | |
1778 | |
2198 | |
1779 | =head2 C<ev_embed> - when one backend isn't enough... |
2199 | =head2 C<ev_embed> - when one backend isn't enough... |
1780 | |
2200 | |
1781 | This is a rather advanced watcher type that lets you embed one event loop |
2201 | This is a rather advanced watcher type that lets you embed one event loop |
… | |
… | |
1787 | prioritise I/O. |
2207 | prioritise I/O. |
1788 | |
2208 | |
1789 | As an example for a bug workaround, the kqueue backend might only support |
2209 | As an example for a bug workaround, the kqueue backend might only support |
1790 | sockets on some platform, so it is unusable as generic backend, but you |
2210 | sockets on some platform, so it is unusable as generic backend, but you |
1791 | still want to make use of it because you have many sockets and it scales |
2211 | still want to make use of it because you have many sockets and it scales |
1792 | so nicely. In this case, you would create a kqueue-based loop and embed it |
2212 | so nicely. In this case, you would create a kqueue-based loop and embed |
1793 | into your default loop (which might use e.g. poll). Overall operation will |
2213 | it into your default loop (which might use e.g. poll). Overall operation |
1794 | be a bit slower because first libev has to poll and then call kevent, but |
2214 | will be a bit slower because first libev has to call C<poll> and then |
1795 | at least you can use both at what they are best. |
2215 | C<kevent>, but at least you can use both mechanisms for what they are |
|
|
2216 | best: C<kqueue> for scalable sockets and C<poll> if you want it to work :) |
1796 | |
2217 | |
1797 | As for prioritising I/O: rarely you have the case where some fds have |
2218 | As for prioritising I/O: under rare circumstances you have the case where |
1798 | to be watched and handled very quickly (with low latency), and even |
2219 | some fds have to be watched and handled very quickly (with low latency), |
1799 | priorities and idle watchers might have too much overhead. In this case |
2220 | and even priorities and idle watchers might have too much overhead. In |
1800 | you would put all the high priority stuff in one loop and all the rest in |
2221 | this case you would put all the high priority stuff in one loop and all |
1801 | a second one, and embed the second one in the first. |
2222 | the rest in a second one, and embed the second one in the first. |
1802 | |
2223 | |
1803 | As long as the watcher is active, the callback will be invoked every time |
2224 | As long as the watcher is active, the callback will be invoked every time |
1804 | there might be events pending in the embedded loop. The callback must then |
2225 | there might be events pending in the embedded loop. The callback must then |
1805 | call C<ev_embed_sweep (mainloop, watcher)> to make a single sweep and invoke |
2226 | call C<ev_embed_sweep (mainloop, watcher)> to make a single sweep and invoke |
1806 | their callbacks (you could also start an idle watcher to give the embedded |
2227 | their callbacks (you could also start an idle watcher to give the embedded |
… | |
… | |
1814 | interested in that. |
2235 | interested in that. |
1815 | |
2236 | |
1816 | Also, there have not currently been made special provisions for forking: |
2237 | Also, there have not currently been made special provisions for forking: |
1817 | when you fork, you not only have to call C<ev_loop_fork> on both loops, |
2238 | when you fork, you not only have to call C<ev_loop_fork> on both loops, |
1818 | but you will also have to stop and restart any C<ev_embed> watchers |
2239 | but you will also have to stop and restart any C<ev_embed> watchers |
1819 | yourself. |
2240 | yourself - but you can use a fork watcher to handle this automatically, |
|
|
2241 | and future versions of libev might do just that. |
1820 | |
2242 | |
1821 | Unfortunately, not all backends are embeddable, only the ones returned by |
2243 | Unfortunately, not all backends are embeddable: only the ones returned by |
1822 | C<ev_embeddable_backends> are, which, unfortunately, does not include any |
2244 | C<ev_embeddable_backends> are, which, unfortunately, does not include any |
1823 | portable one. |
2245 | portable one. |
1824 | |
2246 | |
1825 | So when you want to use this feature you will always have to be prepared |
2247 | So when you want to use this feature you will always have to be prepared |
1826 | that you cannot get an embeddable loop. The recommended way to get around |
2248 | that you cannot get an embeddable loop. The recommended way to get around |
1827 | this is to have a separate variables for your embeddable loop, try to |
2249 | this is to have a separate variables for your embeddable loop, try to |
1828 | create it, and if that fails, use the normal loop for everything: |
2250 | create it, and if that fails, use the normal loop for everything. |
1829 | |
2251 | |
1830 | struct ev_loop *loop_hi = ev_default_init (0); |
2252 | =head3 C<ev_embed> and fork |
1831 | struct ev_loop *loop_lo = 0; |
|
|
1832 | struct ev_embed embed; |
|
|
1833 | |
|
|
1834 | // see if there is a chance of getting one that works |
|
|
1835 | // (remember that a flags value of 0 means autodetection) |
|
|
1836 | loop_lo = ev_embeddable_backends () & ev_recommended_backends () |
|
|
1837 | ? ev_loop_new (ev_embeddable_backends () & ev_recommended_backends ()) |
|
|
1838 | : 0; |
|
|
1839 | |
2253 | |
1840 | // if we got one, then embed it, otherwise default to loop_hi |
2254 | While the C<ev_embed> watcher is running, forks in the embedding loop will |
1841 | if (loop_lo) |
2255 | automatically be applied to the embedded loop as well, so no special |
1842 | { |
2256 | fork handling is required in that case. When the watcher is not running, |
1843 | ev_embed_init (&embed, 0, loop_lo); |
2257 | however, it is still the task of the libev user to call C<ev_loop_fork ()> |
1844 | ev_embed_start (loop_hi, &embed); |
2258 | as applicable. |
1845 | } |
|
|
1846 | else |
|
|
1847 | loop_lo = loop_hi; |
|
|
1848 | |
2259 | |
1849 | =head3 Watcher-Specific Functions and Data Members |
2260 | =head3 Watcher-Specific Functions and Data Members |
1850 | |
2261 | |
1851 | =over 4 |
2262 | =over 4 |
1852 | |
2263 | |
… | |
… | |
1856 | |
2267 | |
1857 | Configures the watcher to embed the given loop, which must be |
2268 | Configures the watcher to embed the given loop, which must be |
1858 | embeddable. If the callback is C<0>, then C<ev_embed_sweep> will be |
2269 | embeddable. If the callback is C<0>, then C<ev_embed_sweep> will be |
1859 | invoked automatically, otherwise it is the responsibility of the callback |
2270 | invoked automatically, otherwise it is the responsibility of the callback |
1860 | to invoke it (it will continue to be called until the sweep has been done, |
2271 | to invoke it (it will continue to be called until the sweep has been done, |
1861 | if you do not want thta, you need to temporarily stop the embed watcher). |
2272 | if you do not want that, you need to temporarily stop the embed watcher). |
1862 | |
2273 | |
1863 | =item ev_embed_sweep (loop, ev_embed *) |
2274 | =item ev_embed_sweep (loop, ev_embed *) |
1864 | |
2275 | |
1865 | Make a single, non-blocking sweep over the embedded loop. This works |
2276 | Make a single, non-blocking sweep over the embedded loop. This works |
1866 | similarly to C<ev_loop (embedded_loop, EVLOOP_NONBLOCK)>, but in the most |
2277 | similarly to C<ev_loop (embedded_loop, EVLOOP_NONBLOCK)>, but in the most |
1867 | apropriate way for embedded loops. |
2278 | appropriate way for embedded loops. |
1868 | |
2279 | |
1869 | =item struct ev_loop *other [read-only] |
2280 | =item struct ev_loop *other [read-only] |
1870 | |
2281 | |
1871 | The embedded event loop. |
2282 | The embedded event loop. |
1872 | |
2283 | |
1873 | =back |
2284 | =back |
|
|
2285 | |
|
|
2286 | =head3 Examples |
|
|
2287 | |
|
|
2288 | Example: Try to get an embeddable event loop and embed it into the default |
|
|
2289 | event loop. If that is not possible, use the default loop. The default |
|
|
2290 | loop is stored in C<loop_hi>, while the embeddable loop is stored in |
|
|
2291 | C<loop_lo> (which is C<loop_hi> in the case no embeddable loop can be |
|
|
2292 | used). |
|
|
2293 | |
|
|
2294 | struct ev_loop *loop_hi = ev_default_init (0); |
|
|
2295 | struct ev_loop *loop_lo = 0; |
|
|
2296 | struct ev_embed embed; |
|
|
2297 | |
|
|
2298 | // see if there is a chance of getting one that works |
|
|
2299 | // (remember that a flags value of 0 means autodetection) |
|
|
2300 | loop_lo = ev_embeddable_backends () & ev_recommended_backends () |
|
|
2301 | ? ev_loop_new (ev_embeddable_backends () & ev_recommended_backends ()) |
|
|
2302 | : 0; |
|
|
2303 | |
|
|
2304 | // if we got one, then embed it, otherwise default to loop_hi |
|
|
2305 | if (loop_lo) |
|
|
2306 | { |
|
|
2307 | ev_embed_init (&embed, 0, loop_lo); |
|
|
2308 | ev_embed_start (loop_hi, &embed); |
|
|
2309 | } |
|
|
2310 | else |
|
|
2311 | loop_lo = loop_hi; |
|
|
2312 | |
|
|
2313 | Example: Check if kqueue is available but not recommended and create |
|
|
2314 | a kqueue backend for use with sockets (which usually work with any |
|
|
2315 | kqueue implementation). Store the kqueue/socket-only event loop in |
|
|
2316 | C<loop_socket>. (One might optionally use C<EVFLAG_NOENV>, too). |
|
|
2317 | |
|
|
2318 | struct ev_loop *loop = ev_default_init (0); |
|
|
2319 | struct ev_loop *loop_socket = 0; |
|
|
2320 | struct ev_embed embed; |
|
|
2321 | |
|
|
2322 | if (ev_supported_backends () & ~ev_recommended_backends () & EVBACKEND_KQUEUE) |
|
|
2323 | if ((loop_socket = ev_loop_new (EVBACKEND_KQUEUE)) |
|
|
2324 | { |
|
|
2325 | ev_embed_init (&embed, 0, loop_socket); |
|
|
2326 | ev_embed_start (loop, &embed); |
|
|
2327 | } |
|
|
2328 | |
|
|
2329 | if (!loop_socket) |
|
|
2330 | loop_socket = loop; |
|
|
2331 | |
|
|
2332 | // now use loop_socket for all sockets, and loop for everything else |
1874 | |
2333 | |
1875 | |
2334 | |
1876 | =head2 C<ev_fork> - the audacity to resume the event loop after a fork |
2335 | =head2 C<ev_fork> - the audacity to resume the event loop after a fork |
1877 | |
2336 | |
1878 | Fork watchers are called when a C<fork ()> was detected (usually because |
2337 | Fork watchers are called when a C<fork ()> was detected (usually because |
… | |
… | |
1894 | believe me. |
2353 | believe me. |
1895 | |
2354 | |
1896 | =back |
2355 | =back |
1897 | |
2356 | |
1898 | |
2357 | |
|
|
2358 | =head2 C<ev_async> - how to wake up another event loop |
|
|
2359 | |
|
|
2360 | In general, you cannot use an C<ev_loop> from multiple threads or other |
|
|
2361 | asynchronous sources such as signal handlers (as opposed to multiple event |
|
|
2362 | loops - those are of course safe to use in different threads). |
|
|
2363 | |
|
|
2364 | Sometimes, however, you need to wake up another event loop you do not |
|
|
2365 | control, for example because it belongs to another thread. This is what |
|
|
2366 | C<ev_async> watchers do: as long as the C<ev_async> watcher is active, you |
|
|
2367 | can signal it by calling C<ev_async_send>, which is thread- and signal |
|
|
2368 | safe. |
|
|
2369 | |
|
|
2370 | This functionality is very similar to C<ev_signal> watchers, as signals, |
|
|
2371 | too, are asynchronous in nature, and signals, too, will be compressed |
|
|
2372 | (i.e. the number of callback invocations may be less than the number of |
|
|
2373 | C<ev_async_sent> calls). |
|
|
2374 | |
|
|
2375 | Unlike C<ev_signal> watchers, C<ev_async> works with any event loop, not |
|
|
2376 | just the default loop. |
|
|
2377 | |
|
|
2378 | =head3 Queueing |
|
|
2379 | |
|
|
2380 | C<ev_async> does not support queueing of data in any way. The reason |
|
|
2381 | is that the author does not know of a simple (or any) algorithm for a |
|
|
2382 | multiple-writer-single-reader queue that works in all cases and doesn't |
|
|
2383 | need elaborate support such as pthreads. |
|
|
2384 | |
|
|
2385 | That means that if you want to queue data, you have to provide your own |
|
|
2386 | queue. But at least I can tell you how to implement locking around your |
|
|
2387 | queue: |
|
|
2388 | |
|
|
2389 | =over 4 |
|
|
2390 | |
|
|
2391 | =item queueing from a signal handler context |
|
|
2392 | |
|
|
2393 | To implement race-free queueing, you simply add to the queue in the signal |
|
|
2394 | handler but you block the signal handler in the watcher callback. Here is |
|
|
2395 | an example that does that for some fictitious SIGUSR1 handler: |
|
|
2396 | |
|
|
2397 | static ev_async mysig; |
|
|
2398 | |
|
|
2399 | static void |
|
|
2400 | sigusr1_handler (void) |
|
|
2401 | { |
|
|
2402 | sometype data; |
|
|
2403 | |
|
|
2404 | // no locking etc. |
|
|
2405 | queue_put (data); |
|
|
2406 | ev_async_send (EV_DEFAULT_ &mysig); |
|
|
2407 | } |
|
|
2408 | |
|
|
2409 | static void |
|
|
2410 | mysig_cb (EV_P_ ev_async *w, int revents) |
|
|
2411 | { |
|
|
2412 | sometype data; |
|
|
2413 | sigset_t block, prev; |
|
|
2414 | |
|
|
2415 | sigemptyset (&block); |
|
|
2416 | sigaddset (&block, SIGUSR1); |
|
|
2417 | sigprocmask (SIG_BLOCK, &block, &prev); |
|
|
2418 | |
|
|
2419 | while (queue_get (&data)) |
|
|
2420 | process (data); |
|
|
2421 | |
|
|
2422 | if (sigismember (&prev, SIGUSR1) |
|
|
2423 | sigprocmask (SIG_UNBLOCK, &block, 0); |
|
|
2424 | } |
|
|
2425 | |
|
|
2426 | (Note: pthreads in theory requires you to use C<pthread_setmask> |
|
|
2427 | instead of C<sigprocmask> when you use threads, but libev doesn't do it |
|
|
2428 | either...). |
|
|
2429 | |
|
|
2430 | =item queueing from a thread context |
|
|
2431 | |
|
|
2432 | The strategy for threads is different, as you cannot (easily) block |
|
|
2433 | threads but you can easily preempt them, so to queue safely you need to |
|
|
2434 | employ a traditional mutex lock, such as in this pthread example: |
|
|
2435 | |
|
|
2436 | static ev_async mysig; |
|
|
2437 | static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; |
|
|
2438 | |
|
|
2439 | static void |
|
|
2440 | otherthread (void) |
|
|
2441 | { |
|
|
2442 | // only need to lock the actual queueing operation |
|
|
2443 | pthread_mutex_lock (&mymutex); |
|
|
2444 | queue_put (data); |
|
|
2445 | pthread_mutex_unlock (&mymutex); |
|
|
2446 | |
|
|
2447 | ev_async_send (EV_DEFAULT_ &mysig); |
|
|
2448 | } |
|
|
2449 | |
|
|
2450 | static void |
|
|
2451 | mysig_cb (EV_P_ ev_async *w, int revents) |
|
|
2452 | { |
|
|
2453 | pthread_mutex_lock (&mymutex); |
|
|
2454 | |
|
|
2455 | while (queue_get (&data)) |
|
|
2456 | process (data); |
|
|
2457 | |
|
|
2458 | pthread_mutex_unlock (&mymutex); |
|
|
2459 | } |
|
|
2460 | |
|
|
2461 | =back |
|
|
2462 | |
|
|
2463 | |
|
|
2464 | =head3 Watcher-Specific Functions and Data Members |
|
|
2465 | |
|
|
2466 | =over 4 |
|
|
2467 | |
|
|
2468 | =item ev_async_init (ev_async *, callback) |
|
|
2469 | |
|
|
2470 | Initialises and configures the async watcher - it has no parameters of any |
|
|
2471 | kind. There is a C<ev_asynd_set> macro, but using it is utterly pointless, |
|
|
2472 | trust me. |
|
|
2473 | |
|
|
2474 | =item ev_async_send (loop, ev_async *) |
|
|
2475 | |
|
|
2476 | Sends/signals/activates the given C<ev_async> watcher, that is, feeds |
|
|
2477 | an C<EV_ASYNC> event on the watcher into the event loop. Unlike |
|
|
2478 | C<ev_feed_event>, this call is safe to do from other threads, signal or |
|
|
2479 | similar contexts (see the discussion of C<EV_ATOMIC_T> in the embedding |
|
|
2480 | section below on what exactly this means). |
|
|
2481 | |
|
|
2482 | This call incurs the overhead of a system call only once per loop iteration, |
|
|
2483 | so while the overhead might be noticeable, it doesn't apply to repeated |
|
|
2484 | calls to C<ev_async_send>. |
|
|
2485 | |
|
|
2486 | =item bool = ev_async_pending (ev_async *) |
|
|
2487 | |
|
|
2488 | Returns a non-zero value when C<ev_async_send> has been called on the |
|
|
2489 | watcher but the event has not yet been processed (or even noted) by the |
|
|
2490 | event loop. |
|
|
2491 | |
|
|
2492 | C<ev_async_send> sets a flag in the watcher and wakes up the loop. When |
|
|
2493 | the loop iterates next and checks for the watcher to have become active, |
|
|
2494 | it will reset the flag again. C<ev_async_pending> can be used to very |
|
|
2495 | quickly check whether invoking the loop might be a good idea. |
|
|
2496 | |
|
|
2497 | Not that this does I<not> check whether the watcher itself is pending, only |
|
|
2498 | whether it has been requested to make this watcher pending. |
|
|
2499 | |
|
|
2500 | =back |
|
|
2501 | |
|
|
2502 | |
1899 | =head1 OTHER FUNCTIONS |
2503 | =head1 OTHER FUNCTIONS |
1900 | |
2504 | |
1901 | There are some other functions of possible interest. Described. Here. Now. |
2505 | There are some other functions of possible interest. Described. Here. Now. |
1902 | |
2506 | |
1903 | =over 4 |
2507 | =over 4 |
1904 | |
2508 | |
1905 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
2509 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
1906 | |
2510 | |
1907 | This function combines a simple timer and an I/O watcher, calls your |
2511 | This function combines a simple timer and an I/O watcher, calls your |
1908 | callback on whichever event happens first and automatically stop both |
2512 | callback on whichever event happens first and automatically stops both |
1909 | watchers. This is useful if you want to wait for a single event on an fd |
2513 | watchers. This is useful if you want to wait for a single event on an fd |
1910 | or timeout without having to allocate/configure/start/stop/free one or |
2514 | or timeout without having to allocate/configure/start/stop/free one or |
1911 | more watchers yourself. |
2515 | more watchers yourself. |
1912 | |
2516 | |
1913 | If C<fd> is less than 0, then no I/O watcher will be started and events |
2517 | If C<fd> is less than 0, then no I/O watcher will be started and the |
1914 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
2518 | C<events> argument is being ignored. Otherwise, an C<ev_io> watcher for |
1915 | C<events> set will be craeted and started. |
2519 | the given C<fd> and C<events> set will be created and started. |
1916 | |
2520 | |
1917 | If C<timeout> is less than 0, then no timeout watcher will be |
2521 | If C<timeout> is less than 0, then no timeout watcher will be |
1918 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
2522 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
1919 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
2523 | repeat = 0) will be started. C<0> is a valid timeout. |
1920 | dubious value. |
|
|
1921 | |
2524 | |
1922 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
2525 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
1923 | passed an C<revents> set like normal event callbacks (a combination of |
2526 | passed an C<revents> set like normal event callbacks (a combination of |
1924 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
2527 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
1925 | value passed to C<ev_once>: |
2528 | value passed to C<ev_once>. Note that it is possible to receive I<both> |
|
|
2529 | a timeout and an io event at the same time - you probably should give io |
|
|
2530 | events precedence. |
1926 | |
2531 | |
|
|
2532 | Example: wait up to ten seconds for data to appear on STDIN_FILENO. |
|
|
2533 | |
1927 | static void stdin_ready (int revents, void *arg) |
2534 | static void stdin_ready (int revents, void *arg) |
1928 | { |
2535 | { |
1929 | if (revents & EV_TIMEOUT) |
|
|
1930 | /* doh, nothing entered */; |
|
|
1931 | else if (revents & EV_READ) |
2536 | if (revents & EV_READ) |
1932 | /* stdin might have data for us, joy! */; |
2537 | /* stdin might have data for us, joy! */; |
|
|
2538 | else if (revents & EV_TIMEOUT) |
|
|
2539 | /* doh, nothing entered */; |
1933 | } |
2540 | } |
1934 | |
2541 | |
1935 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
2542 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
1936 | |
2543 | |
1937 | =item ev_feed_event (ev_loop *, watcher *, int revents) |
2544 | =item ev_feed_event (ev_loop *, watcher *, int revents) |
1938 | |
2545 | |
1939 | Feeds the given event set into the event loop, as if the specified event |
2546 | Feeds the given event set into the event loop, as if the specified event |
1940 | had happened for the specified watcher (which must be a pointer to an |
2547 | had happened for the specified watcher (which must be a pointer to an |
… | |
… | |
1945 | Feed an event on the given fd, as if a file descriptor backend detected |
2552 | Feed an event on the given fd, as if a file descriptor backend detected |
1946 | the given events it. |
2553 | the given events it. |
1947 | |
2554 | |
1948 | =item ev_feed_signal_event (ev_loop *loop, int signum) |
2555 | =item ev_feed_signal_event (ev_loop *loop, int signum) |
1949 | |
2556 | |
1950 | Feed an event as if the given signal occured (C<loop> must be the default |
2557 | Feed an event as if the given signal occurred (C<loop> must be the default |
1951 | loop!). |
2558 | loop!). |
1952 | |
2559 | |
1953 | =back |
2560 | =back |
1954 | |
2561 | |
1955 | |
2562 | |
… | |
… | |
1971 | |
2578 | |
1972 | =item * Priorities are not currently supported. Initialising priorities |
2579 | =item * Priorities are not currently supported. Initialising priorities |
1973 | will fail and all watchers will have the same priority, even though there |
2580 | will fail and all watchers will have the same priority, even though there |
1974 | is an ev_pri field. |
2581 | is an ev_pri field. |
1975 | |
2582 | |
|
|
2583 | =item * In libevent, the last base created gets the signals, in libev, the |
|
|
2584 | first base created (== the default loop) gets the signals. |
|
|
2585 | |
1976 | =item * Other members are not supported. |
2586 | =item * Other members are not supported. |
1977 | |
2587 | |
1978 | =item * The libev emulation is I<not> ABI compatible to libevent, you need |
2588 | =item * The libev emulation is I<not> ABI compatible to libevent, you need |
1979 | to use the libev header file and library. |
2589 | to use the libev header file and library. |
1980 | |
2590 | |
1981 | =back |
2591 | =back |
1982 | |
2592 | |
1983 | =head1 C++ SUPPORT |
2593 | =head1 C++ SUPPORT |
1984 | |
2594 | |
1985 | Libev comes with some simplistic wrapper classes for C++ that mainly allow |
2595 | Libev comes with some simplistic wrapper classes for C++ that mainly allow |
1986 | you to use some convinience methods to start/stop watchers and also change |
2596 | you to use some convenience methods to start/stop watchers and also change |
1987 | the callback model to a model using method callbacks on objects. |
2597 | the callback model to a model using method callbacks on objects. |
1988 | |
2598 | |
1989 | To use it, |
2599 | To use it, |
1990 | |
2600 | |
1991 | #include <ev++.h> |
2601 | #include <ev++.h> |
1992 | |
2602 | |
1993 | This automatically includes F<ev.h> and puts all of its definitions (many |
2603 | This automatically includes F<ev.h> and puts all of its definitions (many |
1994 | of them macros) into the global namespace. All C++ specific things are |
2604 | of them macros) into the global namespace. All C++ specific things are |
1995 | put into the C<ev> namespace. It should support all the same embedding |
2605 | put into the C<ev> namespace. It should support all the same embedding |
1996 | options as F<ev.h>, most notably C<EV_MULTIPLICITY>. |
2606 | options as F<ev.h>, most notably C<EV_MULTIPLICITY>. |
… | |
… | |
2063 | your compiler is good :), then the method will be fully inlined into the |
2673 | your compiler is good :), then the method will be fully inlined into the |
2064 | thunking function, making it as fast as a direct C callback. |
2674 | thunking function, making it as fast as a direct C callback. |
2065 | |
2675 | |
2066 | Example: simple class declaration and watcher initialisation |
2676 | Example: simple class declaration and watcher initialisation |
2067 | |
2677 | |
2068 | struct myclass |
2678 | struct myclass |
2069 | { |
2679 | { |
2070 | void io_cb (ev::io &w, int revents) { } |
2680 | void io_cb (ev::io &w, int revents) { } |
2071 | } |
2681 | } |
2072 | |
2682 | |
2073 | myclass obj; |
2683 | myclass obj; |
2074 | ev::io iow; |
2684 | ev::io iow; |
2075 | iow.set <myclass, &myclass::io_cb> (&obj); |
2685 | iow.set <myclass, &myclass::io_cb> (&obj); |
2076 | |
2686 | |
2077 | =item w->set<function> (void *data = 0) |
2687 | =item w->set<function> (void *data = 0) |
2078 | |
2688 | |
2079 | Also sets a callback, but uses a static method or plain function as |
2689 | Also sets a callback, but uses a static method or plain function as |
2080 | callback. The optional C<data> argument will be stored in the watcher's |
2690 | callback. The optional C<data> argument will be stored in the watcher's |
… | |
… | |
2082 | |
2692 | |
2083 | The prototype of the C<function> must be C<void (*)(ev::TYPE &w, int)>. |
2693 | The prototype of the C<function> must be C<void (*)(ev::TYPE &w, int)>. |
2084 | |
2694 | |
2085 | See the method-C<set> above for more details. |
2695 | See the method-C<set> above for more details. |
2086 | |
2696 | |
2087 | Example: |
2697 | Example: Use a plain function as callback. |
2088 | |
2698 | |
2089 | static void io_cb (ev::io &w, int revents) { } |
2699 | static void io_cb (ev::io &w, int revents) { } |
2090 | iow.set <io_cb> (); |
2700 | iow.set <io_cb> (); |
2091 | |
2701 | |
2092 | =item w->set (struct ev_loop *) |
2702 | =item w->set (struct ev_loop *) |
2093 | |
2703 | |
2094 | Associates a different C<struct ev_loop> with this watcher. You can only |
2704 | Associates a different C<struct ev_loop> with this watcher. You can only |
2095 | do this when the watcher is inactive (and not pending either). |
2705 | do this when the watcher is inactive (and not pending either). |
2096 | |
2706 | |
2097 | =item w->set ([args]) |
2707 | =item w->set ([arguments]) |
2098 | |
2708 | |
2099 | Basically the same as C<ev_TYPE_set>, with the same args. Must be |
2709 | Basically the same as C<ev_TYPE_set>, with the same arguments. Must be |
2100 | called at least once. Unlike the C counterpart, an active watcher gets |
2710 | called at least once. Unlike the C counterpart, an active watcher gets |
2101 | automatically stopped and restarted when reconfiguring it with this |
2711 | automatically stopped and restarted when reconfiguring it with this |
2102 | method. |
2712 | method. |
2103 | |
2713 | |
2104 | =item w->start () |
2714 | =item w->start () |
… | |
… | |
2128 | =back |
2738 | =back |
2129 | |
2739 | |
2130 | Example: Define a class with an IO and idle watcher, start one of them in |
2740 | Example: Define a class with an IO and idle watcher, start one of them in |
2131 | the constructor. |
2741 | the constructor. |
2132 | |
2742 | |
2133 | class myclass |
2743 | class myclass |
2134 | { |
2744 | { |
2135 | ev_io io; void io_cb (ev::io &w, int revents); |
2745 | ev::io io ; void io_cb (ev::io &w, int revents); |
2136 | ev_idle idle void idle_cb (ev::idle &w, int revents); |
2746 | ev::idle idle; void idle_cb (ev::idle &w, int revents); |
2137 | |
2747 | |
2138 | myclass (); |
2748 | myclass (int fd) |
2139 | } |
2749 | { |
2140 | |
|
|
2141 | myclass::myclass (int fd) |
|
|
2142 | { |
|
|
2143 | io .set <myclass, &myclass::io_cb > (this); |
2750 | io .set <myclass, &myclass::io_cb > (this); |
2144 | idle.set <myclass, &myclass::idle_cb> (this); |
2751 | idle.set <myclass, &myclass::idle_cb> (this); |
2145 | |
2752 | |
2146 | io.start (fd, ev::READ); |
2753 | io.start (fd, ev::READ); |
|
|
2754 | } |
2147 | } |
2755 | }; |
|
|
2756 | |
|
|
2757 | |
|
|
2758 | =head1 OTHER LANGUAGE BINDINGS |
|
|
2759 | |
|
|
2760 | Libev does not offer other language bindings itself, but bindings for a |
|
|
2761 | number of languages exist in the form of third-party packages. If you know |
|
|
2762 | any interesting language binding in addition to the ones listed here, drop |
|
|
2763 | me a note. |
|
|
2764 | |
|
|
2765 | =over 4 |
|
|
2766 | |
|
|
2767 | =item Perl |
|
|
2768 | |
|
|
2769 | The EV module implements the full libev API and is actually used to test |
|
|
2770 | libev. EV is developed together with libev. Apart from the EV core module, |
|
|
2771 | there are additional modules that implement libev-compatible interfaces |
|
|
2772 | to C<libadns> (C<EV::ADNS>, but C<AnyEvent::DNS> is preferred nowadays), |
|
|
2773 | C<Net::SNMP> (C<Net::SNMP::EV>) and the C<libglib> event core (C<Glib::EV> |
|
|
2774 | and C<EV::Glib>). |
|
|
2775 | |
|
|
2776 | It can be found and installed via CPAN, its homepage is at |
|
|
2777 | L<http://software.schmorp.de/pkg/EV>. |
|
|
2778 | |
|
|
2779 | =item Python |
|
|
2780 | |
|
|
2781 | Python bindings can be found at L<http://code.google.com/p/pyev/>. It |
|
|
2782 | seems to be quite complete and well-documented. Note, however, that the |
|
|
2783 | patch they require for libev is outright dangerous as it breaks the ABI |
|
|
2784 | for everybody else, and therefore, should never be applied in an installed |
|
|
2785 | libev (if python requires an incompatible ABI then it needs to embed |
|
|
2786 | libev). |
|
|
2787 | |
|
|
2788 | =item Ruby |
|
|
2789 | |
|
|
2790 | Tony Arcieri has written a ruby extension that offers access to a subset |
|
|
2791 | of the libev API and adds file handle abstractions, asynchronous DNS and |
|
|
2792 | more on top of it. It can be found via gem servers. Its homepage is at |
|
|
2793 | L<http://rev.rubyforge.org/>. |
|
|
2794 | |
|
|
2795 | =item D |
|
|
2796 | |
|
|
2797 | Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to |
|
|
2798 | be found at L<http://proj.llucax.com.ar/wiki/evd>. |
|
|
2799 | |
|
|
2800 | =back |
2148 | |
2801 | |
2149 | |
2802 | |
2150 | =head1 MACRO MAGIC |
2803 | =head1 MACRO MAGIC |
2151 | |
2804 | |
2152 | Libev can be compiled with a variety of options, the most fundamantal |
2805 | Libev can be compiled with a variety of options, the most fundamental |
2153 | of which is C<EV_MULTIPLICITY>. This option determines whether (most) |
2806 | of which is C<EV_MULTIPLICITY>. This option determines whether (most) |
2154 | functions and callbacks have an initial C<struct ev_loop *> argument. |
2807 | functions and callbacks have an initial C<struct ev_loop *> argument. |
2155 | |
2808 | |
2156 | To make it easier to write programs that cope with either variant, the |
2809 | To make it easier to write programs that cope with either variant, the |
2157 | following macros are defined: |
2810 | following macros are defined: |
… | |
… | |
2162 | |
2815 | |
2163 | This provides the loop I<argument> for functions, if one is required ("ev |
2816 | This provides the loop I<argument> for functions, if one is required ("ev |
2164 | loop argument"). The C<EV_A> form is used when this is the sole argument, |
2817 | loop argument"). The C<EV_A> form is used when this is the sole argument, |
2165 | C<EV_A_> is used when other arguments are following. Example: |
2818 | C<EV_A_> is used when other arguments are following. Example: |
2166 | |
2819 | |
2167 | ev_unref (EV_A); |
2820 | ev_unref (EV_A); |
2168 | ev_timer_add (EV_A_ watcher); |
2821 | ev_timer_add (EV_A_ watcher); |
2169 | ev_loop (EV_A_ 0); |
2822 | ev_loop (EV_A_ 0); |
2170 | |
2823 | |
2171 | It assumes the variable C<loop> of type C<struct ev_loop *> is in scope, |
2824 | It assumes the variable C<loop> of type C<struct ev_loop *> is in scope, |
2172 | which is often provided by the following macro. |
2825 | which is often provided by the following macro. |
2173 | |
2826 | |
2174 | =item C<EV_P>, C<EV_P_> |
2827 | =item C<EV_P>, C<EV_P_> |
2175 | |
2828 | |
2176 | This provides the loop I<parameter> for functions, if one is required ("ev |
2829 | This provides the loop I<parameter> for functions, if one is required ("ev |
2177 | loop parameter"). The C<EV_P> form is used when this is the sole parameter, |
2830 | loop parameter"). The C<EV_P> form is used when this is the sole parameter, |
2178 | C<EV_P_> is used when other parameters are following. Example: |
2831 | C<EV_P_> is used when other parameters are following. Example: |
2179 | |
2832 | |
2180 | // this is how ev_unref is being declared |
2833 | // this is how ev_unref is being declared |
2181 | static void ev_unref (EV_P); |
2834 | static void ev_unref (EV_P); |
2182 | |
2835 | |
2183 | // this is how you can declare your typical callback |
2836 | // this is how you can declare your typical callback |
2184 | static void cb (EV_P_ ev_timer *w, int revents) |
2837 | static void cb (EV_P_ ev_timer *w, int revents) |
2185 | |
2838 | |
2186 | It declares a parameter C<loop> of type C<struct ev_loop *>, quite |
2839 | It declares a parameter C<loop> of type C<struct ev_loop *>, quite |
2187 | suitable for use with C<EV_A>. |
2840 | suitable for use with C<EV_A>. |
2188 | |
2841 | |
2189 | =item C<EV_DEFAULT>, C<EV_DEFAULT_> |
2842 | =item C<EV_DEFAULT>, C<EV_DEFAULT_> |
2190 | |
2843 | |
2191 | Similar to the other two macros, this gives you the value of the default |
2844 | Similar to the other two macros, this gives you the value of the default |
2192 | loop, if multiple loops are supported ("ev loop default"). |
2845 | loop, if multiple loops are supported ("ev loop default"). |
|
|
2846 | |
|
|
2847 | =item C<EV_DEFAULT_UC>, C<EV_DEFAULT_UC_> |
|
|
2848 | |
|
|
2849 | Usage identical to C<EV_DEFAULT> and C<EV_DEFAULT_>, but requires that the |
|
|
2850 | default loop has been initialised (C<UC> == unchecked). Their behaviour |
|
|
2851 | is undefined when the default loop has not been initialised by a previous |
|
|
2852 | execution of C<EV_DEFAULT>, C<EV_DEFAULT_> or C<ev_default_init (...)>. |
|
|
2853 | |
|
|
2854 | It is often prudent to use C<EV_DEFAULT> when initialising the first |
|
|
2855 | watcher in a function but use C<EV_DEFAULT_UC> afterwards. |
2193 | |
2856 | |
2194 | =back |
2857 | =back |
2195 | |
2858 | |
2196 | Example: Declare and initialise a check watcher, utilising the above |
2859 | Example: Declare and initialise a check watcher, utilising the above |
2197 | macros so it will work regardless of whether multiple loops are supported |
2860 | macros so it will work regardless of whether multiple loops are supported |
2198 | or not. |
2861 | or not. |
2199 | |
2862 | |
2200 | static void |
2863 | static void |
2201 | check_cb (EV_P_ ev_timer *w, int revents) |
2864 | check_cb (EV_P_ ev_timer *w, int revents) |
2202 | { |
2865 | { |
2203 | ev_check_stop (EV_A_ w); |
2866 | ev_check_stop (EV_A_ w); |
2204 | } |
2867 | } |
2205 | |
2868 | |
2206 | ev_check check; |
2869 | ev_check check; |
2207 | ev_check_init (&check, check_cb); |
2870 | ev_check_init (&check, check_cb); |
2208 | ev_check_start (EV_DEFAULT_ &check); |
2871 | ev_check_start (EV_DEFAULT_ &check); |
2209 | ev_loop (EV_DEFAULT_ 0); |
2872 | ev_loop (EV_DEFAULT_ 0); |
2210 | |
2873 | |
2211 | =head1 EMBEDDING |
2874 | =head1 EMBEDDING |
2212 | |
2875 | |
2213 | Libev can (and often is) directly embedded into host |
2876 | Libev can (and often is) directly embedded into host |
2214 | applications. Examples of applications that embed it include the Deliantra |
2877 | applications. Examples of applications that embed it include the Deliantra |
… | |
… | |
2221 | libev somewhere in your source tree). |
2884 | libev somewhere in your source tree). |
2222 | |
2885 | |
2223 | =head2 FILESETS |
2886 | =head2 FILESETS |
2224 | |
2887 | |
2225 | Depending on what features you need you need to include one or more sets of files |
2888 | Depending on what features you need you need to include one or more sets of files |
2226 | in your app. |
2889 | in your application. |
2227 | |
2890 | |
2228 | =head3 CORE EVENT LOOP |
2891 | =head3 CORE EVENT LOOP |
2229 | |
2892 | |
2230 | To include only the libev core (all the C<ev_*> functions), with manual |
2893 | To include only the libev core (all the C<ev_*> functions), with manual |
2231 | configuration (no autoconf): |
2894 | configuration (no autoconf): |
2232 | |
2895 | |
2233 | #define EV_STANDALONE 1 |
2896 | #define EV_STANDALONE 1 |
2234 | #include "ev.c" |
2897 | #include "ev.c" |
2235 | |
2898 | |
2236 | This will automatically include F<ev.h>, too, and should be done in a |
2899 | This will automatically include F<ev.h>, too, and should be done in a |
2237 | single C source file only to provide the function implementations. To use |
2900 | single C source file only to provide the function implementations. To use |
2238 | it, do the same for F<ev.h> in all files wishing to use this API (best |
2901 | it, do the same for F<ev.h> in all files wishing to use this API (best |
2239 | done by writing a wrapper around F<ev.h> that you can include instead and |
2902 | done by writing a wrapper around F<ev.h> that you can include instead and |
2240 | where you can put other configuration options): |
2903 | where you can put other configuration options): |
2241 | |
2904 | |
2242 | #define EV_STANDALONE 1 |
2905 | #define EV_STANDALONE 1 |
2243 | #include "ev.h" |
2906 | #include "ev.h" |
2244 | |
2907 | |
2245 | Both header files and implementation files can be compiled with a C++ |
2908 | Both header files and implementation files can be compiled with a C++ |
2246 | compiler (at least, thats a stated goal, and breakage will be treated |
2909 | compiler (at least, thats a stated goal, and breakage will be treated |
2247 | as a bug). |
2910 | as a bug). |
2248 | |
2911 | |
2249 | You need the following files in your source tree, or in a directory |
2912 | You need the following files in your source tree, or in a directory |
2250 | in your include path (e.g. in libev/ when using -Ilibev): |
2913 | in your include path (e.g. in libev/ when using -Ilibev): |
2251 | |
2914 | |
2252 | ev.h |
2915 | ev.h |
2253 | ev.c |
2916 | ev.c |
2254 | ev_vars.h |
2917 | ev_vars.h |
2255 | ev_wrap.h |
2918 | ev_wrap.h |
2256 | |
2919 | |
2257 | ev_win32.c required on win32 platforms only |
2920 | ev_win32.c required on win32 platforms only |
2258 | |
2921 | |
2259 | ev_select.c only when select backend is enabled (which is enabled by default) |
2922 | ev_select.c only when select backend is enabled (which is enabled by default) |
2260 | ev_poll.c only when poll backend is enabled (disabled by default) |
2923 | ev_poll.c only when poll backend is enabled (disabled by default) |
2261 | ev_epoll.c only when the epoll backend is enabled (disabled by default) |
2924 | ev_epoll.c only when the epoll backend is enabled (disabled by default) |
2262 | ev_kqueue.c only when the kqueue backend is enabled (disabled by default) |
2925 | ev_kqueue.c only when the kqueue backend is enabled (disabled by default) |
2263 | ev_port.c only when the solaris port backend is enabled (disabled by default) |
2926 | ev_port.c only when the solaris port backend is enabled (disabled by default) |
2264 | |
2927 | |
2265 | F<ev.c> includes the backend files directly when enabled, so you only need |
2928 | F<ev.c> includes the backend files directly when enabled, so you only need |
2266 | to compile this single file. |
2929 | to compile this single file. |
2267 | |
2930 | |
2268 | =head3 LIBEVENT COMPATIBILITY API |
2931 | =head3 LIBEVENT COMPATIBILITY API |
2269 | |
2932 | |
2270 | To include the libevent compatibility API, also include: |
2933 | To include the libevent compatibility API, also include: |
2271 | |
2934 | |
2272 | #include "event.c" |
2935 | #include "event.c" |
2273 | |
2936 | |
2274 | in the file including F<ev.c>, and: |
2937 | in the file including F<ev.c>, and: |
2275 | |
2938 | |
2276 | #include "event.h" |
2939 | #include "event.h" |
2277 | |
2940 | |
2278 | in the files that want to use the libevent API. This also includes F<ev.h>. |
2941 | in the files that want to use the libevent API. This also includes F<ev.h>. |
2279 | |
2942 | |
2280 | You need the following additional files for this: |
2943 | You need the following additional files for this: |
2281 | |
2944 | |
2282 | event.h |
2945 | event.h |
2283 | event.c |
2946 | event.c |
2284 | |
2947 | |
2285 | =head3 AUTOCONF SUPPORT |
2948 | =head3 AUTOCONF SUPPORT |
2286 | |
2949 | |
2287 | Instead of using C<EV_STANDALONE=1> and providing your config in |
2950 | Instead of using C<EV_STANDALONE=1> and providing your configuration in |
2288 | whatever way you want, you can also C<m4_include([libev.m4])> in your |
2951 | whatever way you want, you can also C<m4_include([libev.m4])> in your |
2289 | F<configure.ac> and leave C<EV_STANDALONE> undefined. F<ev.c> will then |
2952 | F<configure.ac> and leave C<EV_STANDALONE> undefined. F<ev.c> will then |
2290 | include F<config.h> and configure itself accordingly. |
2953 | include F<config.h> and configure itself accordingly. |
2291 | |
2954 | |
2292 | For this of course you need the m4 file: |
2955 | For this of course you need the m4 file: |
2293 | |
2956 | |
2294 | libev.m4 |
2957 | libev.m4 |
2295 | |
2958 | |
2296 | =head2 PREPROCESSOR SYMBOLS/MACROS |
2959 | =head2 PREPROCESSOR SYMBOLS/MACROS |
2297 | |
2960 | |
2298 | Libev can be configured via a variety of preprocessor symbols you have to define |
2961 | Libev can be configured via a variety of preprocessor symbols you have to |
2299 | before including any of its files. The default is not to build for multiplicity |
2962 | define before including any of its files. The default in the absence of |
2300 | and only include the select backend. |
2963 | autoconf is documented for every option. |
2301 | |
2964 | |
2302 | =over 4 |
2965 | =over 4 |
2303 | |
2966 | |
2304 | =item EV_STANDALONE |
2967 | =item EV_STANDALONE |
2305 | |
2968 | |
… | |
… | |
2310 | F<event.h> that are not directly supported by the libev core alone. |
2973 | F<event.h> that are not directly supported by the libev core alone. |
2311 | |
2974 | |
2312 | =item EV_USE_MONOTONIC |
2975 | =item EV_USE_MONOTONIC |
2313 | |
2976 | |
2314 | If defined to be C<1>, libev will try to detect the availability of the |
2977 | If defined to be C<1>, libev will try to detect the availability of the |
2315 | monotonic clock option at both compiletime and runtime. Otherwise no use |
2978 | monotonic clock option at both compile time and runtime. Otherwise no use |
2316 | of the monotonic clock option will be attempted. If you enable this, you |
2979 | of the monotonic clock option will be attempted. If you enable this, you |
2317 | usually have to link against librt or something similar. Enabling it when |
2980 | usually have to link against librt or something similar. Enabling it when |
2318 | the functionality isn't available is safe, though, although you have |
2981 | the functionality isn't available is safe, though, although you have |
2319 | to make sure you link against any libraries where the C<clock_gettime> |
2982 | to make sure you link against any libraries where the C<clock_gettime> |
2320 | function is hiding in (often F<-lrt>). |
2983 | function is hiding in (often F<-lrt>). |
2321 | |
2984 | |
2322 | =item EV_USE_REALTIME |
2985 | =item EV_USE_REALTIME |
2323 | |
2986 | |
2324 | If defined to be C<1>, libev will try to detect the availability of the |
2987 | If defined to be C<1>, libev will try to detect the availability of the |
2325 | realtime clock option at compiletime (and assume its availability at |
2988 | real-time clock option at compile time (and assume its availability at |
2326 | runtime if successful). Otherwise no use of the realtime clock option will |
2989 | runtime if successful). Otherwise no use of the real-time clock option will |
2327 | be attempted. This effectively replaces C<gettimeofday> by C<clock_get |
2990 | be attempted. This effectively replaces C<gettimeofday> by C<clock_get |
2328 | (CLOCK_REALTIME, ...)> and will not normally affect correctness. See the |
2991 | (CLOCK_REALTIME, ...)> and will not normally affect correctness. See the |
2329 | note about libraries in the description of C<EV_USE_MONOTONIC>, though. |
2992 | note about libraries in the description of C<EV_USE_MONOTONIC>, though. |
2330 | |
2993 | |
2331 | =item EV_USE_NANOSLEEP |
2994 | =item EV_USE_NANOSLEEP |
2332 | |
2995 | |
2333 | If defined to be C<1>, libev will assume that C<nanosleep ()> is available |
2996 | If defined to be C<1>, libev will assume that C<nanosleep ()> is available |
2334 | and will use it for delays. Otherwise it will use C<select ()>. |
2997 | and will use it for delays. Otherwise it will use C<select ()>. |
2335 | |
2998 | |
|
|
2999 | =item EV_USE_EVENTFD |
|
|
3000 | |
|
|
3001 | If defined to be C<1>, then libev will assume that C<eventfd ()> is |
|
|
3002 | available and will probe for kernel support at runtime. This will improve |
|
|
3003 | C<ev_signal> and C<ev_async> performance and reduce resource consumption. |
|
|
3004 | If undefined, it will be enabled if the headers indicate GNU/Linux + Glibc |
|
|
3005 | 2.7 or newer, otherwise disabled. |
|
|
3006 | |
2336 | =item EV_USE_SELECT |
3007 | =item EV_USE_SELECT |
2337 | |
3008 | |
2338 | If undefined or defined to be C<1>, libev will compile in support for the |
3009 | If undefined or defined to be C<1>, libev will compile in support for the |
2339 | C<select>(2) backend. No attempt at autodetection will be done: if no |
3010 | C<select>(2) backend. No attempt at auto-detection will be done: if no |
2340 | other method takes over, select will be it. Otherwise the select backend |
3011 | other method takes over, select will be it. Otherwise the select backend |
2341 | will not be compiled in. |
3012 | will not be compiled in. |
2342 | |
3013 | |
2343 | =item EV_SELECT_USE_FD_SET |
3014 | =item EV_SELECT_USE_FD_SET |
2344 | |
3015 | |
2345 | If defined to C<1>, then the select backend will use the system C<fd_set> |
3016 | If defined to C<1>, then the select backend will use the system C<fd_set> |
2346 | structure. This is useful if libev doesn't compile due to a missing |
3017 | structure. This is useful if libev doesn't compile due to a missing |
2347 | C<NFDBITS> or C<fd_mask> definition or it misguesses the bitset layout on |
3018 | C<NFDBITS> or C<fd_mask> definition or it mis-guesses the bitset layout on |
2348 | exotic systems. This usually limits the range of file descriptors to some |
3019 | exotic systems. This usually limits the range of file descriptors to some |
2349 | low limit such as 1024 or might have other limitations (winsocket only |
3020 | low limit such as 1024 or might have other limitations (winsocket only |
2350 | allows 64 sockets). The C<FD_SETSIZE> macro, set before compilation, might |
3021 | allows 64 sockets). The C<FD_SETSIZE> macro, set before compilation, might |
2351 | influence the size of the C<fd_set> used. |
3022 | influence the size of the C<fd_set> used. |
2352 | |
3023 | |
… | |
… | |
2358 | be used is the winsock select). This means that it will call |
3029 | be used is the winsock select). This means that it will call |
2359 | C<_get_osfhandle> on the fd to convert it to an OS handle. Otherwise, |
3030 | C<_get_osfhandle> on the fd to convert it to an OS handle. Otherwise, |
2360 | it is assumed that all these functions actually work on fds, even |
3031 | it is assumed that all these functions actually work on fds, even |
2361 | on win32. Should not be defined on non-win32 platforms. |
3032 | on win32. Should not be defined on non-win32 platforms. |
2362 | |
3033 | |
|
|
3034 | =item EV_FD_TO_WIN32_HANDLE |
|
|
3035 | |
|
|
3036 | If C<EV_SELECT_IS_WINSOCKET> is enabled, then libev needs a way to map |
|
|
3037 | file descriptors to socket handles. When not defining this symbol (the |
|
|
3038 | default), then libev will call C<_get_osfhandle>, which is usually |
|
|
3039 | correct. In some cases, programs use their own file descriptor management, |
|
|
3040 | in which case they can provide this function to map fds to socket handles. |
|
|
3041 | |
2363 | =item EV_USE_POLL |
3042 | =item EV_USE_POLL |
2364 | |
3043 | |
2365 | If defined to be C<1>, libev will compile in support for the C<poll>(2) |
3044 | If defined to be C<1>, libev will compile in support for the C<poll>(2) |
2366 | backend. Otherwise it will be enabled on non-win32 platforms. It |
3045 | backend. Otherwise it will be enabled on non-win32 platforms. It |
2367 | takes precedence over select. |
3046 | takes precedence over select. |
2368 | |
3047 | |
2369 | =item EV_USE_EPOLL |
3048 | =item EV_USE_EPOLL |
2370 | |
3049 | |
2371 | If defined to be C<1>, libev will compile in support for the Linux |
3050 | If defined to be C<1>, libev will compile in support for the Linux |
2372 | C<epoll>(7) backend. Its availability will be detected at runtime, |
3051 | C<epoll>(7) backend. Its availability will be detected at runtime, |
2373 | otherwise another method will be used as fallback. This is the |
3052 | otherwise another method will be used as fallback. This is the preferred |
2374 | preferred backend for GNU/Linux systems. |
3053 | backend for GNU/Linux systems. If undefined, it will be enabled if the |
|
|
3054 | headers indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
2375 | |
3055 | |
2376 | =item EV_USE_KQUEUE |
3056 | =item EV_USE_KQUEUE |
2377 | |
3057 | |
2378 | If defined to be C<1>, libev will compile in support for the BSD style |
3058 | If defined to be C<1>, libev will compile in support for the BSD style |
2379 | C<kqueue>(2) backend. Its actual availability will be detected at runtime, |
3059 | C<kqueue>(2) backend. Its actual availability will be detected at runtime, |
… | |
… | |
2392 | otherwise another method will be used as fallback. This is the preferred |
3072 | otherwise another method will be used as fallback. This is the preferred |
2393 | backend for Solaris 10 systems. |
3073 | backend for Solaris 10 systems. |
2394 | |
3074 | |
2395 | =item EV_USE_DEVPOLL |
3075 | =item EV_USE_DEVPOLL |
2396 | |
3076 | |
2397 | reserved for future expansion, works like the USE symbols above. |
3077 | Reserved for future expansion, works like the USE symbols above. |
2398 | |
3078 | |
2399 | =item EV_USE_INOTIFY |
3079 | =item EV_USE_INOTIFY |
2400 | |
3080 | |
2401 | If defined to be C<1>, libev will compile in support for the Linux inotify |
3081 | If defined to be C<1>, libev will compile in support for the Linux inotify |
2402 | interface to speed up C<ev_stat> watchers. Its actual availability will |
3082 | interface to speed up C<ev_stat> watchers. Its actual availability will |
2403 | be detected at runtime. |
3083 | be detected at runtime. If undefined, it will be enabled if the headers |
|
|
3084 | indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
|
|
3085 | |
|
|
3086 | =item EV_ATOMIC_T |
|
|
3087 | |
|
|
3088 | Libev requires an integer type (suitable for storing C<0> or C<1>) whose |
|
|
3089 | access is atomic with respect to other threads or signal contexts. No such |
|
|
3090 | type is easily found in the C language, so you can provide your own type |
|
|
3091 | that you know is safe for your purposes. It is used both for signal handler "locking" |
|
|
3092 | as well as for signal and thread safety in C<ev_async> watchers. |
|
|
3093 | |
|
|
3094 | In the absence of this define, libev will use C<sig_atomic_t volatile> |
|
|
3095 | (from F<signal.h>), which is usually good enough on most platforms. |
2404 | |
3096 | |
2405 | =item EV_H |
3097 | =item EV_H |
2406 | |
3098 | |
2407 | The name of the F<ev.h> header file used to include it. The default if |
3099 | The name of the F<ev.h> header file used to include it. The default if |
2408 | undefined is C<< <ev.h> >> in F<event.h> and C<"ev.h"> in F<ev.c>. This |
3100 | undefined is C<"ev.h"> in F<event.h>, F<ev.c> and F<ev++.h>. This can be |
2409 | can be used to virtually rename the F<ev.h> header file in case of conflicts. |
3101 | used to virtually rename the F<ev.h> header file in case of conflicts. |
2410 | |
3102 | |
2411 | =item EV_CONFIG_H |
3103 | =item EV_CONFIG_H |
2412 | |
3104 | |
2413 | If C<EV_STANDALONE> isn't C<1>, this variable can be used to override |
3105 | If C<EV_STANDALONE> isn't C<1>, this variable can be used to override |
2414 | F<ev.c>'s idea of where to find the F<config.h> file, similarly to |
3106 | F<ev.c>'s idea of where to find the F<config.h> file, similarly to |
2415 | C<EV_H>, above. |
3107 | C<EV_H>, above. |
2416 | |
3108 | |
2417 | =item EV_EVENT_H |
3109 | =item EV_EVENT_H |
2418 | |
3110 | |
2419 | Similarly to C<EV_H>, this macro can be used to override F<event.c>'s idea |
3111 | Similarly to C<EV_H>, this macro can be used to override F<event.c>'s idea |
2420 | of how the F<event.h> header can be found. |
3112 | of how the F<event.h> header can be found, the default is C<"event.h">. |
2421 | |
3113 | |
2422 | =item EV_PROTOTYPES |
3114 | =item EV_PROTOTYPES |
2423 | |
3115 | |
2424 | If defined to be C<0>, then F<ev.h> will not define any function |
3116 | If defined to be C<0>, then F<ev.h> will not define any function |
2425 | prototypes, but still define all the structs and other symbols. This is |
3117 | prototypes, but still define all the structs and other symbols. This is |
… | |
… | |
2446 | When doing priority-based operations, libev usually has to linearly search |
3138 | When doing priority-based operations, libev usually has to linearly search |
2447 | all the priorities, so having many of them (hundreds) uses a lot of space |
3139 | all the priorities, so having many of them (hundreds) uses a lot of space |
2448 | and time, so using the defaults of five priorities (-2 .. +2) is usually |
3140 | and time, so using the defaults of five priorities (-2 .. +2) is usually |
2449 | fine. |
3141 | fine. |
2450 | |
3142 | |
2451 | If your embedding app does not need any priorities, defining these both to |
3143 | If your embedding application does not need any priorities, defining these |
2452 | C<0> will save some memory and cpu. |
3144 | both to C<0> will save some memory and CPU. |
2453 | |
3145 | |
2454 | =item EV_PERIODIC_ENABLE |
3146 | =item EV_PERIODIC_ENABLE |
2455 | |
3147 | |
2456 | If undefined or defined to be C<1>, then periodic timers are supported. If |
3148 | If undefined or defined to be C<1>, then periodic timers are supported. If |
2457 | defined to be C<0>, then they are not. Disabling them saves a few kB of |
3149 | defined to be C<0>, then they are not. Disabling them saves a few kB of |
… | |
… | |
2464 | code. |
3156 | code. |
2465 | |
3157 | |
2466 | =item EV_EMBED_ENABLE |
3158 | =item EV_EMBED_ENABLE |
2467 | |
3159 | |
2468 | If undefined or defined to be C<1>, then embed watchers are supported. If |
3160 | If undefined or defined to be C<1>, then embed watchers are supported. If |
2469 | defined to be C<0>, then they are not. |
3161 | defined to be C<0>, then they are not. Embed watchers rely on most other |
|
|
3162 | watcher types, which therefore must not be disabled. |
2470 | |
3163 | |
2471 | =item EV_STAT_ENABLE |
3164 | =item EV_STAT_ENABLE |
2472 | |
3165 | |
2473 | If undefined or defined to be C<1>, then stat watchers are supported. If |
3166 | If undefined or defined to be C<1>, then stat watchers are supported. If |
2474 | defined to be C<0>, then they are not. |
3167 | defined to be C<0>, then they are not. |
… | |
… | |
2476 | =item EV_FORK_ENABLE |
3169 | =item EV_FORK_ENABLE |
2477 | |
3170 | |
2478 | If undefined or defined to be C<1>, then fork watchers are supported. If |
3171 | If undefined or defined to be C<1>, then fork watchers are supported. If |
2479 | defined to be C<0>, then they are not. |
3172 | defined to be C<0>, then they are not. |
2480 | |
3173 | |
|
|
3174 | =item EV_ASYNC_ENABLE |
|
|
3175 | |
|
|
3176 | If undefined or defined to be C<1>, then async watchers are supported. If |
|
|
3177 | defined to be C<0>, then they are not. |
|
|
3178 | |
2481 | =item EV_MINIMAL |
3179 | =item EV_MINIMAL |
2482 | |
3180 | |
2483 | If you need to shave off some kilobytes of code at the expense of some |
3181 | If you need to shave off some kilobytes of code at the expense of some |
2484 | speed, define this symbol to C<1>. Currently only used for gcc to override |
3182 | speed, define this symbol to C<1>. Currently this is used to override some |
2485 | some inlining decisions, saves roughly 30% codesize of amd64. |
3183 | inlining decisions, saves roughly 30% code size on amd64. It also selects a |
|
|
3184 | much smaller 2-heap for timer management over the default 4-heap. |
2486 | |
3185 | |
2487 | =item EV_PID_HASHSIZE |
3186 | =item EV_PID_HASHSIZE |
2488 | |
3187 | |
2489 | C<ev_child> watchers use a small hash table to distribute workload by |
3188 | C<ev_child> watchers use a small hash table to distribute workload by |
2490 | pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more |
3189 | pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more |
2491 | than enough. If you need to manage thousands of children you might want to |
3190 | than enough. If you need to manage thousands of children you might want to |
2492 | increase this value (I<must> be a power of two). |
3191 | increase this value (I<must> be a power of two). |
2493 | |
3192 | |
2494 | =item EV_INOTIFY_HASHSIZE |
3193 | =item EV_INOTIFY_HASHSIZE |
2495 | |
3194 | |
2496 | C<ev_staz> watchers use a small hash table to distribute workload by |
3195 | C<ev_stat> watchers use a small hash table to distribute workload by |
2497 | inotify watch id. The default size is C<16> (or C<1> with C<EV_MINIMAL>), |
3196 | inotify watch id. The default size is C<16> (or C<1> with C<EV_MINIMAL>), |
2498 | usually more than enough. If you need to manage thousands of C<ev_stat> |
3197 | usually more than enough. If you need to manage thousands of C<ev_stat> |
2499 | watchers you might want to increase this value (I<must> be a power of |
3198 | watchers you might want to increase this value (I<must> be a power of |
2500 | two). |
3199 | two). |
2501 | |
3200 | |
|
|
3201 | =item EV_USE_4HEAP |
|
|
3202 | |
|
|
3203 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
|
|
3204 | timer and periodics heaps, libev uses a 4-heap when this symbol is defined |
|
|
3205 | to C<1>. The 4-heap uses more complicated (longer) code but has noticeably |
|
|
3206 | faster performance with many (thousands) of watchers. |
|
|
3207 | |
|
|
3208 | The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0> |
|
|
3209 | (disabled). |
|
|
3210 | |
|
|
3211 | =item EV_HEAP_CACHE_AT |
|
|
3212 | |
|
|
3213 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
|
|
3214 | timer and periodics heaps, libev can cache the timestamp (I<at>) within |
|
|
3215 | the heap structure (selected by defining C<EV_HEAP_CACHE_AT> to C<1>), |
|
|
3216 | which uses 8-12 bytes more per watcher and a few hundred bytes more code, |
|
|
3217 | but avoids random read accesses on heap changes. This improves performance |
|
|
3218 | noticeably with many (hundreds) of watchers. |
|
|
3219 | |
|
|
3220 | The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0> |
|
|
3221 | (disabled). |
|
|
3222 | |
|
|
3223 | =item EV_VERIFY |
|
|
3224 | |
|
|
3225 | Controls how much internal verification (see C<ev_loop_verify ()>) will |
|
|
3226 | be done: If set to C<0>, no internal verification code will be compiled |
|
|
3227 | in. If set to C<1>, then verification code will be compiled in, but not |
|
|
3228 | called. If set to C<2>, then the internal verification code will be |
|
|
3229 | called once per loop, which can slow down libev. If set to C<3>, then the |
|
|
3230 | verification code will be called very frequently, which will slow down |
|
|
3231 | libev considerably. |
|
|
3232 | |
|
|
3233 | The default is C<1>, unless C<EV_MINIMAL> is set, in which case it will be |
|
|
3234 | C<0>. |
|
|
3235 | |
2502 | =item EV_COMMON |
3236 | =item EV_COMMON |
2503 | |
3237 | |
2504 | By default, all watchers have a C<void *data> member. By redefining |
3238 | By default, all watchers have a C<void *data> member. By redefining |
2505 | this macro to a something else you can include more and other types of |
3239 | this macro to a something else you can include more and other types of |
2506 | members. You have to define it each time you include one of the files, |
3240 | members. You have to define it each time you include one of the files, |
2507 | though, and it must be identical each time. |
3241 | though, and it must be identical each time. |
2508 | |
3242 | |
2509 | For example, the perl EV module uses something like this: |
3243 | For example, the perl EV module uses something like this: |
2510 | |
3244 | |
2511 | #define EV_COMMON \ |
3245 | #define EV_COMMON \ |
2512 | SV *self; /* contains this struct */ \ |
3246 | SV *self; /* contains this struct */ \ |
2513 | SV *cb_sv, *fh /* note no trailing ";" */ |
3247 | SV *cb_sv, *fh /* note no trailing ";" */ |
2514 | |
3248 | |
2515 | =item EV_CB_DECLARE (type) |
3249 | =item EV_CB_DECLARE (type) |
2516 | |
3250 | |
2517 | =item EV_CB_INVOKE (watcher, revents) |
3251 | =item EV_CB_INVOKE (watcher, revents) |
2518 | |
3252 | |
… | |
… | |
2523 | definition and a statement, respectively. See the F<ev.h> header file for |
3257 | definition and a statement, respectively. See the F<ev.h> header file for |
2524 | their default definitions. One possible use for overriding these is to |
3258 | their default definitions. One possible use for overriding these is to |
2525 | avoid the C<struct ev_loop *> as first argument in all cases, or to use |
3259 | avoid the C<struct ev_loop *> as first argument in all cases, or to use |
2526 | method calls instead of plain function calls in C++. |
3260 | method calls instead of plain function calls in C++. |
2527 | |
3261 | |
|
|
3262 | =back |
|
|
3263 | |
2528 | =head2 EXPORTED API SYMBOLS |
3264 | =head2 EXPORTED API SYMBOLS |
2529 | |
3265 | |
2530 | If you need to re-export the API (e.g. via a dll) and you need a list of |
3266 | If you need to re-export the API (e.g. via a DLL) and you need a list of |
2531 | exported symbols, you can use the provided F<Symbol.*> files which list |
3267 | exported symbols, you can use the provided F<Symbol.*> files which list |
2532 | all public symbols, one per line: |
3268 | all public symbols, one per line: |
2533 | |
3269 | |
2534 | Symbols.ev for libev proper |
3270 | Symbols.ev for libev proper |
2535 | Symbols.event for the libevent emulation |
3271 | Symbols.event for the libevent emulation |
2536 | |
3272 | |
2537 | This can also be used to rename all public symbols to avoid clashes with |
3273 | This can also be used to rename all public symbols to avoid clashes with |
2538 | multiple versions of libev linked together (which is obviously bad in |
3274 | multiple versions of libev linked together (which is obviously bad in |
2539 | itself, but sometimes it is inconvinient to avoid this). |
3275 | itself, but sometimes it is inconvenient to avoid this). |
2540 | |
3276 | |
2541 | A sed command like this will create wrapper C<#define>'s that you need to |
3277 | A sed command like this will create wrapper C<#define>'s that you need to |
2542 | include before including F<ev.h>: |
3278 | include before including F<ev.h>: |
2543 | |
3279 | |
2544 | <Symbols.ev sed -e "s/.*/#define & myprefix_&/" >wrap.h |
3280 | <Symbols.ev sed -e "s/.*/#define & myprefix_&/" >wrap.h |
… | |
… | |
2561 | file. |
3297 | file. |
2562 | |
3298 | |
2563 | The usage in rxvt-unicode is simpler. It has a F<ev_cpp.h> header file |
3299 | The usage in rxvt-unicode is simpler. It has a F<ev_cpp.h> header file |
2564 | that everybody includes and which overrides some configure choices: |
3300 | that everybody includes and which overrides some configure choices: |
2565 | |
3301 | |
2566 | #define EV_MINIMAL 1 |
3302 | #define EV_MINIMAL 1 |
2567 | #define EV_USE_POLL 0 |
3303 | #define EV_USE_POLL 0 |
2568 | #define EV_MULTIPLICITY 0 |
3304 | #define EV_MULTIPLICITY 0 |
2569 | #define EV_PERIODIC_ENABLE 0 |
3305 | #define EV_PERIODIC_ENABLE 0 |
2570 | #define EV_STAT_ENABLE 0 |
3306 | #define EV_STAT_ENABLE 0 |
2571 | #define EV_FORK_ENABLE 0 |
3307 | #define EV_FORK_ENABLE 0 |
2572 | #define EV_CONFIG_H <config.h> |
3308 | #define EV_CONFIG_H <config.h> |
2573 | #define EV_MINPRI 0 |
3309 | #define EV_MINPRI 0 |
2574 | #define EV_MAXPRI 0 |
3310 | #define EV_MAXPRI 0 |
2575 | |
3311 | |
2576 | #include "ev++.h" |
3312 | #include "ev++.h" |
2577 | |
3313 | |
2578 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
3314 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
2579 | |
3315 | |
2580 | #include "ev_cpp.h" |
3316 | #include "ev_cpp.h" |
2581 | #include "ev.c" |
3317 | #include "ev.c" |
2582 | |
3318 | |
|
|
3319 | =head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES |
2583 | |
3320 | |
|
|
3321 | =head2 THREADS AND COROUTINES |
|
|
3322 | |
|
|
3323 | =head3 THREADS |
|
|
3324 | |
|
|
3325 | All libev functions are reentrant and thread-safe unless explicitly |
|
|
3326 | documented otherwise, but libev implements no locking itself. This means |
|
|
3327 | that you can use as many loops as you want in parallel, as long as there |
|
|
3328 | are no concurrent calls into any libev function with the same loop |
|
|
3329 | parameter (C<ev_default_*> calls have an implicit default loop parameter, |
|
|
3330 | of course): libev guarantees that different event loops share no data |
|
|
3331 | structures that need any locking. |
|
|
3332 | |
|
|
3333 | Or to put it differently: calls with different loop parameters can be done |
|
|
3334 | concurrently from multiple threads, calls with the same loop parameter |
|
|
3335 | must be done serially (but can be done from different threads, as long as |
|
|
3336 | only one thread ever is inside a call at any point in time, e.g. by using |
|
|
3337 | a mutex per loop). |
|
|
3338 | |
|
|
3339 | Specifically to support threads (and signal handlers), libev implements |
|
|
3340 | so-called C<ev_async> watchers, which allow some limited form of |
|
|
3341 | concurrency on the same event loop, namely waking it up "from the |
|
|
3342 | outside". |
|
|
3343 | |
|
|
3344 | If you want to know which design (one loop, locking, or multiple loops |
|
|
3345 | without or something else still) is best for your problem, then I cannot |
|
|
3346 | help you, but here is some generic advice: |
|
|
3347 | |
|
|
3348 | =over 4 |
|
|
3349 | |
|
|
3350 | =item * most applications have a main thread: use the default libev loop |
|
|
3351 | in that thread, or create a separate thread running only the default loop. |
|
|
3352 | |
|
|
3353 | This helps integrating other libraries or software modules that use libev |
|
|
3354 | themselves and don't care/know about threading. |
|
|
3355 | |
|
|
3356 | =item * one loop per thread is usually a good model. |
|
|
3357 | |
|
|
3358 | Doing this is almost never wrong, sometimes a better-performance model |
|
|
3359 | exists, but it is always a good start. |
|
|
3360 | |
|
|
3361 | =item * other models exist, such as the leader/follower pattern, where one |
|
|
3362 | loop is handed through multiple threads in a kind of round-robin fashion. |
|
|
3363 | |
|
|
3364 | Choosing a model is hard - look around, learn, know that usually you can do |
|
|
3365 | better than you currently do :-) |
|
|
3366 | |
|
|
3367 | =item * often you need to talk to some other thread which blocks in the |
|
|
3368 | event loop. |
|
|
3369 | |
|
|
3370 | C<ev_async> watchers can be used to wake them up from other threads safely |
|
|
3371 | (or from signal contexts...). |
|
|
3372 | |
|
|
3373 | An example use would be to communicate signals or other events that only |
|
|
3374 | work in the default loop by registering the signal watcher with the |
|
|
3375 | default loop and triggering an C<ev_async> watcher from the default loop |
|
|
3376 | watcher callback into the event loop interested in the signal. |
|
|
3377 | |
|
|
3378 | =back |
|
|
3379 | |
|
|
3380 | =head3 COROUTINES |
|
|
3381 | |
|
|
3382 | Libev is very accommodating to coroutines ("cooperative threads"): |
|
|
3383 | libev fully supports nesting calls to its functions from different |
|
|
3384 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
|
|
3385 | different coroutines, and switch freely between both coroutines running the |
|
|
3386 | loop, as long as you don't confuse yourself). The only exception is that |
|
|
3387 | you must not do this from C<ev_periodic> reschedule callbacks. |
|
|
3388 | |
|
|
3389 | Care has been taken to ensure that libev does not keep local state inside |
|
|
3390 | C<ev_loop>, and other calls do not usually allow for coroutine switches as |
|
|
3391 | they do not clal any callbacks. |
|
|
3392 | |
|
|
3393 | =head2 COMPILER WARNINGS |
|
|
3394 | |
|
|
3395 | Depending on your compiler and compiler settings, you might get no or a |
|
|
3396 | lot of warnings when compiling libev code. Some people are apparently |
|
|
3397 | scared by this. |
|
|
3398 | |
|
|
3399 | However, these are unavoidable for many reasons. For one, each compiler |
|
|
3400 | has different warnings, and each user has different tastes regarding |
|
|
3401 | warning options. "Warn-free" code therefore cannot be a goal except when |
|
|
3402 | targeting a specific compiler and compiler-version. |
|
|
3403 | |
|
|
3404 | Another reason is that some compiler warnings require elaborate |
|
|
3405 | workarounds, or other changes to the code that make it less clear and less |
|
|
3406 | maintainable. |
|
|
3407 | |
|
|
3408 | And of course, some compiler warnings are just plain stupid, or simply |
|
|
3409 | wrong (because they don't actually warn about the condition their message |
|
|
3410 | seems to warn about). For example, certain older gcc versions had some |
|
|
3411 | warnings that resulted an extreme number of false positives. These have |
|
|
3412 | been fixed, but some people still insist on making code warn-free with |
|
|
3413 | such buggy versions. |
|
|
3414 | |
|
|
3415 | While libev is written to generate as few warnings as possible, |
|
|
3416 | "warn-free" code is not a goal, and it is recommended not to build libev |
|
|
3417 | with any compiler warnings enabled unless you are prepared to cope with |
|
|
3418 | them (e.g. by ignoring them). Remember that warnings are just that: |
|
|
3419 | warnings, not errors, or proof of bugs. |
|
|
3420 | |
|
|
3421 | |
|
|
3422 | =head2 VALGRIND |
|
|
3423 | |
|
|
3424 | Valgrind has a special section here because it is a popular tool that is |
|
|
3425 | highly useful. Unfortunately, valgrind reports are very hard to interpret. |
|
|
3426 | |
|
|
3427 | If you think you found a bug (memory leak, uninitialised data access etc.) |
|
|
3428 | in libev, then check twice: If valgrind reports something like: |
|
|
3429 | |
|
|
3430 | ==2274== definitely lost: 0 bytes in 0 blocks. |
|
|
3431 | ==2274== possibly lost: 0 bytes in 0 blocks. |
|
|
3432 | ==2274== still reachable: 256 bytes in 1 blocks. |
|
|
3433 | |
|
|
3434 | Then there is no memory leak, just as memory accounted to global variables |
|
|
3435 | is not a memleak - the memory is still being refernced, and didn't leak. |
|
|
3436 | |
|
|
3437 | Similarly, under some circumstances, valgrind might report kernel bugs |
|
|
3438 | as if it were a bug in libev (e.g. in realloc or in the poll backend, |
|
|
3439 | although an acceptable workaround has been found here), or it might be |
|
|
3440 | confused. |
|
|
3441 | |
|
|
3442 | Keep in mind that valgrind is a very good tool, but only a tool. Don't |
|
|
3443 | make it into some kind of religion. |
|
|
3444 | |
|
|
3445 | If you are unsure about something, feel free to contact the mailing list |
|
|
3446 | with the full valgrind report and an explanation on why you think this |
|
|
3447 | is a bug in libev (best check the archives, too :). However, don't be |
|
|
3448 | annoyed when you get a brisk "this is no bug" answer and take the chance |
|
|
3449 | of learning how to interpret valgrind properly. |
|
|
3450 | |
|
|
3451 | If you need, for some reason, empty reports from valgrind for your project |
|
|
3452 | I suggest using suppression lists. |
|
|
3453 | |
|
|
3454 | |
|
|
3455 | =head1 PORTABILITY NOTES |
|
|
3456 | |
|
|
3457 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
|
|
3458 | |
|
|
3459 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
|
|
3460 | requires, and its I/O model is fundamentally incompatible with the POSIX |
|
|
3461 | model. Libev still offers limited functionality on this platform in |
|
|
3462 | the form of the C<EVBACKEND_SELECT> backend, and only supports socket |
|
|
3463 | descriptors. This only applies when using Win32 natively, not when using |
|
|
3464 | e.g. cygwin. |
|
|
3465 | |
|
|
3466 | Lifting these limitations would basically require the full |
|
|
3467 | re-implementation of the I/O system. If you are into these kinds of |
|
|
3468 | things, then note that glib does exactly that for you in a very portable |
|
|
3469 | way (note also that glib is the slowest event library known to man). |
|
|
3470 | |
|
|
3471 | There is no supported compilation method available on windows except |
|
|
3472 | embedding it into other applications. |
|
|
3473 | |
|
|
3474 | Not a libev limitation but worth mentioning: windows apparently doesn't |
|
|
3475 | accept large writes: instead of resulting in a partial write, windows will |
|
|
3476 | either accept everything or return C<ENOBUFS> if the buffer is too large, |
|
|
3477 | so make sure you only write small amounts into your sockets (less than a |
|
|
3478 | megabyte seems safe, but this apparently depends on the amount of memory |
|
|
3479 | available). |
|
|
3480 | |
|
|
3481 | Due to the many, low, and arbitrary limits on the win32 platform and |
|
|
3482 | the abysmal performance of winsockets, using a large number of sockets |
|
|
3483 | is not recommended (and not reasonable). If your program needs to use |
|
|
3484 | more than a hundred or so sockets, then likely it needs to use a totally |
|
|
3485 | different implementation for windows, as libev offers the POSIX readiness |
|
|
3486 | notification model, which cannot be implemented efficiently on windows |
|
|
3487 | (Microsoft monopoly games). |
|
|
3488 | |
|
|
3489 | A typical way to use libev under windows is to embed it (see the embedding |
|
|
3490 | section for details) and use the following F<evwrap.h> header file instead |
|
|
3491 | of F<ev.h>: |
|
|
3492 | |
|
|
3493 | #define EV_STANDALONE /* keeps ev from requiring config.h */ |
|
|
3494 | #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */ |
|
|
3495 | |
|
|
3496 | #include "ev.h" |
|
|
3497 | |
|
|
3498 | And compile the following F<evwrap.c> file into your project (make sure |
|
|
3499 | you do I<not> compile the F<ev.c> or any other embedded source files!): |
|
|
3500 | |
|
|
3501 | #include "evwrap.h" |
|
|
3502 | #include "ev.c" |
|
|
3503 | |
|
|
3504 | =over 4 |
|
|
3505 | |
|
|
3506 | =item The winsocket select function |
|
|
3507 | |
|
|
3508 | The winsocket C<select> function doesn't follow POSIX in that it |
|
|
3509 | requires socket I<handles> and not socket I<file descriptors> (it is |
|
|
3510 | also extremely buggy). This makes select very inefficient, and also |
|
|
3511 | requires a mapping from file descriptors to socket handles (the Microsoft |
|
|
3512 | C runtime provides the function C<_open_osfhandle> for this). See the |
|
|
3513 | discussion of the C<EV_SELECT_USE_FD_SET>, C<EV_SELECT_IS_WINSOCKET> and |
|
|
3514 | C<EV_FD_TO_WIN32_HANDLE> preprocessor symbols for more info. |
|
|
3515 | |
|
|
3516 | The configuration for a "naked" win32 using the Microsoft runtime |
|
|
3517 | libraries and raw winsocket select is: |
|
|
3518 | |
|
|
3519 | #define EV_USE_SELECT 1 |
|
|
3520 | #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */ |
|
|
3521 | |
|
|
3522 | Note that winsockets handling of fd sets is O(n), so you can easily get a |
|
|
3523 | complexity in the O(n²) range when using win32. |
|
|
3524 | |
|
|
3525 | =item Limited number of file descriptors |
|
|
3526 | |
|
|
3527 | Windows has numerous arbitrary (and low) limits on things. |
|
|
3528 | |
|
|
3529 | Early versions of winsocket's select only supported waiting for a maximum |
|
|
3530 | of C<64> handles (probably owning to the fact that all windows kernels |
|
|
3531 | can only wait for C<64> things at the same time internally; Microsoft |
|
|
3532 | recommends spawning a chain of threads and wait for 63 handles and the |
|
|
3533 | previous thread in each. Great). |
|
|
3534 | |
|
|
3535 | Newer versions support more handles, but you need to define C<FD_SETSIZE> |
|
|
3536 | to some high number (e.g. C<2048>) before compiling the winsocket select |
|
|
3537 | call (which might be in libev or elsewhere, for example, perl does its own |
|
|
3538 | select emulation on windows). |
|
|
3539 | |
|
|
3540 | Another limit is the number of file descriptors in the Microsoft runtime |
|
|
3541 | libraries, which by default is C<64> (there must be a hidden I<64> fetish |
|
|
3542 | or something like this inside Microsoft). You can increase this by calling |
|
|
3543 | C<_setmaxstdio>, which can increase this limit to C<2048> (another |
|
|
3544 | arbitrary limit), but is broken in many versions of the Microsoft runtime |
|
|
3545 | libraries. |
|
|
3546 | |
|
|
3547 | This might get you to about C<512> or C<2048> sockets (depending on |
|
|
3548 | windows version and/or the phase of the moon). To get more, you need to |
|
|
3549 | wrap all I/O functions and provide your own fd management, but the cost of |
|
|
3550 | calling select (O(n²)) will likely make this unworkable. |
|
|
3551 | |
|
|
3552 | =back |
|
|
3553 | |
|
|
3554 | =head2 PORTABILITY REQUIREMENTS |
|
|
3555 | |
|
|
3556 | In addition to a working ISO-C implementation and of course the |
|
|
3557 | backend-specific APIs, libev relies on a few additional extensions: |
|
|
3558 | |
|
|
3559 | =over 4 |
|
|
3560 | |
|
|
3561 | =item C<void (*)(ev_watcher_type *, int revents)> must have compatible |
|
|
3562 | calling conventions regardless of C<ev_watcher_type *>. |
|
|
3563 | |
|
|
3564 | Libev assumes not only that all watcher pointers have the same internal |
|
|
3565 | structure (guaranteed by POSIX but not by ISO C for example), but it also |
|
|
3566 | assumes that the same (machine) code can be used to call any watcher |
|
|
3567 | callback: The watcher callbacks have different type signatures, but libev |
|
|
3568 | calls them using an C<ev_watcher *> internally. |
|
|
3569 | |
|
|
3570 | =item C<sig_atomic_t volatile> must be thread-atomic as well |
|
|
3571 | |
|
|
3572 | The type C<sig_atomic_t volatile> (or whatever is defined as |
|
|
3573 | C<EV_ATOMIC_T>) must be atomic with respect to accesses from different |
|
|
3574 | threads. This is not part of the specification for C<sig_atomic_t>, but is |
|
|
3575 | believed to be sufficiently portable. |
|
|
3576 | |
|
|
3577 | =item C<sigprocmask> must work in a threaded environment |
|
|
3578 | |
|
|
3579 | Libev uses C<sigprocmask> to temporarily block signals. This is not |
|
|
3580 | allowed in a threaded program (C<pthread_sigmask> has to be used). Typical |
|
|
3581 | pthread implementations will either allow C<sigprocmask> in the "main |
|
|
3582 | thread" or will block signals process-wide, both behaviours would |
|
|
3583 | be compatible with libev. Interaction between C<sigprocmask> and |
|
|
3584 | C<pthread_sigmask> could complicate things, however. |
|
|
3585 | |
|
|
3586 | The most portable way to handle signals is to block signals in all threads |
|
|
3587 | except the initial one, and run the default loop in the initial thread as |
|
|
3588 | well. |
|
|
3589 | |
|
|
3590 | =item C<long> must be large enough for common memory allocation sizes |
|
|
3591 | |
|
|
3592 | To improve portability and simplify its API, libev uses C<long> internally |
|
|
3593 | instead of C<size_t> when allocating its data structures. On non-POSIX |
|
|
3594 | systems (Microsoft...) this might be unexpectedly low, but is still at |
|
|
3595 | least 31 bits everywhere, which is enough for hundreds of millions of |
|
|
3596 | watchers. |
|
|
3597 | |
|
|
3598 | =item C<double> must hold a time value in seconds with enough accuracy |
|
|
3599 | |
|
|
3600 | The type C<double> is used to represent timestamps. It is required to |
|
|
3601 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
|
|
3602 | enough for at least into the year 4000. This requirement is fulfilled by |
|
|
3603 | implementations implementing IEEE 754 (basically all existing ones). |
|
|
3604 | |
|
|
3605 | =back |
|
|
3606 | |
|
|
3607 | If you know of other additional requirements drop me a note. |
|
|
3608 | |
|
|
3609 | |
2584 | =head1 COMPLEXITIES |
3610 | =head1 ALGORITHMIC COMPLEXITIES |
2585 | |
3611 | |
2586 | In this section the complexities of (many of) the algorithms used inside |
3612 | In this section the complexities of (many of) the algorithms used inside |
2587 | libev will be explained. For complexity discussions about backends see the |
3613 | libev will be documented. For complexity discussions about backends see |
2588 | documentation for C<ev_default_init>. |
3614 | the documentation for C<ev_default_init>. |
2589 | |
3615 | |
2590 | All of the following are about amortised time: If an array needs to be |
3616 | All of the following are about amortised time: If an array needs to be |
2591 | extended, libev needs to realloc and move the whole array, but this |
3617 | extended, libev needs to realloc and move the whole array, but this |
2592 | happens asymptotically never with higher number of elements, so O(1) might |
3618 | happens asymptotically rarer with higher number of elements, so O(1) might |
2593 | mean it might do a lengthy realloc operation in rare cases, but on average |
3619 | mean that libev does a lengthy realloc operation in rare cases, but on |
2594 | it is much faster and asymptotically approaches constant time. |
3620 | average it is much faster and asymptotically approaches constant time. |
2595 | |
3621 | |
2596 | =over 4 |
3622 | =over 4 |
2597 | |
3623 | |
2598 | =item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) |
3624 | =item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) |
2599 | |
3625 | |
2600 | This means that, when you have a watcher that triggers in one hour and |
3626 | This means that, when you have a watcher that triggers in one hour and |
2601 | there are 100 watchers that would trigger before that then inserting will |
3627 | there are 100 watchers that would trigger before that, then inserting will |
2602 | have to skip those 100 watchers. |
3628 | have to skip roughly seven (C<ld 100>) of these watchers. |
2603 | |
3629 | |
2604 | =item Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers) |
3630 | =item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) |
2605 | |
3631 | |
2606 | That means that for changing a timer costs less than removing/adding them |
3632 | That means that changing a timer costs less than removing/adding them, |
2607 | as only the relative motion in the event queue has to be paid for. |
3633 | as only the relative motion in the event queue has to be paid for. |
2608 | |
3634 | |
2609 | =item Starting io/check/prepare/idle/signal/child watchers: O(1) |
3635 | =item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) |
2610 | |
3636 | |
2611 | These just add the watcher into an array or at the head of a list. |
3637 | These just add the watcher into an array or at the head of a list. |
|
|
3638 | |
2612 | =item Stopping check/prepare/idle watchers: O(1) |
3639 | =item Stopping check/prepare/idle/fork/async watchers: O(1) |
2613 | |
3640 | |
2614 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) |
3641 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) |
2615 | |
3642 | |
2616 | These watchers are stored in lists then need to be walked to find the |
3643 | These watchers are stored in lists, so they need to be walked to find the |
2617 | correct watcher to remove. The lists are usually short (you don't usually |
3644 | correct watcher to remove. The lists are usually short (you don't usually |
2618 | have many watchers waiting for the same fd or signal). |
3645 | have many watchers waiting for the same fd or signal: one is typical, two |
|
|
3646 | is rare). |
2619 | |
3647 | |
2620 | =item Finding the next timer per loop iteration: O(1) |
3648 | =item Finding the next timer in each loop iteration: O(1) |
|
|
3649 | |
|
|
3650 | By virtue of using a binary or 4-heap, the next timer is always found at a |
|
|
3651 | fixed position in the storage array. |
2621 | |
3652 | |
2622 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
3653 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
2623 | |
3654 | |
2624 | A change means an I/O watcher gets started or stopped, which requires |
3655 | A change means an I/O watcher gets started or stopped, which requires |
2625 | libev to recalculate its status (and possibly tell the kernel). |
3656 | libev to recalculate its status (and possibly tell the kernel, depending |
|
|
3657 | on backend and whether C<ev_io_set> was used). |
2626 | |
3658 | |
2627 | =item Activating one watcher: O(1) |
3659 | =item Activating one watcher (putting it into the pending state): O(1) |
2628 | |
3660 | |
2629 | =item Priority handling: O(number_of_priorities) |
3661 | =item Priority handling: O(number_of_priorities) |
2630 | |
3662 | |
2631 | Priorities are implemented by allocating some space for each |
3663 | Priorities are implemented by allocating some space for each |
2632 | priority. When doing priority-based operations, libev usually has to |
3664 | priority. When doing priority-based operations, libev usually has to |
2633 | linearly search all the priorities. |
3665 | linearly search all the priorities, but starting/stopping and activating |
|
|
3666 | watchers becomes O(1) with respect to priority handling. |
|
|
3667 | |
|
|
3668 | =item Sending an ev_async: O(1) |
|
|
3669 | |
|
|
3670 | =item Processing ev_async_send: O(number_of_async_watchers) |
|
|
3671 | |
|
|
3672 | =item Processing signals: O(max_signal_number) |
|
|
3673 | |
|
|
3674 | Sending involves a system call I<iff> there were no other C<ev_async_send> |
|
|
3675 | calls in the current loop iteration. Checking for async and signal events |
|
|
3676 | involves iterating over all running async watchers or all signal numbers. |
2634 | |
3677 | |
2635 | =back |
3678 | =back |
2636 | |
3679 | |
2637 | |
3680 | |
2638 | =head1 AUTHOR |
3681 | =head1 AUTHOR |