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

Comparing libev/ev.pod (file contents):
Revision 1.189 by root, Tue Sep 30 19:33:33 2008 UTC vs.
Revision 1.197 by root, Tue Oct 21 20:52:30 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
892=item C<EV_ERROR> 894=item C<EV_ERROR>
893 895
894An unspecified error has occurred, the watcher has been stopped. This might 896An unspecified error has occurred, the watcher has been stopped. This might
895happen because the watcher could not be properly started because libev 897happen because the watcher could not be properly started because libev
896ran out of memory, a file descriptor was found to be closed or any other 898ran out of memory, a file descriptor was found to be closed or any other
899problem. Libev considers these application bugs.
900
897problem. You best act on it by reporting the problem and somehow coping 901You best act on it by reporting the problem and somehow coping with the
898with the watcher being stopped. 902watcher being stopped. Note that well-written programs should not receive
903an error ever, so when your watcher receives it, this usually indicates a
904bug in your program.
899 905
900Libev will usually signal a few "dummy" events together with an error, for 906Libev will usually signal a few "dummy" events together with an error, for
901example it might indicate that a fd is readable or writable, and if your 907example it might indicate that a fd is readable or writable, and if your
902callbacks is well-written it can just attempt the operation and cope with 908callbacks is well-written it can just attempt the operation and cope with
903the error from read() or write(). This will not work in multi-threaded 909the error from read() or write(). This will not work in multi-threaded
967 973
968 ev_io_start (EV_DEFAULT_UC, &w); 974 ev_io_start (EV_DEFAULT_UC, &w);
969 975
970=item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher) 976=item C<ev_TYPE_stop> (loop *, ev_TYPE *watcher)
971 977
972Stops the given watcher again (if active) and clears the pending 978Stops the given watcher if active, and clears the pending status (whether
979the watcher was active or not).
980
973status. It is possible that stopped watchers are pending (for example, 981It is possible that stopped watchers are pending - for example,
974non-repeating timers are being stopped when they become pending), but 982non-repeating timers are being stopped when they become pending - but
975C<ev_TYPE_stop> ensures that the watcher is neither active nor pending. If 983calling 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 984pending. 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. 985therefore a good idea to always call its C<ev_TYPE_stop> function.
978 986
979=item bool ev_is_active (ev_TYPE *watcher) 987=item bool ev_is_active (ev_TYPE *watcher)
980 988
981Returns a true value iff the watcher is active (i.e. it has been started 989Returns 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 990and not yet been stopped). As long as a watcher is active you must not modify
1792to exchange stat structures with application programs compiled using the 1800to exchange stat structures with application programs compiled using the
1793default compilation environment. 1801default compilation environment.
1794 1802
1795=head3 Inotify and Kqueue 1803=head3 Inotify and Kqueue
1796 1804
1797When C<inotify (7)> support has been compiled into libev (generally only 1805When C<inotify (7)> support has been compiled into libev (generally
1806only available with Linux 2.6.25 or above due to bugs in earlier
1798available with Linux) and present at runtime, it will be used to speed up 1807implementations) and present at runtime, it will be used to speed up
1799change detection where possible. The inotify descriptor will be created lazily 1808change detection where possible. The inotify descriptor will be created
1800when the first C<ev_stat> watcher is being started. 1809lazily when the first C<ev_stat> watcher is being started.
1801 1810
1802Inotify presence does not change the semantics of C<ev_stat> watchers 1811Inotify presence does not change the semantics of C<ev_stat> watchers
1803except that changes might be detected earlier, and in some cases, to avoid 1812except that changes might be detected earlier, and in some cases, to avoid
1804making regular C<stat> calls. Even in the presence of inotify support 1813making regular C<stat> calls. Even in the presence of inotify support
1805there are many cases where libev has to resort to regular C<stat> polling, 1814there are many cases where libev has to resort to regular C<stat> polling,
2384=over 4 2393=over 4
2385 2394
2386=item queueing from a signal handler context 2395=item queueing from a signal handler context
2387 2396
2388To implement race-free queueing, you simply add to the queue in the signal 2397To 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 2398handler but you block the signal handler in the watcher callback. Here is
2390some fictitious SIGUSR1 handler: 2399an example that does that for some fictitious SIGUSR1 handler:
2391 2400
2392 static ev_async mysig; 2401 static ev_async mysig;
2393 2402
2394 static void 2403 static void
2395 sigusr1_handler (void) 2404 sigusr1_handler (void)
2502=over 4 2511=over 4
2503 2512
2504=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) 2513=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback)
2505 2514
2506This function combines a simple timer and an I/O watcher, calls your 2515This function combines a simple timer and an I/O watcher, calls your
2507callback on whichever event happens first and automatically stop both 2516callback 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 2517watchers. 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 2518or timeout without having to allocate/configure/start/stop/free one or
2510more watchers yourself. 2519more watchers yourself.
2511 2520
2512If C<fd> is less than 0, then no I/O watcher will be started and events 2521If 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 2522C<events> argument is being ignored. Otherwise, an C<ev_io> watcher for
2514C<events> set will be created and started. 2523the given C<fd> and C<events> set will be created and started.
2515 2524
2516If C<timeout> is less than 0, then no timeout watcher will be 2525If C<timeout> is less than 0, then no timeout watcher will be
2517started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and 2526started. 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 2527repeat = 0) will be started. C<0> is a valid timeout.
2519dubious value.
2520 2528
2521The callback has the type C<void (*cb)(int revents, void *arg)> and gets 2529The 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 2530passed 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> 2531C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg>
2524value passed to C<ev_once>: 2532value passed to C<ev_once>. Note that it is possible to receive I<both>
2533a timeout and an io event at the same time - you probably should give io
2534events precedence.
2535
2536Example: wait up to ten seconds for data to appear on STDIN_FILENO.
2525 2537
2526 static void stdin_ready (int revents, void *arg) 2538 static void stdin_ready (int revents, void *arg)
2527 { 2539 {
2540 if (revents & EV_READ)
2541 /* stdin might have data for us, joy! */;
2528 if (revents & EV_TIMEOUT) 2542 else if (revents & EV_TIMEOUT)
2529 /* doh, nothing entered */; 2543 /* doh, nothing entered */;
2530 else if (revents & EV_READ)
2531 /* stdin might have data for us, joy! */;
2532 } 2544 }
2533 2545
2534 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 2546 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0);
2535 2547
2536=item ev_feed_event (ev_loop *, watcher *, int revents) 2548=item ev_feed_event (ev_loop *, watcher *, int revents)
3313=head2 THREADS AND COROUTINES 3325=head2 THREADS AND COROUTINES
3314 3326
3315=head3 THREADS 3327=head3 THREADS
3316 3328
3317All libev functions are reentrant and thread-safe unless explicitly 3329All libev functions are reentrant and thread-safe unless explicitly
3318documented otherwise, but it uses no locking itself. This means that you 3330documented otherwise, but libev implements no locking itself. This means
3319can use as many loops as you want in parallel, as long as there are no 3331that you can use as many loops as you want in parallel, as long as there
3320concurrent calls into any libev function with the same loop parameter 3332are no concurrent calls into any libev function with the same loop
3321(C<ev_default_*> calls have an implicit default loop parameter, of 3333parameter (C<ev_default_*> calls have an implicit default loop parameter,
3322course): libev guarantees that different event loops share no data 3334of course): libev guarantees that different event loops share no data
3323structures that need any locking. 3335structures that need any locking.
3324 3336
3325Or to put it differently: calls with different loop parameters can be done 3337Or to put it differently: calls with different loop parameters can be done
3326concurrently from multiple threads, calls with the same loop parameter 3338concurrently from multiple threads, calls with the same loop parameter
3327must be done serially (but can be done from different threads, as long as 3339must be done serially (but can be done from different threads, as long as
3369 3381
3370=back 3382=back
3371 3383
3372=head3 COROUTINES 3384=head3 COROUTINES
3373 3385
3374Libev is much more accommodating to coroutines ("cooperative threads"): 3386Libev is very accommodating to coroutines ("cooperative threads"):
3375libev fully supports nesting calls to it's functions from different 3387libev fully supports nesting calls to its functions from different
3376coroutines (e.g. you can call C<ev_loop> on the same loop from two 3388coroutines (e.g. you can call C<ev_loop> on the same loop from two
3377different coroutines and switch freely between both coroutines running the 3389different coroutines, and switch freely between both coroutines running the
3378loop, as long as you don't confuse yourself). The only exception is that 3390loop, as long as you don't confuse yourself). The only exception is that
3379you must not do this from C<ev_periodic> reschedule callbacks. 3391you must not do this from C<ev_periodic> reschedule callbacks.
3380 3392
3381Care has been taken to ensure that libev does not keep local state inside 3393Care has been taken to ensure that libev does not keep local state inside
3382C<ev_loop>, and other calls do not usually allow coroutine switches. 3394C<ev_loop>, and other calls do not usually allow for coroutine switches as
3395they do not clal any callbacks.
3383 3396
3384=head2 COMPILER WARNINGS 3397=head2 COMPILER WARNINGS
3385 3398
3386Depending on your compiler and compiler settings, you might get no or a 3399Depending on your compiler and compiler settings, you might get no or a
3387lot of warnings when compiling libev code. Some people are apparently 3400lot of warnings when compiling libev code. Some people are apparently
3408with any compiler warnings enabled unless you are prepared to cope with 3421with any compiler warnings enabled unless you are prepared to cope with
3409them (e.g. by ignoring them). Remember that warnings are just that: 3422them (e.g. by ignoring them). Remember that warnings are just that:
3410warnings, not errors, or proof of bugs. 3423warnings, not errors, or proof of bugs.
3411 3424
3412 3425
3413=head1 VALGRIND 3426=head2 VALGRIND
3414 3427
3415Valgrind has a special section here because it is a popular tool that is 3428Valgrind has a special section here because it is a popular tool that is
3416highly useful. Unfortunately, valgrind reports are very hard to interpret. 3429highly useful. Unfortunately, valgrind reports are very hard to interpret.
3417 3430
3418If you think you found a bug (memory leak, uninitialised data access etc.) 3431If you think you found a bug (memory leak, uninitialised data access etc.)
3441 3454
3442If you need, for some reason, empty reports from valgrind for your project 3455If you need, for some reason, empty reports from valgrind for your project
3443I suggest using suppression lists. 3456I suggest using suppression lists.
3444 3457
3445 3458
3446
3447=head1 COMPLEXITIES
3448
3449In this section the complexities of (many of) the algorithms used inside
3450libev will be explained. For complexity discussions about backends see the
3451documentation for C<ev_default_init>.
3452
3453All of the following are about amortised time: If an array needs to be
3454extended, libev needs to realloc and move the whole array, but this
3455happens asymptotically never with higher number of elements, so O(1) might
3456mean it might do a lengthy realloc operation in rare cases, but on average
3457it 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
3463This means that, when you have a watcher that triggers in one hour and
3464there are 100 watchers that would trigger before that then inserting will
3465have 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
3469That means that changing a timer costs less than removing/adding them
3470as 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
3474These 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
3480These watchers are stored in lists then need to be walked to find the
3481correct watcher to remove. The lists are usually short (you don't usually
3482have many watchers waiting for the same fd or signal).
3483
3484=item Finding the next timer in each loop iteration: O(1)
3485
3486By virtue of using a binary or 4-heap, the next timer is always found at a
3487fixed 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
3491A change means an I/O watcher gets started or stopped, which requires
3492libev to recalculate its status (and possibly tell the kernel, depending
3493on 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
3499Priorities are implemented by allocating some space for each
3500priority. When doing priority-based operations, libev usually has to
3501linearly search all the priorities, but starting/stopping and activating
3502watchers 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
3510Sending involves a system call I<iff> there were no other C<ev_async_send>
3511calls in the current loop iteration. Checking for async and signal events
3512involves iterating over all running async watchers or all signal numbers.
3513
3514=back
3515
3516
3517=head1 PORTABILITY 3459=head1 PORTABILITY NOTES
3518 3460
3519=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS 3461=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
3520 3462
3521Win32 doesn't support any of the standards (e.g. POSIX) that libev 3463Win32 doesn't support any of the standards (e.g. POSIX) that libev
3522requires, and its I/O model is fundamentally incompatible with the POSIX 3464requires, and its I/O model is fundamentally incompatible with the POSIX
3667=back 3609=back
3668 3610
3669If you know of other additional requirements drop me a note. 3611If you know of other additional requirements drop me a note.
3670 3612
3671 3613
3614=head1 ALGORITHMIC COMPLEXITIES
3615
3616In this section the complexities of (many of) the algorithms used inside
3617libev will be documented. For complexity discussions about backends see
3618the documentation for C<ev_default_init>.
3619
3620All of the following are about amortised time: If an array needs to be
3621extended, libev needs to realloc and move the whole array, but this
3622happens asymptotically rarer with higher number of elements, so O(1) might
3623mean that libev does a lengthy realloc operation in rare cases, but on
3624average it is much faster and asymptotically approaches constant time.
3625
3626=over 4
3627
3628=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers)
3629
3630This means that, when you have a watcher that triggers in one hour and
3631there are 100 watchers that would trigger before that, then inserting will
3632have to skip roughly seven (C<ld 100>) of these watchers.
3633
3634=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)
3635
3636That means that changing a timer costs less than removing/adding them,
3637as only the relative motion in the event queue has to be paid for.
3638
3639=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)
3640
3641These just add the watcher into an array or at the head of a list.
3642
3643=item Stopping check/prepare/idle/fork/async watchers: O(1)
3644
3645=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))
3646
3647These watchers are stored in lists, so they need to be walked to find the
3648correct watcher to remove. The lists are usually short (you don't usually
3649have many watchers waiting for the same fd or signal: one is typical, two
3650is rare).
3651
3652=item Finding the next timer in each loop iteration: O(1)
3653
3654By virtue of using a binary or 4-heap, the next timer is always found at a
3655fixed position in the storage array.
3656
3657=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3658
3659A change means an I/O watcher gets started or stopped, which requires
3660libev to recalculate its status (and possibly tell the kernel, depending
3661on backend and whether C<ev_io_set> was used).
3662
3663=item Activating one watcher (putting it into the pending state): O(1)
3664
3665=item Priority handling: O(number_of_priorities)
3666
3667Priorities are implemented by allocating some space for each
3668priority. When doing priority-based operations, libev usually has to
3669linearly search all the priorities, but starting/stopping and activating
3670watchers becomes O(1) with respect to priority handling.
3671
3672=item Sending an ev_async: O(1)
3673
3674=item Processing ev_async_send: O(number_of_async_watchers)
3675
3676=item Processing signals: O(max_signal_number)
3677
3678Sending involves a system call I<iff> there were no other C<ev_async_send>
3679calls in the current loop iteration. Checking for async and signal events
3680involves iterating over all running async watchers or all signal numbers.
3681
3682=back
3683
3684
3672=head1 AUTHOR 3685=head1 AUTHOR
3673 3686
3674Marc Lehmann <libev@schmorp.de>. 3687Marc Lehmann <libev@schmorp.de>.
3675 3688

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines