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

Comparing libev/ev.pod (file contents):
Revision 1.190 by root, Tue Sep 30 19:34:14 2008 UTC vs.
Revision 1.191 by root, Tue Sep 30 19:45:23 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)
3313=head2 THREADS AND COROUTINES 3313=head2 THREADS AND COROUTINES
3314 3314
3315=head3 THREADS 3315=head3 THREADS
3316 3316
3317All libev functions are reentrant and thread-safe unless explicitly 3317All libev functions are reentrant and thread-safe unless explicitly
3318documented otherwise, but it uses no locking itself. This means that you 3318documented 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 3319that 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 3320are no concurrent calls into any libev function with the same loop
3321(C<ev_default_*> calls have an implicit default loop parameter, of 3321parameter (C<ev_default_*> calls have an implicit default loop parameter,
3322course): libev guarantees that different event loops share no data 3322of course): libev guarantees that different event loops share no data
3323structures that need any locking. 3323structures that need any locking.
3324 3324
3325Or 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
3326concurrently from multiple threads, calls with the same loop parameter 3326concurrently from multiple threads, calls with the same loop parameter
3327must 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
3369 3369
3370=back 3370=back
3371 3371
3372=head3 COROUTINES 3372=head3 COROUTINES
3373 3373
3374Libev is much more accommodating to coroutines ("cooperative threads"): 3374Libev is very accommodating to coroutines ("cooperative threads"):
3375libev fully supports nesting calls to it's functions from different 3375libev fully supports nesting calls to its functions from different
3376coroutines (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
3377different coroutines and switch freely between both coroutines running the 3377different coroutines, and switch freely between both coroutines running the
3378loop, 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
3379you must not do this from C<ev_periodic> reschedule callbacks. 3379you must not do this from C<ev_periodic> reschedule callbacks.
3380 3380
3381Care 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
3382C<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.
3383 3384
3384=head2 COMPILER WARNINGS 3385=head2 COMPILER WARNINGS
3385 3386
3386Depending on your compiler and compiler settings, you might get no or a 3387Depending on your compiler and compiler settings, you might get no or a
3387lot of warnings when compiling libev code. Some people are apparently 3388lot of warnings when compiling libev code. Some people are apparently
3439annoyed when you get a brisk "this is no bug" answer and take the chance 3440annoyed when you get a brisk "this is no bug" answer and take the chance
3440of learning how to interpret valgrind properly. 3441of learning how to interpret valgrind properly.
3441 3442
3442If you need, for some reason, empty reports from valgrind for your project 3443If you need, for some reason, empty reports from valgrind for your project
3443I suggest using suppression lists. 3444I suggest using suppression lists.
3444
3445
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 3445
3516 3446
3517=head1 PORTABILITY NOTES 3447=head1 PORTABILITY NOTES
3518 3448
3519=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS 3449=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
3667=back 3597=back
3668 3598
3669If you know of other additional requirements drop me a note. 3599If you know of other additional requirements drop me a note.
3670 3600
3671 3601
3602=head1 ALGORITHMIC COMPLEXITIES
3603
3604In this section the complexities of (many of) the algorithms used inside
3605libev will be documented. For complexity discussions about backends see
3606the documentation for C<ev_default_init>.
3607
3608All of the following are about amortised time: If an array needs to be
3609extended, libev needs to realloc and move the whole array, but this
3610happens asymptotically rarer with higher number of elements, so O(1) might
3611mean that libev does a lengthy realloc operation in rare cases, but on
3612average it is much faster and asymptotically approaches constant time.
3613
3614=over 4
3615
3616=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers)
3617
3618This means that, when you have a watcher that triggers in one hour and
3619there are 100 watchers that would trigger before that, then inserting will
3620have to skip roughly seven (C<ld 100>) of these watchers.
3621
3622=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)
3623
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.
3626
3627=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)
3628
3629These just add the watcher into an array or at the head of a list.
3630
3631=item Stopping check/prepare/idle/fork/async watchers: O(1)
3632
3633=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))
3634
3635These watchers are stored in lists, so they need to be walked to find the
3636correct watcher to remove. The lists are usually short (you don't usually
3637have many watchers waiting for the same fd or signal: one is typical, two
3638is rare).
3639
3640=item Finding the next timer in each loop iteration: O(1)
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
3671
3672
3672=head1 AUTHOR 3673=head1 AUTHOR
3673 3674
3674Marc Lehmann <libev@schmorp.de>. 3675Marc Lehmann <libev@schmorp.de>.
3675 3676

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines