… | |
… | |
214 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
214 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
215 | recommended ones. |
215 | recommended ones. |
216 | |
216 | |
217 | See the description of C<ev_embed> watchers for more info. |
217 | See the description of C<ev_embed> watchers for more info. |
218 | |
218 | |
219 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
219 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) [NOT REENTRANT] |
220 | |
220 | |
221 | Sets the allocation function to use (the prototype is similar - the |
221 | Sets the allocation function to use (the prototype is similar - the |
222 | semantics are identical to the C<realloc> C89/SuS/POSIX function). It is |
222 | semantics are identical to the C<realloc> C89/SuS/POSIX function). It is |
223 | used to allocate and free memory (no surprises here). If it returns zero |
223 | used to allocate and free memory (no surprises here). If it returns zero |
224 | when memory needs to be allocated (C<size != 0>), the library might abort |
224 | when memory needs to be allocated (C<size != 0>), the library might abort |
… | |
… | |
250 | } |
250 | } |
251 | |
251 | |
252 | ... |
252 | ... |
253 | ev_set_allocator (persistent_realloc); |
253 | ev_set_allocator (persistent_realloc); |
254 | |
254 | |
255 | =item ev_set_syserr_cb (void (*cb)(const char *msg)); |
255 | =item ev_set_syserr_cb (void (*cb)(const char *msg)); [NOT REENTRANT] |
256 | |
256 | |
257 | Set the callback function to call on a retryable system call error (such |
257 | Set the callback function to call on a retryable system call error (such |
258 | as failed select, poll, epoll_wait). The message is a printable string |
258 | as failed select, poll, epoll_wait). The message is a printable string |
259 | indicating the system call or subsystem causing the problem. If this |
259 | indicating the system call or subsystem causing the problem. If this |
260 | callback is set, then libev will expect it to remedy the situation, no |
260 | callback is set, then libev will expect it to remedy the situation, no |
… | |
… | |
685 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
685 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
686 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
686 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
687 | |
687 | |
688 | This "unloop state" will be cleared when entering C<ev_loop> again. |
688 | This "unloop state" will be cleared when entering C<ev_loop> again. |
689 | |
689 | |
|
|
690 | It is safe to call C<ev_unloop> from otuside any C<ev_loop> calls. |
|
|
691 | |
690 | =item ev_ref (loop) |
692 | =item ev_ref (loop) |
691 | |
693 | |
692 | =item ev_unref (loop) |
694 | =item ev_unref (loop) |
693 | |
695 | |
694 | Ref/unref can be used to add or remove a reference count on the event |
696 | Ref/unref can be used to add or remove a reference count on the event |
… | |
… | |
892 | =item C<EV_ERROR> |
894 | =item C<EV_ERROR> |
893 | |
895 | |
894 | An unspecified error has occurred, the watcher has been stopped. This might |
896 | An unspecified error has occurred, the watcher has been stopped. This might |
895 | happen because the watcher could not be properly started because libev |
897 | happen because the watcher could not be properly started because libev |
896 | ran out of memory, a file descriptor was found to be closed or any other |
898 | ran out of memory, a file descriptor was found to be closed or any other |
|
|
899 | problem. Libev considers these application bugs. |
|
|
900 | |
897 | problem. You best act on it by reporting the problem and somehow coping |
901 | You best act on it by reporting the problem and somehow coping with the |
898 | with the watcher being stopped. |
902 | watcher being stopped. Note that well-written programs should not receive |
|
|
903 | an error ever, so when your watcher receives it, this usually indicates a |
|
|
904 | bug in your program. |
899 | |
905 | |
900 | Libev will usually signal a few "dummy" events together with an error, for |
906 | Libev will usually signal a few "dummy" events together with an error, for |
901 | example it might indicate that a fd is readable or writable, and if your |
907 | example it might indicate that a fd is readable or writable, and if your |
902 | callbacks is well-written it can just attempt the operation and cope with |
908 | callbacks is well-written it can just attempt the operation and cope with |
903 | the error from read() or write(). This will not work in multi-threaded |
909 | the error from read() or write(). This will not work in multi-threaded |
… | |
… | |
967 | |
973 | |
968 | ev_io_start (EV_DEFAULT_UC, &w); |
974 | ev_io_start (EV_DEFAULT_UC, &w); |
969 | |
975 | |
970 | =item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher) |
976 | =item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher) |
971 | |
977 | |
972 | Stops the given watcher again (if active) and clears the pending |
978 | Stops the given watcher if active, and clears the pending status (whether |
|
|
979 | the watcher was active or not). |
|
|
980 | |
973 | status. It is possible that stopped watchers are pending (for example, |
981 | It is possible that stopped watchers are pending - for example, |
974 | non-repeating timers are being stopped when they become pending), but |
982 | non-repeating timers are being stopped when they become pending - but |
975 | C<ev_TYPE_stop> ensures that the watcher is neither active nor pending. If |
983 | calling C<ev_TYPE_stop> ensures that the watcher is neither active nor |
976 | you want to free or reuse the memory used by the watcher it is therefore a |
984 | pending. If you want to free or reuse the memory used by the watcher it is |
977 | good idea to always call its C<ev_TYPE_stop> function. |
985 | therefore a good idea to always call its C<ev_TYPE_stop> function. |
978 | |
986 | |
979 | =item bool ev_is_active (ev_TYPE *watcher) |
987 | =item bool ev_is_active (ev_TYPE *watcher) |
980 | |
988 | |
981 | Returns a true value iff the watcher is active (i.e. it has been started |
989 | Returns a true value iff the watcher is active (i.e. it has been started |
982 | and not yet been stopped). As long as a watcher is active you must not modify |
990 | and not yet been stopped). As long as a watcher is active you must not modify |
… | |
… | |
1625 | |
1633 | |
1626 | =back |
1634 | =back |
1627 | |
1635 | |
1628 | =head3 Examples |
1636 | =head3 Examples |
1629 | |
1637 | |
1630 | Example: Try to exit cleanly on SIGINT and SIGTERM. |
1638 | Example: Try to exit cleanly on SIGINT. |
1631 | |
1639 | |
1632 | static void |
1640 | static void |
1633 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
1641 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
1634 | { |
1642 | { |
1635 | ev_unloop (loop, EVUNLOOP_ALL); |
1643 | ev_unloop (loop, EVUNLOOP_ALL); |
1636 | } |
1644 | } |
1637 | |
1645 | |
1638 | struct ev_signal signal_watcher; |
1646 | struct ev_signal signal_watcher; |
1639 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
1647 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
1640 | ev_signal_start (loop, &sigint_cb); |
1648 | ev_signal_start (loop, &signal_watcher); |
1641 | |
1649 | |
1642 | |
1650 | |
1643 | =head2 C<ev_child> - watch out for process status changes |
1651 | =head2 C<ev_child> - watch out for process status changes |
1644 | |
1652 | |
1645 | Child watchers trigger when your process receives a SIGCHLD in response to |
1653 | Child watchers trigger when your process receives a SIGCHLD in response to |
… | |
… | |
1792 | to exchange stat structures with application programs compiled using the |
1800 | to exchange stat structures with application programs compiled using the |
1793 | default compilation environment. |
1801 | default compilation environment. |
1794 | |
1802 | |
1795 | =head3 Inotify and Kqueue |
1803 | =head3 Inotify and Kqueue |
1796 | |
1804 | |
1797 | When C<inotify (7)> support has been compiled into libev (generally only |
1805 | When C<inotify (7)> support has been compiled into libev (generally |
|
|
1806 | only available with Linux 2.6.25 or above due to bugs in earlier |
1798 | available with Linux) and present at runtime, it will be used to speed up |
1807 | implementations) and present at runtime, it will be used to speed up |
1799 | change detection where possible. The inotify descriptor will be created lazily |
1808 | change detection where possible. The inotify descriptor will be created |
1800 | when the first C<ev_stat> watcher is being started. |
1809 | lazily when the first C<ev_stat> watcher is being started. |
1801 | |
1810 | |
1802 | Inotify presence does not change the semantics of C<ev_stat> watchers |
1811 | Inotify presence does not change the semantics of C<ev_stat> watchers |
1803 | except that changes might be detected earlier, and in some cases, to avoid |
1812 | except that changes might be detected earlier, and in some cases, to avoid |
1804 | making regular C<stat> calls. Even in the presence of inotify support |
1813 | making regular C<stat> calls. Even in the presence of inotify support |
1805 | there are many cases where libev has to resort to regular C<stat> polling, |
1814 | there are many cases where libev has to resort to regular C<stat> polling, |
… | |
… | |
2242 | So when you want to use this feature you will always have to be prepared |
2251 | So when you want to use this feature you will always have to be prepared |
2243 | that you cannot get an embeddable loop. The recommended way to get around |
2252 | that you cannot get an embeddable loop. The recommended way to get around |
2244 | this is to have a separate variables for your embeddable loop, try to |
2253 | this is to have a separate variables for your embeddable loop, try to |
2245 | create it, and if that fails, use the normal loop for everything. |
2254 | create it, and if that fails, use the normal loop for everything. |
2246 | |
2255 | |
|
|
2256 | =head3 C<ev_embed> and fork |
|
|
2257 | |
|
|
2258 | While the C<ev_embed> watcher is running, forks in the embedding loop will |
|
|
2259 | automatically be applied to the embedded loop as well, so no special |
|
|
2260 | fork handling is required in that case. When the watcher is not running, |
|
|
2261 | however, it is still the task of the libev user to call C<ev_loop_fork ()> |
|
|
2262 | as applicable. |
|
|
2263 | |
2247 | =head3 Watcher-Specific Functions and Data Members |
2264 | =head3 Watcher-Specific Functions and Data Members |
2248 | |
2265 | |
2249 | =over 4 |
2266 | =over 4 |
2250 | |
2267 | |
2251 | =item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) |
2268 | =item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) |
… | |
… | |
2376 | =over 4 |
2393 | =over 4 |
2377 | |
2394 | |
2378 | =item queueing from a signal handler context |
2395 | =item queueing from a signal handler context |
2379 | |
2396 | |
2380 | To implement race-free queueing, you simply add to the queue in the signal |
2397 | To implement race-free queueing, you simply add to the queue in the signal |
2381 | handler but you block the signal handler in the watcher callback. Here is an example that does that for |
2398 | handler but you block the signal handler in the watcher callback. Here is |
2382 | some fictitious SIGUSR1 handler: |
2399 | an example that does that for some fictitious SIGUSR1 handler: |
2383 | |
2400 | |
2384 | static ev_async mysig; |
2401 | static ev_async mysig; |
2385 | |
2402 | |
2386 | static void |
2403 | static void |
2387 | sigusr1_handler (void) |
2404 | sigusr1_handler (void) |
… | |
… | |
2494 | =over 4 |
2511 | =over 4 |
2495 | |
2512 | |
2496 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
2513 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
2497 | |
2514 | |
2498 | This function combines a simple timer and an I/O watcher, calls your |
2515 | This function combines a simple timer and an I/O watcher, calls your |
2499 | callback on whichever event happens first and automatically stop both |
2516 | callback on whichever event happens first and automatically stops both |
2500 | watchers. This is useful if you want to wait for a single event on an fd |
2517 | watchers. This is useful if you want to wait for a single event on an fd |
2501 | or timeout without having to allocate/configure/start/stop/free one or |
2518 | or timeout without having to allocate/configure/start/stop/free one or |
2502 | more watchers yourself. |
2519 | more watchers yourself. |
2503 | |
2520 | |
2504 | If C<fd> is less than 0, then no I/O watcher will be started and events |
2521 | If C<fd> is less than 0, then no I/O watcher will be started and the |
2505 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
2522 | C<events> argument is being ignored. Otherwise, an C<ev_io> watcher for |
2506 | C<events> set will be created and started. |
2523 | the given C<fd> and C<events> set will be created and started. |
2507 | |
2524 | |
2508 | If C<timeout> is less than 0, then no timeout watcher will be |
2525 | If C<timeout> is less than 0, then no timeout watcher will be |
2509 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
2526 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
2510 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
2527 | repeat = 0) will be started. C<0> is a valid timeout. |
2511 | dubious value. |
|
|
2512 | |
2528 | |
2513 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
2529 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
2514 | passed an C<revents> set like normal event callbacks (a combination of |
2530 | passed an C<revents> set like normal event callbacks (a combination of |
2515 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
2531 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
2516 | value passed to C<ev_once>: |
2532 | value passed to C<ev_once>. Note that it is possible to receive I<both> |
|
|
2533 | a timeout and an io event at the same time - you probably should give io |
|
|
2534 | events precedence. |
|
|
2535 | |
|
|
2536 | Example: wait up to ten seconds for data to appear on STDIN_FILENO. |
2517 | |
2537 | |
2518 | static void stdin_ready (int revents, void *arg) |
2538 | static void stdin_ready (int revents, void *arg) |
2519 | { |
2539 | { |
|
|
2540 | if (revents & EV_READ) |
|
|
2541 | /* stdin might have data for us, joy! */; |
2520 | if (revents & EV_TIMEOUT) |
2542 | else if (revents & EV_TIMEOUT) |
2521 | /* doh, nothing entered */; |
2543 | /* doh, nothing entered */; |
2522 | else if (revents & EV_READ) |
|
|
2523 | /* stdin might have data for us, joy! */; |
|
|
2524 | } |
2544 | } |
2525 | |
2545 | |
2526 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
2546 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
2527 | |
2547 | |
2528 | =item ev_feed_event (ev_loop *, watcher *, int revents) |
2548 | =item ev_feed_event (ev_loop *, watcher *, int revents) |
… | |
… | |
3241 | definition and a statement, respectively. See the F<ev.h> header file for |
3261 | definition and a statement, respectively. See the F<ev.h> header file for |
3242 | their default definitions. One possible use for overriding these is to |
3262 | their default definitions. One possible use for overriding these is to |
3243 | avoid the C<struct ev_loop *> as first argument in all cases, or to use |
3263 | avoid the C<struct ev_loop *> as first argument in all cases, or to use |
3244 | method calls instead of plain function calls in C++. |
3264 | method calls instead of plain function calls in C++. |
3245 | |
3265 | |
|
|
3266 | =back |
|
|
3267 | |
3246 | =head2 EXPORTED API SYMBOLS |
3268 | =head2 EXPORTED API SYMBOLS |
3247 | |
3269 | |
3248 | If you need to re-export the API (e.g. via a DLL) and you need a list of |
3270 | If you need to re-export the API (e.g. via a DLL) and you need a list of |
3249 | exported symbols, you can use the provided F<Symbol.*> files which list |
3271 | exported symbols, you can use the provided F<Symbol.*> files which list |
3250 | all public symbols, one per line: |
3272 | all public symbols, one per line: |
… | |
… | |
3296 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
3318 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
3297 | |
3319 | |
3298 | #include "ev_cpp.h" |
3320 | #include "ev_cpp.h" |
3299 | #include "ev.c" |
3321 | #include "ev.c" |
3300 | |
3322 | |
|
|
3323 | =head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES |
3301 | |
3324 | |
3302 | =head1 THREADS AND COROUTINES |
3325 | =head2 THREADS AND COROUTINES |
3303 | |
3326 | |
3304 | =head2 THREADS |
3327 | =head3 THREADS |
3305 | |
3328 | |
3306 | Libev itself is thread-safe (unless the opposite is specifically |
3329 | All libev functions are reentrant and thread-safe unless explicitly |
3307 | documented for a function), but it uses no locking itself. This means that |
3330 | documented otherwise, but libev implements no locking itself. This means |
3308 | you can use as many loops as you want in parallel, as long as only one |
3331 | that you can use as many loops as you want in parallel, as long as there |
3309 | thread ever calls into one libev function with the same loop parameter: |
3332 | are no concurrent calls into any libev function with the same loop |
|
|
3333 | parameter (C<ev_default_*> calls have an implicit default loop parameter, |
3310 | libev guarantees that different event loops share no data structures that |
3334 | of course): libev guarantees that different event loops share no data |
3311 | need locking. |
3335 | structures that need any locking. |
3312 | |
3336 | |
3313 | Or to put it differently: calls with different loop parameters can be done |
3337 | Or to put it differently: calls with different loop parameters can be done |
3314 | concurrently from multiple threads, calls with the same loop parameter |
3338 | concurrently from multiple threads, calls with the same loop parameter |
3315 | must be done serially (but can be done from different threads, as long as |
3339 | must be done serially (but can be done from different threads, as long as |
3316 | only one thread ever is inside a call at any point in time, e.g. by using |
3340 | only one thread ever is inside a call at any point in time, e.g. by using |
3317 | a mutex per loop). |
3341 | a mutex per loop). |
3318 | |
3342 | |
3319 | Specifically to support threads (and signal handlers), libev implements |
3343 | Specifically to support threads (and signal handlers), libev implements |
3320 | so-called C<ev_async> watchers, which allow some limited form of |
3344 | so-called C<ev_async> watchers, which allow some limited form of |
3321 | concurrency on the same event loop. |
3345 | concurrency on the same event loop, namely waking it up "from the |
|
|
3346 | outside". |
3322 | |
3347 | |
3323 | If you want to know which design (one loop, locking, or multiple loops |
3348 | If you want to know which design (one loop, locking, or multiple loops |
3324 | without or something else still) is best for your problem, then I cannot |
3349 | without or something else still) is best for your problem, then I cannot |
3325 | help you. I can give some generic advice however: |
3350 | help you, but here is some generic advice: |
3326 | |
3351 | |
3327 | =over 4 |
3352 | =over 4 |
3328 | |
3353 | |
3329 | =item * most applications have a main thread: use the default libev loop |
3354 | =item * most applications have a main thread: use the default libev loop |
3330 | in that thread, or create a separate thread running only the default loop. |
3355 | in that thread, or create a separate thread running only the default loop. |
… | |
… | |
3354 | default loop and triggering an C<ev_async> watcher from the default loop |
3379 | default loop and triggering an C<ev_async> watcher from the default loop |
3355 | watcher callback into the event loop interested in the signal. |
3380 | watcher callback into the event loop interested in the signal. |
3356 | |
3381 | |
3357 | =back |
3382 | =back |
3358 | |
3383 | |
3359 | =head2 COROUTINES |
3384 | =head3 COROUTINES |
3360 | |
3385 | |
3361 | Libev is much more accommodating to coroutines ("cooperative threads"): |
3386 | Libev is very accommodating to coroutines ("cooperative threads"): |
3362 | libev fully supports nesting calls to it's functions from different |
3387 | libev fully supports nesting calls to its functions from different |
3363 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3388 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3364 | different coroutines and switch freely between both coroutines running the |
3389 | different coroutines, and switch freely between both coroutines running the |
3365 | loop, as long as you don't confuse yourself). The only exception is that |
3390 | loop, as long as you don't confuse yourself). The only exception is that |
3366 | you must not do this from C<ev_periodic> reschedule callbacks. |
3391 | you must not do this from C<ev_periodic> reschedule callbacks. |
3367 | |
3392 | |
3368 | Care has been taken to ensure that libev does not keep local state inside |
3393 | Care has been taken to ensure that libev does not keep local state inside |
3369 | C<ev_loop>, and other calls do not usually allow coroutine switches. |
3394 | C<ev_loop>, and other calls do not usually allow for coroutine switches as |
|
|
3395 | they do not clal any callbacks. |
3370 | |
3396 | |
|
|
3397 | =head2 COMPILER WARNINGS |
3371 | |
3398 | |
3372 | =head1 COMPLEXITIES |
3399 | Depending on your compiler and compiler settings, you might get no or a |
|
|
3400 | lot of warnings when compiling libev code. Some people are apparently |
|
|
3401 | scared by this. |
3373 | |
3402 | |
3374 | In this section the complexities of (many of) the algorithms used inside |
3403 | However, these are unavoidable for many reasons. For one, each compiler |
3375 | libev will be explained. For complexity discussions about backends see the |
3404 | has different warnings, and each user has different tastes regarding |
3376 | documentation for C<ev_default_init>. |
3405 | warning options. "Warn-free" code therefore cannot be a goal except when |
|
|
3406 | targeting a specific compiler and compiler-version. |
3377 | |
3407 | |
3378 | All of the following are about amortised time: If an array needs to be |
3408 | Another reason is that some compiler warnings require elaborate |
3379 | extended, libev needs to realloc and move the whole array, but this |
3409 | workarounds, or other changes to the code that make it less clear and less |
3380 | happens asymptotically never with higher number of elements, so O(1) might |
3410 | maintainable. |
3381 | mean it might do a lengthy realloc operation in rare cases, but on average |
|
|
3382 | it is much faster and asymptotically approaches constant time. |
|
|
3383 | |
3411 | |
3384 | =over 4 |
3412 | And of course, some compiler warnings are just plain stupid, or simply |
|
|
3413 | wrong (because they don't actually warn about the condition their message |
|
|
3414 | seems to warn about). For example, certain older gcc versions had some |
|
|
3415 | warnings that resulted an extreme number of false positives. These have |
|
|
3416 | been fixed, but some people still insist on making code warn-free with |
|
|
3417 | such buggy versions. |
3385 | |
3418 | |
3386 | =item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) |
3419 | While libev is written to generate as few warnings as possible, |
|
|
3420 | "warn-free" code is not a goal, and it is recommended not to build libev |
|
|
3421 | with any compiler warnings enabled unless you are prepared to cope with |
|
|
3422 | them (e.g. by ignoring them). Remember that warnings are just that: |
|
|
3423 | warnings, not errors, or proof of bugs. |
3387 | |
3424 | |
3388 | This means that, when you have a watcher that triggers in one hour and |
|
|
3389 | there are 100 watchers that would trigger before that then inserting will |
|
|
3390 | have to skip roughly seven (C<ld 100>) of these watchers. |
|
|
3391 | |
3425 | |
3392 | =item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) |
3426 | =head2 VALGRIND |
3393 | |
3427 | |
3394 | That means that changing a timer costs less than removing/adding them |
3428 | Valgrind has a special section here because it is a popular tool that is |
3395 | as only the relative motion in the event queue has to be paid for. |
3429 | highly useful. Unfortunately, valgrind reports are very hard to interpret. |
3396 | |
3430 | |
3397 | =item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) |
3431 | If you think you found a bug (memory leak, uninitialised data access etc.) |
|
|
3432 | in libev, then check twice: If valgrind reports something like: |
3398 | |
3433 | |
3399 | These just add the watcher into an array or at the head of a list. |
3434 | ==2274== definitely lost: 0 bytes in 0 blocks. |
|
|
3435 | ==2274== possibly lost: 0 bytes in 0 blocks. |
|
|
3436 | ==2274== still reachable: 256 bytes in 1 blocks. |
3400 | |
3437 | |
3401 | =item Stopping check/prepare/idle/fork/async watchers: O(1) |
3438 | Then there is no memory leak, just as memory accounted to global variables |
|
|
3439 | is not a memleak - the memory is still being refernced, and didn't leak. |
3402 | |
3440 | |
3403 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) |
3441 | Similarly, under some circumstances, valgrind might report kernel bugs |
|
|
3442 | as if it were a bug in libev (e.g. in realloc or in the poll backend, |
|
|
3443 | although an acceptable workaround has been found here), or it might be |
|
|
3444 | confused. |
3404 | |
3445 | |
3405 | These watchers are stored in lists then need to be walked to find the |
3446 | Keep in mind that valgrind is a very good tool, but only a tool. Don't |
3406 | correct watcher to remove. The lists are usually short (you don't usually |
3447 | make it into some kind of religion. |
3407 | have many watchers waiting for the same fd or signal). |
|
|
3408 | |
3448 | |
3409 | =item Finding the next timer in each loop iteration: O(1) |
3449 | If you are unsure about something, feel free to contact the mailing list |
|
|
3450 | with the full valgrind report and an explanation on why you think this |
|
|
3451 | is a bug in libev (best check the archives, too :). However, don't be |
|
|
3452 | annoyed when you get a brisk "this is no bug" answer and take the chance |
|
|
3453 | of learning how to interpret valgrind properly. |
3410 | |
3454 | |
3411 | By virtue of using a binary or 4-heap, the next timer is always found at a |
3455 | If you need, for some reason, empty reports from valgrind for your project |
3412 | fixed position in the storage array. |
3456 | I suggest using suppression lists. |
3413 | |
3457 | |
3414 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
|
|
3415 | |
3458 | |
3416 | A change means an I/O watcher gets started or stopped, which requires |
3459 | =head1 PORTABILITY NOTES |
3417 | libev to recalculate its status (and possibly tell the kernel, depending |
|
|
3418 | on backend and whether C<ev_io_set> was used). |
|
|
3419 | |
3460 | |
3420 | =item Activating one watcher (putting it into the pending state): O(1) |
|
|
3421 | |
|
|
3422 | =item Priority handling: O(number_of_priorities) |
|
|
3423 | |
|
|
3424 | Priorities are implemented by allocating some space for each |
|
|
3425 | priority. When doing priority-based operations, libev usually has to |
|
|
3426 | linearly search all the priorities, but starting/stopping and activating |
|
|
3427 | watchers becomes O(1) with respect to priority handling. |
|
|
3428 | |
|
|
3429 | =item Sending an ev_async: O(1) |
|
|
3430 | |
|
|
3431 | =item Processing ev_async_send: O(number_of_async_watchers) |
|
|
3432 | |
|
|
3433 | =item Processing signals: O(max_signal_number) |
|
|
3434 | |
|
|
3435 | Sending involves a system call I<iff> there were no other C<ev_async_send> |
|
|
3436 | calls in the current loop iteration. Checking for async and signal events |
|
|
3437 | involves iterating over all running async watchers or all signal numbers. |
|
|
3438 | |
|
|
3439 | =back |
|
|
3440 | |
|
|
3441 | |
|
|
3442 | =head1 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
3461 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
3443 | |
3462 | |
3444 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
3463 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
3445 | requires, and its I/O model is fundamentally incompatible with the POSIX |
3464 | requires, and its I/O model is fundamentally incompatible with the POSIX |
3446 | model. Libev still offers limited functionality on this platform in |
3465 | model. Libev still offers limited functionality on this platform in |
3447 | the form of the C<EVBACKEND_SELECT> backend, and only supports socket |
3466 | the form of the C<EVBACKEND_SELECT> backend, and only supports socket |
… | |
… | |
3534 | wrap all I/O functions and provide your own fd management, but the cost of |
3553 | wrap all I/O functions and provide your own fd management, but the cost of |
3535 | calling select (O(n²)) will likely make this unworkable. |
3554 | calling select (O(n²)) will likely make this unworkable. |
3536 | |
3555 | |
3537 | =back |
3556 | =back |
3538 | |
3557 | |
3539 | |
|
|
3540 | =head1 PORTABILITY REQUIREMENTS |
3558 | =head2 PORTABILITY REQUIREMENTS |
3541 | |
3559 | |
3542 | In addition to a working ISO-C implementation, libev relies on a few |
3560 | In addition to a working ISO-C implementation and of course the |
3543 | additional extensions: |
3561 | backend-specific APIs, libev relies on a few additional extensions: |
3544 | |
3562 | |
3545 | =over 4 |
3563 | =over 4 |
3546 | |
3564 | |
3547 | =item C<void (*)(ev_watcher_type *, int revents)> must have compatible |
3565 | =item C<void (*)(ev_watcher_type *, int revents)> must have compatible |
3548 | calling conventions regardless of C<ev_watcher_type *>. |
3566 | calling conventions regardless of C<ev_watcher_type *>. |
… | |
… | |
3573 | except the initial one, and run the default loop in the initial thread as |
3591 | except the initial one, and run the default loop in the initial thread as |
3574 | well. |
3592 | well. |
3575 | |
3593 | |
3576 | =item C<long> must be large enough for common memory allocation sizes |
3594 | =item C<long> must be large enough for common memory allocation sizes |
3577 | |
3595 | |
3578 | To improve portability and simplify using libev, libev uses C<long> |
3596 | To improve portability and simplify its API, libev uses C<long> internally |
3579 | internally instead of C<size_t> when allocating its data structures. On |
3597 | instead of C<size_t> when allocating its data structures. On non-POSIX |
3580 | non-POSIX systems (Microsoft...) this might be unexpectedly low, but |
3598 | systems (Microsoft...) this might be unexpectedly low, but is still at |
3581 | is still at least 31 bits everywhere, which is enough for hundreds of |
3599 | least 31 bits everywhere, which is enough for hundreds of millions of |
3582 | millions of watchers. |
3600 | watchers. |
3583 | |
3601 | |
3584 | =item C<double> must hold a time value in seconds with enough accuracy |
3602 | =item C<double> must hold a time value in seconds with enough accuracy |
3585 | |
3603 | |
3586 | The type C<double> is used to represent timestamps. It is required to |
3604 | The type C<double> is used to represent timestamps. It is required to |
3587 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
3605 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
… | |
… | |
3591 | =back |
3609 | =back |
3592 | |
3610 | |
3593 | If you know of other additional requirements drop me a note. |
3611 | If you know of other additional requirements drop me a note. |
3594 | |
3612 | |
3595 | |
3613 | |
3596 | =head1 COMPILER WARNINGS |
3614 | =head1 ALGORITHMIC COMPLEXITIES |
3597 | |
3615 | |
3598 | Depending on your compiler and compiler settings, you might get no or a |
3616 | In this section the complexities of (many of) the algorithms used inside |
3599 | lot of warnings when compiling libev code. Some people are apparently |
3617 | libev will be documented. For complexity discussions about backends see |
3600 | scared by this. |
3618 | the documentation for C<ev_default_init>. |
3601 | |
3619 | |
3602 | However, these are unavoidable for many reasons. For one, each compiler |
3620 | All of the following are about amortised time: If an array needs to be |
3603 | has different warnings, and each user has different tastes regarding |
3621 | extended, libev needs to realloc and move the whole array, but this |
3604 | warning options. "Warn-free" code therefore cannot be a goal except when |
3622 | happens asymptotically rarer with higher number of elements, so O(1) might |
3605 | targeting a specific compiler and compiler-version. |
3623 | mean that libev does a lengthy realloc operation in rare cases, but on |
|
|
3624 | average it is much faster and asymptotically approaches constant time. |
3606 | |
3625 | |
3607 | Another reason is that some compiler warnings require elaborate |
3626 | =over 4 |
3608 | workarounds, or other changes to the code that make it less clear and less |
|
|
3609 | maintainable. |
|
|
3610 | |
3627 | |
3611 | And of course, some compiler warnings are just plain stupid, or simply |
3628 | =item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) |
3612 | wrong (because they don't actually warn about the condition their message |
|
|
3613 | seems to warn about). |
|
|
3614 | |
3629 | |
3615 | While libev is written to generate as few warnings as possible, |
3630 | This means that, when you have a watcher that triggers in one hour and |
3616 | "warn-free" code is not a goal, and it is recommended not to build libev |
3631 | there are 100 watchers that would trigger before that, then inserting will |
3617 | with any compiler warnings enabled unless you are prepared to cope with |
3632 | have to skip roughly seven (C<ld 100>) of these watchers. |
3618 | them (e.g. by ignoring them). Remember that warnings are just that: |
|
|
3619 | warnings, not errors, or proof of bugs. |
|
|
3620 | |
3633 | |
|
|
3634 | =item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) |
3621 | |
3635 | |
3622 | =head1 VALGRIND |
3636 | That means that changing a timer costs less than removing/adding them, |
|
|
3637 | as only the relative motion in the event queue has to be paid for. |
3623 | |
3638 | |
3624 | Valgrind has a special section here because it is a popular tool that is |
3639 | =item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) |
3625 | highly useful, but valgrind reports are very hard to interpret. |
|
|
3626 | |
3640 | |
3627 | If you think you found a bug (memory leak, uninitialised data access etc.) |
3641 | These just add the watcher into an array or at the head of a list. |
3628 | in libev, then check twice: If valgrind reports something like: |
|
|
3629 | |
3642 | |
3630 | ==2274== definitely lost: 0 bytes in 0 blocks. |
3643 | =item Stopping check/prepare/idle/fork/async watchers: O(1) |
3631 | ==2274== possibly lost: 0 bytes in 0 blocks. |
|
|
3632 | ==2274== still reachable: 256 bytes in 1 blocks. |
|
|
3633 | |
3644 | |
3634 | Then there is no memory leak. Similarly, under some circumstances, |
3645 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) |
3635 | valgrind might report kernel bugs as if it were a bug in libev, or it |
|
|
3636 | might be confused (it is a very good tool, but only a tool). |
|
|
3637 | |
3646 | |
3638 | If you are unsure about something, feel free to contact the mailing list |
3647 | These watchers are stored in lists, so they need to be walked to find the |
3639 | with the full valgrind report and an explanation on why you think this is |
3648 | correct watcher to remove. The lists are usually short (you don't usually |
3640 | a bug in libev. However, don't be annoyed when you get a brisk "this is |
3649 | have many watchers waiting for the same fd or signal: one is typical, two |
3641 | no bug" answer and take the chance of learning how to interpret valgrind |
3650 | is rare). |
3642 | properly. |
|
|
3643 | |
3651 | |
3644 | If you need, for some reason, empty reports from valgrind for your project |
3652 | =item Finding the next timer in each loop iteration: O(1) |
3645 | I suggest using suppression lists. |
3653 | |
|
|
3654 | By virtue of using a binary or 4-heap, the next timer is always found at a |
|
|
3655 | fixed position in the storage array. |
|
|
3656 | |
|
|
3657 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
|
|
3658 | |
|
|
3659 | A change means an I/O watcher gets started or stopped, which requires |
|
|
3660 | libev to recalculate its status (and possibly tell the kernel, depending |
|
|
3661 | on backend and whether C<ev_io_set> was used). |
|
|
3662 | |
|
|
3663 | =item Activating one watcher (putting it into the pending state): O(1) |
|
|
3664 | |
|
|
3665 | =item Priority handling: O(number_of_priorities) |
|
|
3666 | |
|
|
3667 | Priorities are implemented by allocating some space for each |
|
|
3668 | priority. When doing priority-based operations, libev usually has to |
|
|
3669 | linearly search all the priorities, but starting/stopping and activating |
|
|
3670 | watchers becomes O(1) with respect to priority handling. |
|
|
3671 | |
|
|
3672 | =item Sending an ev_async: O(1) |
|
|
3673 | |
|
|
3674 | =item Processing ev_async_send: O(number_of_async_watchers) |
|
|
3675 | |
|
|
3676 | =item Processing signals: O(max_signal_number) |
|
|
3677 | |
|
|
3678 | Sending involves a system call I<iff> there were no other C<ev_async_send> |
|
|
3679 | calls in the current loop iteration. Checking for async and signal events |
|
|
3680 | involves iterating over all running async watchers or all signal numbers. |
|
|
3681 | |
|
|
3682 | =back |
3646 | |
3683 | |
3647 | |
3684 | |
3648 | =head1 AUTHOR |
3685 | =head1 AUTHOR |
3649 | |
3686 | |
3650 | Marc Lehmann <libev@schmorp.de>. |
3687 | Marc Lehmann <libev@schmorp.de>. |