… | |
… | |
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 |
… | |
… | |
1625 | |
1625 | |
1626 | =back |
1626 | =back |
1627 | |
1627 | |
1628 | =head3 Examples |
1628 | =head3 Examples |
1629 | |
1629 | |
1630 | Example: Try to exit cleanly on SIGINT and SIGTERM. |
1630 | Example: Try to exit cleanly on SIGINT. |
1631 | |
1631 | |
1632 | static void |
1632 | static void |
1633 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
1633 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
1634 | { |
1634 | { |
1635 | ev_unloop (loop, EVUNLOOP_ALL); |
1635 | ev_unloop (loop, EVUNLOOP_ALL); |
1636 | } |
1636 | } |
1637 | |
1637 | |
1638 | struct ev_signal signal_watcher; |
1638 | struct ev_signal signal_watcher; |
1639 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
1639 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
1640 | ev_signal_start (loop, &sigint_cb); |
1640 | ev_signal_start (loop, &signal_watcher); |
1641 | |
1641 | |
1642 | |
1642 | |
1643 | =head2 C<ev_child> - watch out for process status changes |
1643 | =head2 C<ev_child> - watch out for process status changes |
1644 | |
1644 | |
1645 | Child watchers trigger when your process receives a SIGCHLD in response to |
1645 | Child watchers trigger when your process receives a SIGCHLD in response to |
… | |
… | |
2242 | So when you want to use this feature you will always have to be prepared |
2242 | 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 |
2243 | 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 |
2244 | 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. |
2245 | create it, and if that fails, use the normal loop for everything. |
2246 | |
2246 | |
|
|
2247 | =head3 C<ev_embed> and fork |
|
|
2248 | |
|
|
2249 | While the C<ev_embed> watcher is running, forks in the embedding loop will |
|
|
2250 | automatically be applied to the embedded loop as well, so no special |
|
|
2251 | fork handling is required in that case. When the watcher is not running, |
|
|
2252 | however, it is still the task of the libev user to call C<ev_loop_fork ()> |
|
|
2253 | as applicable. |
|
|
2254 | |
2247 | =head3 Watcher-Specific Functions and Data Members |
2255 | =head3 Watcher-Specific Functions and Data Members |
2248 | |
2256 | |
2249 | =over 4 |
2257 | =over 4 |
2250 | |
2258 | |
2251 | =item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) |
2259 | =item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) |
… | |
… | |
3298 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
3306 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
3299 | |
3307 | |
3300 | #include "ev_cpp.h" |
3308 | #include "ev_cpp.h" |
3301 | #include "ev.c" |
3309 | #include "ev.c" |
3302 | |
3310 | |
|
|
3311 | =head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES |
3303 | |
3312 | |
3304 | =head1 THREADS AND COROUTINES |
3313 | =head2 THREADS AND COROUTINES |
3305 | |
3314 | |
3306 | =head2 THREADS |
3315 | =head3 THREADS |
3307 | |
3316 | |
3308 | Libev itself is thread-safe (unless the opposite is specifically |
3317 | All libev functions are reentrant and thread-safe unless explicitly |
3309 | documented for a function), but it uses no locking itself. This means that |
3318 | documented otherwise, but it uses no locking itself. This means that you |
3310 | you can use as many loops as you want in parallel, as long as only one |
3319 | can use as many loops as you want in parallel, as long as there are no |
3311 | thread ever calls into one libev function with the same loop parameter: |
3320 | concurrent calls into any libev function with the same loop parameter |
|
|
3321 | (C<ev_default_*> calls have an implicit default loop parameter, of |
3312 | libev guarantees that different event loops share no data structures that |
3322 | course): libev guarantees that different event loops share no data |
3313 | need locking. |
3323 | structures that need any locking. |
3314 | |
3324 | |
3315 | Or to put it differently: calls with different loop parameters can be done |
3325 | Or to put it differently: calls with different loop parameters can be done |
3316 | concurrently from multiple threads, calls with the same loop parameter |
3326 | concurrently from multiple threads, calls with the same loop parameter |
3317 | must be done serially (but can be done from different threads, as long as |
3327 | must be done serially (but can be done from different threads, as long as |
3318 | only one thread ever is inside a call at any point in time, e.g. by using |
3328 | only one thread ever is inside a call at any point in time, e.g. by using |
3319 | a mutex per loop). |
3329 | a mutex per loop). |
3320 | |
3330 | |
3321 | Specifically to support threads (and signal handlers), libev implements |
3331 | Specifically to support threads (and signal handlers), libev implements |
3322 | so-called C<ev_async> watchers, which allow some limited form of |
3332 | so-called C<ev_async> watchers, which allow some limited form of |
3323 | concurrency on the same event loop. |
3333 | concurrency on the same event loop, namely waking it up "from the |
|
|
3334 | outside". |
3324 | |
3335 | |
3325 | If you want to know which design (one loop, locking, or multiple loops |
3336 | If you want to know which design (one loop, locking, or multiple loops |
3326 | without or something else still) is best for your problem, then I cannot |
3337 | without or something else still) is best for your problem, then I cannot |
3327 | help you. I can give some generic advice however: |
3338 | help you, but here is some generic advice: |
3328 | |
3339 | |
3329 | =over 4 |
3340 | =over 4 |
3330 | |
3341 | |
3331 | =item * most applications have a main thread: use the default libev loop |
3342 | =item * most applications have a main thread: use the default libev loop |
3332 | in that thread, or create a separate thread running only the default loop. |
3343 | in that thread, or create a separate thread running only the default loop. |
… | |
… | |
3356 | default loop and triggering an C<ev_async> watcher from the default loop |
3367 | default loop and triggering an C<ev_async> watcher from the default loop |
3357 | watcher callback into the event loop interested in the signal. |
3368 | watcher callback into the event loop interested in the signal. |
3358 | |
3369 | |
3359 | =back |
3370 | =back |
3360 | |
3371 | |
3361 | =head2 COROUTINES |
3372 | =head3 COROUTINES |
3362 | |
3373 | |
3363 | Libev is much more accommodating to coroutines ("cooperative threads"): |
3374 | Libev is much more accommodating to coroutines ("cooperative threads"): |
3364 | libev fully supports nesting calls to it's functions from different |
3375 | libev fully supports nesting calls to it's functions from different |
3365 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3376 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3366 | different coroutines and switch freely between both coroutines running the |
3377 | different coroutines and switch freely between both coroutines running the |
… | |
… | |
3368 | you must not do this from C<ev_periodic> reschedule callbacks. |
3379 | you must not do this from C<ev_periodic> reschedule callbacks. |
3369 | |
3380 | |
3370 | Care has been taken to ensure that libev does not keep local state inside |
3381 | Care has been taken to ensure that libev does not keep local state inside |
3371 | C<ev_loop>, and other calls do not usually allow coroutine switches. |
3382 | C<ev_loop>, and other calls do not usually allow coroutine switches. |
3372 | |
3383 | |
|
|
3384 | =head2 COMPILER WARNINGS |
|
|
3385 | |
|
|
3386 | Depending on your compiler and compiler settings, you might get no or a |
|
|
3387 | lot of warnings when compiling libev code. Some people are apparently |
|
|
3388 | scared by this. |
|
|
3389 | |
|
|
3390 | However, these are unavoidable for many reasons. For one, each compiler |
|
|
3391 | has different warnings, and each user has different tastes regarding |
|
|
3392 | warning options. "Warn-free" code therefore cannot be a goal except when |
|
|
3393 | targeting a specific compiler and compiler-version. |
|
|
3394 | |
|
|
3395 | Another reason is that some compiler warnings require elaborate |
|
|
3396 | workarounds, or other changes to the code that make it less clear and less |
|
|
3397 | maintainable. |
|
|
3398 | |
|
|
3399 | And of course, some compiler warnings are just plain stupid, or simply |
|
|
3400 | wrong (because they don't actually warn about the condition their message |
|
|
3401 | seems to warn about). For example, certain older gcc versions had some |
|
|
3402 | warnings that resulted an extreme number of false positives. These have |
|
|
3403 | been fixed, but some people still insist on making code warn-free with |
|
|
3404 | such buggy versions. |
|
|
3405 | |
|
|
3406 | While libev is written to generate as few warnings as possible, |
|
|
3407 | "warn-free" code is not a goal, and it is recommended not to build libev |
|
|
3408 | with any compiler warnings enabled unless you are prepared to cope with |
|
|
3409 | them (e.g. by ignoring them). Remember that warnings are just that: |
|
|
3410 | warnings, not errors, or proof of bugs. |
|
|
3411 | |
|
|
3412 | |
|
|
3413 | =head2 VALGRIND |
|
|
3414 | |
|
|
3415 | Valgrind has a special section here because it is a popular tool that is |
|
|
3416 | highly useful. Unfortunately, valgrind reports are very hard to interpret. |
|
|
3417 | |
|
|
3418 | If you think you found a bug (memory leak, uninitialised data access etc.) |
|
|
3419 | in libev, then check twice: If valgrind reports something like: |
|
|
3420 | |
|
|
3421 | ==2274== definitely lost: 0 bytes in 0 blocks. |
|
|
3422 | ==2274== possibly lost: 0 bytes in 0 blocks. |
|
|
3423 | ==2274== still reachable: 256 bytes in 1 blocks. |
|
|
3424 | |
|
|
3425 | Then there is no memory leak, just as memory accounted to global variables |
|
|
3426 | is not a memleak - the memory is still being refernced, and didn't leak. |
|
|
3427 | |
|
|
3428 | Similarly, under some circumstances, valgrind might report kernel bugs |
|
|
3429 | as if it were a bug in libev (e.g. in realloc or in the poll backend, |
|
|
3430 | although an acceptable workaround has been found here), or it might be |
|
|
3431 | confused. |
|
|
3432 | |
|
|
3433 | Keep in mind that valgrind is a very good tool, but only a tool. Don't |
|
|
3434 | make it into some kind of religion. |
|
|
3435 | |
|
|
3436 | If you are unsure about something, feel free to contact the mailing list |
|
|
3437 | with the full valgrind report and an explanation on why you think this |
|
|
3438 | is a bug in libev (best check the archives, too :). However, don't be |
|
|
3439 | annoyed when you get a brisk "this is no bug" answer and take the chance |
|
|
3440 | of learning how to interpret valgrind properly. |
|
|
3441 | |
|
|
3442 | If you need, for some reason, empty reports from valgrind for your project |
|
|
3443 | I suggest using suppression lists. |
|
|
3444 | |
|
|
3445 | |
3373 | |
3446 | |
3374 | =head1 COMPLEXITIES |
3447 | =head1 COMPLEXITIES |
3375 | |
3448 | |
3376 | In this section the complexities of (many of) the algorithms used inside |
3449 | In this section the complexities of (many of) the algorithms used inside |
3377 | libev will be explained. For complexity discussions about backends see the |
3450 | libev will be explained. For complexity discussions about backends see the |
… | |
… | |
3439 | involves iterating over all running async watchers or all signal numbers. |
3512 | involves iterating over all running async watchers or all signal numbers. |
3440 | |
3513 | |
3441 | =back |
3514 | =back |
3442 | |
3515 | |
3443 | |
3516 | |
|
|
3517 | =head1 PORTABILITY NOTES |
|
|
3518 | |
3444 | =head1 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
3519 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
3445 | |
3520 | |
3446 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
3521 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
3447 | requires, and its I/O model is fundamentally incompatible with the POSIX |
3522 | requires, and its I/O model is fundamentally incompatible with the POSIX |
3448 | model. Libev still offers limited functionality on this platform in |
3523 | model. Libev still offers limited functionality on this platform in |
3449 | the form of the C<EVBACKEND_SELECT> backend, and only supports socket |
3524 | the form of the C<EVBACKEND_SELECT> backend, and only supports socket |
… | |
… | |
3536 | wrap all I/O functions and provide your own fd management, but the cost of |
3611 | wrap all I/O functions and provide your own fd management, but the cost of |
3537 | calling select (O(n²)) will likely make this unworkable. |
3612 | calling select (O(n²)) will likely make this unworkable. |
3538 | |
3613 | |
3539 | =back |
3614 | =back |
3540 | |
3615 | |
3541 | |
|
|
3542 | =head1 PORTABILITY REQUIREMENTS |
3616 | =head2 PORTABILITY REQUIREMENTS |
3543 | |
3617 | |
3544 | In addition to a working ISO-C implementation, libev relies on a few |
3618 | In addition to a working ISO-C implementation and of course the |
3545 | additional extensions: |
3619 | backend-specific APIs, libev relies on a few additional extensions: |
3546 | |
3620 | |
3547 | =over 4 |
3621 | =over 4 |
3548 | |
3622 | |
3549 | =item C<void (*)(ev_watcher_type *, int revents)> must have compatible |
3623 | =item C<void (*)(ev_watcher_type *, int revents)> must have compatible |
3550 | calling conventions regardless of C<ev_watcher_type *>. |
3624 | calling conventions regardless of C<ev_watcher_type *>. |
… | |
… | |
3575 | except the initial one, and run the default loop in the initial thread as |
3649 | except the initial one, and run the default loop in the initial thread as |
3576 | well. |
3650 | well. |
3577 | |
3651 | |
3578 | =item C<long> must be large enough for common memory allocation sizes |
3652 | =item C<long> must be large enough for common memory allocation sizes |
3579 | |
3653 | |
3580 | To improve portability and simplify using libev, libev uses C<long> |
3654 | To improve portability and simplify its API, libev uses C<long> internally |
3581 | internally instead of C<size_t> when allocating its data structures. On |
3655 | instead of C<size_t> when allocating its data structures. On non-POSIX |
3582 | non-POSIX systems (Microsoft...) this might be unexpectedly low, but |
3656 | systems (Microsoft...) this might be unexpectedly low, but is still at |
3583 | is still at least 31 bits everywhere, which is enough for hundreds of |
3657 | least 31 bits everywhere, which is enough for hundreds of millions of |
3584 | millions of watchers. |
3658 | watchers. |
3585 | |
3659 | |
3586 | =item C<double> must hold a time value in seconds with enough accuracy |
3660 | =item C<double> must hold a time value in seconds with enough accuracy |
3587 | |
3661 | |
3588 | The type C<double> is used to represent timestamps. It is required to |
3662 | The type C<double> is used to represent timestamps. It is required to |
3589 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
3663 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
… | |
… | |
3593 | =back |
3667 | =back |
3594 | |
3668 | |
3595 | If you know of other additional requirements drop me a note. |
3669 | If you know of other additional requirements drop me a note. |
3596 | |
3670 | |
3597 | |
3671 | |
3598 | =head1 COMPILER WARNINGS |
|
|
3599 | |
|
|
3600 | Depending on your compiler and compiler settings, you might get no or a |
|
|
3601 | lot of warnings when compiling libev code. Some people are apparently |
|
|
3602 | scared by this. |
|
|
3603 | |
|
|
3604 | However, these are unavoidable for many reasons. For one, each compiler |
|
|
3605 | has different warnings, and each user has different tastes regarding |
|
|
3606 | warning options. "Warn-free" code therefore cannot be a goal except when |
|
|
3607 | targeting a specific compiler and compiler-version. |
|
|
3608 | |
|
|
3609 | Another reason is that some compiler warnings require elaborate |
|
|
3610 | workarounds, or other changes to the code that make it less clear and less |
|
|
3611 | maintainable. |
|
|
3612 | |
|
|
3613 | And of course, some compiler warnings are just plain stupid, or simply |
|
|
3614 | wrong (because they don't actually warn about the condition their message |
|
|
3615 | seems to warn about). |
|
|
3616 | |
|
|
3617 | While libev is written to generate as few warnings as possible, |
|
|
3618 | "warn-free" code is not a goal, and it is recommended not to build libev |
|
|
3619 | with any compiler warnings enabled unless you are prepared to cope with |
|
|
3620 | them (e.g. by ignoring them). Remember that warnings are just that: |
|
|
3621 | warnings, not errors, or proof of bugs. |
|
|
3622 | |
|
|
3623 | |
|
|
3624 | =head1 VALGRIND |
|
|
3625 | |
|
|
3626 | Valgrind has a special section here because it is a popular tool that is |
|
|
3627 | highly useful, but valgrind reports are very hard to interpret. |
|
|
3628 | |
|
|
3629 | If you think you found a bug (memory leak, uninitialised data access etc.) |
|
|
3630 | in libev, then check twice: If valgrind reports something like: |
|
|
3631 | |
|
|
3632 | ==2274== definitely lost: 0 bytes in 0 blocks. |
|
|
3633 | ==2274== possibly lost: 0 bytes in 0 blocks. |
|
|
3634 | ==2274== still reachable: 256 bytes in 1 blocks. |
|
|
3635 | |
|
|
3636 | Then there is no memory leak. Similarly, under some circumstances, |
|
|
3637 | valgrind might report kernel bugs as if it were a bug in libev, or it |
|
|
3638 | might be confused (it is a very good tool, but only a tool). |
|
|
3639 | |
|
|
3640 | If you are unsure about something, feel free to contact the mailing list |
|
|
3641 | with the full valgrind report and an explanation on why you think this is |
|
|
3642 | a bug in libev. However, don't be annoyed when you get a brisk "this is |
|
|
3643 | no bug" answer and take the chance of learning how to interpret valgrind |
|
|
3644 | properly. |
|
|
3645 | |
|
|
3646 | If you need, for some reason, empty reports from valgrind for your project |
|
|
3647 | I suggest using suppression lists. |
|
|
3648 | |
|
|
3649 | |
|
|
3650 | =head1 AUTHOR |
3672 | =head1 AUTHOR |
3651 | |
3673 | |
3652 | Marc Lehmann <libev@schmorp.de>. |
3674 | Marc Lehmann <libev@schmorp.de>. |
3653 | |
3675 | |