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

Comparing libev/ev.pod (file contents):
Revision 1.183 by root, Tue Sep 23 08:37:38 2008 UTC vs.
Revision 1.197 by root, Tue Oct 21 20:52:30 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
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
1625 1633
1626=back 1634=back
1627 1635
1628=head3 Examples 1636=head3 Examples
1629 1637
1630Example: Try to exit cleanly on SIGINT and SIGTERM. 1638Example: Try to exit cleanly on SIGINT.
1631 1639
1632 static void 1640 static void
1633 sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) 1641 sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
1634 { 1642 {
1635 ev_unloop (loop, EVUNLOOP_ALL); 1643 ev_unloop (loop, EVUNLOOP_ALL);
1636 } 1644 }
1637 1645
1638 struct ev_signal signal_watcher; 1646 struct ev_signal signal_watcher;
1639 ev_signal_init (&signal_watcher, sigint_cb, SIGINT); 1647 ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1640 ev_signal_start (loop, &sigint_cb); 1648 ev_signal_start (loop, &signal_watcher);
1641 1649
1642 1650
1643=head2 C<ev_child> - watch out for process status changes 1651=head2 C<ev_child> - watch out for process status changes
1644 1652
1645Child watchers trigger when your process receives a SIGCHLD in response to 1653Child watchers trigger when your process receives a SIGCHLD in response to
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,
2233when you fork, you not only have to call C<ev_loop_fork> on both loops, 2242when you fork, you not only have to call C<ev_loop_fork> on both loops,
2234but you will also have to stop and restart any C<ev_embed> watchers 2243but you will also have to stop and restart any C<ev_embed> watchers
2235yourself - but you can use a fork watcher to handle this automatically, 2244yourself - but you can use a fork watcher to handle this automatically,
2236and future versions of libev might do just that. 2245and future versions of libev might do just that.
2237 2246
2238Unfortunately, not all backends are embeddable, only the ones returned by 2247Unfortunately, not all backends are embeddable: only the ones returned by
2239C<ev_embeddable_backends> are, which, unfortunately, does not include any 2248C<ev_embeddable_backends> are, which, unfortunately, does not include any
2240portable one. 2249portable one.
2241 2250
2242So when you want to use this feature you will always have to be prepared 2251So 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 2252that 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 2253this is to have a separate variables for your embeddable loop, try to
2245create it, and if that fails, use the normal loop for everything. 2254create it, and if that fails, use the normal loop for everything.
2255
2256=head3 C<ev_embed> and fork
2257
2258While the C<ev_embed> watcher is running, forks in the embedding loop will
2259automatically be applied to the embedded loop as well, so no special
2260fork handling is required in that case. When the watcher is not running,
2261however, it is still the task of the libev user to call C<ev_loop_fork ()>
2262as applicable.
2246 2263
2247=head3 Watcher-Specific Functions and Data Members 2264=head3 Watcher-Specific Functions and Data Members
2248 2265
2249=over 4 2266=over 4
2250 2267
2368is that the author does not know of a simple (or any) algorithm for a 2385is that the author does not know of a simple (or any) algorithm for a
2369multiple-writer-single-reader queue that works in all cases and doesn't 2386multiple-writer-single-reader queue that works in all cases and doesn't
2370need elaborate support such as pthreads. 2387need elaborate support such as pthreads.
2371 2388
2372That means that if you want to queue data, you have to provide your own 2389That means that if you want to queue data, you have to provide your own
2373queue. But at least I can tell you would implement locking around your 2390queue. But at least I can tell you how to implement locking around your
2374queue: 2391queue:
2375 2392
2376=over 4 2393=over 4
2377 2394
2378=item queueing from a signal handler context 2395=item queueing from a signal handler context
2379 2396
2380To 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
2381handler 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
2382some fictitious SIGUSR1 handler: 2399an example that does that for some fictitious SIGUSR1 handler:
2383 2400
2384 static ev_async mysig; 2401 static ev_async mysig;
2385 2402
2386 static void 2403 static void
2387 sigusr1_handler (void) 2404 sigusr1_handler (void)
2454 2471
2455=item ev_async_init (ev_async *, callback) 2472=item ev_async_init (ev_async *, callback)
2456 2473
2457Initialises and configures the async watcher - it has no parameters of any 2474Initialises and configures the async watcher - it has no parameters of any
2458kind. There is a C<ev_asynd_set> macro, but using it is utterly pointless, 2475kind. There is a C<ev_asynd_set> macro, but using it is utterly pointless,
2459believe me. 2476trust me.
2460 2477
2461=item ev_async_send (loop, ev_async *) 2478=item ev_async_send (loop, ev_async *)
2462 2479
2463Sends/signals/activates the given C<ev_async> watcher, that is, feeds 2480Sends/signals/activates the given C<ev_async> watcher, that is, feeds
2464an C<EV_ASYNC> event on the watcher into the event loop. Unlike 2481an C<EV_ASYNC> event on the watcher into the event loop. Unlike
2465C<ev_feed_event>, this call is safe to do in other threads, signal or 2482C<ev_feed_event>, this call is safe to do from other threads, signal or
2466similar contexts (see the discussion of C<EV_ATOMIC_T> in the embedding 2483similar contexts (see the discussion of C<EV_ATOMIC_T> in the embedding
2467section below on what exactly this means). 2484section below on what exactly this means).
2468 2485
2469This call incurs the overhead of a system call only once per loop iteration, 2486This call incurs the overhead of a system call only once per loop iteration,
2470so while the overhead might be noticeable, it doesn't apply to repeated 2487so while the overhead might be noticeable, it doesn't apply to repeated
2494=over 4 2511=over 4
2495 2512
2496=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)
2497 2514
2498This 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
2499callback on whichever event happens first and automatically stop both 2516callback 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 2517watchers. 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 2518or timeout without having to allocate/configure/start/stop/free one or
2502more watchers yourself. 2519more watchers yourself.
2503 2520
2504If 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
2505is 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
2506C<events> set will be created and started. 2523the given C<fd> and C<events> set will be created and started.
2507 2524
2508If 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
2509started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and 2526started. 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 2527repeat = 0) will be started. C<0> is a valid timeout.
2511dubious value.
2512 2528
2513The 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
2514passed an C<revents> set like normal event callbacks (a combination of 2530passed 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> 2531C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg>
2516value 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.
2517 2537
2518 static void stdin_ready (int revents, void *arg) 2538 static void stdin_ready (int revents, void *arg)
2519 { 2539 {
2540 if (revents & EV_READ)
2541 /* stdin might have data for us, joy! */;
2520 if (revents & EV_TIMEOUT) 2542 else if (revents & EV_TIMEOUT)
2521 /* doh, nothing entered */; 2543 /* doh, nothing entered */;
2522 else if (revents & EV_READ)
2523 /* stdin might have data for us, joy! */;
2524 } 2544 }
2525 2545
2526 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 2546 ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0);
2527 2547
2528=item ev_feed_event (ev_loop *, watcher *, int revents) 2548=item ev_feed_event (ev_loop *, watcher *, int revents)
2676 2696
2677The prototype of the C<function> must be C<void (*)(ev::TYPE &w, int)>. 2697The prototype of the C<function> must be C<void (*)(ev::TYPE &w, int)>.
2678 2698
2679See the method-C<set> above for more details. 2699See the method-C<set> above for more details.
2680 2700
2681Example: 2701Example: Use a plain function as callback.
2682 2702
2683 static void io_cb (ev::io &w, int revents) { } 2703 static void io_cb (ev::io &w, int revents) { }
2684 iow.set <io_cb> (); 2704 iow.set <io_cb> ();
2685 2705
2686=item w->set (struct ev_loop *) 2706=item w->set (struct ev_loop *)
2724Example: Define a class with an IO and idle watcher, start one of them in 2744Example: Define a class with an IO and idle watcher, start one of them in
2725the constructor. 2745the constructor.
2726 2746
2727 class myclass 2747 class myclass
2728 { 2748 {
2729 ev::io io; void io_cb (ev::io &w, int revents); 2749 ev::io io ; void io_cb (ev::io &w, int revents);
2730 ev:idle idle void idle_cb (ev::idle &w, int revents); 2750 ev::idle idle; void idle_cb (ev::idle &w, int revents);
2731 2751
2732 myclass (int fd) 2752 myclass (int fd)
2733 { 2753 {
2734 io .set <myclass, &myclass::io_cb > (this); 2754 io .set <myclass, &myclass::io_cb > (this);
2735 idle.set <myclass, &myclass::idle_cb> (this); 2755 idle.set <myclass, &myclass::idle_cb> (this);
2751=item Perl 2771=item Perl
2752 2772
2753The EV module implements the full libev API and is actually used to test 2773The EV module implements the full libev API and is actually used to test
2754libev. EV is developed together with libev. Apart from the EV core module, 2774libev. EV is developed together with libev. Apart from the EV core module,
2755there are additional modules that implement libev-compatible interfaces 2775there are additional modules that implement libev-compatible interfaces
2756to C<libadns> (C<EV::ADNS>), C<Net::SNMP> (C<Net::SNMP::EV>) and the 2776to C<libadns> (C<EV::ADNS>, but C<AnyEvent::DNS> is preferred nowadays),
2757C<libglib> event core (C<Glib::EV> and C<EV::Glib>). 2777C<Net::SNMP> (C<Net::SNMP::EV>) and the C<libglib> event core (C<Glib::EV>
2778and C<EV::Glib>).
2758 2779
2759It can be found and installed via CPAN, its homepage is at 2780It can be found and installed via CPAN, its homepage is at
2760L<http://software.schmorp.de/pkg/EV>. 2781L<http://software.schmorp.de/pkg/EV>.
2761 2782
2762=item Python 2783=item Python
2941 2962
2942=head2 PREPROCESSOR SYMBOLS/MACROS 2963=head2 PREPROCESSOR SYMBOLS/MACROS
2943 2964
2944Libev can be configured via a variety of preprocessor symbols you have to 2965Libev can be configured via a variety of preprocessor symbols you have to
2945define before including any of its files. The default in the absence of 2966define before including any of its files. The default in the absence of
2946autoconf is noted for every option. 2967autoconf is documented for every option.
2947 2968
2948=over 4 2969=over 4
2949 2970
2950=item EV_STANDALONE 2971=item EV_STANDALONE
2951 2972
3121When doing priority-based operations, libev usually has to linearly search 3142When doing priority-based operations, libev usually has to linearly search
3122all the priorities, so having many of them (hundreds) uses a lot of space 3143all the priorities, so having many of them (hundreds) uses a lot of space
3123and time, so using the defaults of five priorities (-2 .. +2) is usually 3144and time, so using the defaults of five priorities (-2 .. +2) is usually
3124fine. 3145fine.
3125 3146
3126If your embedding application does not need any priorities, defining these both to 3147If your embedding application does not need any priorities, defining these
3127C<0> will save some memory and CPU. 3148both to C<0> will save some memory and CPU.
3128 3149
3129=item EV_PERIODIC_ENABLE 3150=item EV_PERIODIC_ENABLE
3130 3151
3131If undefined or defined to be C<1>, then periodic timers are supported. If 3152If undefined or defined to be C<1>, then periodic timers are supported. If
3132defined to be C<0>, then they are not. Disabling them saves a few kB of 3153defined to be C<0>, then they are not. Disabling them saves a few kB of
3139code. 3160code.
3140 3161
3141=item EV_EMBED_ENABLE 3162=item EV_EMBED_ENABLE
3142 3163
3143If undefined or defined to be C<1>, then embed watchers are supported. If 3164If undefined or defined to be C<1>, then embed watchers are supported. If
3144defined to be C<0>, then they are not. 3165defined to be C<0>, then they are not. Embed watchers rely on most other
3166watcher types, which therefore must not be disabled.
3145 3167
3146=item EV_STAT_ENABLE 3168=item EV_STAT_ENABLE
3147 3169
3148If undefined or defined to be C<1>, then stat watchers are supported. If 3170If undefined or defined to be C<1>, then stat watchers are supported. If
3149defined to be C<0>, then they are not. 3171defined to be C<0>, then they are not.
3181two). 3203two).
3182 3204
3183=item EV_USE_4HEAP 3205=item EV_USE_4HEAP
3184 3206
3185Heaps are not very cache-efficient. To improve the cache-efficiency of the 3207Heaps are not very cache-efficient. To improve the cache-efficiency of the
3186timer and periodics heap, libev uses a 4-heap when this symbol is defined 3208timer and periodics heaps, libev uses a 4-heap when this symbol is defined
3187to C<1>. The 4-heap uses more complicated (longer) code but has 3209to C<1>. The 4-heap uses more complicated (longer) code but has noticeably
3188noticeably faster performance with many (thousands) of watchers. 3210faster performance with many (thousands) of watchers.
3189 3211
3190The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0> 3212The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0>
3191(disabled). 3213(disabled).
3192 3214
3193=item EV_HEAP_CACHE_AT 3215=item EV_HEAP_CACHE_AT
3194 3216
3195Heaps are not very cache-efficient. To improve the cache-efficiency of the 3217Heaps are not very cache-efficient. To improve the cache-efficiency of the
3196timer and periodics heap, libev can cache the timestamp (I<at>) within 3218timer and periodics heaps, libev can cache the timestamp (I<at>) within
3197the heap structure (selected by defining C<EV_HEAP_CACHE_AT> to C<1>), 3219the heap structure (selected by defining C<EV_HEAP_CACHE_AT> to C<1>),
3198which uses 8-12 bytes more per watcher and a few hundred bytes more code, 3220which uses 8-12 bytes more per watcher and a few hundred bytes more code,
3199but avoids random read accesses on heap changes. This improves performance 3221but avoids random read accesses on heap changes. This improves performance
3200noticeably with with many (hundreds) of watchers. 3222noticeably with many (hundreds) of watchers.
3201 3223
3202The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0> 3224The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0>
3203(disabled). 3225(disabled).
3204 3226
3205=item EV_VERIFY 3227=item EV_VERIFY
3211called once per loop, which can slow down libev. If set to C<3>, then the 3233called once per loop, which can slow down libev. If set to C<3>, then the
3212verification code will be called very frequently, which will slow down 3234verification code will be called very frequently, which will slow down
3213libev considerably. 3235libev considerably.
3214 3236
3215The default is C<1>, unless C<EV_MINIMAL> is set, in which case it will be 3237The default is C<1>, unless C<EV_MINIMAL> is set, in which case it will be
3216C<0.> 3238C<0>.
3217 3239
3218=item EV_COMMON 3240=item EV_COMMON
3219 3241
3220By default, all watchers have a C<void *data> member. By redefining 3242By default, all watchers have a C<void *data> member. By redefining
3221this macro to a something else you can include more and other types of 3243this macro to a something else you can include more and other types of
3238and the way callbacks are invoked and set. Must expand to a struct member 3260and the way callbacks are invoked and set. Must expand to a struct member
3239definition and a statement, respectively. See the F<ev.h> header file for 3261definition and a statement, respectively. See the F<ev.h> header file for
3240their default definitions. One possible use for overriding these is to 3262their default definitions. One possible use for overriding these is to
3241avoid the C<struct ev_loop *> as first argument in all cases, or to use 3263avoid the C<struct ev_loop *> as first argument in all cases, or to use
3242method calls instead of plain function calls in C++. 3264method calls instead of plain function calls in C++.
3265
3266=back
3243 3267
3244=head2 EXPORTED API SYMBOLS 3268=head2 EXPORTED API SYMBOLS
3245 3269
3246If you need to re-export the API (e.g. via a DLL) and you need a list of 3270If you need to re-export the API (e.g. via a DLL) and you need a list of
3247exported symbols, you can use the provided F<Symbol.*> files which list 3271exported symbols, you can use the provided F<Symbol.*> files which list
3294And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: 3318And a F<ev_cpp.C> implementation file that contains libev proper and is compiled:
3295 3319
3296 #include "ev_cpp.h" 3320 #include "ev_cpp.h"
3297 #include "ev.c" 3321 #include "ev.c"
3298 3322
3323=head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES
3299 3324
3300=head1 THREADS AND COROUTINES 3325=head2 THREADS AND COROUTINES
3301 3326
3302=head2 THREADS 3327=head3 THREADS
3303 3328
3304Libev itself is thread-safe (unless the opposite is specifically 3329All libev functions are reentrant and thread-safe unless explicitly
3305documented for a function), but it uses no locking itself. This means that 3330documented otherwise, but libev implements no locking itself. This means
3306you can use as many loops as you want in parallel, as long as only one 3331that you can use as many loops as you want in parallel, as long as there
3307thread ever calls into one libev function with the same loop parameter: 3332are no concurrent calls into any libev function with the same loop
3333parameter (C<ev_default_*> calls have an implicit default loop parameter,
3308libev guarentees that different event loops share no data structures that 3334of course): libev guarantees that different event loops share no data
3309need locking. 3335structures that need any locking.
3310 3336
3311Or 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
3312concurrently from multiple threads, calls with the same loop parameter 3338concurrently from multiple threads, calls with the same loop parameter
3313must 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
3314only one thread ever is inside a call at any point in time, e.g. by using 3340only one thread ever is inside a call at any point in time, e.g. by using
3315a mutex per loop). 3341a mutex per loop).
3316 3342
3317Specifically to support threads (and signal handlers), libev implements 3343Specifically to support threads (and signal handlers), libev implements
3318so-called C<ev_async> watchers, which allow some limited form of 3344so-called C<ev_async> watchers, which allow some limited form of
3319concurrency on the same event loop. 3345concurrency on the same event loop, namely waking it up "from the
3346outside".
3320 3347
3321If you want to know which design (one loop, locking, or multiple loops 3348If you want to know which design (one loop, locking, or multiple loops
3322without or something else still) is best for your problem, then I cannot 3349without or something else still) is best for your problem, then I cannot
3323help you. I can give some generic advice however: 3350help you, but here is some generic advice:
3324 3351
3325=over 4 3352=over 4
3326 3353
3327=item * most applications have a main thread: use the default libev loop 3354=item * most applications have a main thread: use the default libev loop
3328in that thread, or create a separate thread running only the default loop. 3355in that thread, or create a separate thread running only the default loop.
3352default loop and triggering an C<ev_async> watcher from the default loop 3379default loop and triggering an C<ev_async> watcher from the default loop
3353watcher callback into the event loop interested in the signal. 3380watcher callback into the event loop interested in the signal.
3354 3381
3355=back 3382=back
3356 3383
3357=head2 COROUTINES 3384=head3 COROUTINES
3358 3385
3359Libev is much more accommodating to coroutines ("cooperative threads"): 3386Libev is very accommodating to coroutines ("cooperative threads"):
3360libev fully supports nesting calls to it's functions from different 3387libev fully supports nesting calls to its functions from different
3361coroutines (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
3362different coroutines and switch freely between both coroutines running the 3389different coroutines, and switch freely between both coroutines running the
3363loop, 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
3364you must not do this from C<ev_periodic> reschedule callbacks. 3391you must not do this from C<ev_periodic> reschedule callbacks.
3365 3392
3366Care 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
3367C<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.
3368 3396
3397=head2 COMPILER WARNINGS
3369 3398
3370=head1 COMPLEXITIES 3399Depending on your compiler and compiler settings, you might get no or a
3400lot of warnings when compiling libev code. Some people are apparently
3401scared by this.
3371 3402
3372In this section the complexities of (many of) the algorithms used inside 3403However, these are unavoidable for many reasons. For one, each compiler
3373libev will be explained. For complexity discussions about backends see the 3404has different warnings, and each user has different tastes regarding
3374documentation for C<ev_default_init>. 3405warning options. "Warn-free" code therefore cannot be a goal except when
3406targeting a specific compiler and compiler-version.
3375 3407
3376All of the following are about amortised time: If an array needs to be 3408Another reason is that some compiler warnings require elaborate
3377extended, libev needs to realloc and move the whole array, but this 3409workarounds, or other changes to the code that make it less clear and less
3378happens asymptotically never with higher number of elements, so O(1) might 3410maintainable.
3379mean it might do a lengthy realloc operation in rare cases, but on average
3380it is much faster and asymptotically approaches constant time.
3381 3411
3382=over 4 3412And of course, some compiler warnings are just plain stupid, or simply
3413wrong (because they don't actually warn about the condition their message
3414seems to warn about). For example, certain older gcc versions had some
3415warnings that resulted an extreme number of false positives. These have
3416been fixed, but some people still insist on making code warn-free with
3417such buggy versions.
3383 3418
3384=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) 3419While libev is written to generate as few warnings as possible,
3420"warn-free" code is not a goal, and it is recommended not to build libev
3421with any compiler warnings enabled unless you are prepared to cope with
3422them (e.g. by ignoring them). Remember that warnings are just that:
3423warnings, not errors, or proof of bugs.
3385 3424
3386This means that, when you have a watcher that triggers in one hour and
3387there are 100 watchers that would trigger before that then inserting will
3388have to skip roughly seven (C<ld 100>) of these watchers.
3389 3425
3390=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) 3426=head2 VALGRIND
3391 3427
3392That means that changing a timer costs less than removing/adding them 3428Valgrind has a special section here because it is a popular tool that is
3393as only the relative motion in the event queue has to be paid for. 3429highly useful. Unfortunately, valgrind reports are very hard to interpret.
3394 3430
3395=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) 3431If you think you found a bug (memory leak, uninitialised data access etc.)
3432in libev, then check twice: If valgrind reports something like:
3396 3433
3397These just add the watcher into an array or at the head of a list. 3434 ==2274== definitely lost: 0 bytes in 0 blocks.
3435 ==2274== possibly lost: 0 bytes in 0 blocks.
3436 ==2274== still reachable: 256 bytes in 1 blocks.
3398 3437
3399=item Stopping check/prepare/idle/fork/async watchers: O(1) 3438Then there is no memory leak, just as memory accounted to global variables
3439is not a memleak - the memory is still being refernced, and didn't leak.
3400 3440
3401=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) 3441Similarly, under some circumstances, valgrind might report kernel bugs
3442as if it were a bug in libev (e.g. in realloc or in the poll backend,
3443although an acceptable workaround has been found here), or it might be
3444confused.
3402 3445
3403These watchers are stored in lists then need to be walked to find the 3446Keep in mind that valgrind is a very good tool, but only a tool. Don't
3404correct watcher to remove. The lists are usually short (you don't usually 3447make it into some kind of religion.
3405have many watchers waiting for the same fd or signal).
3406 3448
3407=item Finding the next timer in each loop iteration: O(1) 3449If you are unsure about something, feel free to contact the mailing list
3450with the full valgrind report and an explanation on why you think this
3451is a bug in libev (best check the archives, too :). However, don't be
3452annoyed when you get a brisk "this is no bug" answer and take the chance
3453of learning how to interpret valgrind properly.
3408 3454
3409By virtue of using a binary or 4-heap, the next timer is always found at a 3455If you need, for some reason, empty reports from valgrind for your project
3410fixed position in the storage array. 3456I suggest using suppression lists.
3411 3457
3412=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3413 3458
3414A change means an I/O watcher gets started or stopped, which requires 3459=head1 PORTABILITY NOTES
3415libev to recalculate its status (and possibly tell the kernel, depending
3416on backend and whether C<ev_io_set> was used).
3417 3460
3418=item Activating one watcher (putting it into the pending state): O(1)
3419
3420=item Priority handling: O(number_of_priorities)
3421
3422Priorities are implemented by allocating some space for each
3423priority. When doing priority-based operations, libev usually has to
3424linearly search all the priorities, but starting/stopping and activating
3425watchers becomes O(1) w.r.t. priority handling.
3426
3427=item Sending an ev_async: O(1)
3428
3429=item Processing ev_async_send: O(number_of_async_watchers)
3430
3431=item Processing signals: O(max_signal_number)
3432
3433Sending involves a system call I<iff> there were no other C<ev_async_send>
3434calls in the current loop iteration. Checking for async and signal events
3435involves iterating over all running async watchers or all signal numbers.
3436
3437=back
3438
3439
3440=head1 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS 3461=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
3441 3462
3442Win32 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
3443requires, and its I/O model is fundamentally incompatible with the POSIX 3464requires, and its I/O model is fundamentally incompatible with the POSIX
3444model. Libev still offers limited functionality on this platform in 3465model. Libev still offers limited functionality on this platform in
3445the form of the C<EVBACKEND_SELECT> backend, and only supports socket 3466the form of the C<EVBACKEND_SELECT> backend, and only supports socket
3456 3477
3457Not a libev limitation but worth mentioning: windows apparently doesn't 3478Not a libev limitation but worth mentioning: windows apparently doesn't
3458accept large writes: instead of resulting in a partial write, windows will 3479accept large writes: instead of resulting in a partial write, windows will
3459either accept everything or return C<ENOBUFS> if the buffer is too large, 3480either accept everything or return C<ENOBUFS> if the buffer is too large,
3460so make sure you only write small amounts into your sockets (less than a 3481so make sure you only write small amounts into your sockets (less than a
3461megabyte seems safe, but thsi apparently depends on the amount of memory 3482megabyte seems safe, but this apparently depends on the amount of memory
3462available). 3483available).
3463 3484
3464Due to the many, low, and arbitrary limits on the win32 platform and 3485Due to the many, low, and arbitrary limits on the win32 platform and
3465the abysmal performance of winsockets, using a large number of sockets 3486the abysmal performance of winsockets, using a large number of sockets
3466is not recommended (and not reasonable). If your program needs to use 3487is not recommended (and not reasonable). If your program needs to use
3477 #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */ 3498 #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */
3478 3499
3479 #include "ev.h" 3500 #include "ev.h"
3480 3501
3481And compile the following F<evwrap.c> file into your project (make sure 3502And compile the following F<evwrap.c> file into your project (make sure
3482you do I<not> compile the F<ev.c> or any other embedded soruce files!): 3503you do I<not> compile the F<ev.c> or any other embedded source files!):
3483 3504
3484 #include "evwrap.h" 3505 #include "evwrap.h"
3485 #include "ev.c" 3506 #include "ev.c"
3486 3507
3487=over 4 3508=over 4
3532wrap all I/O functions and provide your own fd management, but the cost of 3553wrap all I/O functions and provide your own fd management, but the cost of
3533calling select (O(n²)) will likely make this unworkable. 3554calling select (O(n²)) will likely make this unworkable.
3534 3555
3535=back 3556=back
3536 3557
3537
3538=head1 PORTABILITY REQUIREMENTS 3558=head2 PORTABILITY REQUIREMENTS
3539 3559
3540In addition to a working ISO-C implementation, libev relies on a few 3560In addition to a working ISO-C implementation and of course the
3541additional extensions: 3561backend-specific APIs, libev relies on a few additional extensions:
3542 3562
3543=over 4 3563=over 4
3544 3564
3545=item C<void (*)(ev_watcher_type *, int revents)> must have compatible 3565=item C<void (*)(ev_watcher_type *, int revents)> must have compatible
3546calling conventions regardless of C<ev_watcher_type *>. 3566calling conventions regardless of C<ev_watcher_type *>.
3552calls them using an C<ev_watcher *> internally. 3572calls them using an C<ev_watcher *> internally.
3553 3573
3554=item C<sig_atomic_t volatile> must be thread-atomic as well 3574=item C<sig_atomic_t volatile> must be thread-atomic as well
3555 3575
3556The type C<sig_atomic_t volatile> (or whatever is defined as 3576The type C<sig_atomic_t volatile> (or whatever is defined as
3557C<EV_ATOMIC_T>) must be atomic w.r.t. accesses from different 3577C<EV_ATOMIC_T>) must be atomic with respect to accesses from different
3558threads. This is not part of the specification for C<sig_atomic_t>, but is 3578threads. This is not part of the specification for C<sig_atomic_t>, but is
3559believed to be sufficiently portable. 3579believed to be sufficiently portable.
3560 3580
3561=item C<sigprocmask> must work in a threaded environment 3581=item C<sigprocmask> must work in a threaded environment
3562 3582
3571except the initial one, and run the default loop in the initial thread as 3591except the initial one, and run the default loop in the initial thread as
3572well. 3592well.
3573 3593
3574=item C<long> must be large enough for common memory allocation sizes 3594=item C<long> must be large enough for common memory allocation sizes
3575 3595
3576To improve portability and simplify using libev, libev uses C<long> 3596To improve portability and simplify its API, libev uses C<long> internally
3577internally instead of C<size_t> when allocating its data structures. On 3597instead of C<size_t> when allocating its data structures. On non-POSIX
3578non-POSIX systems (Microsoft...) this might be unexpectedly low, but 3598systems (Microsoft...) this might be unexpectedly low, but is still at
3579is still at least 31 bits everywhere, which is enough for hundreds of 3599least 31 bits everywhere, which is enough for hundreds of millions of
3580millions of watchers. 3600watchers.
3581 3601
3582=item C<double> must hold a time value in seconds with enough accuracy 3602=item C<double> must hold a time value in seconds with enough accuracy
3583 3603
3584The type C<double> is used to represent timestamps. It is required to 3604The type C<double> is used to represent timestamps. It is required to
3585have at least 51 bits of mantissa (and 9 bits of exponent), which is good 3605have at least 51 bits of mantissa (and 9 bits of exponent), which is good
3589=back 3609=back
3590 3610
3591If you know of other additional requirements drop me a note. 3611If you know of other additional requirements drop me a note.
3592 3612
3593 3613
3594=head1 COMPILER WARNINGS 3614=head1 ALGORITHMIC COMPLEXITIES
3595 3615
3596Depending on your compiler and compiler settings, you might get no or a 3616In this section the complexities of (many of) the algorithms used inside
3597lot of warnings when compiling libev code. Some people are apparently 3617libev will be documented. For complexity discussions about backends see
3598scared by this. 3618the documentation for C<ev_default_init>.
3599 3619
3600However, these are unavoidable for many reasons. For one, each compiler 3620All of the following are about amortised time: If an array needs to be
3601has different warnings, and each user has different tastes regarding 3621extended, libev needs to realloc and move the whole array, but this
3602warning options. "Warn-free" code therefore cannot be a goal except when 3622happens asymptotically rarer with higher number of elements, so O(1) might
3603targeting a specific compiler and compiler-version. 3623mean that libev does a lengthy realloc operation in rare cases, but on
3624average it is much faster and asymptotically approaches constant time.
3604 3625
3605Another reason is that some compiler warnings require elaborate 3626=over 4
3606workarounds, or other changes to the code that make it less clear and less
3607maintainable.
3608 3627
3609And of course, some compiler warnings are just plain stupid, or simply 3628=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers)
3610wrong (because they don't actually warn about the condition their message
3611seems to warn about).
3612 3629
3613While libev is written to generate as few warnings as possible, 3630This means that, when you have a watcher that triggers in one hour and
3614"warn-free" code is not a goal, and it is recommended not to build libev 3631there are 100 watchers that would trigger before that, then inserting will
3615with any compiler warnings enabled unless you are prepared to cope with 3632have to skip roughly seven (C<ld 100>) of these watchers.
3616them (e.g. by ignoring them). Remember that warnings are just that:
3617warnings, not errors, or proof of bugs.
3618 3633
3634=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)
3619 3635
3620=head1 VALGRIND 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.
3621 3638
3622Valgrind has a special section here because it is a popular tool that is 3639=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)
3623highly useful, but valgrind reports are very hard to interpret.
3624 3640
3625If you think you found a bug (memory leak, uninitialised data access etc.) 3641These just add the watcher into an array or at the head of a list.
3626in libev, then check twice: If valgrind reports something like:
3627 3642
3628 ==2274== definitely lost: 0 bytes in 0 blocks. 3643=item Stopping check/prepare/idle/fork/async watchers: O(1)
3629 ==2274== possibly lost: 0 bytes in 0 blocks.
3630 ==2274== still reachable: 256 bytes in 1 blocks.
3631 3644
3632Then there is no memory leak. Similarly, under some circumstances, 3645=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))
3633valgrind might report kernel bugs as if it were a bug in libev, or it
3634might be confused (it is a very good tool, but only a tool).
3635 3646
3636If you are unsure about something, feel free to contact the mailing list 3647These watchers are stored in lists, so they need to be walked to find the
3637with the full valgrind report and an explanation on why you think this is 3648correct watcher to remove. The lists are usually short (you don't usually
3638a bug in libev. However, don't be annoyed when you get a brisk "this is 3649have many watchers waiting for the same fd or signal: one is typical, two
3639no bug" answer and take the chance of learning how to interpret valgrind 3650is rare).
3640properly.
3641 3651
3642If you need, for some reason, empty reports from valgrind for your project 3652=item Finding the next timer in each loop iteration: O(1)
3643I suggest using suppression lists. 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
3644 3683
3645 3684
3646=head1 AUTHOR 3685=head1 AUTHOR
3647 3686
3648Marc Lehmann <libev@schmorp.de>. 3687Marc Lehmann <libev@schmorp.de>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines