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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines