… | |
… | |
2384 | =over 4 |
2384 | =over 4 |
2385 | |
2385 | |
2386 | =item queueing from a signal handler context |
2386 | =item queueing from a signal handler context |
2387 | |
2387 | |
2388 | To implement race-free queueing, you simply add to the queue in the signal |
2388 | To implement race-free queueing, you simply add to the queue in the signal |
2389 | handler but you block the signal handler in the watcher callback. Here is an example that does that for |
2389 | handler but you block the signal handler in the watcher callback. Here is |
2390 | some fictitious SIGUSR1 handler: |
2390 | an example that does that for some fictitious SIGUSR1 handler: |
2391 | |
2391 | |
2392 | static ev_async mysig; |
2392 | static ev_async mysig; |
2393 | |
2393 | |
2394 | static void |
2394 | static void |
2395 | sigusr1_handler (void) |
2395 | sigusr1_handler (void) |
… | |
… | |
2502 | =over 4 |
2502 | =over 4 |
2503 | |
2503 | |
2504 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
2504 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
2505 | |
2505 | |
2506 | This function combines a simple timer and an I/O watcher, calls your |
2506 | This function combines a simple timer and an I/O watcher, calls your |
2507 | callback on whichever event happens first and automatically stop both |
2507 | callback on whichever event happens first and automatically stops both |
2508 | watchers. This is useful if you want to wait for a single event on an fd |
2508 | watchers. This is useful if you want to wait for a single event on an fd |
2509 | or timeout without having to allocate/configure/start/stop/free one or |
2509 | or timeout without having to allocate/configure/start/stop/free one or |
2510 | more watchers yourself. |
2510 | more watchers yourself. |
2511 | |
2511 | |
2512 | If C<fd> is less than 0, then no I/O watcher will be started and events |
2512 | If C<fd> is less than 0, then no I/O watcher will be started and the |
2513 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
2513 | C<events> argument is being ignored. Otherwise, an C<ev_io> watcher for |
2514 | C<events> set will be created and started. |
2514 | the given C<fd> and C<events> set will be created and started. |
2515 | |
2515 | |
2516 | If C<timeout> is less than 0, then no timeout watcher will be |
2516 | If C<timeout> is less than 0, then no timeout watcher will be |
2517 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
2517 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
2518 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
2518 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
2519 | dubious value. |
2519 | dubious value. |
… | |
… | |
3313 | =head2 THREADS AND COROUTINES |
3313 | =head2 THREADS AND COROUTINES |
3314 | |
3314 | |
3315 | =head3 THREADS |
3315 | =head3 THREADS |
3316 | |
3316 | |
3317 | All libev functions are reentrant and thread-safe unless explicitly |
3317 | All libev functions are reentrant and thread-safe unless explicitly |
3318 | documented otherwise, but it uses no locking itself. This means that you |
3318 | documented otherwise, but libev implements no locking itself. This means |
3319 | can use as many loops as you want in parallel, as long as there are no |
3319 | that you can use as many loops as you want in parallel, as long as there |
3320 | concurrent calls into any libev function with the same loop parameter |
3320 | are no concurrent calls into any libev function with the same loop |
3321 | (C<ev_default_*> calls have an implicit default loop parameter, of |
3321 | parameter (C<ev_default_*> calls have an implicit default loop parameter, |
3322 | course): libev guarantees that different event loops share no data |
3322 | of course): libev guarantees that different event loops share no data |
3323 | structures that need any locking. |
3323 | structures that need any locking. |
3324 | |
3324 | |
3325 | 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 |
3326 | concurrently from multiple threads, calls with the same loop parameter |
3326 | concurrently from multiple threads, calls with the same loop parameter |
3327 | 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 |
… | |
… | |
3369 | |
3369 | |
3370 | =back |
3370 | =back |
3371 | |
3371 | |
3372 | =head3 COROUTINES |
3372 | =head3 COROUTINES |
3373 | |
3373 | |
3374 | Libev is much more accommodating to coroutines ("cooperative threads"): |
3374 | Libev is very accommodating to coroutines ("cooperative threads"): |
3375 | libev fully supports nesting calls to it's functions from different |
3375 | libev fully supports nesting calls to its functions from different |
3376 | 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 |
3377 | different coroutines and switch freely between both coroutines running the |
3377 | different coroutines, and switch freely between both coroutines running the |
3378 | loop, as long as you don't confuse yourself). The only exception is that |
3378 | loop, as long as you don't confuse yourself). The only exception is that |
3379 | you must not do this from C<ev_periodic> reschedule callbacks. |
3379 | you must not do this from C<ev_periodic> reschedule callbacks. |
3380 | |
3380 | |
3381 | 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 |
3382 | C<ev_loop>, and other calls do not usually allow coroutine switches. |
3382 | C<ev_loop>, and other calls do not usually allow for coroutine switches as |
|
|
3383 | they do not clal any callbacks. |
3383 | |
3384 | |
3384 | =head2 COMPILER WARNINGS |
3385 | =head2 COMPILER WARNINGS |
3385 | |
3386 | |
3386 | Depending on your compiler and compiler settings, you might get no or a |
3387 | 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 | lot of warnings when compiling libev code. Some people are apparently |
… | |
… | |
3408 | with any compiler warnings enabled unless you are prepared to cope with |
3409 | 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 | them (e.g. by ignoring them). Remember that warnings are just that: |
3410 | warnings, not errors, or proof of bugs. |
3411 | warnings, not errors, or proof of bugs. |
3411 | |
3412 | |
3412 | |
3413 | |
3413 | =head1 VALGRIND |
3414 | =head2 VALGRIND |
3414 | |
3415 | |
3415 | Valgrind has a special section here because it is a popular tool that is |
3416 | 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 | highly useful. Unfortunately, valgrind reports are very hard to interpret. |
3417 | |
3418 | |
3418 | If you think you found a bug (memory leak, uninitialised data access etc.) |
3419 | If you think you found a bug (memory leak, uninitialised data access etc.) |
… | |
… | |
3441 | |
3442 | |
3442 | If you need, for some reason, empty reports from valgrind for your project |
3443 | If you need, for some reason, empty reports from valgrind for your project |
3443 | I suggest using suppression lists. |
3444 | I suggest using suppression lists. |
3444 | |
3445 | |
3445 | |
3446 | |
3446 | |
|
|
3447 | =head1 COMPLEXITIES |
|
|
3448 | |
|
|
3449 | In this section the complexities of (many of) the algorithms used inside |
|
|
3450 | libev will be explained. For complexity discussions about backends see the |
|
|
3451 | documentation for C<ev_default_init>. |
|
|
3452 | |
|
|
3453 | All of the following are about amortised time: If an array needs to be |
|
|
3454 | extended, libev needs to realloc and move the whole array, but this |
|
|
3455 | happens asymptotically never with higher number of elements, so O(1) might |
|
|
3456 | mean it might do a lengthy realloc operation in rare cases, but on average |
|
|
3457 | it is much faster and asymptotically approaches constant time. |
|
|
3458 | |
|
|
3459 | =over 4 |
|
|
3460 | |
|
|
3461 | =item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) |
|
|
3462 | |
|
|
3463 | This means that, when you have a watcher that triggers in one hour and |
|
|
3464 | there are 100 watchers that would trigger before that then inserting will |
|
|
3465 | have to skip roughly seven (C<ld 100>) of these watchers. |
|
|
3466 | |
|
|
3467 | =item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) |
|
|
3468 | |
|
|
3469 | That means that changing a timer costs less than removing/adding them |
|
|
3470 | as only the relative motion in the event queue has to be paid for. |
|
|
3471 | |
|
|
3472 | =item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) |
|
|
3473 | |
|
|
3474 | These just add the watcher into an array or at the head of a list. |
|
|
3475 | |
|
|
3476 | =item Stopping check/prepare/idle/fork/async watchers: O(1) |
|
|
3477 | |
|
|
3478 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) |
|
|
3479 | |
|
|
3480 | These watchers are stored in lists then need to be walked to find the |
|
|
3481 | correct watcher to remove. The lists are usually short (you don't usually |
|
|
3482 | have many watchers waiting for the same fd or signal). |
|
|
3483 | |
|
|
3484 | =item Finding the next timer in each loop iteration: O(1) |
|
|
3485 | |
|
|
3486 | By virtue of using a binary or 4-heap, the next timer is always found at a |
|
|
3487 | fixed position in the storage array. |
|
|
3488 | |
|
|
3489 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
|
|
3490 | |
|
|
3491 | A change means an I/O watcher gets started or stopped, which requires |
|
|
3492 | libev to recalculate its status (and possibly tell the kernel, depending |
|
|
3493 | on backend and whether C<ev_io_set> was used). |
|
|
3494 | |
|
|
3495 | =item Activating one watcher (putting it into the pending state): O(1) |
|
|
3496 | |
|
|
3497 | =item Priority handling: O(number_of_priorities) |
|
|
3498 | |
|
|
3499 | Priorities are implemented by allocating some space for each |
|
|
3500 | priority. When doing priority-based operations, libev usually has to |
|
|
3501 | linearly search all the priorities, but starting/stopping and activating |
|
|
3502 | watchers becomes O(1) with respect to priority handling. |
|
|
3503 | |
|
|
3504 | =item Sending an ev_async: O(1) |
|
|
3505 | |
|
|
3506 | =item Processing ev_async_send: O(number_of_async_watchers) |
|
|
3507 | |
|
|
3508 | =item Processing signals: O(max_signal_number) |
|
|
3509 | |
|
|
3510 | Sending involves a system call I<iff> there were no other C<ev_async_send> |
|
|
3511 | calls in the current loop iteration. Checking for async and signal events |
|
|
3512 | involves iterating over all running async watchers or all signal numbers. |
|
|
3513 | |
|
|
3514 | =back |
|
|
3515 | |
|
|
3516 | |
|
|
3517 | =head1 PORTABILITY |
3447 | =head1 PORTABILITY NOTES |
3518 | |
3448 | |
3519 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
3449 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
3520 | |
3450 | |
3521 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
3451 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
3522 | requires, and its I/O model is fundamentally incompatible with the POSIX |
3452 | requires, and its I/O model is fundamentally incompatible with the POSIX |
… | |
… | |
3667 | =back |
3597 | =back |
3668 | |
3598 | |
3669 | If you know of other additional requirements drop me a note. |
3599 | If you know of other additional requirements drop me a note. |
3670 | |
3600 | |
3671 | |
3601 | |
|
|
3602 | =head1 ALGORITHMIC COMPLEXITIES |
|
|
3603 | |
|
|
3604 | In this section the complexities of (many of) the algorithms used inside |
|
|
3605 | libev will be documented. For complexity discussions about backends see |
|
|
3606 | the documentation for C<ev_default_init>. |
|
|
3607 | |
|
|
3608 | All of the following are about amortised time: If an array needs to be |
|
|
3609 | extended, libev needs to realloc and move the whole array, but this |
|
|
3610 | happens asymptotically rarer with higher number of elements, so O(1) might |
|
|
3611 | mean that libev does a lengthy realloc operation in rare cases, but on |
|
|
3612 | average it is much faster and asymptotically approaches constant time. |
|
|
3613 | |
|
|
3614 | =over 4 |
|
|
3615 | |
|
|
3616 | =item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) |
|
|
3617 | |
|
|
3618 | This means that, when you have a watcher that triggers in one hour and |
|
|
3619 | there are 100 watchers that would trigger before that, then inserting will |
|
|
3620 | have to skip roughly seven (C<ld 100>) of these watchers. |
|
|
3621 | |
|
|
3622 | =item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) |
|
|
3623 | |
|
|
3624 | That means that changing a timer costs less than removing/adding them, |
|
|
3625 | as only the relative motion in the event queue has to be paid for. |
|
|
3626 | |
|
|
3627 | =item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) |
|
|
3628 | |
|
|
3629 | These just add the watcher into an array or at the head of a list. |
|
|
3630 | |
|
|
3631 | =item Stopping check/prepare/idle/fork/async watchers: O(1) |
|
|
3632 | |
|
|
3633 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) |
|
|
3634 | |
|
|
3635 | These watchers are stored in lists, so they need to be walked to find the |
|
|
3636 | correct watcher to remove. The lists are usually short (you don't usually |
|
|
3637 | have many watchers waiting for the same fd or signal: one is typical, two |
|
|
3638 | is rare). |
|
|
3639 | |
|
|
3640 | =item Finding the next timer in each loop iteration: O(1) |
|
|
3641 | |
|
|
3642 | By virtue of using a binary or 4-heap, the next timer is always found at a |
|
|
3643 | fixed position in the storage array. |
|
|
3644 | |
|
|
3645 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
|
|
3646 | |
|
|
3647 | A change means an I/O watcher gets started or stopped, which requires |
|
|
3648 | libev to recalculate its status (and possibly tell the kernel, depending |
|
|
3649 | on backend and whether C<ev_io_set> was used). |
|
|
3650 | |
|
|
3651 | =item Activating one watcher (putting it into the pending state): O(1) |
|
|
3652 | |
|
|
3653 | =item Priority handling: O(number_of_priorities) |
|
|
3654 | |
|
|
3655 | Priorities are implemented by allocating some space for each |
|
|
3656 | priority. When doing priority-based operations, libev usually has to |
|
|
3657 | linearly search all the priorities, but starting/stopping and activating |
|
|
3658 | watchers becomes O(1) with respect to priority handling. |
|
|
3659 | |
|
|
3660 | =item Sending an ev_async: O(1) |
|
|
3661 | |
|
|
3662 | =item Processing ev_async_send: O(number_of_async_watchers) |
|
|
3663 | |
|
|
3664 | =item Processing signals: O(max_signal_number) |
|
|
3665 | |
|
|
3666 | Sending involves a system call I<iff> there were no other C<ev_async_send> |
|
|
3667 | calls in the current loop iteration. Checking for async and signal events |
|
|
3668 | involves iterating over all running async watchers or all signal numbers. |
|
|
3669 | |
|
|
3670 | =back |
|
|
3671 | |
|
|
3672 | |
3672 | =head1 AUTHOR |
3673 | =head1 AUTHOR |
3673 | |
3674 | |
3674 | Marc Lehmann <libev@schmorp.de>. |
3675 | Marc Lehmann <libev@schmorp.de>. |
3675 | |
3676 | |