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.194 by root, Mon Oct 20 16:08:36 2008 UTC

685C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or 685C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or
686C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. 686C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return.
687 687
688This "unloop state" will be cleared when entering C<ev_loop> again. 688This "unloop state" will be cleared when entering C<ev_loop> again.
689 689
690It 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
694Ref/unref can be used to add or remove a reference count on the event 696Ref/unref can be used to add or remove a reference count on the event
1625 1627
1626=back 1628=back
1627 1629
1628=head3 Examples 1630=head3 Examples
1629 1631
1630Example: Try to exit cleanly on SIGINT and SIGTERM. 1632Example: Try to exit cleanly on SIGINT.
1631 1633
1632 static void 1634 static void
1633 sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) 1635 sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
1634 { 1636 {
1635 ev_unloop (loop, EVUNLOOP_ALL); 1637 ev_unloop (loop, EVUNLOOP_ALL);
1636 } 1638 }
1637 1639
1638 struct ev_signal signal_watcher; 1640 struct ev_signal signal_watcher;
1639 ev_signal_init (&signal_watcher, sigint_cb, SIGINT); 1641 ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1640 ev_signal_start (loop, &sigint_cb); 1642 ev_signal_start (loop, &signal_watcher);
1641 1643
1642 1644
1643=head2 C<ev_child> - watch out for process status changes 1645=head2 C<ev_child> - watch out for process status changes
1644 1646
1645Child watchers trigger when your process receives a SIGCHLD in response to 1647Child 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 2244So 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 2245that 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 2246this is to have a separate variables for your embeddable loop, try to
2245create it, and if that fails, use the normal loop for everything. 2247create it, and if that fails, use the normal loop for everything.
2246 2248
2249=head3 C<ev_embed> and fork
2250
2251While the C<ev_embed> watcher is running, forks in the embedding loop will
2252automatically be applied to the embedded loop as well, so no special
2253fork handling is required in that case. When the watcher is not running,
2254however, it is still the task of the libev user to call C<ev_loop_fork ()>
2255as applicable.
2256
2247=head3 Watcher-Specific Functions and Data Members 2257=head3 Watcher-Specific Functions and Data Members
2248 2258
2249=over 4 2259=over 4
2250 2260
2251=item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) 2261=item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)
2376=over 4 2386=over 4
2377 2387
2378=item queueing from a signal handler context 2388=item queueing from a signal handler context
2379 2389
2380To implement race-free queueing, you simply add to the queue in the signal 2390To 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 2391handler but you block the signal handler in the watcher callback. Here is
2382some fictitious SIGUSR1 handler: 2392an example that does that for some fictitious SIGUSR1 handler:
2383 2393
2384 static ev_async mysig; 2394 static ev_async mysig;
2385 2395
2386 static void 2396 static void
2387 sigusr1_handler (void) 2397 sigusr1_handler (void)
2494=over 4 2504=over 4
2495 2505
2496=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) 2506=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback)
2497 2507
2498This function combines a simple timer and an I/O watcher, calls your 2508This function combines a simple timer and an I/O watcher, calls your
2499callback on whichever event happens first and automatically stop both 2509callback on whichever event happens first and automatically stops both
2500watchers. This is useful if you want to wait for a single event on an fd 2510watchers. This is useful if you want to wait for a single event on an fd
2501or timeout without having to allocate/configure/start/stop/free one or 2511or timeout without having to allocate/configure/start/stop/free one or
2502more watchers yourself. 2512more watchers yourself.
2503 2513
2504If C<fd> is less than 0, then no I/O watcher will be started and events 2514If C<fd> is less than 0, then no I/O watcher will be started and the
2505is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and 2515C<events> argument is being ignored. Otherwise, an C<ev_io> watcher for
2506C<events> set will be created and started. 2516the given C<fd> and C<events> set will be created and started.
2507 2517
2508If C<timeout> is less than 0, then no timeout watcher will be 2518If C<timeout> is less than 0, then no timeout watcher will be
2509started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and 2519started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and
2510repeat = 0) will be started. While C<0> is a valid timeout, it is of 2520repeat = 0) will be started. C<0> is a valid timeout.
2511dubious value.
2512 2521
2513The callback has the type C<void (*cb)(int revents, void *arg)> and gets 2522The callback has the type C<void (*cb)(int revents, void *arg)> and gets
2514passed an C<revents> set like normal event callbacks (a combination of 2523passed an C<revents> set like normal event callbacks (a combination of
2515C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> 2524C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg>
2516value passed to C<ev_once>: 2525value passed to C<ev_once>. Note that it is possible to receive I<both>
2526a timeout and an io event at the same time - you probably should give io
2527events precedence.
2528
2529Example: wait up to ten seconds for data to appear on STDIN_FILENO.
2517 2530
2518 static void stdin_ready (int revents, void *arg) 2531 static void stdin_ready (int revents, void *arg)
2519 { 2532 {
2533 if (revents & EV_READ)
2534 /* stdin might have data for us, joy! */;
2520 if (revents & EV_TIMEOUT) 2535 else if (revents & EV_TIMEOUT)
2521 /* doh, nothing entered */; 2536 /* doh, nothing entered */;
2522 else if (revents & EV_READ)
2523 /* stdin might have data for us, joy! */;
2524 } 2537 }
2525 2538
2526 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 2539 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0);
2527 2540
2528=item ev_feed_event (ev_loop *, watcher *, int revents) 2541=item ev_feed_event (ev_loop *, watcher *, int revents)
3298And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: 3311And a F<ev_cpp.C> implementation file that contains libev proper and is compiled:
3299 3312
3300 #include "ev_cpp.h" 3313 #include "ev_cpp.h"
3301 #include "ev.c" 3314 #include "ev.c"
3302 3315
3316=head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES
3303 3317
3304=head1 THREADS AND COROUTINES 3318=head2 THREADS AND COROUTINES
3305 3319
3306=head2 THREADS 3320=head3 THREADS
3307 3321
3308All libev functions are reentrant and thread-safe unless explicitly 3322All libev functions are reentrant and thread-safe unless explicitly
3309documented otherwise, but it uses no locking itself. This means that you 3323documented 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 3324that 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 3325are no concurrent calls into any libev function with the same loop
3312(C<ev_default_*> calls have an implicit default loop parameter, of 3326parameter (C<ev_default_*> calls have an implicit default loop parameter,
3313course): libev guarantees that different event loops share no data 3327of course): libev guarantees that different event loops share no data
3314structures that need any locking. 3328structures that need any locking.
3315 3329
3316Or to put it differently: calls with different loop parameters can be done 3330Or to put it differently: calls with different loop parameters can be done
3317concurrently from multiple threads, calls with the same loop parameter 3331concurrently from multiple threads, calls with the same loop parameter
3318must be done serially (but can be done from different threads, as long as 3332must 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 3372default loop and triggering an C<ev_async> watcher from the default loop
3359watcher callback into the event loop interested in the signal. 3373watcher callback into the event loop interested in the signal.
3360 3374
3361=back 3375=back
3362 3376
3363=head2 COROUTINES 3377=head3 COROUTINES
3364 3378
3365Libev is much more accommodating to coroutines ("cooperative threads"): 3379Libev is very accommodating to coroutines ("cooperative threads"):
3366libev fully supports nesting calls to it's functions from different 3380libev fully supports nesting calls to its functions from different
3367coroutines (e.g. you can call C<ev_loop> on the same loop from two 3381coroutines (e.g. you can call C<ev_loop> on the same loop from two
3368different coroutines and switch freely between both coroutines running the 3382different coroutines, and switch freely between both coroutines running the
3369loop, as long as you don't confuse yourself). The only exception is that 3383loop, as long as you don't confuse yourself). The only exception is that
3370you must not do this from C<ev_periodic> reschedule callbacks. 3384you must not do this from C<ev_periodic> reschedule callbacks.
3371 3385
3372Care has been taken to ensure that libev does not keep local state inside 3386Care 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. 3387C<ev_loop>, and other calls do not usually allow for coroutine switches as
3388they do not clal any callbacks.
3374 3389
3390=head2 COMPILER WARNINGS
3375 3391
3376=head1 COMPLEXITIES 3392Depending on your compiler and compiler settings, you might get no or a
3393lot of warnings when compiling libev code. Some people are apparently
3394scared by this.
3377 3395
3378In this section the complexities of (many of) the algorithms used inside 3396However, these are unavoidable for many reasons. For one, each compiler
3379libev will be explained. For complexity discussions about backends see the 3397has different warnings, and each user has different tastes regarding
3380documentation for C<ev_default_init>. 3398warning options. "Warn-free" code therefore cannot be a goal except when
3399targeting a specific compiler and compiler-version.
3381 3400
3382All of the following are about amortised time: If an array needs to be 3401Another reason is that some compiler warnings require elaborate
3383extended, libev needs to realloc and move the whole array, but this 3402workarounds, 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 3403maintainable.
3385mean it might do a lengthy realloc operation in rare cases, but on average
3386it is much faster and asymptotically approaches constant time.
3387 3404
3388=over 4 3405And of course, some compiler warnings are just plain stupid, or simply
3406wrong (because they don't actually warn about the condition their message
3407seems to warn about). For example, certain older gcc versions had some
3408warnings that resulted an extreme number of false positives. These have
3409been fixed, but some people still insist on making code warn-free with
3410such buggy versions.
3389 3411
3390=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) 3412While libev is written to generate as few warnings as possible,
3413"warn-free" code is not a goal, and it is recommended not to build libev
3414with any compiler warnings enabled unless you are prepared to cope with
3415them (e.g. by ignoring them). Remember that warnings are just that:
3416warnings, not errors, or proof of bugs.
3391 3417
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 3418
3396=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) 3419=head2 VALGRIND
3397 3420
3398That means that changing a timer costs less than removing/adding them 3421Valgrind 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. 3422highly useful. Unfortunately, valgrind reports are very hard to interpret.
3400 3423
3401=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) 3424If you think you found a bug (memory leak, uninitialised data access etc.)
3425in libev, then check twice: If valgrind reports something like:
3402 3426
3403These just add the watcher into an array or at the head of a list. 3427 ==2274== definitely lost: 0 bytes in 0 blocks.
3428 ==2274== possibly lost: 0 bytes in 0 blocks.
3429 ==2274== still reachable: 256 bytes in 1 blocks.
3404 3430
3405=item Stopping check/prepare/idle/fork/async watchers: O(1) 3431Then there is no memory leak, just as memory accounted to global variables
3432is not a memleak - the memory is still being refernced, and didn't leak.
3406 3433
3407=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) 3434Similarly, under some circumstances, valgrind might report kernel bugs
3435as if it were a bug in libev (e.g. in realloc or in the poll backend,
3436although an acceptable workaround has been found here), or it might be
3437confused.
3408 3438
3409These watchers are stored in lists then need to be walked to find the 3439Keep 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 3440make it into some kind of religion.
3411have many watchers waiting for the same fd or signal).
3412 3441
3413=item Finding the next timer in each loop iteration: O(1) 3442If you are unsure about something, feel free to contact the mailing list
3443with the full valgrind report and an explanation on why you think this
3444is a bug in libev (best check the archives, too :). However, don't be
3445annoyed when you get a brisk "this is no bug" answer and take the chance
3446of learning how to interpret valgrind properly.
3414 3447
3415By virtue of using a binary or 4-heap, the next timer is always found at a 3448If you need, for some reason, empty reports from valgrind for your project
3416fixed position in the storage array. 3449I suggest using suppression lists.
3417 3450
3418=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3419 3451
3420A change means an I/O watcher gets started or stopped, which requires 3452=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 3453
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 3454=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
3447 3455
3448Win32 doesn't support any of the standards (e.g. POSIX) that libev 3456Win32 doesn't support any of the standards (e.g. POSIX) that libev
3449requires, and its I/O model is fundamentally incompatible with the POSIX 3457requires, and its I/O model is fundamentally incompatible with the POSIX
3450model. Libev still offers limited functionality on this platform in 3458model. Libev still offers limited functionality on this platform in
3451the form of the C<EVBACKEND_SELECT> backend, and only supports socket 3459the 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 3546wrap all I/O functions and provide your own fd management, but the cost of
3539calling select (O(n²)) will likely make this unworkable. 3547calling select (O(n²)) will likely make this unworkable.
3540 3548
3541=back 3549=back
3542 3550
3543
3544=head1 PORTABILITY REQUIREMENTS 3551=head2 PORTABILITY REQUIREMENTS
3545 3552
3546In addition to a working ISO-C implementation, libev relies on a few 3553In addition to a working ISO-C implementation and of course the
3547additional extensions: 3554backend-specific APIs, libev relies on a few additional extensions:
3548 3555
3549=over 4 3556=over 4
3550 3557
3551=item C<void (*)(ev_watcher_type *, int revents)> must have compatible 3558=item C<void (*)(ev_watcher_type *, int revents)> must have compatible
3552calling conventions regardless of C<ev_watcher_type *>. 3559calling conventions regardless of C<ev_watcher_type *>.
3577except the initial one, and run the default loop in the initial thread as 3584except the initial one, and run the default loop in the initial thread as
3578well. 3585well.
3579 3586
3580=item C<long> must be large enough for common memory allocation sizes 3587=item C<long> must be large enough for common memory allocation sizes
3581 3588
3582To improve portability and simplify using libev, libev uses C<long> 3589To improve portability and simplify its API, libev uses C<long> internally
3583internally instead of C<size_t> when allocating its data structures. On 3590instead of C<size_t> when allocating its data structures. On non-POSIX
3584non-POSIX systems (Microsoft...) this might be unexpectedly low, but 3591systems (Microsoft...) this might be unexpectedly low, but is still at
3585is still at least 31 bits everywhere, which is enough for hundreds of 3592least 31 bits everywhere, which is enough for hundreds of millions of
3586millions of watchers. 3593watchers.
3587 3594
3588=item C<double> must hold a time value in seconds with enough accuracy 3595=item C<double> must hold a time value in seconds with enough accuracy
3589 3596
3590The type C<double> is used to represent timestamps. It is required to 3597The 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 3598have at least 51 bits of mantissa (and 9 bits of exponent), which is good
3595=back 3602=back
3596 3603
3597If you know of other additional requirements drop me a note. 3604If you know of other additional requirements drop me a note.
3598 3605
3599 3606
3600=head1 COMPILER WARNINGS 3607=head1 ALGORITHMIC COMPLEXITIES
3601 3608
3602Depending on your compiler and compiler settings, you might get no or a 3609In this section the complexities of (many of) the algorithms used inside
3603lot of warnings when compiling libev code. Some people are apparently 3610libev will be documented. For complexity discussions about backends see
3604scared by this. 3611the documentation for C<ev_default_init>.
3605 3612
3606However, these are unavoidable for many reasons. For one, each compiler 3613All of the following are about amortised time: If an array needs to be
3607has different warnings, and each user has different tastes regarding 3614extended, libev needs to realloc and move the whole array, but this
3608warning options. "Warn-free" code therefore cannot be a goal except when 3615happens asymptotically rarer with higher number of elements, so O(1) might
3609targeting a specific compiler and compiler-version. 3616mean that libev does a lengthy realloc operation in rare cases, but on
3617average it is much faster and asymptotically approaches constant time.
3610 3618
3611Another reason is that some compiler warnings require elaborate 3619=over 4
3612workarounds, or other changes to the code that make it less clear and less
3613maintainable.
3614 3620
3615And of course, some compiler warnings are just plain stupid, or simply 3621=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 3622
3619While libev is written to generate as few warnings as possible, 3623This 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 3624there are 100 watchers that would trigger before that, then inserting will
3621with any compiler warnings enabled unless you are prepared to cope with 3625have 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 3626
3627=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)
3625 3628
3626=head1 VALGRIND 3629That means that changing a timer costs less than removing/adding them,
3630as only the relative motion in the event queue has to be paid for.
3627 3631
3628Valgrind has a special section here because it is a popular tool that is 3632=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)
3629highly useful, but valgrind reports are very hard to interpret.
3630 3633
3631If you think you found a bug (memory leak, uninitialised data access etc.) 3634These 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 3635
3634 ==2274== definitely lost: 0 bytes in 0 blocks. 3636=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 3637
3638Then there is no memory leak. Similarly, under some circumstances, 3638=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 3639
3642If you are unsure about something, feel free to contact the mailing list 3640These 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 3641correct 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 3642have 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 3643is rare).
3646properly.
3647 3644
3648If you need, for some reason, empty reports from valgrind for your project 3645=item Finding the next timer in each loop iteration: O(1)
3649I suggest using suppression lists. 3646
3647By virtue of using a binary or 4-heap, the next timer is always found at a
3648fixed position in the storage array.
3649
3650=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3651
3652A change means an I/O watcher gets started or stopped, which requires
3653libev to recalculate its status (and possibly tell the kernel, depending
3654on backend and whether C<ev_io_set> was used).
3655
3656=item Activating one watcher (putting it into the pending state): O(1)
3657
3658=item Priority handling: O(number_of_priorities)
3659
3660Priorities are implemented by allocating some space for each
3661priority. When doing priority-based operations, libev usually has to
3662linearly search all the priorities, but starting/stopping and activating
3663watchers becomes O(1) with respect to priority handling.
3664
3665=item Sending an ev_async: O(1)
3666
3667=item Processing ev_async_send: O(number_of_async_watchers)
3668
3669=item Processing signals: O(max_signal_number)
3670
3671Sending involves a system call I<iff> there were no other C<ev_async_send>
3672calls in the current loop iteration. Checking for async and signal events
3673involves iterating over all running async watchers or all signal numbers.
3674
3675=back
3650 3676
3651 3677
3652=head1 AUTHOR 3678=head1 AUTHOR
3653 3679
3654Marc Lehmann <libev@schmorp.de>. 3680Marc Lehmann <libev@schmorp.de>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines