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.193 by root, Wed Oct 1 04:25:25 2008 UTC

2384=over 4 2384=over 4
2385 2385
2386=item queueing from a signal handler context 2386=item queueing from a signal handler context
2387 2387
2388To 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
2389handler 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
2390some fictitious SIGUSR1 handler: 2390an example that does that for some fictitious SIGUSR1 handler:
2391 2391
2392 static ev_async mysig; 2392 static ev_async mysig;
2393 2393
2394 static void 2394 static void
2395 sigusr1_handler (void) 2395 sigusr1_handler (void)
2502=over 4 2502=over 4
2503 2503
2504=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) 2504=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback)
2505 2505
2506This function combines a simple timer and an I/O watcher, calls your 2506This function combines a simple timer and an I/O watcher, calls your
2507callback on whichever event happens first and automatically stop both 2507callback 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 2508watchers. 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 2509or timeout without having to allocate/configure/start/stop/free one or
2510more watchers yourself. 2510more watchers yourself.
2511 2511
2512If C<fd> is less than 0, then no I/O watcher will be started and events 2512If 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 2513C<events> argument is being ignored. Otherwise, an C<ev_io> watcher for
2514C<events> set will be created and started. 2514the given C<fd> and C<events> set will be created and started.
2515 2515
2516If C<timeout> is less than 0, then no timeout watcher will be 2516If C<timeout> is less than 0, then no timeout watcher will be
2517started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and 2517started. 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 2518repeat = 0) will be started. C<0> is a valid timeout.
2519dubious value.
2520 2519
2521The callback has the type C<void (*cb)(int revents, void *arg)> and gets 2520The 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 2521passed 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> 2522C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg>
2524value passed to C<ev_once>: 2523value passed to C<ev_once>. Note that it is possible to receive I<both>
2524a timeout and an io event at the same time - you probably should give io
2525events precedence.
2526
2527Example: wait up to ten seconds for data to appear on STDIN_FILENO.
2525 2528
2526 static void stdin_ready (int revents, void *arg) 2529 static void stdin_ready (int revents, void *arg)
2527 { 2530 {
2531 if (revents & EV_READ)
2532 /* stdin might have data for us, joy! */;
2528 if (revents & EV_TIMEOUT) 2533 else if (revents & EV_TIMEOUT)
2529 /* doh, nothing entered */; 2534 /* doh, nothing entered */;
2530 else if (revents & EV_READ)
2531 /* stdin might have data for us, joy! */;
2532 } 2535 }
2533 2536
2534 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 2537 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0);
2535 2538
2536=item ev_feed_event (ev_loop *, watcher *, int revents) 2539=item ev_feed_event (ev_loop *, watcher *, int revents)
3313=head2 THREADS AND COROUTINES 3316=head2 THREADS AND COROUTINES
3314 3317
3315=head3 THREADS 3318=head3 THREADS
3316 3319
3317All libev functions are reentrant and thread-safe unless explicitly 3320All libev functions are reentrant and thread-safe unless explicitly
3318documented otherwise, but it uses no locking itself. This means that you 3321documented 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 3322that 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 3323are no concurrent calls into any libev function with the same loop
3321(C<ev_default_*> calls have an implicit default loop parameter, of 3324parameter (C<ev_default_*> calls have an implicit default loop parameter,
3322course): libev guarantees that different event loops share no data 3325of course): libev guarantees that different event loops share no data
3323structures that need any locking. 3326structures that need any locking.
3324 3327
3325Or to put it differently: calls with different loop parameters can be done 3328Or to put it differently: calls with different loop parameters can be done
3326concurrently from multiple threads, calls with the same loop parameter 3329concurrently from multiple threads, calls with the same loop parameter
3327must be done serially (but can be done from different threads, as long as 3330must be done serially (but can be done from different threads, as long as
3369 3372
3370=back 3373=back
3371 3374
3372=head3 COROUTINES 3375=head3 COROUTINES
3373 3376
3374Libev is much more accommodating to coroutines ("cooperative threads"): 3377Libev is very accommodating to coroutines ("cooperative threads"):
3375libev fully supports nesting calls to it's functions from different 3378libev fully supports nesting calls to its functions from different
3376coroutines (e.g. you can call C<ev_loop> on the same loop from two 3379coroutines (e.g. you can call C<ev_loop> on the same loop from two
3377different coroutines and switch freely between both coroutines running the 3380different coroutines, and switch freely between both coroutines running the
3378loop, as long as you don't confuse yourself). The only exception is that 3381loop, as long as you don't confuse yourself). The only exception is that
3379you must not do this from C<ev_periodic> reschedule callbacks. 3382you must not do this from C<ev_periodic> reschedule callbacks.
3380 3383
3381Care has been taken to ensure that libev does not keep local state inside 3384Care 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. 3385C<ev_loop>, and other calls do not usually allow for coroutine switches as
3386they do not clal any callbacks.
3383 3387
3384=head2 COMPILER WARNINGS 3388=head2 COMPILER WARNINGS
3385 3389
3386Depending on your compiler and compiler settings, you might get no or a 3390Depending on your compiler and compiler settings, you might get no or a
3387lot of warnings when compiling libev code. Some people are apparently 3391lot of warnings when compiling libev code. Some people are apparently
3408with any compiler warnings enabled unless you are prepared to cope with 3412with any compiler warnings enabled unless you are prepared to cope with
3409them (e.g. by ignoring them). Remember that warnings are just that: 3413them (e.g. by ignoring them). Remember that warnings are just that:
3410warnings, not errors, or proof of bugs. 3414warnings, not errors, or proof of bugs.
3411 3415
3412 3416
3413=head1 VALGRIND 3417=head2 VALGRIND
3414 3418
3415Valgrind has a special section here because it is a popular tool that is 3419Valgrind has a special section here because it is a popular tool that is
3416highly useful. Unfortunately, valgrind reports are very hard to interpret. 3420highly useful. Unfortunately, valgrind reports are very hard to interpret.
3417 3421
3418If you think you found a bug (memory leak, uninitialised data access etc.) 3422If you think you found a bug (memory leak, uninitialised data access etc.)
3441 3445
3442If you need, for some reason, empty reports from valgrind for your project 3446If you need, for some reason, empty reports from valgrind for your project
3443I suggest using suppression lists. 3447I suggest using suppression lists.
3444 3448
3445 3449
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 3450=head1 PORTABILITY NOTES
3518 3451
3519=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS 3452=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
3520 3453
3521Win32 doesn't support any of the standards (e.g. POSIX) that libev 3454Win32 doesn't support any of the standards (e.g. POSIX) that libev
3522requires, and its I/O model is fundamentally incompatible with the POSIX 3455requires, and its I/O model is fundamentally incompatible with the POSIX
3667=back 3600=back
3668 3601
3669If you know of other additional requirements drop me a note. 3602If you know of other additional requirements drop me a note.
3670 3603
3671 3604
3605=head1 ALGORITHMIC COMPLEXITIES
3606
3607In this section the complexities of (many of) the algorithms used inside
3608libev will be documented. For complexity discussions about backends see
3609the documentation for C<ev_default_init>.
3610
3611All of the following are about amortised time: If an array needs to be
3612extended, libev needs to realloc and move the whole array, but this
3613happens asymptotically rarer with higher number of elements, so O(1) might
3614mean that libev does a lengthy realloc operation in rare cases, but on
3615average it is much faster and asymptotically approaches constant time.
3616
3617=over 4
3618
3619=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers)
3620
3621This means that, when you have a watcher that triggers in one hour and
3622there are 100 watchers that would trigger before that, then inserting will
3623have to skip roughly seven (C<ld 100>) of these watchers.
3624
3625=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)
3626
3627That means that changing a timer costs less than removing/adding them,
3628as only the relative motion in the event queue has to be paid for.
3629
3630=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)
3631
3632These just add the watcher into an array or at the head of a list.
3633
3634=item Stopping check/prepare/idle/fork/async watchers: O(1)
3635
3636=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))
3637
3638These watchers are stored in lists, so they need to be walked to find the
3639correct watcher to remove. The lists are usually short (you don't usually
3640have many watchers waiting for the same fd or signal: one is typical, two
3641is rare).
3642
3643=item Finding the next timer in each loop iteration: O(1)
3644
3645By virtue of using a binary or 4-heap, the next timer is always found at a
3646fixed position in the storage array.
3647
3648=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3649
3650A change means an I/O watcher gets started or stopped, which requires
3651libev to recalculate its status (and possibly tell the kernel, depending
3652on backend and whether C<ev_io_set> was used).
3653
3654=item Activating one watcher (putting it into the pending state): O(1)
3655
3656=item Priority handling: O(number_of_priorities)
3657
3658Priorities are implemented by allocating some space for each
3659priority. When doing priority-based operations, libev usually has to
3660linearly search all the priorities, but starting/stopping and activating
3661watchers becomes O(1) with respect to priority handling.
3662
3663=item Sending an ev_async: O(1)
3664
3665=item Processing ev_async_send: O(number_of_async_watchers)
3666
3667=item Processing signals: O(max_signal_number)
3668
3669Sending involves a system call I<iff> there were no other C<ev_async_send>
3670calls in the current loop iteration. Checking for async and signal events
3671involves iterating over all running async watchers or all signal numbers.
3672
3673=back
3674
3675
3672=head1 AUTHOR 3676=head1 AUTHOR
3673 3677
3674Marc Lehmann <libev@schmorp.de>. 3678Marc Lehmann <libev@schmorp.de>.
3675 3679

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines