… | |
… | |
129 | . ds Ae AE |
129 | . ds Ae AE |
130 | .\} |
130 | .\} |
131 | .rm #[ #] #H #V #F C |
131 | .rm #[ #] #H #V #F C |
132 | .\" ======================================================================== |
132 | .\" ======================================================================== |
133 | .\" |
133 | .\" |
134 | .IX Title "EV 1" |
134 | .IX Title "LIBEV 3" |
135 | .TH EV 1 "2008-01-28" "perl v5.10.0" "User Contributed Perl Documentation" |
135 | .TH LIBEV 3 "2008-05-22" "libev-3.41" "libev - high perfromance full featured event loop" |
136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
137 | .\" way too many mistakes in technical documents. |
137 | .\" way too many mistakes in technical documents. |
138 | .if n .ad l |
138 | .if n .ad l |
139 | .nh |
139 | .nh |
140 | .SH "NAME" |
140 | .SH "NAME" |
… | |
… | |
144 | .Vb 1 |
144 | .Vb 1 |
145 | \& #include <ev.h> |
145 | \& #include <ev.h> |
146 | .Ve |
146 | .Ve |
147 | .Sh "\s-1EXAMPLE\s0 \s-1PROGRAM\s0" |
147 | .Sh "\s-1EXAMPLE\s0 \s-1PROGRAM\s0" |
148 | .IX Subsection "EXAMPLE PROGRAM" |
148 | .IX Subsection "EXAMPLE PROGRAM" |
149 | .Vb 1 |
149 | .Vb 2 |
|
|
150 | \& // a single header file is required |
150 | \& #include <ev.h> |
151 | \& #include <ev.h> |
151 | \& |
152 | \& |
|
|
153 | \& // every watcher type has its own typedef\*(Aqd struct |
|
|
154 | \& // with the name ev_<type> |
152 | \& ev_io stdin_watcher; |
155 | \& ev_io stdin_watcher; |
153 | \& ev_timer timeout_watcher; |
156 | \& ev_timer timeout_watcher; |
154 | \& |
157 | \& |
|
|
158 | \& // all watcher callbacks have a similar signature |
155 | \& /* called when data readable on stdin */ |
159 | \& // this callback is called when data is readable on stdin |
156 | \& static void |
160 | \& static void |
157 | \& stdin_cb (EV_P_ struct ev_io *w, int revents) |
161 | \& stdin_cb (EV_P_ struct ev_io *w, int revents) |
158 | \& { |
162 | \& { |
159 | \& /* puts ("stdin ready"); */ |
163 | \& puts ("stdin ready"); |
160 | \& ev_io_stop (EV_A_ w); /* just a syntax example */ |
164 | \& // for one\-shot events, one must manually stop the watcher |
161 | \& ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ |
165 | \& // with its corresponding stop function. |
|
|
166 | \& ev_io_stop (EV_A_ w); |
|
|
167 | \& |
|
|
168 | \& // this causes all nested ev_loop\*(Aqs to stop iterating |
|
|
169 | \& ev_unloop (EV_A_ EVUNLOOP_ALL); |
162 | \& } |
170 | \& } |
163 | \& |
171 | \& |
|
|
172 | \& // another callback, this time for a time\-out |
164 | \& static void |
173 | \& static void |
165 | \& timeout_cb (EV_P_ struct ev_timer *w, int revents) |
174 | \& timeout_cb (EV_P_ struct ev_timer *w, int revents) |
166 | \& { |
175 | \& { |
167 | \& /* puts ("timeout"); */ |
176 | \& puts ("timeout"); |
168 | \& ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ |
177 | \& // this causes the innermost ev_loop to stop iterating |
|
|
178 | \& ev_unloop (EV_A_ EVUNLOOP_ONE); |
169 | \& } |
179 | \& } |
170 | \& |
180 | \& |
171 | \& int |
181 | \& int |
172 | \& main (void) |
182 | \& main (void) |
173 | \& { |
183 | \& { |
|
|
184 | \& // use the default event loop unless you have special needs |
174 | \& struct ev_loop *loop = ev_default_loop (0); |
185 | \& struct ev_loop *loop = ev_default_loop (0); |
175 | \& |
186 | \& |
176 | \& /* initialise an io watcher, then start it */ |
187 | \& // initialise an io watcher, then start it |
|
|
188 | \& // this one will watch for stdin to become readable |
177 | \& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
189 | \& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
178 | \& ev_io_start (loop, &stdin_watcher); |
190 | \& ev_io_start (loop, &stdin_watcher); |
179 | \& |
191 | \& |
|
|
192 | \& // initialise a timer watcher, then start it |
180 | \& /* simple non\-repeating 5.5 second timeout */ |
193 | \& // simple non\-repeating 5.5 second timeout |
181 | \& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
194 | \& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
182 | \& ev_timer_start (loop, &timeout_watcher); |
195 | \& ev_timer_start (loop, &timeout_watcher); |
183 | \& |
196 | \& |
184 | \& /* loop till timeout or data ready */ |
197 | \& // now wait for events to arrive |
185 | \& ev_loop (loop, 0); |
198 | \& ev_loop (loop, 0); |
186 | \& |
199 | \& |
|
|
200 | \& // unloop was called, so exit |
187 | \& return 0; |
201 | \& return 0; |
188 | \& } |
202 | \& } |
189 | .Ve |
203 | .Ve |
190 | .SH "DESCRIPTION" |
204 | .SH "DESCRIPTION" |
191 | .IX Header "DESCRIPTION" |
205 | .IX Header "DESCRIPTION" |
192 | The newest version of this document is also available as a html-formatted |
206 | The newest version of this document is also available as an html-formatted |
193 | web page you might find easier to navigate when reading it for the first |
207 | web page you might find easier to navigate when reading it for the first |
194 | time: <http://cvs.schmorp.de/libev/ev.html>. |
208 | time: <http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>. |
195 | .PP |
209 | .PP |
196 | Libev is an event loop: you register interest in certain events (such as a |
210 | Libev is an event loop: you register interest in certain events (such as a |
197 | file descriptor being readable or a timeout occurring), and it will manage |
211 | file descriptor being readable or a timeout occurring), and it will manage |
198 | these event sources and provide your program with events. |
212 | these event sources and provide your program with events. |
199 | .PP |
213 | .PP |
… | |
… | |
221 | It also is quite fast (see this |
235 | It also is quite fast (see this |
222 | benchmark comparing it to libevent |
236 | benchmark comparing it to libevent |
223 | for example). |
237 | for example). |
224 | .Sh "\s-1CONVENTIONS\s0" |
238 | .Sh "\s-1CONVENTIONS\s0" |
225 | .IX Subsection "CONVENTIONS" |
239 | .IX Subsection "CONVENTIONS" |
226 | Libev is very configurable. In this manual the default configuration will |
240 | Libev is very configurable. In this manual the default (and most common) |
227 | be described, which supports multiple event loops. For more info about |
241 | configuration will be described, which supports multiple event loops. For |
228 | various configuration options please have a look at \fB\s-1EMBED\s0\fR section in |
242 | more info about various configuration options please have a look at |
229 | this manual. If libev was configured without support for multiple event |
243 | \&\fB\s-1EMBED\s0\fR section in this manual. If libev was configured without support |
230 | loops, then all functions taking an initial argument of name \f(CW\*(C`loop\*(C'\fR |
244 | for multiple event loops, then all functions taking an initial argument of |
231 | (which is always of type \f(CW\*(C`struct ev_loop *\*(C'\fR) will not have this argument. |
245 | name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`struct ev_loop *\*(C'\fR) will not have |
|
|
246 | this argument. |
232 | .Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0" |
247 | .Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0" |
233 | .IX Subsection "TIME REPRESENTATION" |
248 | .IX Subsection "TIME REPRESENTATION" |
234 | Libev represents time as a single floating point number, representing the |
249 | Libev represents time as a single floating point number, representing the |
235 | (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near |
250 | (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near |
236 | the beginning of 1970, details are complicated, don't ask). This type is |
251 | the beginning of 1970, details are complicated, don't ask). This type is |
237 | called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases |
252 | called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases |
238 | to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on |
253 | to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on |
239 | it, you should treat it as some floatingpoint value. Unlike the name |
254 | it, you should treat it as some floatingpoint value. Unlike the name |
240 | component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences |
255 | component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences |
241 | throughout libev. |
256 | throughout libev. |
|
|
257 | .SH "ERROR HANDLING" |
|
|
258 | .IX Header "ERROR HANDLING" |
|
|
259 | Libev knows three classes of errors: operating system errors, usage errors |
|
|
260 | and internal errors (bugs). |
|
|
261 | .PP |
|
|
262 | When libev catches an operating system error it cannot handle (for example |
|
|
263 | a syscall indicating a condition libev cannot fix), it calls the callback |
|
|
264 | set via \f(CW\*(C`ev_set_syserr_cb\*(C'\fR, which is supposed to fix the problem or |
|
|
265 | abort. The default is to print a diagnostic message and to call \f(CW\*(C`abort |
|
|
266 | ()\*(C'\fR. |
|
|
267 | .PP |
|
|
268 | When libev detects a usage error such as a negative timer interval, then |
|
|
269 | it will print a diagnostic message and abort (via the \f(CW\*(C`assert\*(C'\fR mechanism, |
|
|
270 | so \f(CW\*(C`NDEBUG\*(C'\fR will disable this checking): these are programming errors in |
|
|
271 | the libev caller and need to be fixed there. |
|
|
272 | .PP |
|
|
273 | Libev also has a few internal error-checking \f(CW\*(C`assert\*(C'\fRions, and also has |
|
|
274 | extensive consistency checking code. These do not trigger under normal |
|
|
275 | circumstances, as they indicate either a bug in libev or worse. |
242 | .SH "GLOBAL FUNCTIONS" |
276 | .SH "GLOBAL FUNCTIONS" |
243 | .IX Header "GLOBAL FUNCTIONS" |
277 | .IX Header "GLOBAL FUNCTIONS" |
244 | These functions can be called anytime, even before initialising the |
278 | These functions can be called anytime, even before initialising the |
245 | library in any way. |
279 | library in any way. |
246 | .IP "ev_tstamp ev_time ()" 4 |
280 | .IP "ev_tstamp ev_time ()" 4 |
… | |
… | |
313 | .Sp |
347 | .Sp |
314 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
348 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
315 | .IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 |
349 | .IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 |
316 | .IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" |
350 | .IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" |
317 | Sets the allocation function to use (the prototype is similar \- the |
351 | Sets the allocation function to use (the prototype is similar \- the |
318 | semantics is identical \- to the realloc C function). It is used to |
352 | semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is |
319 | allocate and free memory (no surprises here). If it returns zero when |
353 | used to allocate and free memory (no surprises here). If it returns zero |
320 | memory needs to be allocated, the library might abort or take some |
354 | when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort |
321 | potentially destructive action. The default is your system realloc |
355 | or take some potentially destructive action. |
322 | function. |
356 | .Sp |
|
|
357 | Since some systems (at least OpenBSD and Darwin) fail to implement |
|
|
358 | correct \f(CW\*(C`realloc\*(C'\fR semantics, libev will use a wrapper around the system |
|
|
359 | \&\f(CW\*(C`realloc\*(C'\fR and \f(CW\*(C`free\*(C'\fR functions by default. |
323 | .Sp |
360 | .Sp |
324 | You could override this function in high-availability programs to, say, |
361 | You could override this function in high-availability programs to, say, |
325 | free some memory if it cannot allocate memory, to use a special allocator, |
362 | free some memory if it cannot allocate memory, to use a special allocator, |
326 | or even to sleep a while and retry until some memory is available. |
363 | or even to sleep a while and retry until some memory is available. |
327 | .Sp |
364 | .Sp |
328 | Example: Replace the libev allocator with one that waits a bit and then |
365 | Example: Replace the libev allocator with one that waits a bit and then |
329 | retries). |
366 | retries (example requires a standards-compliant \f(CW\*(C`realloc\*(C'\fR). |
330 | .Sp |
367 | .Sp |
331 | .Vb 6 |
368 | .Vb 6 |
332 | \& static void * |
369 | \& static void * |
333 | \& persistent_realloc (void *ptr, size_t size) |
370 | \& persistent_realloc (void *ptr, size_t size) |
334 | \& { |
371 | \& { |
… | |
… | |
372 | .SH "FUNCTIONS CONTROLLING THE EVENT LOOP" |
409 | .SH "FUNCTIONS CONTROLLING THE EVENT LOOP" |
373 | .IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" |
410 | .IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" |
374 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two |
411 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two |
375 | types of such loops, the \fIdefault\fR loop, which supports signals and child |
412 | types of such loops, the \fIdefault\fR loop, which supports signals and child |
376 | events, and dynamically created loops which do not. |
413 | events, and dynamically created loops which do not. |
377 | .PP |
|
|
378 | If you use threads, a common model is to run the default event loop |
|
|
379 | in your main thread (or in a separate thread) and for each thread you |
|
|
380 | create, you also create another event loop. Libev itself does no locking |
|
|
381 | whatsoever, so if you mix calls to the same event loop in different |
|
|
382 | threads, make sure you lock (this is usually a bad idea, though, even if |
|
|
383 | done correctly, because it's hideous and inefficient). |
|
|
384 | .IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4 |
414 | .IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4 |
385 | .IX Item "struct ev_loop *ev_default_loop (unsigned int flags)" |
415 | .IX Item "struct ev_loop *ev_default_loop (unsigned int flags)" |
386 | This will initialise the default event loop if it hasn't been initialised |
416 | This will initialise the default event loop if it hasn't been initialised |
387 | yet and return it. If the default loop could not be initialised, returns |
417 | yet and return it. If the default loop could not be initialised, returns |
388 | false. If it already was initialised it simply returns it (and ignores the |
418 | false. If it already was initialised it simply returns it (and ignores the |
389 | flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards). |
419 | flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards). |
390 | .Sp |
420 | .Sp |
391 | If you don't know what event loop to use, use the one returned from this |
421 | If you don't know what event loop to use, use the one returned from this |
392 | function. |
422 | function. |
|
|
423 | .Sp |
|
|
424 | Note that this function is \fInot\fR thread-safe, so if you want to use it |
|
|
425 | from multiple threads, you have to lock (note also that this is unlikely, |
|
|
426 | as loops cannot bes hared easily between threads anyway). |
393 | .Sp |
427 | .Sp |
394 | The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and |
428 | The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and |
395 | \&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler |
429 | \&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler |
396 | for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either |
430 | for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either |
397 | create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you |
431 | create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you |
… | |
… | |
425 | enabling this flag. |
459 | enabling this flag. |
426 | .Sp |
460 | .Sp |
427 | This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, |
461 | This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, |
428 | and thus this might slow down your event loop if you do a lot of loop |
462 | and thus this might slow down your event loop if you do a lot of loop |
429 | iterations and little real work, but is usually not noticeable (on my |
463 | iterations and little real work, but is usually not noticeable (on my |
430 | Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence |
464 | GNU/Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence |
431 | without a syscall and thus \fIvery\fR fast, but my Linux system also has |
465 | without a syscall and thus \fIvery\fR fast, but my GNU/Linux system also has |
432 | \&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster). |
466 | \&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster). |
433 | .Sp |
467 | .Sp |
434 | The big advantage of this flag is that you can forget about fork (and |
468 | The big advantage of this flag is that you can forget about fork (and |
435 | forget about forgetting to tell libev about forking) when you use this |
469 | forget about forgetting to tell libev about forking) when you use this |
436 | flag. |
470 | flag. |
… | |
… | |
449 | To get good performance out of this backend you need a high amount of |
483 | To get good performance out of this backend you need a high amount of |
450 | parallelity (most of the file descriptors should be busy). If you are |
484 | parallelity (most of the file descriptors should be busy). If you are |
451 | writing a server, you should \f(CW\*(C`accept ()\*(C'\fR in a loop to accept as many |
485 | writing a server, you should \f(CW\*(C`accept ()\*(C'\fR in a loop to accept as many |
452 | connections as possible during one iteration. You might also want to have |
486 | connections as possible during one iteration. You might also want to have |
453 | a look at \f(CW\*(C`ev_set_io_collect_interval ()\*(C'\fR to increase the amount of |
487 | a look at \f(CW\*(C`ev_set_io_collect_interval ()\*(C'\fR to increase the amount of |
454 | readyness notifications you get per iteration. |
488 | readiness notifications you get per iteration. |
455 | .ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 |
489 | .ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 |
456 | .el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 |
490 | .el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 |
457 | .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" |
491 | .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" |
458 | And this is your standard \fIpoll\fR\|(2) backend. It's more complicated |
492 | And this is your standard \fIpoll\fR\|(2) backend. It's more complicated |
459 | than select, but handles sparse fds better and has no artificial |
493 | than select, but handles sparse fds better and has no artificial |
… | |
… | |
467 | For few fds, this backend is a bit little slower than poll and select, |
501 | For few fds, this backend is a bit little slower than poll and select, |
468 | but it scales phenomenally better. While poll and select usually scale |
502 | but it scales phenomenally better. While poll and select usually scale |
469 | like O(total_fds) where n is the total number of fds (or the highest fd), |
503 | like O(total_fds) where n is the total number of fds (or the highest fd), |
470 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
504 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
471 | of shortcomings, such as silently dropping events in some hard-to-detect |
505 | of shortcomings, such as silently dropping events in some hard-to-detect |
472 | cases and rewiring a syscall per fd change, no fork support and bad |
506 | cases and requiring a syscall per fd change, no fork support and bad |
473 | support for dup. |
507 | support for dup. |
474 | .Sp |
508 | .Sp |
475 | While stopping, setting and starting an I/O watcher in the same iteration |
509 | While stopping, setting and starting an I/O watcher in the same iteration |
476 | will result in some caching, there is still a syscall per such incident |
510 | will result in some caching, there is still a syscall per such incident |
477 | (because the fd could point to a different file description now), so its |
511 | (because the fd could point to a different file description now), so its |
… | |
… | |
538 | While this backend scales well, it requires one system call per active |
572 | While this backend scales well, it requires one system call per active |
539 | file descriptor per loop iteration. For small and medium numbers of file |
573 | file descriptor per loop iteration. For small and medium numbers of file |
540 | descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend |
574 | descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend |
541 | might perform better. |
575 | might perform better. |
542 | .Sp |
576 | .Sp |
543 | On the positive side, ignoring the spurious readyness notifications, this |
577 | On the positive side, ignoring the spurious readiness notifications, this |
544 | backend actually performed to specification in all tests and is fully |
578 | backend actually performed to specification in all tests and is fully |
545 | embeddable, which is a rare feat among the OS-specific backends. |
579 | embeddable, which is a rare feat among the OS-specific backends. |
546 | .ie n .IP """EVBACKEND_ALL""" 4 |
580 | .ie n .IP """EVBACKEND_ALL""" 4 |
547 | .el .IP "\f(CWEVBACKEND_ALL\fR" 4 |
581 | .el .IP "\f(CWEVBACKEND_ALL\fR" 4 |
548 | .IX Item "EVBACKEND_ALL" |
582 | .IX Item "EVBACKEND_ALL" |
… | |
… | |
584 | .IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" |
618 | .IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" |
585 | Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is |
619 | Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is |
586 | always distinct from the default loop. Unlike the default loop, it cannot |
620 | always distinct from the default loop. Unlike the default loop, it cannot |
587 | handle signal and child watchers, and attempts to do so will be greeted by |
621 | handle signal and child watchers, and attempts to do so will be greeted by |
588 | undefined behaviour (or a failed assertion if assertions are enabled). |
622 | undefined behaviour (or a failed assertion if assertions are enabled). |
|
|
623 | .Sp |
|
|
624 | Note that this function \fIis\fR thread-safe, and the recommended way to use |
|
|
625 | libev with threads is indeed to create one loop per thread, and using the |
|
|
626 | default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread. |
589 | .Sp |
627 | .Sp |
590 | Example: Try to create a event loop that uses epoll and nothing else. |
628 | Example: Try to create a event loop that uses epoll and nothing else. |
591 | .Sp |
629 | .Sp |
592 | .Vb 3 |
630 | .Vb 3 |
593 | \& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); |
631 | \& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); |
… | |
… | |
639 | .IP "ev_loop_fork (loop)" 4 |
677 | .IP "ev_loop_fork (loop)" 4 |
640 | .IX Item "ev_loop_fork (loop)" |
678 | .IX Item "ev_loop_fork (loop)" |
641 | Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by |
679 | Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by |
642 | \&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop |
680 | \&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop |
643 | after fork, and how you do this is entirely your own problem. |
681 | after fork, and how you do this is entirely your own problem. |
|
|
682 | .IP "int ev_is_default_loop (loop)" 4 |
|
|
683 | .IX Item "int ev_is_default_loop (loop)" |
|
|
684 | Returns true when the given loop actually is the default loop, false otherwise. |
644 | .IP "unsigned int ev_loop_count (loop)" 4 |
685 | .IP "unsigned int ev_loop_count (loop)" 4 |
645 | .IX Item "unsigned int ev_loop_count (loop)" |
686 | .IX Item "unsigned int ev_loop_count (loop)" |
646 | Returns the count of loop iterations for the loop, which is identical to |
687 | Returns the count of loop iterations for the loop, which is identical to |
647 | the number of times libev did poll for new events. It starts at \f(CW0\fR and |
688 | the number of times libev did poll for new events. It starts at \f(CW0\fR and |
648 | happily wraps around with enough iterations. |
689 | happily wraps around with enough iterations. |
… | |
… | |
804 | Many (busy) programs can usually benefit by setting the io collect |
845 | Many (busy) programs can usually benefit by setting the io collect |
805 | interval to a value near \f(CW0.1\fR or so, which is often enough for |
846 | interval to a value near \f(CW0.1\fR or so, which is often enough for |
806 | interactive servers (of course not for games), likewise for timeouts. It |
847 | interactive servers (of course not for games), likewise for timeouts. It |
807 | usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR, |
848 | usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR, |
808 | as this approsaches the timing granularity of most systems. |
849 | as this approsaches the timing granularity of most systems. |
|
|
850 | .IP "ev_loop_verify (loop)" 4 |
|
|
851 | .IX Item "ev_loop_verify (loop)" |
|
|
852 | This function only does something when \f(CW\*(C`EV_VERIFY\*(C'\fR support has been |
|
|
853 | compiled in. It tries to go through all internal structures and checks |
|
|
854 | them for validity. If anything is found to be inconsistent, it will print |
|
|
855 | an error message to standard error and call \f(CW\*(C`abort ()\*(C'\fR. |
|
|
856 | .Sp |
|
|
857 | This can be used to catch bugs inside libev itself: under normal |
|
|
858 | circumstances, this function will never abort as of course libev keeps its |
|
|
859 | data structures consistent. |
809 | .SH "ANATOMY OF A WATCHER" |
860 | .SH "ANATOMY OF A WATCHER" |
810 | .IX Header "ANATOMY OF A WATCHER" |
861 | .IX Header "ANATOMY OF A WATCHER" |
811 | A watcher is a structure that you create and register to record your |
862 | A watcher is a structure that you create and register to record your |
812 | interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to |
863 | interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to |
813 | become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: |
864 | become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: |
… | |
… | |
914 | .ie n .IP """EV_FORK""" 4 |
965 | .ie n .IP """EV_FORK""" 4 |
915 | .el .IP "\f(CWEV_FORK\fR" 4 |
966 | .el .IP "\f(CWEV_FORK\fR" 4 |
916 | .IX Item "EV_FORK" |
967 | .IX Item "EV_FORK" |
917 | The event loop has been resumed in the child process after fork (see |
968 | The event loop has been resumed in the child process after fork (see |
918 | \&\f(CW\*(C`ev_fork\*(C'\fR). |
969 | \&\f(CW\*(C`ev_fork\*(C'\fR). |
|
|
970 | .ie n .IP """EV_ASYNC""" 4 |
|
|
971 | .el .IP "\f(CWEV_ASYNC\fR" 4 |
|
|
972 | .IX Item "EV_ASYNC" |
|
|
973 | The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR). |
919 | .ie n .IP """EV_ERROR""" 4 |
974 | .ie n .IP """EV_ERROR""" 4 |
920 | .el .IP "\f(CWEV_ERROR\fR" 4 |
975 | .el .IP "\f(CWEV_ERROR\fR" 4 |
921 | .IX Item "EV_ERROR" |
976 | .IX Item "EV_ERROR" |
922 | An unspecified error has occured, the watcher has been stopped. This might |
977 | An unspecified error has occured, the watcher has been stopped. This might |
923 | happen because the watcher could not be properly started because libev |
978 | happen because the watcher could not be properly started because libev |
… | |
… | |
1137 | If you must do this, then force the use of a known-to-be-good backend |
1192 | If you must do this, then force the use of a known-to-be-good backend |
1138 | (at the time of this writing, this includes only \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and |
1193 | (at the time of this writing, this includes only \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and |
1139 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR). |
1194 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR). |
1140 | .PP |
1195 | .PP |
1141 | Another thing you have to watch out for is that it is quite easy to |
1196 | Another thing you have to watch out for is that it is quite easy to |
1142 | receive \*(L"spurious\*(R" readyness notifications, that is your callback might |
1197 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1143 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1198 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1144 | because there is no data. Not only are some backends known to create a |
1199 | because there is no data. Not only are some backends known to create a |
1145 | lot of those (for example solaris ports), it is very easy to get into |
1200 | lot of those (for example solaris ports), it is very easy to get into |
1146 | this situation even with a relatively standard program structure. Thus |
1201 | this situation even with a relatively standard program structure. Thus |
1147 | it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning |
1202 | it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning |
… | |
… | |
1196 | .PP |
1251 | .PP |
1197 | To support fork in your programs, you either have to call |
1252 | To support fork in your programs, you either have to call |
1198 | \&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child, |
1253 | \&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child, |
1199 | enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or |
1254 | enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or |
1200 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
1255 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
|
|
1256 | .PP |
|
|
1257 | \fIThe special problem of \s-1SIGPIPE\s0\fR |
|
|
1258 | .IX Subsection "The special problem of SIGPIPE" |
|
|
1259 | .PP |
|
|
1260 | While not really specific to libev, it is easy to forget about \s-1SIGPIPE:\s0 |
|
|
1261 | when reading from a pipe whose other end has been closed, your program |
|
|
1262 | gets send a \s-1SIGPIPE\s0, which, by default, aborts your program. For most |
|
|
1263 | programs this is sensible behaviour, for daemons, this is usually |
|
|
1264 | undesirable. |
|
|
1265 | .PP |
|
|
1266 | So when you encounter spurious, unexplained daemon exits, make sure you |
|
|
1267 | ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon |
|
|
1268 | somewhere, as that would have given you a big clue). |
1201 | .PP |
1269 | .PP |
1202 | \fIWatcher-Specific Functions\fR |
1270 | \fIWatcher-Specific Functions\fR |
1203 | .IX Subsection "Watcher-Specific Functions" |
1271 | .IX Subsection "Watcher-Specific Functions" |
1204 | .IP "ev_io_init (ev_io *, callback, int fd, int events)" 4 |
1272 | .IP "ev_io_init (ev_io *, callback, int fd, int events)" 4 |
1205 | .IX Item "ev_io_init (ev_io *, callback, int fd, int events)" |
1273 | .IX Item "ev_io_init (ev_io *, callback, int fd, int events)" |
… | |
… | |
1244 | .IX Subsection "ev_timer - relative and optionally repeating timeouts" |
1312 | .IX Subsection "ev_timer - relative and optionally repeating timeouts" |
1245 | Timer watchers are simple relative timers that generate an event after a |
1313 | Timer watchers are simple relative timers that generate an event after a |
1246 | given time, and optionally repeating in regular intervals after that. |
1314 | given time, and optionally repeating in regular intervals after that. |
1247 | .PP |
1315 | .PP |
1248 | The timers are based on real time, that is, if you register an event that |
1316 | The timers are based on real time, that is, if you register an event that |
1249 | times out after an hour and you reset your system clock to last years |
1317 | times out after an hour and you reset your system clock to january last |
1250 | time, it will still time out after (roughly) and hour. \*(L"Roughly\*(R" because |
1318 | year, it will still time out after (roughly) and hour. \*(L"Roughly\*(R" because |
1251 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1319 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1252 | monotonic clock option helps a lot here). |
1320 | monotonic clock option helps a lot here). |
1253 | .PP |
1321 | .PP |
1254 | The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR |
1322 | The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR |
1255 | time. This is usually the right thing as this timestamp refers to the time |
1323 | time. This is usually the right thing as this timestamp refers to the time |
… | |
… | |
1259 | .PP |
1327 | .PP |
1260 | .Vb 1 |
1328 | .Vb 1 |
1261 | \& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.); |
1329 | \& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.); |
1262 | .Ve |
1330 | .Ve |
1263 | .PP |
1331 | .PP |
1264 | The callback is guarenteed to be invoked only when its timeout has passed, |
1332 | The callback is guarenteed to be invoked only after its timeout has passed, |
1265 | but if multiple timers become ready during the same loop iteration then |
1333 | but if multiple timers become ready during the same loop iteration then |
1266 | order of execution is undefined. |
1334 | order of execution is undefined. |
1267 | .PP |
1335 | .PP |
1268 | \fIWatcher-Specific Functions and Data Members\fR |
1336 | \fIWatcher-Specific Functions and Data Members\fR |
1269 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1337 | .IX Subsection "Watcher-Specific Functions and Data Members" |
… | |
… | |
1271 | .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" |
1339 | .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" |
1272 | .PD 0 |
1340 | .PD 0 |
1273 | .IP "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" 4 |
1341 | .IP "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" 4 |
1274 | .IX Item "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" |
1342 | .IX Item "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" |
1275 | .PD |
1343 | .PD |
1276 | Configure the timer to trigger after \f(CW\*(C`after\*(C'\fR seconds. If \f(CW\*(C`repeat\*(C'\fR is |
1344 | Configure the timer to trigger after \f(CW\*(C`after\*(C'\fR seconds. If \f(CW\*(C`repeat\*(C'\fR |
1277 | \&\f(CW0.\fR, then it will automatically be stopped. If it is positive, then the |
1345 | is \f(CW0.\fR, then it will automatically be stopped once the timeout is |
1278 | timer will automatically be configured to trigger again \f(CW\*(C`repeat\*(C'\fR seconds |
1346 | reached. If it is positive, then the timer will automatically be |
1279 | later, again, and again, until stopped manually. |
1347 | configured to trigger again \f(CW\*(C`repeat\*(C'\fR seconds later, again, and again, |
|
|
1348 | until stopped manually. |
1280 | .Sp |
1349 | .Sp |
1281 | The timer itself will do a best-effort at avoiding drift, that is, if you |
1350 | The timer itself will do a best-effort at avoiding drift, that is, if |
1282 | configure a timer to trigger every 10 seconds, then it will trigger at |
1351 | you configure a timer to trigger every 10 seconds, then it will normally |
1283 | exactly 10 second intervals. If, however, your program cannot keep up with |
1352 | trigger at exactly 10 second intervals. If, however, your program cannot |
1284 | the timer (because it takes longer than those 10 seconds to do stuff) the |
1353 | keep up with the timer (because it takes longer than those 10 seconds to |
1285 | timer will not fire more than once per event loop iteration. |
1354 | do stuff) the timer will not fire more than once per event loop iteration. |
1286 | .IP "ev_timer_again (loop)" 4 |
1355 | .IP "ev_timer_again (loop, ev_timer *)" 4 |
1287 | .IX Item "ev_timer_again (loop)" |
1356 | .IX Item "ev_timer_again (loop, ev_timer *)" |
1288 | This will act as if the timer timed out and restart it again if it is |
1357 | This will act as if the timer timed out and restart it again if it is |
1289 | repeating. The exact semantics are: |
1358 | repeating. The exact semantics are: |
1290 | .Sp |
1359 | .Sp |
1291 | If the timer is pending, its pending status is cleared. |
1360 | If the timer is pending, its pending status is cleared. |
1292 | .Sp |
1361 | .Sp |
… | |
… | |
1369 | Periodic watchers are also timers of a kind, but they are very versatile |
1438 | Periodic watchers are also timers of a kind, but they are very versatile |
1370 | (and unfortunately a bit complex). |
1439 | (and unfortunately a bit complex). |
1371 | .PP |
1440 | .PP |
1372 | Unlike \f(CW\*(C`ev_timer\*(C'\fR's, they are not based on real time (or relative time) |
1441 | Unlike \f(CW\*(C`ev_timer\*(C'\fR's, they are not based on real time (or relative time) |
1373 | but on wallclock time (absolute time). You can tell a periodic watcher |
1442 | but on wallclock time (absolute time). You can tell a periodic watcher |
1374 | to trigger \*(L"at\*(R" some specific point in time. For example, if you tell a |
1443 | to trigger after some specific point in time. For example, if you tell a |
1375 | periodic watcher to trigger in 10 seconds (by specifiying e.g. \f(CW\*(C`ev_now () |
1444 | periodic watcher to trigger in 10 seconds (by specifiying e.g. \f(CW\*(C`ev_now () |
1376 | + 10.\*(C'\fR) and then reset your system clock to the last year, then it will |
1445 | + 10.\*(C'\fR, that is, an absolute time not a delay) and then reset your system |
|
|
1446 | clock to january of the previous year, then it will take more than year |
1377 | take a year to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would trigger |
1447 | to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger |
1378 | roughly 10 seconds later). |
1448 | roughly 10 seconds later as it uses a relative timeout). |
1379 | .PP |
1449 | .PP |
1380 | They can also be used to implement vastly more complex timers, such as |
1450 | \&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers, |
1381 | triggering an event on each midnight, local time or other, complicated, |
1451 | such as triggering an event on each \*(L"midnight, local time\*(R", or other |
1382 | rules. |
1452 | complicated, rules. |
1383 | .PP |
1453 | .PP |
1384 | As with timers, the callback is guarenteed to be invoked only when the |
1454 | As with timers, the callback is guarenteed to be invoked only when the |
1385 | time (\f(CW\*(C`at\*(C'\fR) has been passed, but if multiple periodic timers become ready |
1455 | time (\f(CW\*(C`at\*(C'\fR) has passed, but if multiple periodic timers become ready |
1386 | during the same loop iteration then order of execution is undefined. |
1456 | during the same loop iteration then order of execution is undefined. |
1387 | .PP |
1457 | .PP |
1388 | \fIWatcher-Specific Functions and Data Members\fR |
1458 | \fIWatcher-Specific Functions and Data Members\fR |
1389 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1459 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1390 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 |
1460 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 |
… | |
… | |
1397 | operation, and we will explain them from simplest to complex: |
1467 | operation, and we will explain them from simplest to complex: |
1398 | .RS 4 |
1468 | .RS 4 |
1399 | .IP "\(bu" 4 |
1469 | .IP "\(bu" 4 |
1400 | absolute timer (at = time, interval = reschedule_cb = 0) |
1470 | absolute timer (at = time, interval = reschedule_cb = 0) |
1401 | .Sp |
1471 | .Sp |
1402 | In this configuration the watcher triggers an event at the wallclock time |
1472 | In this configuration the watcher triggers an event after the wallclock |
1403 | \&\f(CW\*(C`at\*(C'\fR and doesn't repeat. It will not adjust when a time jump occurs, |
1473 | time \f(CW\*(C`at\*(C'\fR has passed and doesn't repeat. It will not adjust when a time |
1404 | that is, if it is to be run at January 1st 2011 then it will run when the |
1474 | jump occurs, that is, if it is to be run at January 1st 2011 then it will |
1405 | system time reaches or surpasses this time. |
1475 | run when the system time reaches or surpasses this time. |
1406 | .IP "\(bu" 4 |
1476 | .IP "\(bu" 4 |
1407 | non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1477 | repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1408 | .Sp |
1478 | .Sp |
1409 | In this mode the watcher will always be scheduled to time out at the next |
1479 | In this mode the watcher will always be scheduled to time out at the next |
1410 | \&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) |
1480 | \&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) |
1411 | and then repeat, regardless of any time jumps. |
1481 | and then repeat, regardless of any time jumps. |
1412 | .Sp |
1482 | .Sp |
1413 | This can be used to create timers that do not drift with respect to system |
1483 | This can be used to create timers that do not drift with respect to system |
1414 | time: |
1484 | time, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each hour, on |
|
|
1485 | the hour: |
1415 | .Sp |
1486 | .Sp |
1416 | .Vb 1 |
1487 | .Vb 1 |
1417 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1488 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1418 | .Ve |
1489 | .Ve |
1419 | .Sp |
1490 | .Sp |
… | |
… | |
1426 | \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible |
1497 | \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible |
1427 | time where \f(CW\*(C`time = at (mod interval)\*(C'\fR, regardless of any time jumps. |
1498 | time where \f(CW\*(C`time = at (mod interval)\*(C'\fR, regardless of any time jumps. |
1428 | .Sp |
1499 | .Sp |
1429 | For numerical stability it is preferable that the \f(CW\*(C`at\*(C'\fR value is near |
1500 | For numerical stability it is preferable that the \f(CW\*(C`at\*(C'\fR value is near |
1430 | \&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for |
1501 | \&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for |
1431 | this value. |
1502 | this value, and in fact is often specified as zero. |
|
|
1503 | .Sp |
|
|
1504 | Note also that there is an upper limit to how often a timer can fire (cpu |
|
|
1505 | speed for example), so if \f(CW\*(C`interval\*(C'\fR is very small then timing stability |
|
|
1506 | will of course detoriate. Libev itself tries to be exact to be about one |
|
|
1507 | millisecond (if the \s-1OS\s0 supports it and the machine is fast enough). |
1432 | .IP "\(bu" 4 |
1508 | .IP "\(bu" 4 |
1433 | manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1509 | manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1434 | .Sp |
1510 | .Sp |
1435 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`at\*(C'\fR are both being |
1511 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`at\*(C'\fR are both being |
1436 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1512 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1437 | reschedule callback will be called with the watcher as first, and the |
1513 | reschedule callback will be called with the watcher as first, and the |
1438 | current time as second argument. |
1514 | current time as second argument. |
1439 | .Sp |
1515 | .Sp |
1440 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, |
1516 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, |
1441 | ever, or make any event loop modifications\fR. If you need to stop it, |
1517 | ever, or make \s-1ANY\s0 event loop modifications whatsoever\fR. |
1442 | return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop it afterwards (e.g. by |
1518 | .Sp |
|
|
1519 | If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop |
1443 | starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is legal). |
1520 | it afterwards (e.g. by starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is the |
|
|
1521 | only event loop modification you are allowed to do). |
1444 | .Sp |
1522 | .Sp |
1445 | Its prototype is \f(CW\*(C`ev_tstamp (*reschedule_cb)(struct ev_periodic *w, |
1523 | The callback prototype is \f(CW\*(C`ev_tstamp (*reschedule_cb)(struct ev_periodic |
1446 | ev_tstamp now)\*(C'\fR, e.g.: |
1524 | *w, ev_tstamp now)\*(C'\fR, e.g.: |
1447 | .Sp |
1525 | .Sp |
1448 | .Vb 4 |
1526 | .Vb 4 |
1449 | \& static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
1527 | \& static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
1450 | \& { |
1528 | \& { |
1451 | \& return now + 60.; |
1529 | \& return now + 60.; |
… | |
… | |
1455 | It must return the next time to trigger, based on the passed time value |
1533 | It must return the next time to trigger, based on the passed time value |
1456 | (that is, the lowest time value larger than to the second argument). It |
1534 | (that is, the lowest time value larger than to the second argument). It |
1457 | will usually be called just before the callback will be triggered, but |
1535 | will usually be called just before the callback will be triggered, but |
1458 | might be called at other times, too. |
1536 | might be called at other times, too. |
1459 | .Sp |
1537 | .Sp |
1460 | \&\s-1NOTE:\s0 \fIThis callback must always return a time that is later than the |
1538 | \&\s-1NOTE:\s0 \fIThis callback must always return a time that is higher than or |
1461 | passed \f(CI\*(C`now\*(C'\fI value\fR. Not even \f(CW\*(C`now\*(C'\fR itself will do, it \fImust\fR be larger. |
1539 | equal to the passed \f(CI\*(C`now\*(C'\fI value\fR. |
1462 | .Sp |
1540 | .Sp |
1463 | This can be used to create very complex timers, such as a timer that |
1541 | This can be used to create very complex timers, such as a timer that |
1464 | triggers on each midnight, local time. To do this, you would calculate the |
1542 | triggers on \*(L"next midnight, local time\*(R". To do this, you would calculate the |
1465 | next midnight after \f(CW\*(C`now\*(C'\fR and return the timestamp value for this. How |
1543 | next midnight after \f(CW\*(C`now\*(C'\fR and return the timestamp value for this. How |
1466 | you do this is, again, up to you (but it is not trivial, which is the main |
1544 | you do this is, again, up to you (but it is not trivial, which is the main |
1467 | reason I omitted it as an example). |
1545 | reason I omitted it as an example). |
1468 | .RE |
1546 | .RE |
1469 | .RS 4 |
1547 | .RS 4 |
… | |
… | |
1472 | .IX Item "ev_periodic_again (loop, ev_periodic *)" |
1550 | .IX Item "ev_periodic_again (loop, ev_periodic *)" |
1473 | Simply stops and restarts the periodic watcher again. This is only useful |
1551 | Simply stops and restarts the periodic watcher again. This is only useful |
1474 | when you changed some parameters or the reschedule callback would return |
1552 | when you changed some parameters or the reschedule callback would return |
1475 | a different time than the last time it was called (e.g. in a crond like |
1553 | a different time than the last time it was called (e.g. in a crond like |
1476 | program when the crontabs have changed). |
1554 | program when the crontabs have changed). |
|
|
1555 | .IP "ev_tstamp ev_periodic_at (ev_periodic *)" 4 |
|
|
1556 | .IX Item "ev_tstamp ev_periodic_at (ev_periodic *)" |
|
|
1557 | When active, returns the absolute time that the watcher is supposed to |
|
|
1558 | trigger next. |
1477 | .IP "ev_tstamp offset [read\-write]" 4 |
1559 | .IP "ev_tstamp offset [read\-write]" 4 |
1478 | .IX Item "ev_tstamp offset [read-write]" |
1560 | .IX Item "ev_tstamp offset [read-write]" |
1479 | When repeating, this contains the offset value, otherwise this is the |
1561 | When repeating, this contains the offset value, otherwise this is the |
1480 | absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR). |
1562 | absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR). |
1481 | .Sp |
1563 | .Sp |
… | |
… | |
1489 | .IP "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read\-write]" 4 |
1571 | .IP "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read\-write]" 4 |
1490 | .IX Item "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write]" |
1572 | .IX Item "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write]" |
1491 | The current reschedule callback, or \f(CW0\fR, if this functionality is |
1573 | The current reschedule callback, or \f(CW0\fR, if this functionality is |
1492 | switched off. Can be changed any time, but changes only take effect when |
1574 | switched off. Can be changed any time, but changes only take effect when |
1493 | the periodic timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. |
1575 | the periodic timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. |
1494 | .IP "ev_tstamp at [read\-only]" 4 |
|
|
1495 | .IX Item "ev_tstamp at [read-only]" |
|
|
1496 | When active, contains the absolute time that the watcher is supposed to |
|
|
1497 | trigger next. |
|
|
1498 | .PP |
1576 | .PP |
1499 | \fIExamples\fR |
1577 | \fIExamples\fR |
1500 | .IX Subsection "Examples" |
1578 | .IX Subsection "Examples" |
1501 | .PP |
1579 | .PP |
1502 | Example: Call a callback every hour, or, more precisely, whenever the |
1580 | Example: Call a callback every hour, or, more precisely, whenever the |
… | |
… | |
1549 | first watcher gets started will libev actually register a signal watcher |
1627 | first watcher gets started will libev actually register a signal watcher |
1550 | with the kernel (thus it coexists with your own signal handlers as long |
1628 | with the kernel (thus it coexists with your own signal handlers as long |
1551 | as you don't register any with libev). Similarly, when the last signal |
1629 | as you don't register any with libev). Similarly, when the last signal |
1552 | watcher for a signal is stopped libev will reset the signal handler to |
1630 | watcher for a signal is stopped libev will reset the signal handler to |
1553 | \&\s-1SIG_DFL\s0 (regardless of what it was set to before). |
1631 | \&\s-1SIG_DFL\s0 (regardless of what it was set to before). |
|
|
1632 | .PP |
|
|
1633 | If possible and supported, libev will install its handlers with |
|
|
1634 | \&\f(CW\*(C`SA_RESTART\*(C'\fR behaviour enabled, so syscalls should not be unduly |
|
|
1635 | interrupted. If you have a problem with syscalls getting interrupted by |
|
|
1636 | signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock |
|
|
1637 | them in an \f(CW\*(C`ev_prepare\*(C'\fR watcher. |
1554 | .PP |
1638 | .PP |
1555 | \fIWatcher-Specific Functions and Data Members\fR |
1639 | \fIWatcher-Specific Functions and Data Members\fR |
1556 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1640 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1557 | .IP "ev_signal_init (ev_signal *, callback, int signum)" 4 |
1641 | .IP "ev_signal_init (ev_signal *, callback, int signum)" 4 |
1558 | .IX Item "ev_signal_init (ev_signal *, callback, int signum)" |
1642 | .IX Item "ev_signal_init (ev_signal *, callback, int signum)" |
… | |
… | |
1563 | Configures the watcher to trigger on the given signal number (usually one |
1647 | Configures the watcher to trigger on the given signal number (usually one |
1564 | of the \f(CW\*(C`SIGxxx\*(C'\fR constants). |
1648 | of the \f(CW\*(C`SIGxxx\*(C'\fR constants). |
1565 | .IP "int signum [read\-only]" 4 |
1649 | .IP "int signum [read\-only]" 4 |
1566 | .IX Item "int signum [read-only]" |
1650 | .IX Item "int signum [read-only]" |
1567 | The signal the watcher watches out for. |
1651 | The signal the watcher watches out for. |
|
|
1652 | .PP |
|
|
1653 | \fIExamples\fR |
|
|
1654 | .IX Subsection "Examples" |
|
|
1655 | .PP |
|
|
1656 | Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0. |
|
|
1657 | .PP |
|
|
1658 | .Vb 5 |
|
|
1659 | \& static void |
|
|
1660 | \& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
|
|
1661 | \& { |
|
|
1662 | \& ev_unloop (loop, EVUNLOOP_ALL); |
|
|
1663 | \& } |
|
|
1664 | \& |
|
|
1665 | \& struct ev_signal signal_watcher; |
|
|
1666 | \& ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
|
|
1667 | \& ev_signal_start (loop, &sigint_cb); |
|
|
1668 | .Ve |
1568 | .ie n .Sh """ev_child"" \- watch out for process status changes" |
1669 | .ie n .Sh """ev_child"" \- watch out for process status changes" |
1569 | .el .Sh "\f(CWev_child\fP \- watch out for process status changes" |
1670 | .el .Sh "\f(CWev_child\fP \- watch out for process status changes" |
1570 | .IX Subsection "ev_child - watch out for process status changes" |
1671 | .IX Subsection "ev_child - watch out for process status changes" |
1571 | Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to |
1672 | Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to |
1572 | some child status changes (most typically when a child of yours dies). |
1673 | some child status changes (most typically when a child of yours dies). It |
|
|
1674 | is permissible to install a child watcher \fIafter\fR the child has been |
|
|
1675 | forked (which implies it might have already exited), as long as the event |
|
|
1676 | loop isn't entered (or is continued from a watcher). |
|
|
1677 | .PP |
|
|
1678 | Only the default event loop is capable of handling signals, and therefore |
|
|
1679 | you can only rgeister child watchers in the default event loop. |
|
|
1680 | .PP |
|
|
1681 | \fIProcess Interaction\fR |
|
|
1682 | .IX Subsection "Process Interaction" |
|
|
1683 | .PP |
|
|
1684 | Libev grabs \f(CW\*(C`SIGCHLD\*(C'\fR as soon as the default event loop is |
|
|
1685 | initialised. This is necessary to guarantee proper behaviour even if |
|
|
1686 | the first child watcher is started after the child exits. The occurance |
|
|
1687 | of \f(CW\*(C`SIGCHLD\*(C'\fR is recorded asynchronously, but child reaping is done |
|
|
1688 | synchronously as part of the event loop processing. Libev always reaps all |
|
|
1689 | children, even ones not watched. |
|
|
1690 | .PP |
|
|
1691 | \fIOverriding the Built-In Processing\fR |
|
|
1692 | .IX Subsection "Overriding the Built-In Processing" |
|
|
1693 | .PP |
|
|
1694 | Libev offers no special support for overriding the built-in child |
|
|
1695 | processing, but if your application collides with libev's default child |
|
|
1696 | handler, you can override it easily by installing your own handler for |
|
|
1697 | \&\f(CW\*(C`SIGCHLD\*(C'\fR after initialising the default loop, and making sure the |
|
|
1698 | default loop never gets destroyed. You are encouraged, however, to use an |
|
|
1699 | event-based approach to child reaping and thus use libev's support for |
|
|
1700 | that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely. |
1573 | .PP |
1701 | .PP |
1574 | \fIWatcher-Specific Functions and Data Members\fR |
1702 | \fIWatcher-Specific Functions and Data Members\fR |
1575 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1703 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1576 | .IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 |
1704 | .IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 |
1577 | .IX Item "ev_child_init (ev_child *, callback, int pid, int trace)" |
1705 | .IX Item "ev_child_init (ev_child *, callback, int pid, int trace)" |
… | |
… | |
1599 | \&\f(CW\*(C`waitpid\*(C'\fR and \f(CW\*(C`sys/wait.h\*(C'\fR documentation for details). |
1727 | \&\f(CW\*(C`waitpid\*(C'\fR and \f(CW\*(C`sys/wait.h\*(C'\fR documentation for details). |
1600 | .PP |
1728 | .PP |
1601 | \fIExamples\fR |
1729 | \fIExamples\fR |
1602 | .IX Subsection "Examples" |
1730 | .IX Subsection "Examples" |
1603 | .PP |
1731 | .PP |
1604 | Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0. |
1732 | Example: \f(CW\*(C`fork()\*(C'\fR a new process and install a child handler to wait for |
|
|
1733 | its completion. |
1605 | .PP |
1734 | .PP |
1606 | .Vb 5 |
1735 | .Vb 1 |
|
|
1736 | \& ev_child cw; |
|
|
1737 | \& |
1607 | \& static void |
1738 | \& static void |
1608 | \& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
1739 | \& child_cb (EV_P_ struct ev_child *w, int revents) |
1609 | \& { |
1740 | \& { |
1610 | \& ev_unloop (loop, EVUNLOOP_ALL); |
1741 | \& ev_child_stop (EV_A_ w); |
|
|
1742 | \& printf ("process %d exited with status %x\en", w\->rpid, w\->rstatus); |
1611 | \& } |
1743 | \& } |
1612 | \& |
1744 | \& |
1613 | \& struct ev_signal signal_watcher; |
1745 | \& pid_t pid = fork (); |
1614 | \& ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
1746 | \& |
1615 | \& ev_signal_start (loop, &sigint_cb); |
1747 | \& if (pid < 0) |
|
|
1748 | \& // error |
|
|
1749 | \& else if (pid == 0) |
|
|
1750 | \& { |
|
|
1751 | \& // the forked child executes here |
|
|
1752 | \& exit (1); |
|
|
1753 | \& } |
|
|
1754 | \& else |
|
|
1755 | \& { |
|
|
1756 | \& ev_child_init (&cw, child_cb, pid, 0); |
|
|
1757 | \& ev_child_start (EV_DEFAULT_ &cw); |
|
|
1758 | \& } |
1616 | .Ve |
1759 | .Ve |
1617 | .ie n .Sh """ev_stat"" \- did the file attributes just change?" |
1760 | .ie n .Sh """ev_stat"" \- did the file attributes just change?" |
1618 | .el .Sh "\f(CWev_stat\fP \- did the file attributes just change?" |
1761 | .el .Sh "\f(CWev_stat\fP \- did the file attributes just change?" |
1619 | .IX Subsection "ev_stat - did the file attributes just change?" |
1762 | .IX Subsection "ev_stat - did the file attributes just change?" |
1620 | This watches a filesystem path for attribute changes. That is, it calls |
1763 | This watches a filesystem path for attribute changes. That is, it calls |
… | |
… | |
1643 | as even with OS-supported change notifications, this can be |
1786 | as even with OS-supported change notifications, this can be |
1644 | resource-intensive. |
1787 | resource-intensive. |
1645 | .PP |
1788 | .PP |
1646 | At the time of this writing, only the Linux inotify interface is |
1789 | At the time of this writing, only the Linux inotify interface is |
1647 | implemented (implementing kqueue support is left as an exercise for the |
1790 | implemented (implementing kqueue support is left as an exercise for the |
|
|
1791 | reader, note, however, that the author sees no way of implementing ev_stat |
1648 | reader). Inotify will be used to give hints only and should not change the |
1792 | semantics with kqueue). Inotify will be used to give hints only and should |
1649 | semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometimes needs |
1793 | not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev |
1650 | to fall back to regular polling again even with inotify, but changes are |
1794 | sometimes needs to fall back to regular polling again even with inotify, |
1651 | usually detected immediately, and if the file exists there will be no |
1795 | but changes are usually detected immediately, and if the file exists there |
1652 | polling. |
1796 | will be no polling. |
|
|
1797 | .PP |
|
|
1798 | \fI\s-1ABI\s0 Issues (Largefile Support)\fR |
|
|
1799 | .IX Subsection "ABI Issues (Largefile Support)" |
|
|
1800 | .PP |
|
|
1801 | Libev by default (unless the user overrides this) uses the default |
|
|
1802 | compilation environment, which means that on systems with optionally |
|
|
1803 | disabled large file support, you get the 32 bit version of the stat |
|
|
1804 | structure. When using the library from programs that change the \s-1ABI\s0 to |
|
|
1805 | use 64 bit file offsets the programs will fail. In that case you have to |
|
|
1806 | compile libev with the same flags to get binary compatibility. This is |
|
|
1807 | obviously the case with any flags that change the \s-1ABI\s0, but the problem is |
|
|
1808 | most noticably with ev_stat and largefile support. |
1653 | .PP |
1809 | .PP |
1654 | \fIInotify\fR |
1810 | \fIInotify\fR |
1655 | .IX Subsection "Inotify" |
1811 | .IX Subsection "Inotify" |
1656 | .PP |
1812 | .PP |
1657 | When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only |
1813 | When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only |
1658 | available on Linux) and present at runtime, it will be used to speed up |
1814 | available on Linux) and present at runtime, it will be used to speed up |
1659 | change detection where possible. The inotify descriptor will be created lazily |
1815 | change detection where possible. The inotify descriptor will be created lazily |
1660 | when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. |
1816 | when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. |
1661 | .PP |
1817 | .PP |
1662 | Inotify presense does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers |
1818 | Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers |
1663 | except that changes might be detected earlier, and in some cases, to avoid |
1819 | except that changes might be detected earlier, and in some cases, to avoid |
1664 | making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presense of inotify support |
1820 | making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support |
1665 | there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling. |
1821 | there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling. |
1666 | .PP |
1822 | .PP |
1667 | (There is no support for kqueue, as apparently it cannot be used to |
1823 | (There is no support for kqueue, as apparently it cannot be used to |
1668 | implement this functionality, due to the requirement of having a file |
1824 | implement this functionality, due to the requirement of having a file |
1669 | descriptor open on the object at all times). |
1825 | descriptor open on the object at all times). |
… | |
… | |
1673 | .PP |
1829 | .PP |
1674 | The \f(CW\*(C`stat ()\*(C'\fR syscall only supports full-second resolution portably, and |
1830 | The \f(CW\*(C`stat ()\*(C'\fR syscall only supports full-second resolution portably, and |
1675 | even on systems where the resolution is higher, many filesystems still |
1831 | even on systems where the resolution is higher, many filesystems still |
1676 | only support whole seconds. |
1832 | only support whole seconds. |
1677 | .PP |
1833 | .PP |
1678 | That means that, if the time is the only thing that changes, you might |
1834 | That means that, if the time is the only thing that changes, you can |
1679 | miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and calls |
1835 | easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and |
1680 | your callback, which does something. When there is another update within |
1836 | calls your callback, which does something. When there is another update |
1681 | the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it. |
1837 | within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it as the stat |
|
|
1838 | data does not change. |
1682 | .PP |
1839 | .PP |
1683 | The solution to this is to delay acting on a change for a second (or till |
1840 | The solution to this is to delay acting on a change for slightly more |
1684 | the next second boundary), using a roughly one-second delay \f(CW\*(C`ev_timer\*(C'\fR |
1841 | than a second (or till slightly after the next full second boundary), using |
1685 | (\f(CW\*(C`ev_timer_set (w, 0., 1.01); ev_timer_again (loop, w)\*(C'\fR). The \f(CW.01\fR |
1842 | a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02); |
1686 | is added to work around small timing inconsistencies of some operating |
1843 | ev_timer_again (loop, w)\*(C'\fR). |
1687 | systems. |
1844 | .PP |
|
|
1845 | The \f(CW.02\fR offset is added to work around small timing inconsistencies |
|
|
1846 | of some operating systems (where the second counter of the current time |
|
|
1847 | might be be delayed. One such system is the Linux kernel, where a call to |
|
|
1848 | \&\f(CW\*(C`gettimeofday\*(C'\fR might return a timestamp with a full second later than |
|
|
1849 | a subsequent \f(CW\*(C`time\*(C'\fR call \- if the equivalent of \f(CW\*(C`time ()\*(C'\fR is used to |
|
|
1850 | update file times then there will be a small window where the kernel uses |
|
|
1851 | the previous second to update file times but libev might already execute |
|
|
1852 | the timer callback). |
1688 | .PP |
1853 | .PP |
1689 | \fIWatcher-Specific Functions and Data Members\fR |
1854 | \fIWatcher-Specific Functions and Data Members\fR |
1690 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1855 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1691 | .IP "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 4 |
1856 | .IP "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 4 |
1692 | .IX Item "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" |
1857 | .IX Item "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" |
… | |
… | |
1698 | \&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to |
1863 | \&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to |
1699 | be detected and should normally be specified as \f(CW0\fR to let libev choose |
1864 | be detected and should normally be specified as \f(CW0\fR to let libev choose |
1700 | a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same |
1865 | a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same |
1701 | path for as long as the watcher is active. |
1866 | path for as long as the watcher is active. |
1702 | .Sp |
1867 | .Sp |
1703 | The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, |
1868 | The callback will receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, relative |
1704 | relative to the attributes at the time the watcher was started (or the |
1869 | to the attributes at the time the watcher was started (or the last change |
1705 | last change was detected). |
1870 | was detected). |
1706 | .IP "ev_stat_stat (ev_stat *)" 4 |
1871 | .IP "ev_stat_stat (loop, ev_stat *)" 4 |
1707 | .IX Item "ev_stat_stat (ev_stat *)" |
1872 | .IX Item "ev_stat_stat (loop, ev_stat *)" |
1708 | Updates the stat buffer immediately with new values. If you change the |
1873 | Updates the stat buffer immediately with new values. If you change the |
1709 | watched path in your callback, you could call this fucntion to avoid |
1874 | watched path in your callback, you could call this function to avoid |
1710 | detecting this change (while introducing a race condition). Can also be |
1875 | detecting this change (while introducing a race condition if you are not |
1711 | useful simply to find out the new values. |
1876 | the only one changing the path). Can also be useful simply to find out the |
|
|
1877 | new values. |
1712 | .IP "ev_statdata attr [read\-only]" 4 |
1878 | .IP "ev_statdata attr [read\-only]" 4 |
1713 | .IX Item "ev_statdata attr [read-only]" |
1879 | .IX Item "ev_statdata attr [read-only]" |
1714 | The most-recently detected attributes of the file. Although the type is of |
1880 | The most-recently detected attributes of the file. Although the type is |
1715 | \&\f(CW\*(C`ev_statdata\*(C'\fR, this is usually the (or one of the) \f(CW\*(C`struct stat\*(C'\fR types |
1881 | \&\f(CW\*(C`ev_statdata\*(C'\fR, this is usually the (or one of the) \f(CW\*(C`struct stat\*(C'\fR types |
|
|
1882 | suitable for your system, but you can only rely on the POSIX-standardised |
1716 | suitable for your system. If the \f(CW\*(C`st_nlink\*(C'\fR member is \f(CW0\fR, then there |
1883 | members to be present. If the \f(CW\*(C`st_nlink\*(C'\fR member is \f(CW0\fR, then there was |
1717 | was some error while \f(CW\*(C`stat\*(C'\fRing the file. |
1884 | some error while \f(CW\*(C`stat\*(C'\fRing the file. |
1718 | .IP "ev_statdata prev [read\-only]" 4 |
1885 | .IP "ev_statdata prev [read\-only]" 4 |
1719 | .IX Item "ev_statdata prev [read-only]" |
1886 | .IX Item "ev_statdata prev [read-only]" |
1720 | The previous attributes of the file. The callback gets invoked whenever |
1887 | The previous attributes of the file. The callback gets invoked whenever |
1721 | \&\f(CW\*(C`prev\*(C'\fR != \f(CW\*(C`attr\*(C'\fR. |
1888 | \&\f(CW\*(C`prev\*(C'\fR != \f(CW\*(C`attr\*(C'\fR, or, more precisely, one or more of these members |
|
|
1889 | differ: \f(CW\*(C`st_dev\*(C'\fR, \f(CW\*(C`st_ino\*(C'\fR, \f(CW\*(C`st_mode\*(C'\fR, \f(CW\*(C`st_nlink\*(C'\fR, \f(CW\*(C`st_uid\*(C'\fR, |
|
|
1890 | \&\f(CW\*(C`st_gid\*(C'\fR, \f(CW\*(C`st_rdev\*(C'\fR, \f(CW\*(C`st_size\*(C'\fR, \f(CW\*(C`st_atime\*(C'\fR, \f(CW\*(C`st_mtime\*(C'\fR, \f(CW\*(C`st_ctime\*(C'\fR. |
1722 | .IP "ev_tstamp interval [read\-only]" 4 |
1891 | .IP "ev_tstamp interval [read\-only]" 4 |
1723 | .IX Item "ev_tstamp interval [read-only]" |
1892 | .IX Item "ev_tstamp interval [read-only]" |
1724 | The specified interval. |
1893 | The specified interval. |
1725 | .IP "const char *path [read\-only]" 4 |
1894 | .IP "const char *path [read\-only]" 4 |
1726 | .IX Item "const char *path [read-only]" |
1895 | .IX Item "const char *path [read-only]" |
… | |
… | |
1780 | \& } |
1949 | \& } |
1781 | \& |
1950 | \& |
1782 | \& ... |
1951 | \& ... |
1783 | \& ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.); |
1952 | \& ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.); |
1784 | \& ev_stat_start (loop, &passwd); |
1953 | \& ev_stat_start (loop, &passwd); |
1785 | \& ev_timer_init (&timer, timer_cb, 0., 1.01); |
1954 | \& ev_timer_init (&timer, timer_cb, 0., 1.02); |
1786 | .Ve |
1955 | .Ve |
1787 | .ie n .Sh """ev_idle"" \- when you've got nothing better to do..." |
1956 | .ie n .Sh """ev_idle"" \- when you've got nothing better to do..." |
1788 | .el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." |
1957 | .el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." |
1789 | .IX Subsection "ev_idle - when you've got nothing better to do..." |
1958 | .IX Subsection "ev_idle - when you've got nothing better to do..." |
1790 | Idle watchers trigger events when no other events of the same or higher |
1959 | Idle watchers trigger events when no other events of the same or higher |
… | |
… | |
1876 | .PP |
2045 | .PP |
1877 | It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) |
2046 | It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) |
1878 | priority, to ensure that they are being run before any other watchers |
2047 | priority, to ensure that they are being run before any other watchers |
1879 | after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, |
2048 | after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, |
1880 | too) should not activate (\*(L"feed\*(R") events into libev. While libev fully |
2049 | too) should not activate (\*(L"feed\*(R") events into libev. While libev fully |
1881 | supports this, they will be called before other \f(CW\*(C`ev_check\*(C'\fR watchers |
2050 | supports this, they might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers |
1882 | did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other |
2051 | did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other |
1883 | (non-libev) event loops those other event loops might be in an unusable |
2052 | (non-libev) event loops those other event loops might be in an unusable |
1884 | state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to |
2053 | state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to |
1885 | coexist peacefully with others). |
2054 | coexist peacefully with others). |
1886 | .PP |
2055 | .PP |
… | |
… | |
1900 | .IX Subsection "Examples" |
2069 | .IX Subsection "Examples" |
1901 | .PP |
2070 | .PP |
1902 | There are a number of principal ways to embed other event loops or modules |
2071 | There are a number of principal ways to embed other event loops or modules |
1903 | into libev. Here are some ideas on how to include libadns into libev |
2072 | into libev. Here are some ideas on how to include libadns into libev |
1904 | (there is a Perl module named \f(CW\*(C`EV::ADNS\*(C'\fR that does this, which you could |
2073 | (there is a Perl module named \f(CW\*(C`EV::ADNS\*(C'\fR that does this, which you could |
1905 | use for an actually working example. Another Perl module named \f(CW\*(C`EV::Glib\*(C'\fR |
2074 | use as a working example. Another Perl module named \f(CW\*(C`EV::Glib\*(C'\fR embeds a |
1906 | embeds a Glib main context into libev, and finally, \f(CW\*(C`Glib::EV\*(C'\fR embeds \s-1EV\s0 |
2075 | Glib main context into libev, and finally, \f(CW\*(C`Glib::EV\*(C'\fR embeds \s-1EV\s0 into the |
1907 | into the Glib event loop). |
2076 | Glib event loop). |
1908 | .PP |
2077 | .PP |
1909 | Method 1: Add \s-1IO\s0 watchers and a timeout watcher in a prepare handler, |
2078 | Method 1: Add \s-1IO\s0 watchers and a timeout watcher in a prepare handler, |
1910 | and in a check watcher, destroy them and call into libadns. What follows |
2079 | and in a check watcher, destroy them and call into libadns. What follows |
1911 | is pseudo-code only of course. This requires you to either use a low |
2080 | is pseudo-code only of course. This requires you to either use a low |
1912 | priority for the check watcher or use \f(CW\*(C`ev_clear_pending\*(C'\fR explicitly, as |
2081 | priority for the check watcher or use \f(CW\*(C`ev_clear_pending\*(C'\fR explicitly, as |
… | |
… | |
2173 | .IP "ev_fork_init (ev_signal *, callback)" 4 |
2342 | .IP "ev_fork_init (ev_signal *, callback)" 4 |
2174 | .IX Item "ev_fork_init (ev_signal *, callback)" |
2343 | .IX Item "ev_fork_init (ev_signal *, callback)" |
2175 | Initialises and configures the fork watcher \- it has no parameters of any |
2344 | Initialises and configures the fork watcher \- it has no parameters of any |
2176 | kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless, |
2345 | kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless, |
2177 | believe me. |
2346 | believe me. |
|
|
2347 | .ie n .Sh """ev_async"" \- how to wake up another event loop" |
|
|
2348 | .el .Sh "\f(CWev_async\fP \- how to wake up another event loop" |
|
|
2349 | .IX Subsection "ev_async - how to wake up another event loop" |
|
|
2350 | In general, you cannot use an \f(CW\*(C`ev_loop\*(C'\fR from multiple threads or other |
|
|
2351 | asynchronous sources such as signal handlers (as opposed to multiple event |
|
|
2352 | loops \- those are of course safe to use in different threads). |
|
|
2353 | .PP |
|
|
2354 | Sometimes, however, you need to wake up another event loop you do not |
|
|
2355 | control, for example because it belongs to another thread. This is what |
|
|
2356 | \&\f(CW\*(C`ev_async\*(C'\fR watchers do: as long as the \f(CW\*(C`ev_async\*(C'\fR watcher is active, you |
|
|
2357 | can signal it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal |
|
|
2358 | safe. |
|
|
2359 | .PP |
|
|
2360 | This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals, |
|
|
2361 | too, are asynchronous in nature, and signals, too, will be compressed |
|
|
2362 | (i.e. the number of callback invocations may be less than the number of |
|
|
2363 | \&\f(CW\*(C`ev_async_sent\*(C'\fR calls). |
|
|
2364 | .PP |
|
|
2365 | Unlike \f(CW\*(C`ev_signal\*(C'\fR watchers, \f(CW\*(C`ev_async\*(C'\fR works with any event loop, not |
|
|
2366 | just the default loop. |
|
|
2367 | .PP |
|
|
2368 | \fIQueueing\fR |
|
|
2369 | .IX Subsection "Queueing" |
|
|
2370 | .PP |
|
|
2371 | \&\f(CW\*(C`ev_async\*(C'\fR does not support queueing of data in any way. The reason |
|
|
2372 | is that the author does not know of a simple (or any) algorithm for a |
|
|
2373 | multiple-writer-single-reader queue that works in all cases and doesn't |
|
|
2374 | need elaborate support such as pthreads. |
|
|
2375 | .PP |
|
|
2376 | That means that if you want to queue data, you have to provide your own |
|
|
2377 | queue. But at least I can tell you would implement locking around your |
|
|
2378 | queue: |
|
|
2379 | .IP "queueing from a signal handler context" 4 |
|
|
2380 | .IX Item "queueing from a signal handler context" |
|
|
2381 | To implement race-free queueing, you simply add to the queue in the signal |
|
|
2382 | handler but you block the signal handler in the watcher callback. Here is an example that does that for |
|
|
2383 | some fictitiuous \s-1SIGUSR1\s0 handler: |
|
|
2384 | .Sp |
|
|
2385 | .Vb 1 |
|
|
2386 | \& static ev_async mysig; |
|
|
2387 | \& |
|
|
2388 | \& static void |
|
|
2389 | \& sigusr1_handler (void) |
|
|
2390 | \& { |
|
|
2391 | \& sometype data; |
|
|
2392 | \& |
|
|
2393 | \& // no locking etc. |
|
|
2394 | \& queue_put (data); |
|
|
2395 | \& ev_async_send (EV_DEFAULT_ &mysig); |
|
|
2396 | \& } |
|
|
2397 | \& |
|
|
2398 | \& static void |
|
|
2399 | \& mysig_cb (EV_P_ ev_async *w, int revents) |
|
|
2400 | \& { |
|
|
2401 | \& sometype data; |
|
|
2402 | \& sigset_t block, prev; |
|
|
2403 | \& |
|
|
2404 | \& sigemptyset (&block); |
|
|
2405 | \& sigaddset (&block, SIGUSR1); |
|
|
2406 | \& sigprocmask (SIG_BLOCK, &block, &prev); |
|
|
2407 | \& |
|
|
2408 | \& while (queue_get (&data)) |
|
|
2409 | \& process (data); |
|
|
2410 | \& |
|
|
2411 | \& if (sigismember (&prev, SIGUSR1) |
|
|
2412 | \& sigprocmask (SIG_UNBLOCK, &block, 0); |
|
|
2413 | \& } |
|
|
2414 | .Ve |
|
|
2415 | .Sp |
|
|
2416 | (Note: pthreads in theory requires you to use \f(CW\*(C`pthread_setmask\*(C'\fR |
|
|
2417 | instead of \f(CW\*(C`sigprocmask\*(C'\fR when you use threads, but libev doesn't do it |
|
|
2418 | either...). |
|
|
2419 | .IP "queueing from a thread context" 4 |
|
|
2420 | .IX Item "queueing from a thread context" |
|
|
2421 | The strategy for threads is different, as you cannot (easily) block |
|
|
2422 | threads but you can easily preempt them, so to queue safely you need to |
|
|
2423 | employ a traditional mutex lock, such as in this pthread example: |
|
|
2424 | .Sp |
|
|
2425 | .Vb 2 |
|
|
2426 | \& static ev_async mysig; |
|
|
2427 | \& static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; |
|
|
2428 | \& |
|
|
2429 | \& static void |
|
|
2430 | \& otherthread (void) |
|
|
2431 | \& { |
|
|
2432 | \& // only need to lock the actual queueing operation |
|
|
2433 | \& pthread_mutex_lock (&mymutex); |
|
|
2434 | \& queue_put (data); |
|
|
2435 | \& pthread_mutex_unlock (&mymutex); |
|
|
2436 | \& |
|
|
2437 | \& ev_async_send (EV_DEFAULT_ &mysig); |
|
|
2438 | \& } |
|
|
2439 | \& |
|
|
2440 | \& static void |
|
|
2441 | \& mysig_cb (EV_P_ ev_async *w, int revents) |
|
|
2442 | \& { |
|
|
2443 | \& pthread_mutex_lock (&mymutex); |
|
|
2444 | \& |
|
|
2445 | \& while (queue_get (&data)) |
|
|
2446 | \& process (data); |
|
|
2447 | \& |
|
|
2448 | \& pthread_mutex_unlock (&mymutex); |
|
|
2449 | \& } |
|
|
2450 | .Ve |
|
|
2451 | .PP |
|
|
2452 | \fIWatcher-Specific Functions and Data Members\fR |
|
|
2453 | .IX Subsection "Watcher-Specific Functions and Data Members" |
|
|
2454 | .IP "ev_async_init (ev_async *, callback)" 4 |
|
|
2455 | .IX Item "ev_async_init (ev_async *, callback)" |
|
|
2456 | Initialises and configures the async watcher \- it has no parameters of any |
|
|
2457 | kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless, |
|
|
2458 | believe me. |
|
|
2459 | .IP "ev_async_send (loop, ev_async *)" 4 |
|
|
2460 | .IX Item "ev_async_send (loop, ev_async *)" |
|
|
2461 | Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds |
|
|
2462 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike |
|
|
2463 | \&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do in other threads, signal or |
|
|
2464 | similar contexts (see the dicusssion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
|
|
2465 | section below on what exactly this means). |
|
|
2466 | .Sp |
|
|
2467 | This call incurs the overhead of a syscall only once per loop iteration, |
|
|
2468 | so while the overhead might be noticable, it doesn't apply to repeated |
|
|
2469 | calls to \f(CW\*(C`ev_async_send\*(C'\fR. |
|
|
2470 | .IP "bool = ev_async_pending (ev_async *)" 4 |
|
|
2471 | .IX Item "bool = ev_async_pending (ev_async *)" |
|
|
2472 | Returns a non-zero value when \f(CW\*(C`ev_async_send\*(C'\fR has been called on the |
|
|
2473 | watcher but the event has not yet been processed (or even noted) by the |
|
|
2474 | event loop. |
|
|
2475 | .Sp |
|
|
2476 | \&\f(CW\*(C`ev_async_send\*(C'\fR sets a flag in the watcher and wakes up the loop. When |
|
|
2477 | the loop iterates next and checks for the watcher to have become active, |
|
|
2478 | it will reset the flag again. \f(CW\*(C`ev_async_pending\*(C'\fR can be used to very |
|
|
2479 | quickly check wether invoking the loop might be a good idea. |
|
|
2480 | .Sp |
|
|
2481 | Not that this does \fInot\fR check wether the watcher itself is pending, only |
|
|
2482 | wether it has been requested to make this watcher pending. |
2178 | .SH "OTHER FUNCTIONS" |
2483 | .SH "OTHER FUNCTIONS" |
2179 | .IX Header "OTHER FUNCTIONS" |
2484 | .IX Header "OTHER FUNCTIONS" |
2180 | There are some other functions of possible interest. Described. Here. Now. |
2485 | There are some other functions of possible interest. Described. Here. Now. |
2181 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
2486 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
2182 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
2487 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
… | |
… | |
2239 | it a private \s-1API\s0). |
2544 | it a private \s-1API\s0). |
2240 | .IP "\(bu" 4 |
2545 | .IP "\(bu" 4 |
2241 | Priorities are not currently supported. Initialising priorities |
2546 | Priorities are not currently supported. Initialising priorities |
2242 | will fail and all watchers will have the same priority, even though there |
2547 | will fail and all watchers will have the same priority, even though there |
2243 | is an ev_pri field. |
2548 | is an ev_pri field. |
|
|
2549 | .IP "\(bu" 4 |
|
|
2550 | In libevent, the last base created gets the signals, in libev, the |
|
|
2551 | first base created (== the default loop) gets the signals. |
2244 | .IP "\(bu" 4 |
2552 | .IP "\(bu" 4 |
2245 | Other members are not supported. |
2553 | Other members are not supported. |
2246 | .IP "\(bu" 4 |
2554 | .IP "\(bu" 4 |
2247 | The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need |
2555 | The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need |
2248 | to use the libev header file and library. |
2556 | to use the libev header file and library. |
… | |
… | |
2406 | \& |
2714 | \& |
2407 | \& io.start (fd, ev::READ); |
2715 | \& io.start (fd, ev::READ); |
2408 | \& } |
2716 | \& } |
2409 | \& }; |
2717 | \& }; |
2410 | .Ve |
2718 | .Ve |
|
|
2719 | .SH "OTHER LANGUAGE BINDINGS" |
|
|
2720 | .IX Header "OTHER LANGUAGE BINDINGS" |
|
|
2721 | Libev does not offer other language bindings itself, but bindings for a |
|
|
2722 | numbe rof languages exist in the form of third-party packages. If you know |
|
|
2723 | any interesting language binding in addition to the ones listed here, drop |
|
|
2724 | me a note. |
|
|
2725 | .IP "Perl" 4 |
|
|
2726 | .IX Item "Perl" |
|
|
2727 | The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test |
|
|
2728 | libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module, |
|
|
2729 | there are additional modules that implement libev-compatible interfaces |
|
|
2730 | to \f(CW\*(C`libadns\*(C'\fR (\f(CW\*(C`EV::ADNS\*(C'\fR), \f(CW\*(C`Net::SNMP\*(C'\fR (\f(CW\*(C`Net::SNMP::EV\*(C'\fR) and the |
|
|
2731 | \&\f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR and \f(CW\*(C`EV::Glib\*(C'\fR). |
|
|
2732 | .Sp |
|
|
2733 | It can be found and installed via \s-1CPAN\s0, its homepage is found at |
|
|
2734 | <http://software.schmorp.de/pkg/EV>. |
|
|
2735 | .IP "Ruby" 4 |
|
|
2736 | .IX Item "Ruby" |
|
|
2737 | Tony Arcieri has written a ruby extension that offers access to a subset |
|
|
2738 | of the libev \s-1API\s0 and adds filehandle abstractions, asynchronous \s-1DNS\s0 and |
|
|
2739 | more on top of it. It can be found via gem servers. Its homepage is at |
|
|
2740 | <http://rev.rubyforge.org/>. |
|
|
2741 | .IP "D" 4 |
|
|
2742 | .IX Item "D" |
|
|
2743 | Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to |
|
|
2744 | be found at <http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. |
2411 | .SH "MACRO MAGIC" |
2745 | .SH "MACRO MAGIC" |
2412 | .IX Header "MACRO MAGIC" |
2746 | .IX Header "MACRO MAGIC" |
2413 | Libev can be compiled with a variety of options, the most fundamantal |
2747 | Libev can be compiled with a variety of options, the most fundamantal |
2414 | of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) |
2748 | of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) |
2415 | functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. |
2749 | functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. |
… | |
… | |
2451 | .ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4 |
2785 | .ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4 |
2452 | .el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4 |
2786 | .el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4 |
2453 | .IX Item "EV_DEFAULT, EV_DEFAULT_" |
2787 | .IX Item "EV_DEFAULT, EV_DEFAULT_" |
2454 | Similar to the other two macros, this gives you the value of the default |
2788 | Similar to the other two macros, this gives you the value of the default |
2455 | loop, if multiple loops are supported (\*(L"ev loop default\*(R"). |
2789 | loop, if multiple loops are supported (\*(L"ev loop default\*(R"). |
|
|
2790 | .ie n .IP """EV_DEFAULT_UC""\fR, \f(CW""EV_DEFAULT_UC_""" 4 |
|
|
2791 | .el .IP "\f(CWEV_DEFAULT_UC\fR, \f(CWEV_DEFAULT_UC_\fR" 4 |
|
|
2792 | .IX Item "EV_DEFAULT_UC, EV_DEFAULT_UC_" |
|
|
2793 | Usage identical to \f(CW\*(C`EV_DEFAULT\*(C'\fR and \f(CW\*(C`EV_DEFAULT_\*(C'\fR, but requires that the |
|
|
2794 | default loop has been initialised (\f(CW\*(C`UC\*(C'\fR == unchecked). Their behaviour |
|
|
2795 | is undefined when the default loop has not been initialised by a previous |
|
|
2796 | execution of \f(CW\*(C`EV_DEFAULT\*(C'\fR, \f(CW\*(C`EV_DEFAULT_\*(C'\fR or \f(CW\*(C`ev_default_init (...)\*(C'\fR. |
|
|
2797 | .Sp |
|
|
2798 | It is often prudent to use \f(CW\*(C`EV_DEFAULT\*(C'\fR when initialising the first |
|
|
2799 | watcher in a function but use \f(CW\*(C`EV_DEFAULT_UC\*(C'\fR afterwards. |
2456 | .PP |
2800 | .PP |
2457 | Example: Declare and initialise a check watcher, utilising the above |
2801 | Example: Declare and initialise a check watcher, utilising the above |
2458 | macros so it will work regardless of whether multiple loops are supported |
2802 | macros so it will work regardless of whether multiple loops are supported |
2459 | or not. |
2803 | or not. |
2460 | .PP |
2804 | .PP |
… | |
… | |
2570 | .Vb 1 |
2914 | .Vb 1 |
2571 | \& libev.m4 |
2915 | \& libev.m4 |
2572 | .Ve |
2916 | .Ve |
2573 | .Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" |
2917 | .Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" |
2574 | .IX Subsection "PREPROCESSOR SYMBOLS/MACROS" |
2918 | .IX Subsection "PREPROCESSOR SYMBOLS/MACROS" |
2575 | Libev can be configured via a variety of preprocessor symbols you have to define |
2919 | Libev can be configured via a variety of preprocessor symbols you have to |
2576 | before including any of its files. The default is not to build for multiplicity |
2920 | define before including any of its files. The default in the absense of |
2577 | and only include the select backend. |
2921 | autoconf is noted for every option. |
2578 | .IP "\s-1EV_STANDALONE\s0" 4 |
2922 | .IP "\s-1EV_STANDALONE\s0" 4 |
2579 | .IX Item "EV_STANDALONE" |
2923 | .IX Item "EV_STANDALONE" |
2580 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
2924 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
2581 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
2925 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
2582 | implementations for some libevent functions (such as logging, which is not |
2926 | implementations for some libevent functions (such as logging, which is not |
… | |
… | |
2601 | note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. |
2945 | note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. |
2602 | .IP "\s-1EV_USE_NANOSLEEP\s0" 4 |
2946 | .IP "\s-1EV_USE_NANOSLEEP\s0" 4 |
2603 | .IX Item "EV_USE_NANOSLEEP" |
2947 | .IX Item "EV_USE_NANOSLEEP" |
2604 | If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available |
2948 | If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available |
2605 | and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. |
2949 | and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. |
|
|
2950 | .IP "\s-1EV_USE_EVENTFD\s0" 4 |
|
|
2951 | .IX Item "EV_USE_EVENTFD" |
|
|
2952 | If defined to be \f(CW1\fR, then libev will assume that \f(CW\*(C`eventfd ()\*(C'\fR is |
|
|
2953 | available and will probe for kernel support at runtime. This will improve |
|
|
2954 | \&\f(CW\*(C`ev_signal\*(C'\fR and \f(CW\*(C`ev_async\*(C'\fR performance and reduce resource consumption. |
|
|
2955 | If undefined, it will be enabled if the headers indicate GNU/Linux + Glibc |
|
|
2956 | 2.7 or newer, otherwise disabled. |
2606 | .IP "\s-1EV_USE_SELECT\s0" 4 |
2957 | .IP "\s-1EV_USE_SELECT\s0" 4 |
2607 | .IX Item "EV_USE_SELECT" |
2958 | .IX Item "EV_USE_SELECT" |
2608 | If undefined or defined to be \f(CW1\fR, libev will compile in support for the |
2959 | If undefined or defined to be \f(CW1\fR, libev will compile in support for the |
2609 | \&\f(CW\*(C`select\*(C'\fR(2) backend. No attempt at autodetection will be done: if no |
2960 | \&\f(CW\*(C`select\*(C'\fR(2) backend. No attempt at autodetection will be done: if no |
2610 | other method takes over, select will be it. Otherwise the select backend |
2961 | other method takes over, select will be it. Otherwise the select backend |
… | |
… | |
2641 | takes precedence over select. |
2992 | takes precedence over select. |
2642 | .IP "\s-1EV_USE_EPOLL\s0" 4 |
2993 | .IP "\s-1EV_USE_EPOLL\s0" 4 |
2643 | .IX Item "EV_USE_EPOLL" |
2994 | .IX Item "EV_USE_EPOLL" |
2644 | If defined to be \f(CW1\fR, libev will compile in support for the Linux |
2995 | If defined to be \f(CW1\fR, libev will compile in support for the Linux |
2645 | \&\f(CW\*(C`epoll\*(C'\fR(7) backend. Its availability will be detected at runtime, |
2996 | \&\f(CW\*(C`epoll\*(C'\fR(7) backend. Its availability will be detected at runtime, |
2646 | otherwise another method will be used as fallback. This is the |
2997 | otherwise another method will be used as fallback. This is the preferred |
2647 | preferred backend for GNU/Linux systems. |
2998 | backend for GNU/Linux systems. If undefined, it will be enabled if the |
|
|
2999 | headers indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
2648 | .IP "\s-1EV_USE_KQUEUE\s0" 4 |
3000 | .IP "\s-1EV_USE_KQUEUE\s0" 4 |
2649 | .IX Item "EV_USE_KQUEUE" |
3001 | .IX Item "EV_USE_KQUEUE" |
2650 | If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style |
3002 | If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style |
2651 | \&\f(CW\*(C`kqueue\*(C'\fR(2) backend. Its actual availability will be detected at runtime, |
3003 | \&\f(CW\*(C`kqueue\*(C'\fR(2) backend. Its actual availability will be detected at runtime, |
2652 | otherwise another method will be used as fallback. This is the preferred |
3004 | otherwise another method will be used as fallback. This is the preferred |
… | |
… | |
2667 | reserved for future expansion, works like the \s-1USE\s0 symbols above. |
3019 | reserved for future expansion, works like the \s-1USE\s0 symbols above. |
2668 | .IP "\s-1EV_USE_INOTIFY\s0" 4 |
3020 | .IP "\s-1EV_USE_INOTIFY\s0" 4 |
2669 | .IX Item "EV_USE_INOTIFY" |
3021 | .IX Item "EV_USE_INOTIFY" |
2670 | If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify |
3022 | If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify |
2671 | interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will |
3023 | interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will |
2672 | be detected at runtime. |
3024 | be detected at runtime. If undefined, it will be enabled if the headers |
|
|
3025 | indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
|
|
3026 | .IP "\s-1EV_ATOMIC_T\s0" 4 |
|
|
3027 | .IX Item "EV_ATOMIC_T" |
|
|
3028 | Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose |
|
|
3029 | access is atomic with respect to other threads or signal contexts. No such |
|
|
3030 | type is easily found in the C language, so you can provide your own type |
|
|
3031 | that you know is safe for your purposes. It is used both for signal handler \*(L"locking\*(R" |
|
|
3032 | as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR watchers. |
|
|
3033 | .Sp |
|
|
3034 | In the absense of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR |
|
|
3035 | (from \fIsignal.h\fR), which is usually good enough on most platforms. |
2673 | .IP "\s-1EV_H\s0" 4 |
3036 | .IP "\s-1EV_H\s0" 4 |
2674 | .IX Item "EV_H" |
3037 | .IX Item "EV_H" |
2675 | The name of the \fIev.h\fR header file used to include it. The default if |
3038 | The name of the \fIev.h\fR header file used to include it. The default if |
2676 | undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be |
3039 | undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be |
2677 | used to virtually rename the \fIev.h\fR header file in case of conflicts. |
3040 | used to virtually rename the \fIev.h\fR header file in case of conflicts. |
… | |
… | |
2735 | defined to be \f(CW0\fR, then they are not. |
3098 | defined to be \f(CW0\fR, then they are not. |
2736 | .IP "\s-1EV_FORK_ENABLE\s0" 4 |
3099 | .IP "\s-1EV_FORK_ENABLE\s0" 4 |
2737 | .IX Item "EV_FORK_ENABLE" |
3100 | .IX Item "EV_FORK_ENABLE" |
2738 | If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If |
3101 | If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If |
2739 | defined to be \f(CW0\fR, then they are not. |
3102 | defined to be \f(CW0\fR, then they are not. |
|
|
3103 | .IP "\s-1EV_ASYNC_ENABLE\s0" 4 |
|
|
3104 | .IX Item "EV_ASYNC_ENABLE" |
|
|
3105 | If undefined or defined to be \f(CW1\fR, then async watchers are supported. If |
|
|
3106 | defined to be \f(CW0\fR, then they are not. |
2740 | .IP "\s-1EV_MINIMAL\s0" 4 |
3107 | .IP "\s-1EV_MINIMAL\s0" 4 |
2741 | .IX Item "EV_MINIMAL" |
3108 | .IX Item "EV_MINIMAL" |
2742 | If you need to shave off some kilobytes of code at the expense of some |
3109 | If you need to shave off some kilobytes of code at the expense of some |
2743 | speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override |
3110 | speed, define this symbol to \f(CW1\fR. Currently this is used to override some |
2744 | some inlining decisions, saves roughly 30% codesize of amd64. |
3111 | inlining decisions, saves roughly 30% codesize of amd64. It also selects a |
|
|
3112 | much smaller 2\-heap for timer management over the default 4\-heap. |
2745 | .IP "\s-1EV_PID_HASHSIZE\s0" 4 |
3113 | .IP "\s-1EV_PID_HASHSIZE\s0" 4 |
2746 | .IX Item "EV_PID_HASHSIZE" |
3114 | .IX Item "EV_PID_HASHSIZE" |
2747 | \&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by |
3115 | \&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by |
2748 | pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more |
3116 | pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more |
2749 | than enough. If you need to manage thousands of children you might want to |
3117 | than enough. If you need to manage thousands of children you might want to |
… | |
… | |
2753 | \&\f(CW\*(C`ev_stat\*(C'\fR watchers use a small hash table to distribute workload by |
3121 | \&\f(CW\*(C`ev_stat\*(C'\fR watchers use a small hash table to distribute workload by |
2754 | inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), |
3122 | inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), |
2755 | usually more than enough. If you need to manage thousands of \f(CW\*(C`ev_stat\*(C'\fR |
3123 | usually more than enough. If you need to manage thousands of \f(CW\*(C`ev_stat\*(C'\fR |
2756 | watchers you might want to increase this value (\fImust\fR be a power of |
3124 | watchers you might want to increase this value (\fImust\fR be a power of |
2757 | two). |
3125 | two). |
|
|
3126 | .IP "\s-1EV_USE_4HEAP\s0" 4 |
|
|
3127 | .IX Item "EV_USE_4HEAP" |
|
|
3128 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
|
|
3129 | timer and periodics heap, libev uses a 4\-heap when this symbol is defined |
|
|
3130 | to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has |
|
|
3131 | noticably faster performance with many (thousands) of watchers. |
|
|
3132 | .Sp |
|
|
3133 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
|
|
3134 | (disabled). |
|
|
3135 | .IP "\s-1EV_HEAP_CACHE_AT\s0" 4 |
|
|
3136 | .IX Item "EV_HEAP_CACHE_AT" |
|
|
3137 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
|
|
3138 | timer and periodics heap, libev can cache the timestamp (\fIat\fR) within |
|
|
3139 | the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR), |
|
|
3140 | which uses 8\-12 bytes more per watcher and a few hundred bytes more code, |
|
|
3141 | but avoids random read accesses on heap changes. This improves performance |
|
|
3142 | noticably with with many (hundreds) of watchers. |
|
|
3143 | .Sp |
|
|
3144 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
|
|
3145 | (disabled). |
|
|
3146 | .IP "\s-1EV_VERIFY\s0" 4 |
|
|
3147 | .IX Item "EV_VERIFY" |
|
|
3148 | Controls how much internal verification (see \f(CW\*(C`ev_loop_verify ()\*(C'\fR) will |
|
|
3149 | be done: If set to \f(CW0\fR, no internal verification code will be compiled |
|
|
3150 | in. If set to \f(CW1\fR, then verification code will be compiled in, but not |
|
|
3151 | called. If set to \f(CW2\fR, then the internal verification code will be |
|
|
3152 | called once per loop, which can slow down libev. If set to \f(CW3\fR, then the |
|
|
3153 | verification code will be called very frequently, which will slow down |
|
|
3154 | libev considerably. |
|
|
3155 | .Sp |
|
|
3156 | The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be |
|
|
3157 | \&\f(CW0.\fR |
2758 | .IP "\s-1EV_COMMON\s0" 4 |
3158 | .IP "\s-1EV_COMMON\s0" 4 |
2759 | .IX Item "EV_COMMON" |
3159 | .IX Item "EV_COMMON" |
2760 | By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining |
3160 | By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining |
2761 | this macro to a something else you can include more and other types of |
3161 | this macro to a something else you can include more and other types of |
2762 | members. You have to define it each time you include one of the files, |
3162 | members. You have to define it each time you include one of the files, |
… | |
… | |
2844 | .PP |
3244 | .PP |
2845 | .Vb 2 |
3245 | .Vb 2 |
2846 | \& #include "ev_cpp.h" |
3246 | \& #include "ev_cpp.h" |
2847 | \& #include "ev.c" |
3247 | \& #include "ev.c" |
2848 | .Ve |
3248 | .Ve |
|
|
3249 | .SH "THREADS AND COROUTINES" |
|
|
3250 | .IX Header "THREADS AND COROUTINES" |
|
|
3251 | .Sh "\s-1THREADS\s0" |
|
|
3252 | .IX Subsection "THREADS" |
|
|
3253 | Libev itself is completely threadsafe, but it uses no locking. This |
|
|
3254 | means that you can use as many loops as you want in parallel, as long as |
|
|
3255 | only one thread ever calls into one libev function with the same loop |
|
|
3256 | parameter. |
|
|
3257 | .PP |
|
|
3258 | Or put differently: calls with different loop parameters can be done in |
|
|
3259 | parallel from multiple threads, calls with the same loop parameter must be |
|
|
3260 | done serially (but can be done from different threads, as long as only one |
|
|
3261 | thread ever is inside a call at any point in time, e.g. by using a mutex |
|
|
3262 | per loop). |
|
|
3263 | .PP |
|
|
3264 | If you want to know which design is best for your problem, then I cannot |
|
|
3265 | help you but by giving some generic advice: |
|
|
3266 | .IP "\(bu" 4 |
|
|
3267 | most applications have a main thread: use the default libev loop |
|
|
3268 | in that thread, or create a seperate thread running only the default loop. |
|
|
3269 | .Sp |
|
|
3270 | This helps integrating other libraries or software modules that use libev |
|
|
3271 | themselves and don't care/know about threading. |
|
|
3272 | .IP "\(bu" 4 |
|
|
3273 | one loop per thread is usually a good model. |
|
|
3274 | .Sp |
|
|
3275 | Doing this is almost never wrong, sometimes a better-performance model |
|
|
3276 | exists, but it is always a good start. |
|
|
3277 | .IP "\(bu" 4 |
|
|
3278 | other models exist, such as the leader/follower pattern, where one |
|
|
3279 | loop is handed through multiple threads in a kind of round-robbin fashion. |
|
|
3280 | .Sp |
|
|
3281 | Chosing a model is hard \- look around, learn, know that usually you cna do |
|
|
3282 | better than you currently do :\-) |
|
|
3283 | .IP "\(bu" 4 |
|
|
3284 | often you need to talk to some other thread which blocks in the |
|
|
3285 | event loop \- \f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other |
|
|
3286 | threads safely (or from signal contexts...). |
|
|
3287 | .Sh "\s-1COROUTINES\s0" |
|
|
3288 | .IX Subsection "COROUTINES" |
|
|
3289 | Libev is much more accomodating to coroutines (\*(L"cooperative threads\*(R"): |
|
|
3290 | libev fully supports nesting calls to it's functions from different |
|
|
3291 | coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two |
|
|
3292 | different coroutines and switch freely between both coroutines running the |
|
|
3293 | loop, as long as you don't confuse yourself). The only exception is that |
|
|
3294 | you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks. |
|
|
3295 | .PP |
|
|
3296 | Care has been invested into making sure that libev does not keep local |
|
|
3297 | state inside \f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine |
|
|
3298 | switches. |
2849 | .SH "COMPLEXITIES" |
3299 | .SH "COMPLEXITIES" |
2850 | .IX Header "COMPLEXITIES" |
3300 | .IX Header "COMPLEXITIES" |
2851 | In this section the complexities of (many of) the algorithms used inside |
3301 | In this section the complexities of (many of) the algorithms used inside |
2852 | libev will be explained. For complexity discussions about backends see the |
3302 | libev will be explained. For complexity discussions about backends see the |
2853 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
3303 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
… | |
… | |
2864 | have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers. |
3314 | have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers. |
2865 | .IP "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 4 |
3315 | .IP "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 4 |
2866 | .IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" |
3316 | .IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" |
2867 | That means that changing a timer costs less than removing/adding them |
3317 | That means that changing a timer costs less than removing/adding them |
2868 | as only the relative motion in the event queue has to be paid for. |
3318 | as only the relative motion in the event queue has to be paid for. |
2869 | .IP "Starting io/check/prepare/idle/signal/child watchers: O(1)" 4 |
3319 | .IP "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)" 4 |
2870 | .IX Item "Starting io/check/prepare/idle/signal/child watchers: O(1)" |
3320 | .IX Item "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)" |
2871 | These just add the watcher into an array or at the head of a list. |
3321 | These just add the watcher into an array or at the head of a list. |
2872 | .IP "Stopping check/prepare/idle watchers: O(1)" 4 |
3322 | .IP "Stopping check/prepare/idle/fork/async watchers: O(1)" 4 |
2873 | .IX Item "Stopping check/prepare/idle watchers: O(1)" |
3323 | .IX Item "Stopping check/prepare/idle/fork/async watchers: O(1)" |
2874 | .PD 0 |
3324 | .PD 0 |
2875 | .IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4 |
3325 | .IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4 |
2876 | .IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))" |
3326 | .IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))" |
2877 | .PD |
3327 | .PD |
2878 | These watchers are stored in lists then need to be walked to find the |
3328 | These watchers are stored in lists then need to be walked to find the |
2879 | correct watcher to remove. The lists are usually short (you don't usually |
3329 | correct watcher to remove. The lists are usually short (you don't usually |
2880 | have many watchers waiting for the same fd or signal). |
3330 | have many watchers waiting for the same fd or signal). |
2881 | .IP "Finding the next timer in each loop iteration: O(1)" 4 |
3331 | .IP "Finding the next timer in each loop iteration: O(1)" 4 |
2882 | .IX Item "Finding the next timer in each loop iteration: O(1)" |
3332 | .IX Item "Finding the next timer in each loop iteration: O(1)" |
2883 | By virtue of using a binary heap, the next timer is always found at the |
3333 | By virtue of using a binary or 4\-heap, the next timer is always found at a |
2884 | beginning of the storage array. |
3334 | fixed position in the storage array. |
2885 | .IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 |
3335 | .IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 |
2886 | .IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" |
3336 | .IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" |
2887 | A change means an I/O watcher gets started or stopped, which requires |
3337 | A change means an I/O watcher gets started or stopped, which requires |
2888 | libev to recalculate its status (and possibly tell the kernel, depending |
3338 | libev to recalculate its status (and possibly tell the kernel, depending |
2889 | on backend and wether \f(CW\*(C`ev_io_set\*(C'\fR was used). |
3339 | on backend and wether \f(CW\*(C`ev_io_set\*(C'\fR was used). |
… | |
… | |
2894 | .IX Item "Priority handling: O(number_of_priorities)" |
3344 | .IX Item "Priority handling: O(number_of_priorities)" |
2895 | .PD |
3345 | .PD |
2896 | Priorities are implemented by allocating some space for each |
3346 | Priorities are implemented by allocating some space for each |
2897 | priority. When doing priority-based operations, libev usually has to |
3347 | priority. When doing priority-based operations, libev usually has to |
2898 | linearly search all the priorities, but starting/stopping and activating |
3348 | linearly search all the priorities, but starting/stopping and activating |
2899 | watchers becomes O(1) w.r.t. prioritiy handling. |
3349 | watchers becomes O(1) w.r.t. priority handling. |
|
|
3350 | .IP "Sending an ev_async: O(1)" 4 |
|
|
3351 | .IX Item "Sending an ev_async: O(1)" |
|
|
3352 | .PD 0 |
|
|
3353 | .IP "Processing ev_async_send: O(number_of_async_watchers)" 4 |
|
|
3354 | .IX Item "Processing ev_async_send: O(number_of_async_watchers)" |
|
|
3355 | .IP "Processing signals: O(max_signal_number)" 4 |
|
|
3356 | .IX Item "Processing signals: O(max_signal_number)" |
|
|
3357 | .PD |
|
|
3358 | Sending involves a syscall \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR |
|
|
3359 | calls in the current loop iteration. Checking for async and signal events |
|
|
3360 | involves iterating over all running async watchers or all signal numbers. |
2900 | .SH "Win32 platform limitations and workarounds" |
3361 | .SH "Win32 platform limitations and workarounds" |
2901 | .IX Header "Win32 platform limitations and workarounds" |
3362 | .IX Header "Win32 platform limitations and workarounds" |
2902 | Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev |
3363 | Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev |
2903 | requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0 |
3364 | requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0 |
2904 | model. Libev still offers limited functionality on this platform in |
3365 | model. Libev still offers limited functionality on this platform in |
2905 | the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket |
3366 | the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket |
2906 | descriptors. This only applies when using Win32 natively, not when using |
3367 | descriptors. This only applies when using Win32 natively, not when using |
2907 | e.g. cygwin. |
3368 | e.g. cygwin. |
2908 | .PP |
3369 | .PP |
|
|
3370 | Lifting these limitations would basically require the full |
|
|
3371 | re-implementation of the I/O system. If you are into these kinds of |
|
|
3372 | things, then note that glib does exactly that for you in a very portable |
|
|
3373 | way (note also that glib is the slowest event library known to man). |
|
|
3374 | .PP |
2909 | There is no supported compilation method available on windows except |
3375 | There is no supported compilation method available on windows except |
2910 | embedding it into other applications. |
3376 | embedding it into other applications. |
2911 | .PP |
3377 | .PP |
2912 | Due to the many, low, and arbitrary limits on the win32 platform and the |
3378 | Due to the many, low, and arbitrary limits on the win32 platform and |
2913 | abysmal performance of winsockets, using a large number of sockets is not |
3379 | the abysmal performance of winsockets, using a large number of sockets |
2914 | recommended (and not reasonable). If your program needs to use more than |
3380 | is not recommended (and not reasonable). If your program needs to use |
2915 | a hundred or so sockets, then likely it needs to use a totally different |
3381 | more than a hundred or so sockets, then likely it needs to use a totally |
2916 | implementation for windows, as libev offers the \s-1POSIX\s0 model, which cannot |
3382 | different implementation for windows, as libev offers the \s-1POSIX\s0 readiness |
2917 | be implemented efficiently on windows (microsoft monopoly games). |
3383 | notification model, which cannot be implemented efficiently on windows |
|
|
3384 | (microsoft monopoly games). |
2918 | .IP "The winsocket select function" 4 |
3385 | .IP "The winsocket select function" 4 |
2919 | .IX Item "The winsocket select function" |
3386 | .IX Item "The winsocket select function" |
2920 | The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it requires |
3387 | The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it |
2921 | socket \fIhandles\fR and not socket \fIfile descriptors\fR. This makes select |
3388 | requires socket \fIhandles\fR and not socket \fIfile descriptors\fR (it is |
2922 | very inefficient, and also requires a mapping from file descriptors |
3389 | also extremely buggy). This makes select very inefficient, and also |
2923 | to socket handles. See the discussion of the \f(CW\*(C`EV_SELECT_USE_FD_SET\*(C'\fR, |
3390 | requires a mapping from file descriptors to socket handles. See the |
2924 | \&\f(CW\*(C`EV_SELECT_IS_WINSOCKET\*(C'\fR and \f(CW\*(C`EV_FD_TO_WIN32_HANDLE\*(C'\fR preprocessor |
3391 | discussion of the \f(CW\*(C`EV_SELECT_USE_FD_SET\*(C'\fR, \f(CW\*(C`EV_SELECT_IS_WINSOCKET\*(C'\fR and |
2925 | symbols for more info. |
3392 | \&\f(CW\*(C`EV_FD_TO_WIN32_HANDLE\*(C'\fR preprocessor symbols for more info. |
2926 | .Sp |
3393 | .Sp |
2927 | The configuration for a \*(L"naked\*(R" win32 using the microsoft runtime |
3394 | The configuration for a \*(L"naked\*(R" win32 using the microsoft runtime |
2928 | libraries and raw winsocket select is: |
3395 | libraries and raw winsocket select is: |
2929 | .Sp |
3396 | .Sp |
2930 | .Vb 2 |
3397 | .Vb 2 |
… | |
… | |
2934 | .Sp |
3401 | .Sp |
2935 | Note that winsockets handling of fd sets is O(n), so you can easily get a |
3402 | Note that winsockets handling of fd sets is O(n), so you can easily get a |
2936 | complexity in the O(nA\*^X) range when using win32. |
3403 | complexity in the O(nA\*^X) range when using win32. |
2937 | .IP "Limited number of file descriptors" 4 |
3404 | .IP "Limited number of file descriptors" 4 |
2938 | .IX Item "Limited number of file descriptors" |
3405 | .IX Item "Limited number of file descriptors" |
2939 | Windows has numerous arbitrary (and low) limits on things. Early versions |
3406 | Windows has numerous arbitrary (and low) limits on things. |
2940 | of winsocket's select only supported waiting for a max. of \f(CW64\fR handles |
3407 | .Sp |
2941 | (probably owning to the fact that all windows kernels can only wait for |
3408 | Early versions of winsocket's select only supported waiting for a maximum |
2942 | \&\f(CW64\fR things at the same time internally; microsoft recommends spawning a |
3409 | of \f(CW64\fR handles (probably owning to the fact that all windows kernels |
2943 | chain of threads and wait for 63 handles and the previous thread in each). |
3410 | can only wait for \f(CW64\fR things at the same time internally; microsoft |
|
|
3411 | recommends spawning a chain of threads and wait for 63 handles and the |
|
|
3412 | previous thread in each. Great). |
2944 | .Sp |
3413 | .Sp |
2945 | Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR |
3414 | Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR |
2946 | to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select |
3415 | to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select |
2947 | call (which might be in libev or elsewhere, for example, perl does its own |
3416 | call (which might be in libev or elsewhere, for example, perl does its own |
2948 | select emulation on windows). |
3417 | select emulation on windows). |
… | |
… | |
2956 | .Sp |
3425 | .Sp |
2957 | This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on |
3426 | This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on |
2958 | windows version and/or the phase of the moon). To get more, you need to |
3427 | windows version and/or the phase of the moon). To get more, you need to |
2959 | wrap all I/O functions and provide your own fd management, but the cost of |
3428 | wrap all I/O functions and provide your own fd management, but the cost of |
2960 | calling select (O(nA\*^X)) will likely make this unworkable. |
3429 | calling select (O(nA\*^X)) will likely make this unworkable. |
|
|
3430 | .SH "PORTABILITY REQUIREMENTS" |
|
|
3431 | .IX Header "PORTABILITY REQUIREMENTS" |
|
|
3432 | In addition to a working ISO-C implementation, libev relies on a few |
|
|
3433 | additional extensions: |
|
|
3434 | .ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4 |
|
|
3435 | .el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4 |
|
|
3436 | .IX Item "sig_atomic_t volatile must be thread-atomic as well" |
|
|
3437 | The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as |
|
|
3438 | \&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic w.r.t. accesses from different |
|
|
3439 | threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is |
|
|
3440 | believed to be sufficiently portable. |
|
|
3441 | .ie n .IP """sigprocmask"" must work in a threaded environment" 4 |
|
|
3442 | .el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4 |
|
|
3443 | .IX Item "sigprocmask must work in a threaded environment" |
|
|
3444 | Libev uses \f(CW\*(C`sigprocmask\*(C'\fR to temporarily block signals. This is not |
|
|
3445 | allowed in a threaded program (\f(CW\*(C`pthread_sigmask\*(C'\fR has to be used). Typical |
|
|
3446 | pthread implementations will either allow \f(CW\*(C`sigprocmask\*(C'\fR in the \*(L"main |
|
|
3447 | thread\*(R" or will block signals process-wide, both behaviours would |
|
|
3448 | be compatible with libev. Interaction between \f(CW\*(C`sigprocmask\*(C'\fR and |
|
|
3449 | \&\f(CW\*(C`pthread_sigmask\*(C'\fR could complicate things, however. |
|
|
3450 | .Sp |
|
|
3451 | The most portable way to handle signals is to block signals in all threads |
|
|
3452 | except the initial one, and run the default loop in the initial thread as |
|
|
3453 | well. |
|
|
3454 | .ie n .IP """long"" must be large enough for common memory allocation sizes" 4 |
|
|
3455 | .el .IP "\f(CWlong\fR must be large enough for common memory allocation sizes" 4 |
|
|
3456 | .IX Item "long must be large enough for common memory allocation sizes" |
|
|
3457 | To improve portability and simplify using libev, libev uses \f(CW\*(C`long\*(C'\fR |
|
|
3458 | internally instead of \f(CW\*(C`size_t\*(C'\fR when allocating its data structures. On |
|
|
3459 | non-POSIX systems (Microsoft...) this might be unexpectedly low, but |
|
|
3460 | is still at least 31 bits everywhere, which is enough for hundreds of |
|
|
3461 | millions of watchers. |
|
|
3462 | .ie n .IP """double"" must hold a time value in seconds with enough accuracy" 4 |
|
|
3463 | .el .IP "\f(CWdouble\fR must hold a time value in seconds with enough accuracy" 4 |
|
|
3464 | .IX Item "double must hold a time value in seconds with enough accuracy" |
|
|
3465 | The type \f(CW\*(C`double\*(C'\fR is used to represent timestamps. It is required to |
|
|
3466 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
|
|
3467 | enough for at least into the year 4000. This requirement is fulfilled by |
|
|
3468 | implementations implementing \s-1IEEE\s0 754 (basically all existing ones). |
|
|
3469 | .PP |
|
|
3470 | If you know of other additional requirements drop me a note. |
|
|
3471 | .SH "COMPILER WARNINGS" |
|
|
3472 | .IX Header "COMPILER WARNINGS" |
|
|
3473 | Depending on your compiler and compiler settings, you might get no or a |
|
|
3474 | lot of warnings when compiling libev code. Some people are apparently |
|
|
3475 | scared by this. |
|
|
3476 | .PP |
|
|
3477 | However, these are unavoidable for many reasons. For one, each compiler |
|
|
3478 | has different warnings, and each user has different tastes regarding |
|
|
3479 | warning options. \*(L"Warn-free\*(R" code therefore cannot be a goal except when |
|
|
3480 | targetting a specific compiler and compiler-version. |
|
|
3481 | .PP |
|
|
3482 | Another reason is that some compiler warnings require elaborate |
|
|
3483 | workarounds, or other changes to the code that make it less clear and less |
|
|
3484 | maintainable. |
|
|
3485 | .PP |
|
|
3486 | And of course, some compiler warnings are just plain stupid, or simply |
|
|
3487 | wrong (because they don't actually warn about the cindition their message |
|
|
3488 | seems to warn about). |
|
|
3489 | .PP |
|
|
3490 | While libev is written to generate as few warnings as possible, |
|
|
3491 | \&\*(L"warn-free\*(R" code is not a goal, and it is recommended not to build libev |
|
|
3492 | with any compiler warnings enabled unless you are prepared to cope with |
|
|
3493 | them (e.g. by ignoring them). Remember that warnings are just that: |
|
|
3494 | warnings, not errors, or proof of bugs. |
|
|
3495 | .SH "VALGRIND" |
|
|
3496 | .IX Header "VALGRIND" |
|
|
3497 | Valgrind has a special section here because it is a popular tool that is |
|
|
3498 | highly useful, but valgrind reports are very hard to interpret. |
|
|
3499 | .PP |
|
|
3500 | If you think you found a bug (memory leak, uninitialised data access etc.) |
|
|
3501 | in libev, then check twice: If valgrind reports something like: |
|
|
3502 | .PP |
|
|
3503 | .Vb 3 |
|
|
3504 | \& ==2274== definitely lost: 0 bytes in 0 blocks. |
|
|
3505 | \& ==2274== possibly lost: 0 bytes in 0 blocks. |
|
|
3506 | \& ==2274== still reachable: 256 bytes in 1 blocks. |
|
|
3507 | .Ve |
|
|
3508 | .PP |
|
|
3509 | then there is no memory leak. Similarly, under some circumstances, |
|
|
3510 | valgrind might report kernel bugs as if it were a bug in libev, or it |
|
|
3511 | might be confused (it is a very good tool, but only a tool). |
|
|
3512 | .PP |
|
|
3513 | If you are unsure about something, feel free to contact the mailing list |
|
|
3514 | with the full valgrind report and an explanation on why you think this is |
|
|
3515 | a bug in libev. However, don't be annoyed when you get a brisk \*(L"this is |
|
|
3516 | no bug\*(R" answer and take the chance of learning how to interpret valgrind |
|
|
3517 | properly. |
|
|
3518 | .PP |
|
|
3519 | If you need, for some reason, empty reports from valgrind for your project |
|
|
3520 | I suggest using suppression lists. |
2961 | .SH "AUTHOR" |
3521 | .SH "AUTHOR" |
2962 | .IX Header "AUTHOR" |
3522 | .IX Header "AUTHOR" |
2963 | Marc Lehmann <libev@schmorp.de>. |
3523 | Marc Lehmann <libev@schmorp.de>. |
2964 | .SH "POD ERRORS" |
3524 | .SH "POD ERRORS" |
2965 | .IX Header "POD ERRORS" |
3525 | .IX Header "POD ERRORS" |
2966 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
3526 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
2967 | .IP "Around line 2686:" 4 |
3527 | .IP "Around line 3107:" 4 |
2968 | .IX Item "Around line 2686:" |
3528 | .IX Item "Around line 3107:" |
2969 | You forgot a '=back' before '=head2' |
3529 | You forgot a '=back' before '=head2' |