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

Comparing libev/ev.3 (file contents):
Revision 1.63 by root, Wed Apr 2 15:23:11 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-04-02" "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
486For 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,
487but it scales phenomenally better. While poll and select usually scale 483but it scales phenomenally better. While poll and select usually scale
488like 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),
489epoll 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
490of shortcomings, such as silently dropping events in some hard-to-detect 486of shortcomings, such as silently dropping events in some hard-to-detect
491cases 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
492support for dup. 488support for dup.
493.Sp 489.Sp
494While 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
495will 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
496(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
2496.IP "\(bu" 4 2492.IP "\(bu" 4
2497Priorities are not currently supported. Initialising priorities 2493Priorities are not currently supported. Initialising priorities
2498will 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
2499is an ev_pri field. 2495is an ev_pri field.
2500.IP "\(bu" 4 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.
2499.IP "\(bu" 4
2501Other members are not supported. 2500Other members are not supported.
2502.IP "\(bu" 4 2501.IP "\(bu" 4
2503The 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
2504to use the libev header file and library. 2503to use the libev header file and library.
2505.SH "\*(C+ SUPPORT" 2504.SH "\*(C+ SUPPORT"
2733.ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4 2732.ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4
2734.el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4 2733.el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4
2735.IX Item "EV_DEFAULT, EV_DEFAULT_" 2734.IX Item "EV_DEFAULT, EV_DEFAULT_"
2736Similar 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
2737loop, 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.
2738.PP 2747.PP
2739Example: Declare and initialise a check watcher, utilising the above 2748Example: Declare and initialise a check watcher, utilising the above
2740macros so it will work regardless of whether multiple loops are supported 2749macros so it will work regardless of whether multiple loops are supported
2741or not. 2750or not.
2742.PP 2751.PP
2852.Vb 1 2861.Vb 1
2853\& libev.m4 2862\& libev.m4
2854.Ve 2863.Ve
2855.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" 2864.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0"
2856.IX Subsection "PREPROCESSOR SYMBOLS/MACROS" 2865.IX Subsection "PREPROCESSOR SYMBOLS/MACROS"
2857Libev 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
2858before 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
2859and only include the select backend. 2868autoconf is noted for every option.
2860.IP "\s-1EV_STANDALONE\s0" 4 2869.IP "\s-1EV_STANDALONE\s0" 4
2861.IX Item "EV_STANDALONE" 2870.IX Item "EV_STANDALONE"
2862Must 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
2863keeps libev from including \fIconfig.h\fR, and it also defines dummy 2872keeps libev from including \fIconfig.h\fR, and it also defines dummy
2864implementations for some libevent functions (such as logging, which is not 2873implementations for some libevent functions (such as logging, which is not
2883note 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.
2884.IP "\s-1EV_USE_NANOSLEEP\s0" 4 2893.IP "\s-1EV_USE_NANOSLEEP\s0" 4
2885.IX Item "EV_USE_NANOSLEEP" 2894.IX Item "EV_USE_NANOSLEEP"
2886If 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
2887and 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.
2888.IP "\s-1EV_USE_SELECT\s0" 4 2904.IP "\s-1EV_USE_SELECT\s0" 4
2889.IX Item "EV_USE_SELECT" 2905.IX Item "EV_USE_SELECT"
2890If 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
2891\&\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
2892other method takes over, select will be it. Otherwise the select backend 2908other method takes over, select will be it. Otherwise the select backend
2923takes precedence over select. 2939takes precedence over select.
2924.IP "\s-1EV_USE_EPOLL\s0" 4 2940.IP "\s-1EV_USE_EPOLL\s0" 4
2925.IX Item "EV_USE_EPOLL" 2941.IX Item "EV_USE_EPOLL"
2926If 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
2927\&\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,
2928otherwise another method will be used as fallback. This is the 2944otherwise another method will be used as fallback. This is the preferred
2929preferred 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.
2930.IP "\s-1EV_USE_KQUEUE\s0" 4 2947.IP "\s-1EV_USE_KQUEUE\s0" 4
2931.IX Item "EV_USE_KQUEUE" 2948.IX Item "EV_USE_KQUEUE"
2932If 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
2933\&\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,
2934otherwise another method will be used as fallback. This is the preferred 2951otherwise another method will be used as fallback. This is the preferred
2949reserved for future expansion, works like the \s-1USE\s0 symbols above. 2966reserved for future expansion, works like the \s-1USE\s0 symbols above.
2950.IP "\s-1EV_USE_INOTIFY\s0" 4 2967.IP "\s-1EV_USE_INOTIFY\s0" 4
2951.IX Item "EV_USE_INOTIFY" 2968.IX Item "EV_USE_INOTIFY"
2952If 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
2953interface 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
2954be 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.
2955.IP "\s-1EV_ATOMIC_T\s0" 4 2973.IP "\s-1EV_ATOMIC_T\s0" 4
2956.IX Item "EV_ATOMIC_T" 2974.IX Item "EV_ATOMIC_T"
2957Libev 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
2958access 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
2959type 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
3140.PP 3158.PP
3141.Vb 2 3159.Vb 2
3142\& #include "ev_cpp.h" 3160\& #include "ev_cpp.h"
3143\& #include "ev.c" 3161\& #include "ev.c"
3144.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.
3145.SH "COMPLEXITIES" 3213.SH "COMPLEXITIES"
3146.IX Header "COMPLEXITIES" 3214.IX Header "COMPLEXITIES"
3147In this section the complexities of (many of) the algorithms used inside 3215In this section the complexities of (many of) the algorithms used inside
3148libev will be explained. For complexity discussions about backends see the 3216libev will be explained. For complexity discussions about backends see the
3149documentation for \f(CW\*(C`ev_default_init\*(C'\fR. 3217documentation for \f(CW\*(C`ev_default_init\*(C'\fR.
3269.IX Header "AUTHOR" 3337.IX Header "AUTHOR"
3270Marc Lehmann <libev@schmorp.de>. 3338Marc Lehmann <libev@schmorp.de>.
3271.SH "POD ERRORS" 3339.SH "POD ERRORS"
3272.IX Header "POD ERRORS" 3340.IX Header "POD ERRORS"
3273Hey! \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
3274.IP "Around line 2996:" 4 3342.IP "Around line 3015:" 4
3275.IX Item "Around line 2996:" 3343.IX Item "Around line 3015:"
3276You forgot a '=back' before '=head2' 3344You forgot a '=back' before '=head2'

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines