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

Comparing libev/ev.pod (file contents):
Revision 1.184 by root, Tue Sep 23 09:11:14 2008 UTC vs.
Revision 1.196 by root, Tue Oct 21 20:04:14 2008 UTC

214C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for 214C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for
215recommended ones. 215recommended ones.
216 216
217See the description of C<ev_embed> watchers for more info. 217See the description of C<ev_embed> watchers for more info.
218 218
219=item ev_set_allocator (void *(*cb)(void *ptr, long size)) 219=item ev_set_allocator (void *(*cb)(void *ptr, long size)) [NOT REENTRANT]
220 220
221Sets the allocation function to use (the prototype is similar - the 221Sets the allocation function to use (the prototype is similar - the
222semantics are identical to the C<realloc> C89/SuS/POSIX function). It is 222semantics are identical to the C<realloc> C89/SuS/POSIX function). It is
223used to allocate and free memory (no surprises here). If it returns zero 223used to allocate and free memory (no surprises here). If it returns zero
224when memory needs to be allocated (C<size != 0>), the library might abort 224when memory needs to be allocated (C<size != 0>), the library might abort
250 } 250 }
251 251
252 ... 252 ...
253 ev_set_allocator (persistent_realloc); 253 ev_set_allocator (persistent_realloc);
254 254
255=item ev_set_syserr_cb (void (*cb)(const char *msg)); 255=item ev_set_syserr_cb (void (*cb)(const char *msg)); [NOT REENTRANT]
256 256
257Set the callback function to call on a retryable system call error (such 257Set the callback function to call on a retryable system call error (such
258as failed select, poll, epoll_wait). The message is a printable string 258as failed select, poll, epoll_wait). The message is a printable string
259indicating the system call or subsystem causing the problem. If this 259indicating the system call or subsystem causing the problem. If this
260callback is set, then libev will expect it to remedy the situation, no 260callback is set, then libev will expect it to remedy the situation, no
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
1792to exchange stat structures with application programs compiled using the 1796to exchange stat structures with application programs compiled using the
1793default compilation environment. 1797default compilation environment.
1794 1798
1795=head3 Inotify and Kqueue 1799=head3 Inotify and Kqueue
1796 1800
1797When C<inotify (7)> support has been compiled into libev (generally only 1801When C<inotify (7)> support has been compiled into libev (generally
1802only 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 1803implementations) and present at runtime, it will be used to speed up
1799change detection where possible. The inotify descriptor will be created lazily 1804change detection where possible. The inotify descriptor will be created
1800when the first C<ev_stat> watcher is being started. 1805lazily when the first C<ev_stat> watcher is being started.
1801 1806
1802Inotify presence does not change the semantics of C<ev_stat> watchers 1807Inotify presence does not change the semantics of C<ev_stat> watchers
1803except that changes might be detected earlier, and in some cases, to avoid 1808except that changes might be detected earlier, and in some cases, to avoid
1804making regular C<stat> calls. Even in the presence of inotify support 1809making 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, 1810there are many cases where libev has to resort to regular C<stat> polling,
2242So when you want to use this feature you will always have to be prepared 2247So 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 2248that 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 2249this is to have a separate variables for your embeddable loop, try to
2245create it, and if that fails, use the normal loop for everything. 2250create it, and if that fails, use the normal loop for everything.
2246 2251
2252=head3 C<ev_embed> and fork
2253
2254While the C<ev_embed> watcher is running, forks in the embedding loop will
2255automatically be applied to the embedded loop as well, so no special
2256fork handling is required in that case. When the watcher is not running,
2257however, it is still the task of the libev user to call C<ev_loop_fork ()>
2258as applicable.
2259
2247=head3 Watcher-Specific Functions and Data Members 2260=head3 Watcher-Specific Functions and Data Members
2248 2261
2249=over 4 2262=over 4
2250 2263
2251=item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) 2264=item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)
2376=over 4 2389=over 4
2377 2390
2378=item queueing from a signal handler context 2391=item queueing from a signal handler context
2379 2392
2380To implement race-free queueing, you simply add to the queue in the signal 2393To 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 2394handler but you block the signal handler in the watcher callback. Here is
2382some fictitious SIGUSR1 handler: 2395an example that does that for some fictitious SIGUSR1 handler:
2383 2396
2384 static ev_async mysig; 2397 static ev_async mysig;
2385 2398
2386 static void 2399 static void
2387 sigusr1_handler (void) 2400 sigusr1_handler (void)
2494=over 4 2507=over 4
2495 2508
2496=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) 2509=item ev_once (loop, int fd, int events, ev_tstamp timeout, callback)
2497 2510
2498This function combines a simple timer and an I/O watcher, calls your 2511This function combines a simple timer and an I/O watcher, calls your
2499callback on whichever event happens first and automatically stop both 2512callback 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 2513watchers. 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 2514or timeout without having to allocate/configure/start/stop/free one or
2502more watchers yourself. 2515more watchers yourself.
2503 2516
2504If C<fd> is less than 0, then no I/O watcher will be started and events 2517If 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 2518C<events> argument is being ignored. Otherwise, an C<ev_io> watcher for
2506C<events> set will be created and started. 2519the given C<fd> and C<events> set will be created and started.
2507 2520
2508If C<timeout> is less than 0, then no timeout watcher will be 2521If C<timeout> is less than 0, then no timeout watcher will be
2509started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and 2522started. 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 2523repeat = 0) will be started. C<0> is a valid timeout.
2511dubious value.
2512 2524
2513The callback has the type C<void (*cb)(int revents, void *arg)> and gets 2525The 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 2526passed 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> 2527C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg>
2516value passed to C<ev_once>: 2528value passed to C<ev_once>. Note that it is possible to receive I<both>
2529a timeout and an io event at the same time - you probably should give io
2530events precedence.
2531
2532Example: wait up to ten seconds for data to appear on STDIN_FILENO.
2517 2533
2518 static void stdin_ready (int revents, void *arg) 2534 static void stdin_ready (int revents, void *arg)
2519 { 2535 {
2536 if (revents & EV_READ)
2537 /* stdin might have data for us, joy! */;
2520 if (revents & EV_TIMEOUT) 2538 else if (revents & EV_TIMEOUT)
2521 /* doh, nothing entered */; 2539 /* doh, nothing entered */;
2522 else if (revents & EV_READ)
2523 /* stdin might have data for us, joy! */;
2524 } 2540 }
2525 2541
2526 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 2542 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0);
2527 2543
2528=item ev_feed_event (ev_loop *, watcher *, int revents) 2544=item ev_feed_event (ev_loop *, watcher *, int revents)
3241definition and a statement, respectively. See the F<ev.h> header file for 3257definition and a statement, respectively. See the F<ev.h> header file for
3242their default definitions. One possible use for overriding these is to 3258their default definitions. One possible use for overriding these is to
3243avoid the C<struct ev_loop *> as first argument in all cases, or to use 3259avoid the C<struct ev_loop *> as first argument in all cases, or to use
3244method calls instead of plain function calls in C++. 3260method calls instead of plain function calls in C++.
3245 3261
3262=back
3263
3246=head2 EXPORTED API SYMBOLS 3264=head2 EXPORTED API SYMBOLS
3247 3265
3248If you need to re-export the API (e.g. via a DLL) and you need a list of 3266If you need to re-export the API (e.g. via a DLL) and you need a list of
3249exported symbols, you can use the provided F<Symbol.*> files which list 3267exported symbols, you can use the provided F<Symbol.*> files which list
3250all public symbols, one per line: 3268all public symbols, one per line:
3296And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: 3314And a F<ev_cpp.C> implementation file that contains libev proper and is compiled:
3297 3315
3298 #include "ev_cpp.h" 3316 #include "ev_cpp.h"
3299 #include "ev.c" 3317 #include "ev.c"
3300 3318
3319=head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES
3301 3320
3302=head1 THREADS AND COROUTINES 3321=head2 THREADS AND COROUTINES
3303 3322
3304=head2 THREADS 3323=head3 THREADS
3305 3324
3306Libev itself is thread-safe (unless the opposite is specifically 3325All libev functions are reentrant and thread-safe unless explicitly
3307documented for a function), but it uses no locking itself. This means that 3326documented otherwise, but libev implements no locking itself. This means
3308you can use as many loops as you want in parallel, as long as only one 3327that you can use as many loops as you want in parallel, as long as there
3309thread ever calls into one libev function with the same loop parameter: 3328are no concurrent calls into any libev function with the same loop
3329parameter (C<ev_default_*> calls have an implicit default loop parameter,
3310libev guarantees that different event loops share no data structures that 3330of course): libev guarantees that different event loops share no data
3311need locking. 3331structures that need any locking.
3312 3332
3313Or to put it differently: calls with different loop parameters can be done 3333Or to put it differently: calls with different loop parameters can be done
3314concurrently from multiple threads, calls with the same loop parameter 3334concurrently from multiple threads, calls with the same loop parameter
3315must be done serially (but can be done from different threads, as long as 3335must be done serially (but can be done from different threads, as long as
3316only one thread ever is inside a call at any point in time, e.g. by using 3336only one thread ever is inside a call at any point in time, e.g. by using
3317a mutex per loop). 3337a mutex per loop).
3318 3338
3319Specifically to support threads (and signal handlers), libev implements 3339Specifically to support threads (and signal handlers), libev implements
3320so-called C<ev_async> watchers, which allow some limited form of 3340so-called C<ev_async> watchers, which allow some limited form of
3321concurrency on the same event loop. 3341concurrency on the same event loop, namely waking it up "from the
3342outside".
3322 3343
3323If you want to know which design (one loop, locking, or multiple loops 3344If you want to know which design (one loop, locking, or multiple loops
3324without or something else still) is best for your problem, then I cannot 3345without or something else still) is best for your problem, then I cannot
3325help you. I can give some generic advice however: 3346help you, but here is some generic advice:
3326 3347
3327=over 4 3348=over 4
3328 3349
3329=item * most applications have a main thread: use the default libev loop 3350=item * most applications have a main thread: use the default libev loop
3330in that thread, or create a separate thread running only the default loop. 3351in that thread, or create a separate thread running only the default loop.
3354default loop and triggering an C<ev_async> watcher from the default loop 3375default loop and triggering an C<ev_async> watcher from the default loop
3355watcher callback into the event loop interested in the signal. 3376watcher callback into the event loop interested in the signal.
3356 3377
3357=back 3378=back
3358 3379
3359=head2 COROUTINES 3380=head3 COROUTINES
3360 3381
3361Libev is much more accommodating to coroutines ("cooperative threads"): 3382Libev is very accommodating to coroutines ("cooperative threads"):
3362libev fully supports nesting calls to it's functions from different 3383libev fully supports nesting calls to its functions from different
3363coroutines (e.g. you can call C<ev_loop> on the same loop from two 3384coroutines (e.g. you can call C<ev_loop> on the same loop from two
3364different coroutines and switch freely between both coroutines running the 3385different coroutines, and switch freely between both coroutines running the
3365loop, as long as you don't confuse yourself). The only exception is that 3386loop, as long as you don't confuse yourself). The only exception is that
3366you must not do this from C<ev_periodic> reschedule callbacks. 3387you must not do this from C<ev_periodic> reschedule callbacks.
3367 3388
3368Care has been taken to ensure that libev does not keep local state inside 3389Care has been taken to ensure that libev does not keep local state inside
3369C<ev_loop>, and other calls do not usually allow coroutine switches. 3390C<ev_loop>, and other calls do not usually allow for coroutine switches as
3391they do not clal any callbacks.
3370 3392
3393=head2 COMPILER WARNINGS
3371 3394
3372=head1 COMPLEXITIES 3395Depending on your compiler and compiler settings, you might get no or a
3396lot of warnings when compiling libev code. Some people are apparently
3397scared by this.
3373 3398
3374In this section the complexities of (many of) the algorithms used inside 3399However, these are unavoidable for many reasons. For one, each compiler
3375libev will be explained. For complexity discussions about backends see the 3400has different warnings, and each user has different tastes regarding
3376documentation for C<ev_default_init>. 3401warning options. "Warn-free" code therefore cannot be a goal except when
3402targeting a specific compiler and compiler-version.
3377 3403
3378All of the following are about amortised time: If an array needs to be 3404Another reason is that some compiler warnings require elaborate
3379extended, libev needs to realloc and move the whole array, but this 3405workarounds, or other changes to the code that make it less clear and less
3380happens asymptotically never with higher number of elements, so O(1) might 3406maintainable.
3381mean it might do a lengthy realloc operation in rare cases, but on average
3382it is much faster and asymptotically approaches constant time.
3383 3407
3384=over 4 3408And of course, some compiler warnings are just plain stupid, or simply
3409wrong (because they don't actually warn about the condition their message
3410seems to warn about). For example, certain older gcc versions had some
3411warnings that resulted an extreme number of false positives. These have
3412been fixed, but some people still insist on making code warn-free with
3413such buggy versions.
3385 3414
3386=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) 3415While libev is written to generate as few warnings as possible,
3416"warn-free" code is not a goal, and it is recommended not to build libev
3417with any compiler warnings enabled unless you are prepared to cope with
3418them (e.g. by ignoring them). Remember that warnings are just that:
3419warnings, not errors, or proof of bugs.
3387 3420
3388This means that, when you have a watcher that triggers in one hour and
3389there are 100 watchers that would trigger before that then inserting will
3390have to skip roughly seven (C<ld 100>) of these watchers.
3391 3421
3392=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) 3422=head2 VALGRIND
3393 3423
3394That means that changing a timer costs less than removing/adding them 3424Valgrind has a special section here because it is a popular tool that is
3395as only the relative motion in the event queue has to be paid for. 3425highly useful. Unfortunately, valgrind reports are very hard to interpret.
3396 3426
3397=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) 3427If you think you found a bug (memory leak, uninitialised data access etc.)
3428in libev, then check twice: If valgrind reports something like:
3398 3429
3399These just add the watcher into an array or at the head of a list. 3430 ==2274== definitely lost: 0 bytes in 0 blocks.
3431 ==2274== possibly lost: 0 bytes in 0 blocks.
3432 ==2274== still reachable: 256 bytes in 1 blocks.
3400 3433
3401=item Stopping check/prepare/idle/fork/async watchers: O(1) 3434Then there is no memory leak, just as memory accounted to global variables
3435is not a memleak - the memory is still being refernced, and didn't leak.
3402 3436
3403=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) 3437Similarly, under some circumstances, valgrind might report kernel bugs
3438as if it were a bug in libev (e.g. in realloc or in the poll backend,
3439although an acceptable workaround has been found here), or it might be
3440confused.
3404 3441
3405These watchers are stored in lists then need to be walked to find the 3442Keep in mind that valgrind is a very good tool, but only a tool. Don't
3406correct watcher to remove. The lists are usually short (you don't usually 3443make it into some kind of religion.
3407have many watchers waiting for the same fd or signal).
3408 3444
3409=item Finding the next timer in each loop iteration: O(1) 3445If you are unsure about something, feel free to contact the mailing list
3446with the full valgrind report and an explanation on why you think this
3447is a bug in libev (best check the archives, too :). However, don't be
3448annoyed when you get a brisk "this is no bug" answer and take the chance
3449of learning how to interpret valgrind properly.
3410 3450
3411By virtue of using a binary or 4-heap, the next timer is always found at a 3451If you need, for some reason, empty reports from valgrind for your project
3412fixed position in the storage array. 3452I suggest using suppression lists.
3413 3453
3414=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3415 3454
3416A change means an I/O watcher gets started or stopped, which requires 3455=head1 PORTABILITY NOTES
3417libev to recalculate its status (and possibly tell the kernel, depending
3418on backend and whether C<ev_io_set> was used).
3419 3456
3420=item Activating one watcher (putting it into the pending state): O(1)
3421
3422=item Priority handling: O(number_of_priorities)
3423
3424Priorities are implemented by allocating some space for each
3425priority. When doing priority-based operations, libev usually has to
3426linearly search all the priorities, but starting/stopping and activating
3427watchers becomes O(1) with respect to priority handling.
3428
3429=item Sending an ev_async: O(1)
3430
3431=item Processing ev_async_send: O(number_of_async_watchers)
3432
3433=item Processing signals: O(max_signal_number)
3434
3435Sending involves a system call I<iff> there were no other C<ev_async_send>
3436calls in the current loop iteration. Checking for async and signal events
3437involves iterating over all running async watchers or all signal numbers.
3438
3439=back
3440
3441
3442=head1 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS 3457=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
3443 3458
3444Win32 doesn't support any of the standards (e.g. POSIX) that libev 3459Win32 doesn't support any of the standards (e.g. POSIX) that libev
3445requires, and its I/O model is fundamentally incompatible with the POSIX 3460requires, and its I/O model is fundamentally incompatible with the POSIX
3446model. Libev still offers limited functionality on this platform in 3461model. Libev still offers limited functionality on this platform in
3447the form of the C<EVBACKEND_SELECT> backend, and only supports socket 3462the form of the C<EVBACKEND_SELECT> backend, and only supports socket
3534wrap all I/O functions and provide your own fd management, but the cost of 3549wrap all I/O functions and provide your own fd management, but the cost of
3535calling select (O(n²)) will likely make this unworkable. 3550calling select (O(n²)) will likely make this unworkable.
3536 3551
3537=back 3552=back
3538 3553
3539
3540=head1 PORTABILITY REQUIREMENTS 3554=head2 PORTABILITY REQUIREMENTS
3541 3555
3542In addition to a working ISO-C implementation, libev relies on a few 3556In addition to a working ISO-C implementation and of course the
3543additional extensions: 3557backend-specific APIs, libev relies on a few additional extensions:
3544 3558
3545=over 4 3559=over 4
3546 3560
3547=item C<void (*)(ev_watcher_type *, int revents)> must have compatible 3561=item C<void (*)(ev_watcher_type *, int revents)> must have compatible
3548calling conventions regardless of C<ev_watcher_type *>. 3562calling conventions regardless of C<ev_watcher_type *>.
3573except the initial one, and run the default loop in the initial thread as 3587except the initial one, and run the default loop in the initial thread as
3574well. 3588well.
3575 3589
3576=item C<long> must be large enough for common memory allocation sizes 3590=item C<long> must be large enough for common memory allocation sizes
3577 3591
3578To improve portability and simplify using libev, libev uses C<long> 3592To improve portability and simplify its API, libev uses C<long> internally
3579internally instead of C<size_t> when allocating its data structures. On 3593instead of C<size_t> when allocating its data structures. On non-POSIX
3580non-POSIX systems (Microsoft...) this might be unexpectedly low, but 3594systems (Microsoft...) this might be unexpectedly low, but is still at
3581is still at least 31 bits everywhere, which is enough for hundreds of 3595least 31 bits everywhere, which is enough for hundreds of millions of
3582millions of watchers. 3596watchers.
3583 3597
3584=item C<double> must hold a time value in seconds with enough accuracy 3598=item C<double> must hold a time value in seconds with enough accuracy
3585 3599
3586The type C<double> is used to represent timestamps. It is required to 3600The type C<double> is used to represent timestamps. It is required to
3587have at least 51 bits of mantissa (and 9 bits of exponent), which is good 3601have at least 51 bits of mantissa (and 9 bits of exponent), which is good
3591=back 3605=back
3592 3606
3593If you know of other additional requirements drop me a note. 3607If you know of other additional requirements drop me a note.
3594 3608
3595 3609
3596=head1 COMPILER WARNINGS 3610=head1 ALGORITHMIC COMPLEXITIES
3597 3611
3598Depending on your compiler and compiler settings, you might get no or a 3612In this section the complexities of (many of) the algorithms used inside
3599lot of warnings when compiling libev code. Some people are apparently 3613libev will be documented. For complexity discussions about backends see
3600scared by this. 3614the documentation for C<ev_default_init>.
3601 3615
3602However, these are unavoidable for many reasons. For one, each compiler 3616All of the following are about amortised time: If an array needs to be
3603has different warnings, and each user has different tastes regarding 3617extended, libev needs to realloc and move the whole array, but this
3604warning options. "Warn-free" code therefore cannot be a goal except when 3618happens asymptotically rarer with higher number of elements, so O(1) might
3605targeting a specific compiler and compiler-version. 3619mean that libev does a lengthy realloc operation in rare cases, but on
3620average it is much faster and asymptotically approaches constant time.
3606 3621
3607Another reason is that some compiler warnings require elaborate 3622=over 4
3608workarounds, or other changes to the code that make it less clear and less
3609maintainable.
3610 3623
3611And of course, some compiler warnings are just plain stupid, or simply 3624=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers)
3612wrong (because they don't actually warn about the condition their message
3613seems to warn about).
3614 3625
3615While libev is written to generate as few warnings as possible, 3626This means that, when you have a watcher that triggers in one hour and
3616"warn-free" code is not a goal, and it is recommended not to build libev 3627there are 100 watchers that would trigger before that, then inserting will
3617with any compiler warnings enabled unless you are prepared to cope with 3628have to skip roughly seven (C<ld 100>) of these watchers.
3618them (e.g. by ignoring them). Remember that warnings are just that:
3619warnings, not errors, or proof of bugs.
3620 3629
3630=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)
3621 3631
3622=head1 VALGRIND 3632That means that changing a timer costs less than removing/adding them,
3633as only the relative motion in the event queue has to be paid for.
3623 3634
3624Valgrind has a special section here because it is a popular tool that is 3635=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)
3625highly useful, but valgrind reports are very hard to interpret.
3626 3636
3627If you think you found a bug (memory leak, uninitialised data access etc.) 3637These just add the watcher into an array or at the head of a list.
3628in libev, then check twice: If valgrind reports something like:
3629 3638
3630 ==2274== definitely lost: 0 bytes in 0 blocks. 3639=item Stopping check/prepare/idle/fork/async watchers: O(1)
3631 ==2274== possibly lost: 0 bytes in 0 blocks.
3632 ==2274== still reachable: 256 bytes in 1 blocks.
3633 3640
3634Then there is no memory leak. Similarly, under some circumstances, 3641=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))
3635valgrind might report kernel bugs as if it were a bug in libev, or it
3636might be confused (it is a very good tool, but only a tool).
3637 3642
3638If you are unsure about something, feel free to contact the mailing list 3643These watchers are stored in lists, so they need to be walked to find the
3639with the full valgrind report and an explanation on why you think this is 3644correct watcher to remove. The lists are usually short (you don't usually
3640a bug in libev. However, don't be annoyed when you get a brisk "this is 3645have many watchers waiting for the same fd or signal: one is typical, two
3641no bug" answer and take the chance of learning how to interpret valgrind 3646is rare).
3642properly.
3643 3647
3644If you need, for some reason, empty reports from valgrind for your project 3648=item Finding the next timer in each loop iteration: O(1)
3645I suggest using suppression lists. 3649
3650By virtue of using a binary or 4-heap, the next timer is always found at a
3651fixed position in the storage array.
3652
3653=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3654
3655A change means an I/O watcher gets started or stopped, which requires
3656libev to recalculate its status (and possibly tell the kernel, depending
3657on backend and whether C<ev_io_set> was used).
3658
3659=item Activating one watcher (putting it into the pending state): O(1)
3660
3661=item Priority handling: O(number_of_priorities)
3662
3663Priorities are implemented by allocating some space for each
3664priority. When doing priority-based operations, libev usually has to
3665linearly search all the priorities, but starting/stopping and activating
3666watchers becomes O(1) with respect to priority handling.
3667
3668=item Sending an ev_async: O(1)
3669
3670=item Processing ev_async_send: O(number_of_async_watchers)
3671
3672=item Processing signals: O(max_signal_number)
3673
3674Sending involves a system call I<iff> there were no other C<ev_async_send>
3675calls in the current loop iteration. Checking for async and signal events
3676involves iterating over all running async watchers or all signal numbers.
3677
3678=back
3646 3679
3647 3680
3648=head1 AUTHOR 3681=head1 AUTHOR
3649 3682
3650Marc Lehmann <libev@schmorp.de>. 3683Marc Lehmann <libev@schmorp.de>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines