… | |
… | |
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 |
329 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
329 | See 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))" |
332 | Sets the allocation function to use (the prototype is similar \- the |
332 | Sets the allocation function to use (the prototype is similar \- the |
333 | semantics is identical \- to the realloc C function). It is used to |
333 | semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is |
334 | allocate and free memory (no surprises here). If it returns zero when |
334 | used to allocate and free memory (no surprises here). If it returns zero |
335 | memory needs to be allocated, the library might abort or take some |
335 | when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort |
336 | potentially destructive action. The default is your system realloc |
336 | or take some potentially destructive action. |
337 | function. |
337 | .Sp |
|
|
338 | Since some systems (at least OpenBSD and Darwin) fail to implement |
|
|
339 | correct \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 |
339 | You could override this function in high-availability programs to, say, |
342 | You could override this function in high-availability programs to, say, |
340 | free some memory if it cannot allocate memory, to use a special allocator, |
343 | free some memory if it cannot allocate memory, to use a special allocator, |
341 | or even to sleep a while and retry until some memory is available. |
344 | or even to sleep a while and retry until some memory is available. |
342 | .Sp |
345 | .Sp |
343 | Example: Replace the libev allocator with one that waits a bit and then |
346 | Example: Replace the libev allocator with one that waits a bit and then |
344 | retries). |
347 | retries (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" |
389 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two |
392 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two |
390 | types of such loops, the \fIdefault\fR loop, which supports signals and child |
393 | types of such loops, the \fIdefault\fR loop, which supports signals and child |
391 | events, and dynamically created loops which do not. |
394 | events, and dynamically created loops which do not. |
392 | .PP |
|
|
393 | If you use threads, a common model is to run the default event loop |
|
|
394 | in your main thread (or in a separate thread) and for each thread you |
|
|
395 | create, you also create another event loop. Libev itself does no locking |
|
|
396 | whatsoever, so if you mix calls to the same event loop in different |
|
|
397 | threads, make sure you lock (this is usually a bad idea, though, even if |
|
|
398 | done 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)" |
401 | This will initialise the default event loop if it hasn't been initialised |
397 | This will initialise the default event loop if it hasn't been initialised |
402 | yet and return it. If the default loop could not be initialised, returns |
398 | yet and return it. If the default loop could not be initialised, returns |
403 | false. If it already was initialised it simply returns it (and ignores the |
399 | false. If it already was initialised it simply returns it (and ignores the |
… | |
… | |
486 | For few fds, this backend is a bit little slower than poll and select, |
482 | For few fds, this backend is a bit little slower than poll and select, |
487 | but it scales phenomenally better. While poll and select usually scale |
483 | but it scales phenomenally better. While poll and select usually scale |
488 | like O(total_fds) where n is the total number of fds (or the highest fd), |
484 | like O(total_fds) where n is the total number of fds (or the highest fd), |
489 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
485 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
490 | of shortcomings, such as silently dropping events in some hard-to-detect |
486 | of shortcomings, such as silently dropping events in some hard-to-detect |
491 | cases and rewiring a syscall per fd change, no fork support and bad |
487 | cases and requiring a syscall per fd change, no fork support and bad |
492 | support for dup. |
488 | support for dup. |
493 | .Sp |
489 | .Sp |
494 | While stopping, setting and starting an I/O watcher in the same iteration |
490 | While stopping, setting and starting an I/O watcher in the same iteration |
495 | will result in some caching, there is still a syscall per such incident |
491 | will 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 |
2497 | Priorities are not currently supported. Initialising priorities |
2493 | Priorities are not currently supported. Initialising priorities |
2498 | will fail and all watchers will have the same priority, even though there |
2494 | will fail and all watchers will have the same priority, even though there |
2499 | is an ev_pri field. |
2495 | is an ev_pri field. |
2500 | .IP "\(bu" 4 |
2496 | .IP "\(bu" 4 |
|
|
2497 | In libevent, the last base created gets the signals, in libev, the |
|
|
2498 | first base created (== the default loop) gets the signals. |
|
|
2499 | .IP "\(bu" 4 |
2501 | Other members are not supported. |
2500 | Other members are not supported. |
2502 | .IP "\(bu" 4 |
2501 | .IP "\(bu" 4 |
2503 | The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need |
2502 | The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need |
2504 | to use the libev header file and library. |
2503 | to 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_" |
2736 | Similar to the other two macros, this gives you the value of the default |
2735 | Similar to the other two macros, this gives you the value of the default |
2737 | loop, if multiple loops are supported (\*(L"ev loop default\*(R"). |
2736 | loop, 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_" |
|
|
2740 | Usage identical to \f(CW\*(C`EV_DEFAULT\*(C'\fR and \f(CW\*(C`EV_DEFAULT_\*(C'\fR, but requires that the |
|
|
2741 | default loop has been initialised (\f(CW\*(C`UC\*(C'\fR == unchecked). Their behaviour |
|
|
2742 | is undefined when the default loop has not been initialised by a previous |
|
|
2743 | execution 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 |
|
|
2745 | It is often prudent to use \f(CW\*(C`EV_DEFAULT\*(C'\fR when initialising the first |
|
|
2746 | watcher in a function but use \f(CW\*(C`EV_DEFAULT_UC\*(C'\fR afterwards. |
2738 | .PP |
2747 | .PP |
2739 | Example: Declare and initialise a check watcher, utilising the above |
2748 | Example: Declare and initialise a check watcher, utilising the above |
2740 | macros so it will work regardless of whether multiple loops are supported |
2749 | macros so it will work regardless of whether multiple loops are supported |
2741 | or not. |
2750 | or 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" |
2857 | Libev can be configured via a variety of preprocessor symbols you have to define |
2866 | Libev can be configured via a variety of preprocessor symbols you have to |
2858 | before including any of its files. The default is not to build for multiplicity |
2867 | define before including any of its files. The default in the absense of |
2859 | and only include the select backend. |
2868 | autoconf 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" |
2862 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
2871 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
2863 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
2872 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
2864 | implementations for some libevent functions (such as logging, which is not |
2873 | implementations for some libevent functions (such as logging, which is not |
… | |
… | |
2883 | note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. |
2892 | note 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" |
2886 | If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available |
2895 | If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available |
2887 | and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. |
2896 | and 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" |
|
|
2899 | If defined to be \f(CW1\fR, then libev will assume that \f(CW\*(C`eventfd ()\*(C'\fR is |
|
|
2900 | available 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. |
|
|
2902 | If undefined, it will be enabled if the headers indicate GNU/Linux + Glibc |
|
|
2903 | 2.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" |
2890 | If undefined or defined to be \f(CW1\fR, libev will compile in support for the |
2906 | If 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 |
2892 | other method takes over, select will be it. Otherwise the select backend |
2908 | other method takes over, select will be it. Otherwise the select backend |
… | |
… | |
2923 | takes precedence over select. |
2939 | takes 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" |
2926 | If defined to be \f(CW1\fR, libev will compile in support for the Linux |
2942 | If 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, |
2928 | otherwise another method will be used as fallback. This is the |
2944 | otherwise another method will be used as fallback. This is the preferred |
2929 | preferred backend for GNU/Linux systems. |
2945 | backend for GNU/Linux systems. If undefined, it will be enabled if the |
|
|
2946 | headers 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" |
2932 | If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style |
2949 | If 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, |
2934 | otherwise another method will be used as fallback. This is the preferred |
2951 | otherwise another method will be used as fallback. This is the preferred |
… | |
… | |
2949 | reserved for future expansion, works like the \s-1USE\s0 symbols above. |
2966 | reserved 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" |
2952 | If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify |
2969 | If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify |
2953 | interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will |
2970 | interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will |
2954 | be detected at runtime. |
2971 | be detected at runtime. If undefined, it will be enabled if the headers |
|
|
2972 | indicate 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" |
2957 | Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose |
2975 | Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose |
2958 | access is atomic with respect to other threads or signal contexts. No such |
2976 | access is atomic with respect to other threads or signal contexts. No such |
2959 | type is easily found in the C language, so you can provide your own type |
2977 | type 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" |
|
|
3167 | Libev itself is completely threadsafe, but it uses no locking. This |
|
|
3168 | means that you can use as many loops as you want in parallel, as long as |
|
|
3169 | only one thread ever calls into one libev function with the same loop |
|
|
3170 | parameter. |
|
|
3171 | .PP |
|
|
3172 | Or put differently: calls with different loop parameters can be done in |
|
|
3173 | parallel from multiple threads, calls with the same loop parameter must be |
|
|
3174 | done serially (but can be done from different threads, as long as only one |
|
|
3175 | thread ever is inside a call at any point in time, e.g. by using a mutex |
|
|
3176 | per loop). |
|
|
3177 | .PP |
|
|
3178 | If you want to know which design is best for your problem, then I cannot |
|
|
3179 | help you but by giving some generic advice: |
|
|
3180 | .IP "\(bu" 4 |
|
|
3181 | most applications have a main thread: use the default libev loop |
|
|
3182 | in that thread, or create a seperate thread running only the default loop. |
|
|
3183 | .Sp |
|
|
3184 | This helps integrating other libraries or software modules that use libev |
|
|
3185 | themselves and don't care/know about threading. |
|
|
3186 | .IP "\(bu" 4 |
|
|
3187 | one loop per thread is usually a good model. |
|
|
3188 | .Sp |
|
|
3189 | Doing this is almost never wrong, sometimes a better-performance model |
|
|
3190 | exists, but it is always a good start. |
|
|
3191 | .IP "\(bu" 4 |
|
|
3192 | other models exist, such as the leader/follower pattern, where one |
|
|
3193 | loop is handed through multiple threads in a kind of round-robbin fashion. |
|
|
3194 | .Sp |
|
|
3195 | Chosing a model is hard \- look around, learn, know that usually you cna do |
|
|
3196 | better than you currently do :\-) |
|
|
3197 | .IP "\(bu" 4 |
|
|
3198 | often you need to talk to some other thread which blocks in the |
|
|
3199 | event loop \- \f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other |
|
|
3200 | threads safely (or from signal contexts...). |
|
|
3201 | .Sh "\s-1COROUTINES\s0" |
|
|
3202 | .IX Subsection "COROUTINES" |
|
|
3203 | Libev is much more accomodating to coroutines (\*(L"cooperative threads\*(R"): |
|
|
3204 | libev fully supports nesting calls to it's functions from different |
|
|
3205 | coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two |
|
|
3206 | different coroutines and switch freely between both coroutines running the |
|
|
3207 | loop, as long as you don't confuse yourself). The only exception is that |
|
|
3208 | you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks. |
|
|
3209 | .PP |
|
|
3210 | Care has been invested into making sure that libev does not keep local |
|
|
3211 | state inside \f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine |
|
|
3212 | switches. |
3145 | .SH "COMPLEXITIES" |
3213 | .SH "COMPLEXITIES" |
3146 | .IX Header "COMPLEXITIES" |
3214 | .IX Header "COMPLEXITIES" |
3147 | In this section the complexities of (many of) the algorithms used inside |
3215 | In this section the complexities of (many of) the algorithms used inside |
3148 | libev will be explained. For complexity discussions about backends see the |
3216 | libev will be explained. For complexity discussions about backends see the |
3149 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
3217 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
… | |
… | |
3269 | .IX Header "AUTHOR" |
3337 | .IX Header "AUTHOR" |
3270 | Marc Lehmann <libev@schmorp.de>. |
3338 | Marc Lehmann <libev@schmorp.de>. |
3271 | .SH "POD ERRORS" |
3339 | .SH "POD ERRORS" |
3272 | .IX Header "POD ERRORS" |
3340 | .IX Header "POD ERRORS" |
3273 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
3341 | Hey! \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:" |
3276 | You forgot a '=back' before '=head2' |
3344 | You forgot a '=back' before '=head2' |