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

Comparing libev/ev.3 (file contents):
Revision 1.62 by root, Sun Mar 16 16:38:23 2008 UTC vs.
Revision 1.64 by root, Wed Apr 16 17:08:29 2008 UTC

130.\} 130.\}
131.rm #[ #] #H #V #F C 131.rm #[ #] #H #V #F C
132.\" ======================================================================== 132.\" ========================================================================
133.\" 133.\"
134.IX Title "EV 1" 134.IX Title "EV 1"
135.TH EV 1 "2008-03-13" "perl v5.10.0" "User Contributed Perl Documentation" 135.TH EV 1 "2008-04-11" "perl v5.10.0" "User Contributed Perl Documentation"
136.\" For nroff, turn off justification. Always turn off hyphenation; it makes 136.\" For nroff, turn off justification. Always turn off hyphenation; it makes
137.\" way too many mistakes in technical documents. 137.\" way too many mistakes in technical documents.
138.if n .ad l 138.if n .ad l
139.nh 139.nh
140.SH "NAME" 140.SH "NAME"
328.Sp 328.Sp
329See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. 329See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
330.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 330.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4
331.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" 331.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))"
332Sets the allocation function to use (the prototype is similar \- the 332Sets the allocation function to use (the prototype is similar \- the
333semantics is identical \- to the realloc C function). It is used to 333semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is
334allocate and free memory (no surprises here). If it returns zero when 334used to allocate and free memory (no surprises here). If it returns zero
335memory needs to be allocated, the library might abort or take some 335when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort
336potentially destructive action. The default is your system realloc 336or take some potentially destructive action.
337function. 337.Sp
338Since some systems (at least OpenBSD and Darwin) fail to implement
339correct \f(CW\*(C`realloc\*(C'\fR semantics, libev will use a wrapper around the system
340\&\f(CW\*(C`realloc\*(C'\fR and \f(CW\*(C`free\*(C'\fR functions by default.
338.Sp 341.Sp
339You could override this function in high-availability programs to, say, 342You could override this function in high-availability programs to, say,
340free some memory if it cannot allocate memory, to use a special allocator, 343free some memory if it cannot allocate memory, to use a special allocator,
341or even to sleep a while and retry until some memory is available. 344or even to sleep a while and retry until some memory is available.
342.Sp 345.Sp
343Example: Replace the libev allocator with one that waits a bit and then 346Example: Replace the libev allocator with one that waits a bit and then
344retries). 347retries (example requires a standards-compliant \f(CW\*(C`realloc\*(C'\fR).
345.Sp 348.Sp
346.Vb 6 349.Vb 6
347\& static void * 350\& static void *
348\& persistent_realloc (void *ptr, size_t size) 351\& persistent_realloc (void *ptr, size_t size)
349\& { 352\& {
387.SH "FUNCTIONS CONTROLLING THE EVENT LOOP" 390.SH "FUNCTIONS CONTROLLING THE EVENT LOOP"
388.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" 391.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP"
389An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two 392An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two
390types of such loops, the \fIdefault\fR loop, which supports signals and child 393types of such loops, the \fIdefault\fR loop, which supports signals and child
391events, and dynamically created loops which do not. 394events, and dynamically created loops which do not.
392.PP
393If you use threads, a common model is to run the default event loop
394in your main thread (or in a separate thread) and for each thread you
395create, you also create another event loop. Libev itself does no locking
396whatsoever, so if you mix calls to the same event loop in different
397threads, make sure you lock (this is usually a bad idea, though, even if
398done correctly, because it's hideous and inefficient).
399.IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4 395.IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4
400.IX Item "struct ev_loop *ev_default_loop (unsigned int flags)" 396.IX Item "struct ev_loop *ev_default_loop (unsigned int flags)"
401This will initialise the default event loop if it hasn't been initialised 397This will initialise the default event loop if it hasn't been initialised
402yet and return it. If the default loop could not be initialised, returns 398yet and return it. If the default loop could not be initialised, returns
403false. If it already was initialised it simply returns it (and ignores the 399false. If it already was initialised it simply returns it (and ignores the
404flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards). 400flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards).
405.Sp 401.Sp
406If you don't know what event loop to use, use the one returned from this 402If you don't know what event loop to use, use the one returned from this
407function. 403function.
404.Sp
405Note that this function is \fInot\fR thread-safe, so if you want to use it
406from multiple threads, you have to lock (note also that this is unlikely,
407as loops cannot bes hared easily between threads anyway).
408.Sp 408.Sp
409The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and 409The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and
410\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler 410\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler
411for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either 411for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either
412create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you 412create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you
482For few fds, this backend is a bit little slower than poll and select, 482For few fds, this backend is a bit little slower than poll and select,
483but it scales phenomenally better. While poll and select usually scale 483but it scales phenomenally better. While poll and select usually scale
484like O(total_fds) where n is the total number of fds (or the highest fd), 484like O(total_fds) where n is the total number of fds (or the highest fd),
485epoll scales either O(1) or O(active_fds). The epoll design has a number 485epoll scales either O(1) or O(active_fds). The epoll design has a number
486of shortcomings, such as silently dropping events in some hard-to-detect 486of shortcomings, such as silently dropping events in some hard-to-detect
487cases and rewiring a syscall per fd change, no fork support and bad 487cases and requiring a syscall per fd change, no fork support and bad
488support for dup. 488support for dup.
489.Sp 489.Sp
490While stopping, setting and starting an I/O watcher in the same iteration 490While stopping, setting and starting an I/O watcher in the same iteration
491will result in some caching, there is still a syscall per such incident 491will result in some caching, there is still a syscall per such incident
492(because the fd could point to a different file description now), so its 492(because the fd could point to a different file description now), so its
599.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" 599.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)"
600Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is 600Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is
601always distinct from the default loop. Unlike the default loop, it cannot 601always distinct from the default loop. Unlike the default loop, it cannot
602handle signal and child watchers, and attempts to do so will be greeted by 602handle signal and child watchers, and attempts to do so will be greeted by
603undefined behaviour (or a failed assertion if assertions are enabled). 603undefined behaviour (or a failed assertion if assertions are enabled).
604.Sp
605Note that this function \fIis\fR thread-safe, and the recommended way to use
606libev with threads is indeed to create one loop per thread, and using the
607default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread.
604.Sp 608.Sp
605Example: Try to create a event loop that uses epoll and nothing else. 609Example: Try to create a event loop that uses epoll and nothing else.
606.Sp 610.Sp
607.Vb 3 611.Vb 3
608\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); 612\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
1219To support fork in your programs, you either have to call 1223To support fork in your programs, you either have to call
1220\&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child, 1224\&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child,
1221enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or 1225enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or
1222\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. 1226\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
1223.PP 1227.PP
1228\fIThe special problem of \s-1SIGPIPE\s0\fR
1229.IX Subsection "The special problem of SIGPIPE"
1230.PP
1231While not really specific to libev, it is easy to forget about \s-1SIGPIPE:\s0
1232when reading from a pipe whose other end has been closed, your program
1233gets send a \s-1SIGPIPE\s0, which, by default, aborts your program. For most
1234programs this is sensible behaviour, for daemons, this is usually
1235undesirable.
1236.PP
1237So when you encounter spurious, unexplained daemon exits, make sure you
1238ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon
1239somewhere, as that would have given you a big clue).
1240.PP
1224\fIWatcher-Specific Functions\fR 1241\fIWatcher-Specific Functions\fR
1225.IX Subsection "Watcher-Specific Functions" 1242.IX Subsection "Watcher-Specific Functions"
1226.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4 1243.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4
1227.IX Item "ev_io_init (ev_io *, callback, int fd, int events)" 1244.IX Item "ev_io_init (ev_io *, callback, int fd, int events)"
1228.PD 0 1245.PD 0
1736semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometimes needs 1753semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometimes needs
1737to fall back to regular polling again even with inotify, but changes are 1754to fall back to regular polling again even with inotify, but changes are
1738usually detected immediately, and if the file exists there will be no 1755usually detected immediately, and if the file exists there will be no
1739polling. 1756polling.
1740.PP 1757.PP
1758\fI\s-1ABI\s0 Issues (Largefile Support)\fR
1759.IX Subsection "ABI Issues (Largefile Support)"
1760.PP
1761Libev by default (unless the user overrides this) uses the default
1762compilation environment, which means that on systems with optionally
1763disabled large file support, you get the 32 bit version of the stat
1764structure. When using the library from programs that change the \s-1ABI\s0 to
1765use 64 bit file offsets the programs will fail. In that case you have to
1766compile libev with the same flags to get binary compatibility. This is
1767obviously the case with any flags that change the \s-1ABI\s0, but the problem is
1768most noticably with ev_stat and largefile support.
1769.PP
1741\fIInotify\fR 1770\fIInotify\fR
1742.IX Subsection "Inotify" 1771.IX Subsection "Inotify"
1743.PP 1772.PP
1744When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only 1773When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only
1745available on Linux) and present at runtime, it will be used to speed up 1774available on Linux) and present at runtime, it will be used to speed up
2383section below on what exactly this means). 2412section below on what exactly this means).
2384.Sp 2413.Sp
2385This call incurs the overhead of a syscall only once per loop iteration, 2414This call incurs the overhead of a syscall only once per loop iteration,
2386so while the overhead might be noticable, it doesn't apply to repeated 2415so while the overhead might be noticable, it doesn't apply to repeated
2387calls to \f(CW\*(C`ev_async_send\*(C'\fR. 2416calls to \f(CW\*(C`ev_async_send\*(C'\fR.
2417.IP "bool = ev_async_pending (ev_async *)" 4
2418.IX Item "bool = ev_async_pending (ev_async *)"
2419Returns a non-zero value when \f(CW\*(C`ev_async_send\*(C'\fR has been called on the
2420watcher but the event has not yet been processed (or even noted) by the
2421event loop.
2422.Sp
2423\&\f(CW\*(C`ev_async_send\*(C'\fR sets a flag in the watcher and wakes up the loop. When
2424the loop iterates next and checks for the watcher to have become active,
2425it will reset the flag again. \f(CW\*(C`ev_async_pending\*(C'\fR can be used to very
2426quickly check wether invoking the loop might be a good idea.
2427.Sp
2428Not that this does \fInot\fR check wether the watcher itself is pending, only
2429wether it has been requested to make this watcher pending.
2388.SH "OTHER FUNCTIONS" 2430.SH "OTHER FUNCTIONS"
2389.IX Header "OTHER FUNCTIONS" 2431.IX Header "OTHER FUNCTIONS"
2390There are some other functions of possible interest. Described. Here. Now. 2432There are some other functions of possible interest. Described. Here. Now.
2391.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 2433.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4
2392.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 2434.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"
2449it a private \s-1API\s0). 2491it a private \s-1API\s0).
2450.IP "\(bu" 4 2492.IP "\(bu" 4
2451Priorities are not currently supported. Initialising priorities 2493Priorities are not currently supported. Initialising priorities
2452will fail and all watchers will have the same priority, even though there 2494will fail and all watchers will have the same priority, even though there
2453is an ev_pri field. 2495is an ev_pri field.
2496.IP "\(bu" 4
2497In libevent, the last base created gets the signals, in libev, the
2498first base created (== the default loop) gets the signals.
2454.IP "\(bu" 4 2499.IP "\(bu" 4
2455Other members are not supported. 2500Other members are not supported.
2456.IP "\(bu" 4 2501.IP "\(bu" 4
2457The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need 2502The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need
2458to use the libev header file and library. 2503to use the libev header file and library.
2687.ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4 2732.ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4
2688.el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4 2733.el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4
2689.IX Item "EV_DEFAULT, EV_DEFAULT_" 2734.IX Item "EV_DEFAULT, EV_DEFAULT_"
2690Similar to the other two macros, this gives you the value of the default 2735Similar to the other two macros, this gives you the value of the default
2691loop, if multiple loops are supported (\*(L"ev loop default\*(R"). 2736loop, if multiple loops are supported (\*(L"ev loop default\*(R").
2737.ie n .IP """EV_DEFAULT_UC""\fR, \f(CW""EV_DEFAULT_UC_""" 4
2738.el .IP "\f(CWEV_DEFAULT_UC\fR, \f(CWEV_DEFAULT_UC_\fR" 4
2739.IX Item "EV_DEFAULT_UC, EV_DEFAULT_UC_"
2740Usage identical to \f(CW\*(C`EV_DEFAULT\*(C'\fR and \f(CW\*(C`EV_DEFAULT_\*(C'\fR, but requires that the
2741default loop has been initialised (\f(CW\*(C`UC\*(C'\fR == unchecked). Their behaviour
2742is undefined when the default loop has not been initialised by a previous
2743execution of \f(CW\*(C`EV_DEFAULT\*(C'\fR, \f(CW\*(C`EV_DEFAULT_\*(C'\fR or \f(CW\*(C`ev_default_init (...)\*(C'\fR.
2744.Sp
2745It is often prudent to use \f(CW\*(C`EV_DEFAULT\*(C'\fR when initialising the first
2746watcher in a function but use \f(CW\*(C`EV_DEFAULT_UC\*(C'\fR afterwards.
2692.PP 2747.PP
2693Example: Declare and initialise a check watcher, utilising the above 2748Example: Declare and initialise a check watcher, utilising the above
2694macros so it will work regardless of whether multiple loops are supported 2749macros so it will work regardless of whether multiple loops are supported
2695or not. 2750or not.
2696.PP 2751.PP
2806.Vb 1 2861.Vb 1
2807\& libev.m4 2862\& libev.m4
2808.Ve 2863.Ve
2809.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" 2864.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0"
2810.IX Subsection "PREPROCESSOR SYMBOLS/MACROS" 2865.IX Subsection "PREPROCESSOR SYMBOLS/MACROS"
2811Libev can be configured via a variety of preprocessor symbols you have to define 2866Libev can be configured via a variety of preprocessor symbols you have to
2812before including any of its files. The default is not to build for multiplicity 2867define before including any of its files. The default in the absense of
2813and only include the select backend. 2868autoconf is noted for every option.
2814.IP "\s-1EV_STANDALONE\s0" 4 2869.IP "\s-1EV_STANDALONE\s0" 4
2815.IX Item "EV_STANDALONE" 2870.IX Item "EV_STANDALONE"
2816Must always be \f(CW1\fR if you do not use autoconf configuration, which 2871Must always be \f(CW1\fR if you do not use autoconf configuration, which
2817keeps libev from including \fIconfig.h\fR, and it also defines dummy 2872keeps libev from including \fIconfig.h\fR, and it also defines dummy
2818implementations for some libevent functions (such as logging, which is not 2873implementations for some libevent functions (such as logging, which is not
2837note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. 2892note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though.
2838.IP "\s-1EV_USE_NANOSLEEP\s0" 4 2893.IP "\s-1EV_USE_NANOSLEEP\s0" 4
2839.IX Item "EV_USE_NANOSLEEP" 2894.IX Item "EV_USE_NANOSLEEP"
2840If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available 2895If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available
2841and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. 2896and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR.
2897.IP "\s-1EV_USE_EVENTFD\s0" 4
2898.IX Item "EV_USE_EVENTFD"
2899If defined to be \f(CW1\fR, then libev will assume that \f(CW\*(C`eventfd ()\*(C'\fR is
2900available and will probe for kernel support at runtime. This will improve
2901\&\f(CW\*(C`ev_signal\*(C'\fR and \f(CW\*(C`ev_async\*(C'\fR performance and reduce resource consumption.
2902If undefined, it will be enabled if the headers indicate GNU/Linux + Glibc
29032.7 or newer, otherwise disabled.
2842.IP "\s-1EV_USE_SELECT\s0" 4 2904.IP "\s-1EV_USE_SELECT\s0" 4
2843.IX Item "EV_USE_SELECT" 2905.IX Item "EV_USE_SELECT"
2844If undefined or defined to be \f(CW1\fR, libev will compile in support for the 2906If undefined or defined to be \f(CW1\fR, libev will compile in support for the
2845\&\f(CW\*(C`select\*(C'\fR(2) backend. No attempt at autodetection will be done: if no 2907\&\f(CW\*(C`select\*(C'\fR(2) backend. No attempt at autodetection will be done: if no
2846other method takes over, select will be it. Otherwise the select backend 2908other method takes over, select will be it. Otherwise the select backend
2877takes precedence over select. 2939takes precedence over select.
2878.IP "\s-1EV_USE_EPOLL\s0" 4 2940.IP "\s-1EV_USE_EPOLL\s0" 4
2879.IX Item "EV_USE_EPOLL" 2941.IX Item "EV_USE_EPOLL"
2880If defined to be \f(CW1\fR, libev will compile in support for the Linux 2942If defined to be \f(CW1\fR, libev will compile in support for the Linux
2881\&\f(CW\*(C`epoll\*(C'\fR(7) backend. Its availability will be detected at runtime, 2943\&\f(CW\*(C`epoll\*(C'\fR(7) backend. Its availability will be detected at runtime,
2882otherwise another method will be used as fallback. This is the 2944otherwise another method will be used as fallback. This is the preferred
2883preferred backend for GNU/Linux systems. 2945backend for GNU/Linux systems. If undefined, it will be enabled if the
2946headers indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled.
2884.IP "\s-1EV_USE_KQUEUE\s0" 4 2947.IP "\s-1EV_USE_KQUEUE\s0" 4
2885.IX Item "EV_USE_KQUEUE" 2948.IX Item "EV_USE_KQUEUE"
2886If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style 2949If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style
2887\&\f(CW\*(C`kqueue\*(C'\fR(2) backend. Its actual availability will be detected at runtime, 2950\&\f(CW\*(C`kqueue\*(C'\fR(2) backend. Its actual availability will be detected at runtime,
2888otherwise another method will be used as fallback. This is the preferred 2951otherwise another method will be used as fallback. This is the preferred
2903reserved for future expansion, works like the \s-1USE\s0 symbols above. 2966reserved for future expansion, works like the \s-1USE\s0 symbols above.
2904.IP "\s-1EV_USE_INOTIFY\s0" 4 2967.IP "\s-1EV_USE_INOTIFY\s0" 4
2905.IX Item "EV_USE_INOTIFY" 2968.IX Item "EV_USE_INOTIFY"
2906If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify 2969If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify
2907interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will 2970interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will
2908be detected at runtime. 2971be detected at runtime. If undefined, it will be enabled if the headers
2972indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled.
2909.IP "\s-1EV_ATOMIC_T\s0" 4 2973.IP "\s-1EV_ATOMIC_T\s0" 4
2910.IX Item "EV_ATOMIC_T" 2974.IX Item "EV_ATOMIC_T"
2911Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose 2975Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose
2912access is atomic with respect to other threads or signal contexts. No such 2976access is atomic with respect to other threads or signal contexts. No such
2913type is easily found in the C language, so you can provide your own type 2977type is easily found in the C language, so you can provide your own type
3094.PP 3158.PP
3095.Vb 2 3159.Vb 2
3096\& #include "ev_cpp.h" 3160\& #include "ev_cpp.h"
3097\& #include "ev.c" 3161\& #include "ev.c"
3098.Ve 3162.Ve
3163.SH "THREADS AND COROUTINES"
3164.IX Header "THREADS AND COROUTINES"
3165.Sh "\s-1THREADS\s0"
3166.IX Subsection "THREADS"
3167Libev itself is completely threadsafe, but it uses no locking. This
3168means that you can use as many loops as you want in parallel, as long as
3169only one thread ever calls into one libev function with the same loop
3170parameter.
3171.PP
3172Or put differently: calls with different loop parameters can be done in
3173parallel from multiple threads, calls with the same loop parameter must be
3174done serially (but can be done from different threads, as long as only one
3175thread ever is inside a call at any point in time, e.g. by using a mutex
3176per loop).
3177.PP
3178If you want to know which design is best for your problem, then I cannot
3179help you but by giving some generic advice:
3180.IP "\(bu" 4
3181most applications have a main thread: use the default libev loop
3182in that thread, or create a seperate thread running only the default loop.
3183.Sp
3184This helps integrating other libraries or software modules that use libev
3185themselves and don't care/know about threading.
3186.IP "\(bu" 4
3187one loop per thread is usually a good model.
3188.Sp
3189Doing this is almost never wrong, sometimes a better-performance model
3190exists, but it is always a good start.
3191.IP "\(bu" 4
3192other models exist, such as the leader/follower pattern, where one
3193loop is handed through multiple threads in a kind of round-robbin fashion.
3194.Sp
3195Chosing a model is hard \- look around, learn, know that usually you cna do
3196better than you currently do :\-)
3197.IP "\(bu" 4
3198often you need to talk to some other thread which blocks in the
3199event loop \- \f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other
3200threads safely (or from signal contexts...).
3201.Sh "\s-1COROUTINES\s0"
3202.IX Subsection "COROUTINES"
3203Libev is much more accomodating to coroutines (\*(L"cooperative threads\*(R"):
3204libev fully supports nesting calls to it's functions from different
3205coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two
3206different coroutines and switch freely between both coroutines running the
3207loop, as long as you don't confuse yourself). The only exception is that
3208you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks.
3209.PP
3210Care has been invested into making sure that libev does not keep local
3211state inside \f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine
3212switches.
3099.SH "COMPLEXITIES" 3213.SH "COMPLEXITIES"
3100.IX Header "COMPLEXITIES" 3214.IX Header "COMPLEXITIES"
3101In this section the complexities of (many of) the algorithms used inside 3215In this section the complexities of (many of) the algorithms used inside
3102libev will be explained. For complexity discussions about backends see the 3216libev will be explained. For complexity discussions about backends see the
3103documentation for \f(CW\*(C`ev_default_init\*(C'\fR. 3217documentation for \f(CW\*(C`ev_default_init\*(C'\fR.
3223.IX Header "AUTHOR" 3337.IX Header "AUTHOR"
3224Marc Lehmann <libev@schmorp.de>. 3338Marc Lehmann <libev@schmorp.de>.
3225.SH "POD ERRORS" 3339.SH "POD ERRORS"
3226.IX Header "POD ERRORS" 3340.IX Header "POD ERRORS"
3227Hey! \fBThe above document had some coding errors, which are explained below:\fR 3341Hey! \fBThe above document had some coding errors, which are explained below:\fR
3228.IP "Around line 2951:" 4 3342.IP "Around line 3015:" 4
3229.IX Item "Around line 2951:" 3343.IX Item "Around line 3015:"
3230You forgot a '=back' before '=head2' 3344You forgot a '=back' before '=head2'

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines