ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.186 by root, Wed Sep 24 07:56:14 2008 UTC vs.
Revision 1.191 by root, Tue Sep 30 19:45:23 2008 UTC

1625 1625
1626=back 1626=back
1627 1627
1628=head3 Examples 1628=head3 Examples
1629 1629
1630Example: Try to exit cleanly on SIGINT and SIGTERM. 1630Example: 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
1645Child watchers trigger when your process receives a SIGCHLD in response to 1645Child watchers trigger when your process receives a SIGCHLD in response to
2242So when you want to use this feature you will always have to be prepared 2242So when you want to use this feature you will always have to be prepared
2243that you cannot get an embeddable loop. The recommended way to get around 2243that you cannot get an embeddable loop. The recommended way to get around
2244this is to have a separate variables for your embeddable loop, try to 2244this is to have a separate variables for your embeddable loop, try to
2245create it, and if that fails, use the normal loop for everything. 2245create it, and if that fails, use the normal loop for everything.
2246 2246
2247=head3 C<ev_embed> and fork
2248
2249While the C<ev_embed> watcher is running, forks in the embedding loop will
2250automatically be applied to the embedded loop as well, so no special
2251fork handling is required in that case. When the watcher is not running,
2252however, it is still the task of the libev user to call C<ev_loop_fork ()>
2253as 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)
2376=over 4 2384=over 4
2377 2385
2378=item queueing from a signal handler context 2386=item queueing from a signal handler context
2379 2387
2380To implement race-free queueing, you simply add to the queue in the signal 2388To implement race-free queueing, you simply add to the queue in the signal
2381handler but you block the signal handler in the watcher callback. Here is an example that does that for 2389handler but you block the signal handler in the watcher callback. Here is
2382some fictitious SIGUSR1 handler: 2390an example that does that for some fictitious SIGUSR1 handler:
2383 2391
2384 static ev_async mysig; 2392 static ev_async mysig;
2385 2393
2386 static void 2394 static void
2387 sigusr1_handler (void) 2395 sigusr1_handler (void)
3298And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: 3306And 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
3308All libev functions are reentrant and thread-safe unless explicitly 3317All libev functions are reentrant and thread-safe unless explicitly
3309documented otherwise, but it uses no locking itself. This means that you 3318documented otherwise, but libev implements no locking itself. This means
3310can use as many loops as you want in parallel, as long as there are no 3319that you can use as many loops as you want in parallel, as long as there
3311concurrent calls into any libev function with the same loop parameter 3320are no concurrent calls into any libev function with the same loop
3312(C<ev_default_*> calls have an implicit default loop parameter, of 3321parameter (C<ev_default_*> calls have an implicit default loop parameter,
3313course): libev guarantees that different event loops share no data 3322of course): libev guarantees that different event loops share no data
3314structures that need any locking. 3323structures that need any locking.
3315 3324
3316Or to put it differently: calls with different loop parameters can be done 3325Or to put it differently: calls with different loop parameters can be done
3317concurrently from multiple threads, calls with the same loop parameter 3326concurrently from multiple threads, calls with the same loop parameter
3318must be done serially (but can be done from different threads, as long as 3327must be done serially (but can be done from different threads, as long as
3358default loop and triggering an C<ev_async> watcher from the default loop 3367default loop and triggering an C<ev_async> watcher from the default loop
3359watcher callback into the event loop interested in the signal. 3368watcher callback into the event loop interested in the signal.
3360 3369
3361=back 3370=back
3362 3371
3363=head2 COROUTINES 3372=head3 COROUTINES
3364 3373
3365Libev is much more accommodating to coroutines ("cooperative threads"): 3374Libev is very accommodating to coroutines ("cooperative threads"):
3366libev fully supports nesting calls to it's functions from different 3375libev fully supports nesting calls to its functions from different
3367coroutines (e.g. you can call C<ev_loop> on the same loop from two 3376coroutines (e.g. you can call C<ev_loop> on the same loop from two
3368different coroutines and switch freely between both coroutines running the 3377different coroutines, and switch freely between both coroutines running the
3369loop, as long as you don't confuse yourself). The only exception is that 3378loop, as long as you don't confuse yourself). The only exception is that
3370you must not do this from C<ev_periodic> reschedule callbacks. 3379you must not do this from C<ev_periodic> reschedule callbacks.
3371 3380
3372Care has been taken to ensure that libev does not keep local state inside 3381Care has been taken to ensure that libev does not keep local state inside
3373C<ev_loop>, and other calls do not usually allow coroutine switches. 3382C<ev_loop>, and other calls do not usually allow for coroutine switches as
3383they do not clal any callbacks.
3374 3384
3385=head2 COMPILER WARNINGS
3375 3386
3376=head1 COMPLEXITIES 3387Depending on your compiler and compiler settings, you might get no or a
3388lot of warnings when compiling libev code. Some people are apparently
3389scared by this.
3377 3390
3378In this section the complexities of (many of) the algorithms used inside 3391However, these are unavoidable for many reasons. For one, each compiler
3379libev will be explained. For complexity discussions about backends see the 3392has different warnings, and each user has different tastes regarding
3380documentation for C<ev_default_init>. 3393warning options. "Warn-free" code therefore cannot be a goal except when
3394targeting a specific compiler and compiler-version.
3381 3395
3382All of the following are about amortised time: If an array needs to be 3396Another reason is that some compiler warnings require elaborate
3383extended, libev needs to realloc and move the whole array, but this 3397workarounds, or other changes to the code that make it less clear and less
3384happens asymptotically never with higher number of elements, so O(1) might 3398maintainable.
3385mean it might do a lengthy realloc operation in rare cases, but on average
3386it is much faster and asymptotically approaches constant time.
3387 3399
3388=over 4 3400And of course, some compiler warnings are just plain stupid, or simply
3401wrong (because they don't actually warn about the condition their message
3402seems to warn about). For example, certain older gcc versions had some
3403warnings that resulted an extreme number of false positives. These have
3404been fixed, but some people still insist on making code warn-free with
3405such buggy versions.
3389 3406
3390=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) 3407While libev is written to generate as few warnings as possible,
3408"warn-free" code is not a goal, and it is recommended not to build libev
3409with any compiler warnings enabled unless you are prepared to cope with
3410them (e.g. by ignoring them). Remember that warnings are just that:
3411warnings, not errors, or proof of bugs.
3391 3412
3392This means that, when you have a watcher that triggers in one hour and
3393there are 100 watchers that would trigger before that then inserting will
3394have to skip roughly seven (C<ld 100>) of these watchers.
3395 3413
3396=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) 3414=head2 VALGRIND
3397 3415
3398That means that changing a timer costs less than removing/adding them 3416Valgrind has a special section here because it is a popular tool that is
3399as only the relative motion in the event queue has to be paid for. 3417highly useful. Unfortunately, valgrind reports are very hard to interpret.
3400 3418
3401=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) 3419If you think you found a bug (memory leak, uninitialised data access etc.)
3420in libev, then check twice: If valgrind reports something like:
3402 3421
3403These just add the watcher into an array or at the head of a list. 3422 ==2274== definitely lost: 0 bytes in 0 blocks.
3423 ==2274== possibly lost: 0 bytes in 0 blocks.
3424 ==2274== still reachable: 256 bytes in 1 blocks.
3404 3425
3405=item Stopping check/prepare/idle/fork/async watchers: O(1) 3426Then there is no memory leak, just as memory accounted to global variables
3427is not a memleak - the memory is still being refernced, and didn't leak.
3406 3428
3407=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) 3429Similarly, under some circumstances, valgrind might report kernel bugs
3430as if it were a bug in libev (e.g. in realloc or in the poll backend,
3431although an acceptable workaround has been found here), or it might be
3432confused.
3408 3433
3409These watchers are stored in lists then need to be walked to find the 3434Keep in mind that valgrind is a very good tool, but only a tool. Don't
3410correct watcher to remove. The lists are usually short (you don't usually 3435make it into some kind of religion.
3411have many watchers waiting for the same fd or signal).
3412 3436
3413=item Finding the next timer in each loop iteration: O(1) 3437If you are unsure about something, feel free to contact the mailing list
3438with the full valgrind report and an explanation on why you think this
3439is a bug in libev (best check the archives, too :). However, don't be
3440annoyed when you get a brisk "this is no bug" answer and take the chance
3441of learning how to interpret valgrind properly.
3414 3442
3415By virtue of using a binary or 4-heap, the next timer is always found at a 3443If you need, for some reason, empty reports from valgrind for your project
3416fixed position in the storage array. 3444I suggest using suppression lists.
3417 3445
3418=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3419 3446
3420A change means an I/O watcher gets started or stopped, which requires 3447=head1 PORTABILITY NOTES
3421libev to recalculate its status (and possibly tell the kernel, depending
3422on backend and whether C<ev_io_set> was used).
3423 3448
3424=item Activating one watcher (putting it into the pending state): O(1)
3425
3426=item Priority handling: O(number_of_priorities)
3427
3428Priorities are implemented by allocating some space for each
3429priority. When doing priority-based operations, libev usually has to
3430linearly search all the priorities, but starting/stopping and activating
3431watchers becomes O(1) with respect to priority handling.
3432
3433=item Sending an ev_async: O(1)
3434
3435=item Processing ev_async_send: O(number_of_async_watchers)
3436
3437=item Processing signals: O(max_signal_number)
3438
3439Sending involves a system call I<iff> there were no other C<ev_async_send>
3440calls in the current loop iteration. Checking for async and signal events
3441involves iterating over all running async watchers or all signal numbers.
3442
3443=back
3444
3445
3446=head1 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS 3449=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
3447 3450
3448Win32 doesn't support any of the standards (e.g. POSIX) that libev 3451Win32 doesn't support any of the standards (e.g. POSIX) that libev
3449requires, and its I/O model is fundamentally incompatible with the POSIX 3452requires, and its I/O model is fundamentally incompatible with the POSIX
3450model. Libev still offers limited functionality on this platform in 3453model. Libev still offers limited functionality on this platform in
3451the form of the C<EVBACKEND_SELECT> backend, and only supports socket 3454the form of the C<EVBACKEND_SELECT> backend, and only supports socket
3538wrap all I/O functions and provide your own fd management, but the cost of 3541wrap all I/O functions and provide your own fd management, but the cost of
3539calling select (O(n²)) will likely make this unworkable. 3542calling select (O(n²)) will likely make this unworkable.
3540 3543
3541=back 3544=back
3542 3545
3543
3544=head1 PORTABILITY REQUIREMENTS 3546=head2 PORTABILITY REQUIREMENTS
3545 3547
3546In addition to a working ISO-C implementation, libev relies on a few 3548In addition to a working ISO-C implementation and of course the
3547additional extensions: 3549backend-specific APIs, libev relies on a few additional extensions:
3548 3550
3549=over 4 3551=over 4
3550 3552
3551=item C<void (*)(ev_watcher_type *, int revents)> must have compatible 3553=item C<void (*)(ev_watcher_type *, int revents)> must have compatible
3552calling conventions regardless of C<ev_watcher_type *>. 3554calling conventions regardless of C<ev_watcher_type *>.
3577except the initial one, and run the default loop in the initial thread as 3579except the initial one, and run the default loop in the initial thread as
3578well. 3580well.
3579 3581
3580=item C<long> must be large enough for common memory allocation sizes 3582=item C<long> must be large enough for common memory allocation sizes
3581 3583
3582To improve portability and simplify using libev, libev uses C<long> 3584To improve portability and simplify its API, libev uses C<long> internally
3583internally instead of C<size_t> when allocating its data structures. On 3585instead of C<size_t> when allocating its data structures. On non-POSIX
3584non-POSIX systems (Microsoft...) this might be unexpectedly low, but 3586systems (Microsoft...) this might be unexpectedly low, but is still at
3585is still at least 31 bits everywhere, which is enough for hundreds of 3587least 31 bits everywhere, which is enough for hundreds of millions of
3586millions of watchers. 3588watchers.
3587 3589
3588=item C<double> must hold a time value in seconds with enough accuracy 3590=item C<double> must hold a time value in seconds with enough accuracy
3589 3591
3590The type C<double> is used to represent timestamps. It is required to 3592The type C<double> is used to represent timestamps. It is required to
3591have at least 51 bits of mantissa (and 9 bits of exponent), which is good 3593have at least 51 bits of mantissa (and 9 bits of exponent), which is good
3595=back 3597=back
3596 3598
3597If you know of other additional requirements drop me a note. 3599If you know of other additional requirements drop me a note.
3598 3600
3599 3601
3600=head1 COMPILER WARNINGS 3602=head1 ALGORITHMIC COMPLEXITIES
3601 3603
3602Depending on your compiler and compiler settings, you might get no or a 3604In this section the complexities of (many of) the algorithms used inside
3603lot of warnings when compiling libev code. Some people are apparently 3605libev will be documented. For complexity discussions about backends see
3604scared by this. 3606the documentation for C<ev_default_init>.
3605 3607
3606However, these are unavoidable for many reasons. For one, each compiler 3608All of the following are about amortised time: If an array needs to be
3607has different warnings, and each user has different tastes regarding 3609extended, libev needs to realloc and move the whole array, but this
3608warning options. "Warn-free" code therefore cannot be a goal except when 3610happens asymptotically rarer with higher number of elements, so O(1) might
3609targeting a specific compiler and compiler-version. 3611mean that libev does a lengthy realloc operation in rare cases, but on
3612average it is much faster and asymptotically approaches constant time.
3610 3613
3611Another reason is that some compiler warnings require elaborate 3614=over 4
3612workarounds, or other changes to the code that make it less clear and less
3613maintainable.
3614 3615
3615And of course, some compiler warnings are just plain stupid, or simply 3616=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers)
3616wrong (because they don't actually warn about the condition their message
3617seems to warn about).
3618 3617
3619While libev is written to generate as few warnings as possible, 3618This means that, when you have a watcher that triggers in one hour and
3620"warn-free" code is not a goal, and it is recommended not to build libev 3619there are 100 watchers that would trigger before that, then inserting will
3621with any compiler warnings enabled unless you are prepared to cope with 3620have to skip roughly seven (C<ld 100>) of these watchers.
3622them (e.g. by ignoring them). Remember that warnings are just that:
3623warnings, not errors, or proof of bugs.
3624 3621
3622=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)
3625 3623
3626=head1 VALGRIND 3624That means that changing a timer costs less than removing/adding them,
3625as only the relative motion in the event queue has to be paid for.
3627 3626
3628Valgrind has a special section here because it is a popular tool that is 3627=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)
3629highly useful, but valgrind reports are very hard to interpret.
3630 3628
3631If you think you found a bug (memory leak, uninitialised data access etc.) 3629These just add the watcher into an array or at the head of a list.
3632in libev, then check twice: If valgrind reports something like:
3633 3630
3634 ==2274== definitely lost: 0 bytes in 0 blocks. 3631=item Stopping check/prepare/idle/fork/async watchers: O(1)
3635 ==2274== possibly lost: 0 bytes in 0 blocks.
3636 ==2274== still reachable: 256 bytes in 1 blocks.
3637 3632
3638Then there is no memory leak. Similarly, under some circumstances, 3633=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))
3639valgrind might report kernel bugs as if it were a bug in libev, or it
3640might be confused (it is a very good tool, but only a tool).
3641 3634
3642If you are unsure about something, feel free to contact the mailing list 3635These watchers are stored in lists, so they need to be walked to find the
3643with the full valgrind report and an explanation on why you think this is 3636correct watcher to remove. The lists are usually short (you don't usually
3644a bug in libev. However, don't be annoyed when you get a brisk "this is 3637have many watchers waiting for the same fd or signal: one is typical, two
3645no bug" answer and take the chance of learning how to interpret valgrind 3638is rare).
3646properly.
3647 3639
3648If you need, for some reason, empty reports from valgrind for your project 3640=item Finding the next timer in each loop iteration: O(1)
3649I suggest using suppression lists. 3641
3642By virtue of using a binary or 4-heap, the next timer is always found at a
3643fixed 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
3647A change means an I/O watcher gets started or stopped, which requires
3648libev to recalculate its status (and possibly tell the kernel, depending
3649on 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
3655Priorities are implemented by allocating some space for each
3656priority. When doing priority-based operations, libev usually has to
3657linearly search all the priorities, but starting/stopping and activating
3658watchers 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
3666Sending involves a system call I<iff> there were no other C<ev_async_send>
3667calls in the current loop iteration. Checking for async and signal events
3668involves iterating over all running async watchers or all signal numbers.
3669
3670=back
3650 3671
3651 3672
3652=head1 AUTHOR 3673=head1 AUTHOR
3653 3674
3654Marc Lehmann <libev@schmorp.de>. 3675Marc Lehmann <libev@schmorp.de>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines