… | |
… | |
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 |
… | |
… | |
967 | |
969 | |
968 | ev_io_start (EV_DEFAULT_UC, &w); |
970 | ev_io_start (EV_DEFAULT_UC, &w); |
969 | |
971 | |
970 | =item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher) |
972 | =item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher) |
971 | |
973 | |
972 | Stops the given watcher again (if active) and clears the pending |
974 | Stops the given watcher if active, and clears the pending status (whether |
|
|
975 | the watcher was active or not). |
|
|
976 | |
973 | status. It is possible that stopped watchers are pending (for example, |
977 | It is possible that stopped watchers are pending - for example, |
974 | non-repeating timers are being stopped when they become pending), but |
978 | 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 |
979 | 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 |
980 | 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. |
981 | therefore a good idea to always call its C<ev_TYPE_stop> function. |
978 | |
982 | |
979 | =item bool ev_is_active (ev_TYPE *watcher) |
983 | =item bool ev_is_active (ev_TYPE *watcher) |
980 | |
984 | |
981 | Returns a true value iff the watcher is active (i.e. it has been started |
985 | Returns a true value iff the watcher is active (i.e. it has been started |
982 | and not yet been stopped). As long as a watcher is active you must not modify |
986 | and not yet been stopped). As long as a watcher is active you must not modify |
… | |
… | |
1792 | to exchange stat structures with application programs compiled using the |
1796 | to exchange stat structures with application programs compiled using the |
1793 | default compilation environment. |
1797 | default compilation environment. |
1794 | |
1798 | |
1795 | =head3 Inotify and Kqueue |
1799 | =head3 Inotify and Kqueue |
1796 | |
1800 | |
1797 | When C<inotify (7)> support has been compiled into libev (generally only |
1801 | When C<inotify (7)> support has been compiled into libev (generally |
|
|
1802 | only available with Linux 2.6.25 or above due to bugs in earlier |
1798 | available with Linux) and present at runtime, it will be used to speed up |
1803 | implementations) and present at runtime, it will be used to speed up |
1799 | change detection where possible. The inotify descriptor will be created lazily |
1804 | change detection where possible. The inotify descriptor will be created |
1800 | when the first C<ev_stat> watcher is being started. |
1805 | lazily when the first C<ev_stat> watcher is being started. |
1801 | |
1806 | |
1802 | Inotify presence does not change the semantics of C<ev_stat> watchers |
1807 | 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 |
1808 | 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 |
1809 | 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, |
1810 | there are many cases where libev has to resort to regular C<stat> polling, |
… | |
… | |
2384 | =over 4 |
2389 | =over 4 |
2385 | |
2390 | |
2386 | =item queueing from a signal handler context |
2391 | =item queueing from a signal handler context |
2387 | |
2392 | |
2388 | To implement race-free queueing, you simply add to the queue in the signal |
2393 | 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 |
2394 | handler but you block the signal handler in the watcher callback. Here is |
2390 | some fictitious SIGUSR1 handler: |
2395 | an example that does that for some fictitious SIGUSR1 handler: |
2391 | |
2396 | |
2392 | static ev_async mysig; |
2397 | static ev_async mysig; |
2393 | |
2398 | |
2394 | static void |
2399 | static void |
2395 | sigusr1_handler (void) |
2400 | sigusr1_handler (void) |
… | |
… | |
2502 | =over 4 |
2507 | =over 4 |
2503 | |
2508 | |
2504 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
2509 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
2505 | |
2510 | |
2506 | This function combines a simple timer and an I/O watcher, calls your |
2511 | This function combines a simple timer and an I/O watcher, calls your |
2507 | callback on whichever event happens first and automatically stop both |
2512 | 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 |
2513 | 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 |
2514 | or timeout without having to allocate/configure/start/stop/free one or |
2510 | more watchers yourself. |
2515 | more watchers yourself. |
2511 | |
2516 | |
2512 | If C<fd> is less than 0, then no I/O watcher will be started and events |
2517 | If C<fd> is less than 0, then no I/O watcher will be started and the |
2513 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
2518 | C<events> argument is being ignored. Otherwise, an C<ev_io> watcher for |
2514 | C<events> set will be created and started. |
2519 | the given C<fd> and C<events> set will be created and started. |
2515 | |
2520 | |
2516 | If C<timeout> is less than 0, then no timeout watcher will be |
2521 | If C<timeout> is less than 0, then no timeout watcher will be |
2517 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
2522 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
2518 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
2523 | repeat = 0) will be started. C<0> is a valid timeout. |
2519 | dubious value. |
|
|
2520 | |
2524 | |
2521 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
2525 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
2522 | passed an C<revents> set like normal event callbacks (a combination of |
2526 | passed an C<revents> set like normal event callbacks (a combination of |
2523 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
2527 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
2524 | value passed to C<ev_once>: |
2528 | value passed to C<ev_once>. Note that it is possible to receive I<both> |
|
|
2529 | a timeout and an io event at the same time - you probably should give io |
|
|
2530 | events precedence. |
|
|
2531 | |
|
|
2532 | Example: wait up to ten seconds for data to appear on STDIN_FILENO. |
2525 | |
2533 | |
2526 | static void stdin_ready (int revents, void *arg) |
2534 | static void stdin_ready (int revents, void *arg) |
2527 | { |
2535 | { |
|
|
2536 | if (revents & EV_READ) |
|
|
2537 | /* stdin might have data for us, joy! */; |
2528 | if (revents & EV_TIMEOUT) |
2538 | else if (revents & EV_TIMEOUT) |
2529 | /* doh, nothing entered */; |
2539 | /* doh, nothing entered */; |
2530 | else if (revents & EV_READ) |
|
|
2531 | /* stdin might have data for us, joy! */; |
|
|
2532 | } |
2540 | } |
2533 | |
2541 | |
2534 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
2542 | ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
2535 | |
2543 | |
2536 | =item ev_feed_event (ev_loop *, watcher *, int revents) |
2544 | =item ev_feed_event (ev_loop *, watcher *, int revents) |
… | |
… | |
3313 | =head2 THREADS AND COROUTINES |
3321 | =head2 THREADS AND COROUTINES |
3314 | |
3322 | |
3315 | =head3 THREADS |
3323 | =head3 THREADS |
3316 | |
3324 | |
3317 | All libev functions are reentrant and thread-safe unless explicitly |
3325 | All libev functions are reentrant and thread-safe unless explicitly |
3318 | documented otherwise, but it uses no locking itself. This means that you |
3326 | 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 |
3327 | 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 |
3328 | are no concurrent calls into any libev function with the same loop |
3321 | (C<ev_default_*> calls have an implicit default loop parameter, of |
3329 | parameter (C<ev_default_*> calls have an implicit default loop parameter, |
3322 | course): libev guarantees that different event loops share no data |
3330 | of course): libev guarantees that different event loops share no data |
3323 | structures that need any locking. |
3331 | structures that need any locking. |
3324 | |
3332 | |
3325 | Or to put it differently: calls with different loop parameters can be done |
3333 | Or to put it differently: calls with different loop parameters can be done |
3326 | concurrently from multiple threads, calls with the same loop parameter |
3334 | 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 |
3335 | must be done serially (but can be done from different threads, as long as |
… | |
… | |
3369 | |
3377 | |
3370 | =back |
3378 | =back |
3371 | |
3379 | |
3372 | =head3 COROUTINES |
3380 | =head3 COROUTINES |
3373 | |
3381 | |
3374 | Libev is much more accommodating to coroutines ("cooperative threads"): |
3382 | Libev is very accommodating to coroutines ("cooperative threads"): |
3375 | libev fully supports nesting calls to it's functions from different |
3383 | 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 |
3384 | 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 |
3385 | 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 |
3386 | 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. |
3387 | you must not do this from C<ev_periodic> reschedule callbacks. |
3380 | |
3388 | |
3381 | Care has been taken to ensure that libev does not keep local state inside |
3389 | 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. |
3390 | C<ev_loop>, and other calls do not usually allow for coroutine switches as |
|
|
3391 | they do not clal any callbacks. |
3383 | |
3392 | |
3384 | =head2 COMPILER WARNINGS |
3393 | =head2 COMPILER WARNINGS |
3385 | |
3394 | |
3386 | Depending on your compiler and compiler settings, you might get no or a |
3395 | 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 |
3396 | lot of warnings when compiling libev code. Some people are apparently |
… | |
… | |
3439 | annoyed when you get a brisk "this is no bug" answer and take the chance |
3448 | annoyed when you get a brisk "this is no bug" answer and take the chance |
3440 | of learning how to interpret valgrind properly. |
3449 | of learning how to interpret valgrind properly. |
3441 | |
3450 | |
3442 | If you need, for some reason, empty reports from valgrind for your project |
3451 | If you need, for some reason, empty reports from valgrind for your project |
3443 | I suggest using suppression lists. |
3452 | I suggest using suppression lists. |
3444 | |
|
|
3445 | |
|
|
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 | |
3453 | |
3516 | |
3454 | |
3517 | =head1 PORTABILITY NOTES |
3455 | =head1 PORTABILITY NOTES |
3518 | |
3456 | |
3519 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
3457 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
… | |
… | |
3667 | =back |
3605 | =back |
3668 | |
3606 | |
3669 | If you know of other additional requirements drop me a note. |
3607 | If you know of other additional requirements drop me a note. |
3670 | |
3608 | |
3671 | |
3609 | |
|
|
3610 | =head1 ALGORITHMIC COMPLEXITIES |
|
|
3611 | |
|
|
3612 | In this section the complexities of (many of) the algorithms used inside |
|
|
3613 | libev will be documented. For complexity discussions about backends see |
|
|
3614 | the documentation for C<ev_default_init>. |
|
|
3615 | |
|
|
3616 | All of the following are about amortised time: If an array needs to be |
|
|
3617 | extended, libev needs to realloc and move the whole array, but this |
|
|
3618 | happens asymptotically rarer with higher number of elements, so O(1) might |
|
|
3619 | mean that libev does a lengthy realloc operation in rare cases, but on |
|
|
3620 | average it is much faster and asymptotically approaches constant time. |
|
|
3621 | |
|
|
3622 | =over 4 |
|
|
3623 | |
|
|
3624 | =item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) |
|
|
3625 | |
|
|
3626 | This means that, when you have a watcher that triggers in one hour and |
|
|
3627 | there are 100 watchers that would trigger before that, then inserting will |
|
|
3628 | have to skip roughly seven (C<ld 100>) of these watchers. |
|
|
3629 | |
|
|
3630 | =item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) |
|
|
3631 | |
|
|
3632 | That means that changing a timer costs less than removing/adding them, |
|
|
3633 | as only the relative motion in the event queue has to be paid for. |
|
|
3634 | |
|
|
3635 | =item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) |
|
|
3636 | |
|
|
3637 | These just add the watcher into an array or at the head of a list. |
|
|
3638 | |
|
|
3639 | =item Stopping check/prepare/idle/fork/async watchers: O(1) |
|
|
3640 | |
|
|
3641 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) |
|
|
3642 | |
|
|
3643 | These watchers are stored in lists, so they need to be walked to find the |
|
|
3644 | correct watcher to remove. The lists are usually short (you don't usually |
|
|
3645 | have many watchers waiting for the same fd or signal: one is typical, two |
|
|
3646 | is rare). |
|
|
3647 | |
|
|
3648 | =item Finding the next timer in each loop iteration: O(1) |
|
|
3649 | |
|
|
3650 | By virtue of using a binary or 4-heap, the next timer is always found at a |
|
|
3651 | fixed position in the storage array. |
|
|
3652 | |
|
|
3653 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
|
|
3654 | |
|
|
3655 | A change means an I/O watcher gets started or stopped, which requires |
|
|
3656 | libev to recalculate its status (and possibly tell the kernel, depending |
|
|
3657 | on backend and whether C<ev_io_set> was used). |
|
|
3658 | |
|
|
3659 | =item Activating one watcher (putting it into the pending state): O(1) |
|
|
3660 | |
|
|
3661 | =item Priority handling: O(number_of_priorities) |
|
|
3662 | |
|
|
3663 | Priorities are implemented by allocating some space for each |
|
|
3664 | priority. When doing priority-based operations, libev usually has to |
|
|
3665 | linearly search all the priorities, but starting/stopping and activating |
|
|
3666 | watchers becomes O(1) with respect to priority handling. |
|
|
3667 | |
|
|
3668 | =item Sending an ev_async: O(1) |
|
|
3669 | |
|
|
3670 | =item Processing ev_async_send: O(number_of_async_watchers) |
|
|
3671 | |
|
|
3672 | =item Processing signals: O(max_signal_number) |
|
|
3673 | |
|
|
3674 | Sending involves a system call I<iff> there were no other C<ev_async_send> |
|
|
3675 | calls in the current loop iteration. Checking for async and signal events |
|
|
3676 | involves iterating over all running async watchers or all signal numbers. |
|
|
3677 | |
|
|
3678 | =back |
|
|
3679 | |
|
|
3680 | |
3672 | =head1 AUTHOR |
3681 | =head1 AUTHOR |
3673 | |
3682 | |
3674 | Marc Lehmann <libev@schmorp.de>. |
3683 | Marc Lehmann <libev@schmorp.de>. |
3675 | |
3684 | |