From 7f483d77d8cfe5e3d345e3eeae311fe0dd0c838c Mon Sep 17 00:00:00 2001 From: root Date: Mon, 25 Oct 2010 10:50:13 +0000 Subject: 4.00 --- Symbols.ev | 2 + ev.3 | 1135 +++++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 776 insertions(+), 361 deletions(-) diff --git a/Symbols.ev b/Symbols.ev index f401b30..8e2465f 100644 --- a/Symbols.ev +++ b/Symbols.ev @@ -7,6 +7,8 @@ ev_check_start ev_check_stop ev_child_start ev_child_stop +ev_cleanup_start +ev_cleanup_stop ev_clear_pending ev_default_loop ev_default_loop_ptr diff --git a/ev.3 b/ev.3 index 2c3c846..bab2e81 100644 --- a/ev.3 +++ b/ev.3 @@ -124,7 +124,7 @@ .\" ======================================================================== .\" .IX Title "LIBEV 3" -.TH LIBEV 3 "2009-12-31" "libev-3.9" "libev - high performance full featured event loop" +.TH LIBEV 3 "2010-10-25" "libev-4.00" "libev - high performance full featured event loop" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -159,8 +159,8 @@ libev \- a high performance full\-featured event loop written in C \& // with its corresponding stop function. \& ev_io_stop (EV_A_ w); \& -\& // this causes all nested ev_loop\*(Aqs to stop iterating -\& ev_unloop (EV_A_ EVUNLOOP_ALL); +\& // this causes all nested ev_run\*(Aqs to stop iterating +\& ev_break (EV_A_ EVBREAK_ALL); \& } \& \& // another callback, this time for a time\-out @@ -168,15 +168,15 @@ libev \- a high performance full\-featured event loop written in C \& timeout_cb (EV_P_ ev_timer *w, int revents) \& { \& puts ("timeout"); -\& // this causes the innermost ev_loop to stop iterating -\& ev_unloop (EV_A_ EVUNLOOP_ONE); +\& // this causes the innermost ev_run to stop iterating +\& ev_break (EV_A_ EVBREAK_ONE); \& } \& \& int \& main (void) \& { \& // use the default event loop unless you have special needs -\& struct ev_loop *loop = ev_default_loop (0); +\& struct ev_loop *loop = EV_DEFAULT; \& \& // initialise an io watcher, then start it \& // this one will watch for stdin to become readable @@ -189,7 +189,7 @@ libev \- a high performance full\-featured event loop written in C \& ev_timer_start (loop, &timeout_watcher); \& \& // now wait for events to arrive -\& ev_loop (loop, 0); +\& ev_run (loop, 0); \& \& // unloop was called, so exit \& return 0; @@ -208,8 +208,15 @@ libev, its usage and the rationale behind its design, it is not a tutorial on event-based programming, nor will it introduce event-based programming with libev. .PP -Familarity with event based programming techniques in general is assumed +Familiarity with event based programming techniques in general is assumed throughout this document. +.SH "WHAT TO READ WHEN IN A HURRY" +.IX Header "WHAT TO READ WHEN IN A HURRY" +This manual tries to be very detailed, but unfortunately, this also makes +it very long. If you just want to know the basics of libev, I suggest +reading \*(L"\s-1ANATOMY\s0 \s-1OF\s0 A \s-1WATCHER\s0\*(R", then the \*(L"\s-1EXAMPLE\s0 \s-1PROGRAM\s0\*(R" above and +look up the missing functions in \*(L"\s-1GLOBAL\s0 \s-1FUNCTIONS\s0\*(R" and the \f(CW\*(C`ev_io\*(C'\fR and +\&\f(CW\*(C`ev_timer\*(C'\fR sections in \*(L"\s-1WATCHER\s0 \s-1TYPES\s0\*(R". .SH "ABOUT LIBEV" .IX Header "ABOUT LIBEV" Libev is an event loop: you register interest in certain events (such as a @@ -253,13 +260,14 @@ this argument. .SS "\s-1TIME\s0 \s-1REPRESENTATION\s0" .IX Subsection "TIME REPRESENTATION" Libev represents time as a single floating point number, representing -the (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere -near the beginning of 1970, details are complicated, don't ask). This -type is called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually -aliases to the \f(CW\*(C`double\*(C'\fR type in C. When you need to do any calculations -on it, you should treat it as some floating point value. Unlike the name -component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences -throughout libev. +the (fractional) number of seconds since the (\s-1POSIX\s0) epoch (in practice +somewhere near the beginning of 1970, details are complicated, don't +ask). This type is called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use +too. It usually aliases to the \f(CW\*(C`double\*(C'\fR type in C. When you need to do +any calculations on it, you should treat it as some floating point value. +.PP +Unlike the name component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for +time differences (e.g. delays) throughout libev. .SH "ERROR HANDLING" .IX Header "ERROR HANDLING" Libev knows three classes of errors: operating system errors, usage errors @@ -287,7 +295,8 @@ library in any way. .IX Item "ev_tstamp ev_time ()" Returns the current time as libev would use it. Please note that the \&\f(CW\*(C`ev_now\*(C'\fR function is usually faster and also often returns the timestamp -you actually want to know. +you actually want to know. Also interesting is the combination of +\&\f(CW\*(C`ev_update_now\*(C'\fR and \f(CW\*(C`ev_now\*(C'\fR. .IP "ev_sleep (ev_tstamp interval)" 4 .IX Item "ev_sleep (ev_tstamp interval)" Sleep for the given interval: The current thread will be blocked until @@ -314,7 +323,8 @@ compatible to older versions, so a larger minor version alone is usually not a problem. .Sp Example: Make sure we haven't accidentally been linked against the wrong -version. +version (note, however, that this will not detect other \s-1ABI\s0 mismatches, +such as \s-1LFS\s0 or reentrancy). .Sp .Vb 3 \& assert (("libev version mismatch", @@ -337,19 +347,20 @@ a must have and can we have a torrent of it please!!!11 .Ve .IP "unsigned int ev_recommended_backends ()" 4 .IX Item "unsigned int ev_recommended_backends ()" -Return the set of all backends compiled into this binary of libev and also -recommended for this platform. This set is often smaller than the one -returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on -most BSDs and will not be auto-detected unless you explicitly request it -(assuming you know what you are doing). This is the set of backends that -libev will probe for if you specify no backends explicitly. +Return the set of all backends compiled into this binary of libev and +also recommended for this platform, meaning it will work for most file +descriptor types. This set is often smaller than the one returned by +\&\f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on most BSDs +and will not be auto-detected unless you explicitly request it (assuming +you know what you are doing). This is the set of backends that libev will +probe for if you specify no backends explicitly. .IP "unsigned int ev_embeddable_backends ()" 4 .IX Item "unsigned int ev_embeddable_backends ()" Returns the set of backends that are embeddable in other event loops. This -is the theoretical, all-platform, value. To find which backends -might be supported on the current system, you would need to look at -\&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for -recommended ones. +value is platform-specific but can include backends not available on the +current system. To find which embeddable backends might be supported on +the current system, you would need to look at \f(CW\*(C`ev_embeddable_backends () +& ev_supported_backends ()\*(C'\fR, likewise for recommended ones. .Sp See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. .IP "ev_set_allocator (void *(*cb)(void *ptr, long size)) [\s-1NOT\s0 \s-1REENTRANT\s0]" 4 @@ -412,35 +423,63 @@ Example: This is basically the same thing that libev does internally, too. \& ... \& ev_set_syserr_cb (fatal_error); .Ve -.SH "FUNCTIONS CONTROLLING THE EVENT LOOP" -.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" -An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR (the \f(CW\*(C`struct\*(C'\fR -is \fInot\fR optional in this case, as there is also an \f(CW\*(C`ev_loop\*(C'\fR -\&\fIfunction\fR). +.SH "FUNCTIONS CONTROLLING EVENT LOOPS" +.IX Header "FUNCTIONS CONTROLLING EVENT LOOPS" +An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR (the \f(CW\*(C`struct\*(C'\fR is +\&\fInot\fR optional in this case unless libev 3 compatibility is disabled, as +libev 3 had an \f(CW\*(C`ev_loop\*(C'\fR function colliding with the struct name). .PP The library knows two types of such loops, the \fIdefault\fR loop, which -supports signals and child events, and dynamically created loops which do -not. +supports child process events, and dynamically created event loops which +do not. .IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4 .IX Item "struct ev_loop *ev_default_loop (unsigned int flags)" -This will initialise the default event loop if it hasn't been initialised -yet and return it. If the default loop could not be initialised, returns -false. If it already was initialised it simply returns it (and ignores the -flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards). +This returns the \*(L"default\*(R" event loop object, which is what you should +normally use when you just need \*(L"the event loop\*(R". Event loop objects and +the \f(CW\*(C`flags\*(C'\fR parameter are described in more detail in the entry for +\&\f(CW\*(C`ev_loop_new\*(C'\fR. +.Sp +If the default loop is already initialised then this function simply +returns it (and ignores the flags. If that is troubling you, check +\&\f(CW\*(C`ev_backend ()\*(C'\fR afterwards). Otherwise it will create it with the given +flags, which should almost always be \f(CW0\fR, unless the caller is also the +one calling \f(CW\*(C`ev_run\*(C'\fR or otherwise qualifies as \*(L"the main program\*(R". .Sp If you don't know what event loop to use, use the one returned from this -function. +function (or via the \f(CW\*(C`EV_DEFAULT\*(C'\fR macro). .Sp Note that this function is \fInot\fR thread-safe, so if you want to use it -from multiple threads, you have to lock (note also that this is unlikely, -as loops cannot be shared easily between threads anyway). +from multiple threads, you have to employ some kind of mutex (note also +that this case is unlikely, as loops cannot be shared easily between +threads anyway). +.Sp +The default loop is the only loop that can handle \f(CW\*(C`ev_child\*(C'\fR watchers, +and to do this, it always registers a handler for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is +a problem for your application you can either create a dynamic loop with +\&\f(CW\*(C`ev_loop_new\*(C'\fR which doesn't do that, or you can simply overwrite the +\&\f(CW\*(C`SIGCHLD\*(C'\fR signal handler \fIafter\fR calling \f(CW\*(C`ev_default_init\*(C'\fR. +.Sp +Example: This is the most typical usage. +.Sp +.Vb 2 +\& if (!ev_default_loop (0)) +\& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); +.Ve .Sp -The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and -\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler -for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your application you can either -create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you -can simply overwrite the \f(CW\*(C`SIGCHLD\*(C'\fR signal handler \fIafter\fR calling -\&\f(CW\*(C`ev_default_init\*(C'\fR. +Example: Restrict libev to the select and poll backends, and do not allow +environment settings to be taken into account: +.Sp +.Vb 1 +\& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); +.Ve +.IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4 +.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" +This will create and initialise a new event loop object. If the loop +could not be initialised, returns false. +.Sp +Note that this function \fIis\fR thread-safe, and one common way to use +libev with threads is indeed to create one loop per thread, and using the +default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread. .Sp The flags argument can be used to specify special behaviour or specific backends to use, and is usually specified as \f(CW0\fR (or \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). @@ -464,9 +503,8 @@ around bugs. .ie n .IP """EVFLAG_FORKCHECK""" 4 .el .IP "\f(CWEVFLAG_FORKCHECK\fR" 4 .IX Item "EVFLAG_FORKCHECK" -Instead of calling \f(CW\*(C`ev_default_fork\*(C'\fR or \f(CW\*(C`ev_loop_fork\*(C'\fR manually after -a fork, you can also make libev check for a fork in each iteration by -enabling this flag. +Instead of calling \f(CW\*(C`ev_loop_fork\*(C'\fR manually after a fork, you can also +make libev check for a fork in each iteration by enabling this flag. .Sp This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, and thus this might slow down your event loop if you do a lot of loop @@ -558,7 +596,9 @@ of course \fIdoesn't\fR, and epoll just loves to report events for totally even remove them from the set) than registered in the set (especially on \s-1SMP\s0 systems). Libev tries to counter these spurious notifications by employing an additional generation counter and comparing that against the -events to filter out spurious ones, recreating the set when required. +events to filter out spurious ones, recreating the set when required. Last +not least, it also refuses to work with some file descriptors which work +perfectly fine with \f(CW\*(C`select\*(C'\fR (files, many character devices...). .Sp While stopping, setting and starting an I/O watcher in the same iteration will result in some caching, there is still a system call per such @@ -666,50 +706,24 @@ then only these backends will be tried (in the reverse order as listed here). If none are specified, all backends in \f(CW\*(C`ev_recommended_backends ()\*(C'\fR will be tried. .Sp -Example: This is the most typical usage. -.Sp -.Vb 2 -\& if (!ev_default_loop (0)) -\& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); -.Ve -.Sp -Example: Restrict libev to the select and poll backends, and do not allow -environment settings to be taken into account: +Example: Try to create a event loop that uses epoll and nothing else. .Sp -.Vb 1 -\& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); +.Vb 3 +\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); +\& if (!epoller) +\& fatal ("no epoll found here, maybe it hides under your chair"); .Ve .Sp Example: Use whatever libev has to offer, but make sure that kqueue is -used if available (warning, breaks stuff, best use only with your own -private event loop and only if you know the \s-1OS\s0 supports your types of -fds): +used if available. .Sp .Vb 1 -\& ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); +\& struct ev_loop *loop = ev_loop_new (ev_recommended_backends () | EVBACKEND_KQUEUE); .Ve .RE -.IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4 -.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" -Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is -always distinct from the default loop. Unlike the default loop, it cannot -handle signal and child watchers, and attempts to do so will be greeted by -undefined behaviour (or a failed assertion if assertions are enabled). -.Sp -Note that this function \fIis\fR thread-safe, and the recommended way to use -libev with threads is indeed to create one loop per thread, and using the -default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread. -.Sp -Example: Try to create a event loop that uses epoll and nothing else. -.Sp -.Vb 3 -\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); -\& if (!epoller) -\& fatal ("no epoll found here, maybe it hides under your chair"); -.Ve -.IP "ev_default_destroy ()" 4 -.IX Item "ev_default_destroy ()" -Destroys the default loop again (frees all memory and kernel state +.IP "ev_loop_destroy (loop)" 4 +.IX Item "ev_loop_destroy (loop)" +Destroys an event loop object (frees all memory and kernel state etc.). None of the active event watchers will be stopped in the normal sense, so e.g. \f(CW\*(C`ev_is_active\*(C'\fR might still return true. It is your responsibility to either stop all watchers cleanly yourself \fIbefore\fR @@ -721,64 +735,76 @@ Note that certain global state, such as signal state (and installed signal handlers), will not be freed by this function, and related watchers (such as signal and child watchers) would need to be stopped manually. .Sp -In general it is not advisable to call this function except in the -rare occasion where you really need to free e.g. the signal handling -pipe fds. If you need dynamically allocated loops it is better to use -\&\f(CW\*(C`ev_loop_new\*(C'\fR and \f(CW\*(C`ev_loop_destroy\*(C'\fR. -.IP "ev_loop_destroy (loop)" 4 -.IX Item "ev_loop_destroy (loop)" -Like \f(CW\*(C`ev_default_destroy\*(C'\fR, but destroys an event loop created by an -earlier call to \f(CW\*(C`ev_loop_new\*(C'\fR. -.IP "ev_default_fork ()" 4 -.IX Item "ev_default_fork ()" -This function sets a flag that causes subsequent \f(CW\*(C`ev_loop\*(C'\fR iterations -to reinitialise the kernel state for backends that have one. Despite the +This function is normally used on loop objects allocated by +\&\f(CW\*(C`ev_loop_new\*(C'\fR, but it can also be used on the default loop returned by +\&\f(CW\*(C`ev_default_loop\*(C'\fR, in which case it is not thread-safe. +.Sp +Note that it is not advisable to call this function on the default loop +except in the rare occasion where you really need to free it's resources. +If you need dynamically allocated loops it is better to use \f(CW\*(C`ev_loop_new\*(C'\fR +and \f(CW\*(C`ev_loop_destroy\*(C'\fR. +.IP "ev_loop_fork (loop)" 4 +.IX Item "ev_loop_fork (loop)" +This function sets a flag that causes subsequent \f(CW\*(C`ev_run\*(C'\fR iterations to +reinitialise the kernel state for backends that have one. Despite the name, you can call it anytime, but it makes most sense after forking, in -the child process (or both child and parent, but that again makes little -sense). You \fImust\fR call it in the child before using any of the libev -functions, and it will only take effect at the next \f(CW\*(C`ev_loop\*(C'\fR iteration. +the child process. You \fImust\fR call it (or use \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR) in the +child before resuming or calling \f(CW\*(C`ev_run\*(C'\fR. +.Sp +Again, you \fIhave\fR to call it on \fIany\fR loop that you want to re-use after +a fork, \fIeven if you do not plan to use the loop in the parent\fR. This is +because some kernel interfaces *cough* \fIkqueue\fR *cough* do funny things +during fork. .Sp On the other hand, you only need to call this function in the child -process if and only if you want to use the event library in the child. If -you just fork+exec, you don't have to call it at all. +process if and only if you want to use the event loop in the child. If +you just fork+exec or create a new loop in the child, you don't have to +call it at all (in fact, \f(CW\*(C`epoll\*(C'\fR is so badly broken that it makes a +difference, but libev will usually detect this case on its own and do a +costly reset of the backend). .Sp The function itself is quite fast and it's usually not a problem to call -it just in case after a fork. To make this easy, the function will fit in -quite nicely into a call to \f(CW\*(C`pthread_atfork\*(C'\fR: +it just in case after a fork. .Sp -.Vb 1 -\& pthread_atfork (0, 0, ev_default_fork); +Example: Automate calling \f(CW\*(C`ev_loop_fork\*(C'\fR on the default loop when +using pthreads. +.Sp +.Vb 5 +\& static void +\& post_fork_child (void) +\& { +\& ev_loop_fork (EV_DEFAULT); +\& } +\& +\& ... +\& pthread_atfork (0, 0, post_fork_child); .Ve -.IP "ev_loop_fork (loop)" 4 -.IX Item "ev_loop_fork (loop)" -Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by -\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop -after fork that you want to re-use in the child, and how you do this is -entirely your own problem. .IP "int ev_is_default_loop (loop)" 4 .IX Item "int ev_is_default_loop (loop)" Returns true when the given loop is, in fact, the default loop, and false otherwise. -.IP "unsigned int ev_loop_count (loop)" 4 -.IX Item "unsigned int ev_loop_count (loop)" -Returns the count of loop iterations for the loop, which is identical to -the number of times libev did poll for new events. It starts at \f(CW0\fR and -happily wraps around with enough iterations. +.IP "unsigned int ev_iteration (loop)" 4 +.IX Item "unsigned int ev_iteration (loop)" +Returns the current iteration count for the event loop, which is identical +to the number of times libev did poll for new events. It starts at \f(CW0\fR +and happily wraps around with enough iterations. .Sp This value can sometimes be useful as a generation counter of sorts (it \&\*(L"ticks\*(R" the number of loop iterations), as it roughly corresponds with -\&\f(CW\*(C`ev_prepare\*(C'\fR and \f(CW\*(C`ev_check\*(C'\fR calls. -.IP "unsigned int ev_loop_depth (loop)" 4 -.IX Item "unsigned int ev_loop_depth (loop)" -Returns the number of times \f(CW\*(C`ev_loop\*(C'\fR was entered minus the number of -times \f(CW\*(C`ev_loop\*(C'\fR was exited, in other words, the recursion depth. -.Sp -Outside \f(CW\*(C`ev_loop\*(C'\fR, this number is zero. In a callback, this number is -\&\f(CW1\fR, unless \f(CW\*(C`ev_loop\*(C'\fR was invoked recursively (or from another thread), +\&\f(CW\*(C`ev_prepare\*(C'\fR and \f(CW\*(C`ev_check\*(C'\fR calls \- and is incremented between the +prepare and check phases. +.IP "unsigned int ev_depth (loop)" 4 +.IX Item "unsigned int ev_depth (loop)" +Returns the number of times \f(CW\*(C`ev_run\*(C'\fR was entered minus the number of +times \f(CW\*(C`ev_run\*(C'\fR was exited, in other words, the recursion depth. +.Sp +Outside \f(CW\*(C`ev_run\*(C'\fR, this number is zero. In a callback, this number is +\&\f(CW1\fR, unless \f(CW\*(C`ev_run\*(C'\fR was invoked recursively (or from another thread), in which case it is higher. .Sp -Leaving \f(CW\*(C`ev_loop\*(C'\fR abnormally (setjmp/longjmp, cancelling the thread -etc.), doesn't count as exit. +Leaving \f(CW\*(C`ev_run\*(C'\fR abnormally (setjmp/longjmp, cancelling the thread +etc.), doesn't count as \*(L"exit\*(R" \- consider this as a hint to avoid such +ungentleman-like behaviour unless it's really convenient. .IP "unsigned int ev_backend (loop)" 4 .IX Item "unsigned int ev_backend (loop)" Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in @@ -794,7 +820,7 @@ event occurring (or more correctly, libev finding out about it). .IX Item "ev_now_update (loop)" Establishes the current time by querying the kernel, updating the time returned by \f(CW\*(C`ev_now ()\*(C'\fR in the progress. This is a costly operation and -is usually done automatically within \f(CW\*(C`ev_loop ()\*(C'\fR. +is usually done automatically within \f(CW\*(C`ev_run ()\*(C'\fR. .Sp This function is rarely useful, but when some event callback runs for a very long time without entering the event loop, updating libev's idea of @@ -807,8 +833,8 @@ See also \*(L"The special problem of time updates\*(R" in the \f(CW\*(C`ev_timer .IP "ev_resume (loop)" 4 .IX Item "ev_resume (loop)" .PD -These two functions suspend and resume a loop, for use when the loop is -not used for a while and timeouts should not be processed. +These two functions suspend and resume an event loop, for use when the +loop is not used for a while and timeouts should not be processed. .Sp A typical use case would be an interactive program such as a game: When the user presses \f(CW\*(C`^Z\*(C'\fR to suspend the game and resumes it an hour later it @@ -820,7 +846,7 @@ in your \f(CW\*(C`SIGTSTP\*(C'\fR handler, sending yourself a \f(CW\*(C`SIGSTOP\ Effectively, all \f(CW\*(C`ev_timer\*(C'\fR watchers will be delayed by the time spend between \f(CW\*(C`ev_suspend\*(C'\fR and \f(CW\*(C`ev_resume\*(C'\fR, and all \f(CW\*(C`ev_periodic\*(C'\fR watchers will be rescheduled (that is, they will lose any events that would have -occured while suspended). +occurred while suspended). .Sp After calling \f(CW\*(C`ev_suspend\*(C'\fR you \fBmust not\fR call \fIany\fR function on the given loop other than \f(CW\*(C`ev_resume\*(C'\fR, and you \fBmust not\fR call \f(CW\*(C`ev_resume\*(C'\fR @@ -828,28 +854,32 @@ without a previous call to \f(CW\*(C`ev_suspend\*(C'\fR. .Sp Calling \f(CW\*(C`ev_suspend\*(C'\fR/\f(CW\*(C`ev_resume\*(C'\fR has the side effect of updating the event loop time (see \f(CW\*(C`ev_now_update\*(C'\fR). -.IP "ev_loop (loop, int flags)" 4 -.IX Item "ev_loop (loop, int flags)" +.IP "ev_run (loop, int flags)" 4 +.IX Item "ev_run (loop, int flags)" Finally, this is it, the event handler. This function usually is called after you have initialised all your watchers and you want to start -handling events. +handling events. It will ask the operating system for any new events, call +the watcher callbacks, an then repeat the whole process indefinitely: This +is why event loops are called \fIloops\fR. .Sp -If the flags argument is specified as \f(CW0\fR, it will not return until -either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called. +If the flags argument is specified as \f(CW0\fR, it will keep handling events +until either no event watchers are active anymore or \f(CW\*(C`ev_break\*(C'\fR was +called. .Sp -Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than +Please note that an explicit \f(CW\*(C`ev_break\*(C'\fR is usually better than relying on all watchers to be stopped when deciding when a program has finished (especially in interactive programs), but having a program that automatically loops as long as it has to and no longer by virtue of relying on its watchers stopping correctly, that is truly a thing of beauty. .Sp -A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle -those events and any already outstanding ones, but will not block your -process in case there are no events and will return after one iteration of -the loop. +A flags value of \f(CW\*(C`EVRUN_NOWAIT\*(C'\fR will look for new events, will handle +those events and any already outstanding ones, but will not wait and +block your process in case there are no events and will return after one +iteration of the loop. This is sometimes useful to poll and handle new +events while doing lengthy calculations, to keep the program responsive. .Sp -A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if +A flags value of \f(CW\*(C`EVRUN_ONCE\*(C'\fR will look for new events (waiting if necessary) and will handle those and any already outstanding ones. It will block your process until at least one new event arrives (which could be an event internal to libev itself, so there is no guarantee that a @@ -858,37 +888,46 @@ iteration of the loop. .Sp This is useful if you are waiting for some external event in conjunction with something not expressible using other libev watchers (i.e. "roll your -own \f(CW\*(C`ev_loop\*(C'\fR"). However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is +own \f(CW\*(C`ev_run\*(C'\fR"). However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is usually a better approach for this kind of thing. .Sp -Here are the gory details of what \f(CW\*(C`ev_loop\*(C'\fR does: +Here are the gory details of what \f(CW\*(C`ev_run\*(C'\fR does: .Sp .Vb 10 +\& \- Increment loop depth. +\& \- Reset the ev_break status. \& \- Before the first iteration, call any pending watchers. -\& * If EVFLAG_FORKCHECK was used, check for a fork. +\& LOOP: +\& \- If EVFLAG_FORKCHECK was used, check for a fork. \& \- If a fork was detected (by any means), queue and call all fork watchers. \& \- Queue and call all prepare watchers. +\& \- If ev_break was called, goto FINISH. \& \- If we have been forked, detach and recreate the kernel state \& as to not disturb the other process. \& \- Update the kernel state with all outstanding changes. \& \- Update the "event loop time" (ev_now ()). \& \- Calculate for how long to sleep or block, if at all -\& (active idle watchers, EVLOOP_NONBLOCK or not having +\& (active idle watchers, EVRUN_NOWAIT or not having \& any active watchers at all will result in not sleeping). \& \- Sleep if the I/O and timer collect interval say so. +\& \- Increment loop iteration counter. \& \- Block the process, waiting for any events. \& \- Queue all outstanding I/O (fd) events. \& \- Update the "event loop time" (ev_now ()), and do time jump adjustments. \& \- Queue all expired timers. \& \- Queue all expired periodics. -\& \- Unless any events are pending now, queue all idle watchers. +\& \- Queue all idle watchers with priority higher than that of pending events. \& \- Queue all check watchers. \& \- Call all queued watchers in reverse order (i.e. check watchers first). \& Signals and child watchers are implemented as I/O watchers, and will \& be handled here by queueing them when their watcher gets executed. -\& \- If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK -\& were used, or there are no active watchers, return, otherwise -\& continue with step *. +\& \- If ev_break has been called, or EVRUN_ONCE or EVRUN_NOWAIT +\& were used, or there are no active watchers, goto FINISH, otherwise +\& continue with step LOOP. +\& FINISH: +\& \- Reset the ev_break status iff it was EVBREAK_ONE. +\& \- Decrement the loop depth. +\& \- Return. .Ve .Sp Example: Queue some jobs and then loop until no events are outstanding @@ -897,19 +936,19 @@ anymore. .Vb 4 \& ... queue jobs here, make sure they register event watchers as long \& ... as they still have work to do (even an idle watcher will do..) -\& ev_loop (my_loop, 0); +\& ev_run (my_loop, 0); \& ... jobs done or somebody called unloop. yeah! .Ve -.IP "ev_unloop (loop, how)" 4 -.IX Item "ev_unloop (loop, how)" -Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it +.IP "ev_break (loop, how)" 4 +.IX Item "ev_break (loop, how)" +Can be used to make a call to \f(CW\*(C`ev_run\*(C'\fR return early (but only after it has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either -\&\f(CW\*(C`EVUNLOOP_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_loop\*(C'\fR call return, or -\&\f(CW\*(C`EVUNLOOP_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_loop\*(C'\fR calls return. +\&\f(CW\*(C`EVBREAK_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_run\*(C'\fR call return, or +\&\f(CW\*(C`EVBREAK_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_run\*(C'\fR calls return. .Sp -This \*(L"unloop state\*(R" will be cleared when entering \f(CW\*(C`ev_loop\*(C'\fR again. +This \*(L"unloop state\*(R" will be cleared when entering \f(CW\*(C`ev_run\*(C'\fR again. .Sp -It is safe to call \f(CW\*(C`ev_unloop\*(C'\fR from otuside any \f(CW\*(C`ev_loop\*(C'\fR calls. +It is safe to call \f(CW\*(C`ev_break\*(C'\fR from outside any \f(CW\*(C`ev_run\*(C'\fR calls. ##TODO## .IP "ev_ref (loop)" 4 .IX Item "ev_ref (loop)" .PD 0 @@ -918,15 +957,15 @@ It is safe to call \f(CW\*(C`ev_unloop\*(C'\fR from otuside any \f(CW\*(C`ev_loo .PD Ref/unref can be used to add or remove a reference count on the event loop: Every watcher keeps one reference, and as long as the reference -count is nonzero, \f(CW\*(C`ev_loop\*(C'\fR will not return on its own. +count is nonzero, \f(CW\*(C`ev_run\*(C'\fR will not return on its own. .Sp This is useful when you have a watcher that you never intend to -unregister, but that nevertheless should not keep \f(CW\*(C`ev_loop\*(C'\fR from +unregister, but that nevertheless should not keep \f(CW\*(C`ev_run\*(C'\fR from returning. In such a case, call \f(CW\*(C`ev_unref\*(C'\fR after starting, and \f(CW\*(C`ev_ref\*(C'\fR before stopping it. .Sp As an example, libev itself uses this for its internal signal pipe: It -is not visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from +is not visible to the libev user and should not keep \f(CW\*(C`ev_run\*(C'\fR from exiting if no event watchers registered by it are active. It is also an excellent way to do this for generic recurring timers or from within third-party libraries. Just remember to \fIunref after start\fR and \fIref @@ -935,7 +974,7 @@ before, respectively. Note also that libev might stop watchers itself (e.g. non-repeating timers) in which case you have to \f(CW\*(C`ev_ref\*(C'\fR in the callback). .Sp -Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR +Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_run\*(C'\fR running when nothing else is active. .Sp .Vb 4 @@ -995,7 +1034,7 @@ as this approaches the timing granularity of most systems. Note that if you do transactions with the outside world and you can't increase the parallelity, then this setting will limit your transaction rate (if you need to poll once per transaction and the I/O collect interval is 0.01, -then you can't do more than 100 transations per second). +then you can't do more than 100 transactions per second). .Sp Setting the \fItimeout collect interval\fR can improve the opportunity for saving power, as the program will \*(L"bundle\*(R" timer callback invocations that @@ -1014,8 +1053,12 @@ more often than 100 times per second: .IP "ev_invoke_pending (loop)" 4 .IX Item "ev_invoke_pending (loop)" This call will simply invoke all pending watchers while resetting their -pending state. Normally, \f(CW\*(C`ev_loop\*(C'\fR does this automatically when required, -but when overriding the invoke callback this call comes handy. +pending state. Normally, \f(CW\*(C`ev_run\*(C'\fR does this automatically when required, +but when overriding the invoke callback this call comes handy. This +function can be invoked from a watcher \- this can be useful for example +when you want to do some lengthy calculation and want to pass further +event handling to another thread (you still have to make sure only one +thread executes within \f(CW\*(C`ev_invoke_pending\*(C'\fR or \f(CW\*(C`ev_run\*(C'\fR of course). .IP "int ev_pending_count (loop)" 4 .IX Item "int ev_pending_count (loop)" Returns the number of pending watchers \- zero indicates that no watchers @@ -1023,7 +1066,7 @@ are pending. .IP "ev_set_invoke_pending_cb (loop, void (*invoke_pending_cb)(\s-1EV_P\s0))" 4 .IX Item "ev_set_invoke_pending_cb (loop, void (*invoke_pending_cb)(EV_P))" This overrides the invoke pending functionality of the loop: Instead of -invoking all pending watchers when there are any, \f(CW\*(C`ev_loop\*(C'\fR will call +invoking all pending watchers when there are any, \f(CW\*(C`ev_run\*(C'\fR will call this callback instead. This is useful, for example, when you want to invoke the actual watchers inside another context (another thread etc.). .Sp @@ -1035,10 +1078,10 @@ Sometimes you want to share the same loop between multiple threads. This can be done relatively simply by putting mutex_lock/unlock calls around each call to a libev function. .Sp -However, \f(CW\*(C`ev_loop\*(C'\fR can run an indefinite time, so it is not feasible to -wait for it to return. One way around this is to wake up the loop via -\&\f(CW\*(C`ev_unloop\*(C'\fR and \f(CW\*(C`av_async_send\*(C'\fR, another way is to set these \fIrelease\fR -and \fIacquire\fR callbacks on the loop. +However, \f(CW\*(C`ev_run\*(C'\fR can run an indefinite time, so it is not feasible +to wait for it to return. One way around this is to wake up the event +loop via \f(CW\*(C`ev_break\*(C'\fR and \f(CW\*(C`av_async_send\*(C'\fR, another way is to set these +\&\fIrelease\fR and \fIacquire\fR callbacks on the loop. .Sp When set, then \f(CW\*(C`release\*(C'\fR will be called just before the thread is suspended waiting for new events, and \f(CW\*(C`acquire\*(C'\fR is called just @@ -1051,10 +1094,10 @@ While event loop modifications are allowed between invocations of \&\f(CW\*(C`release\*(C'\fR and \f(CW\*(C`acquire\*(C'\fR (that's their only purpose after all), no modifications done will affect the event loop, i.e. adding watchers will have no effect on the set of file descriptors being watched, or the time -waited. Use an \f(CW\*(C`ev_async\*(C'\fR watcher to wake up \f(CW\*(C`ev_loop\*(C'\fR when you want it +waited. Use an \f(CW\*(C`ev_async\*(C'\fR watcher to wake up \f(CW\*(C`ev_run\*(C'\fR when you want it to take note of any changes you made. .Sp -In theory, threads executing \f(CW\*(C`ev_loop\*(C'\fR will be async-cancel safe between +In theory, threads executing \f(CW\*(C`ev_run\*(C'\fR will be async-cancel safe between invocations of \f(CW\*(C`release\*(C'\fR and \f(CW\*(C`acquire\*(C'\fR. .Sp See also the locking example in the \f(CW\*(C`THREADS\*(C'\fR section later in this @@ -1073,8 +1116,8 @@ These two functions can be used to associate arbitrary data with a loop, and are intended solely for the \f(CW\*(C`invoke_pending_cb\*(C'\fR, \f(CW\*(C`release\*(C'\fR and \&\f(CW\*(C`acquire\*(C'\fR callbacks described above, but of course can be (ab\-)used for any other purpose as well. -.IP "ev_loop_verify (loop)" 4 -.IX Item "ev_loop_verify (loop)" +.IP "ev_verify (loop)" 4 +.IX Item "ev_verify (loop)" This function only does something when \f(CW\*(C`EV_VERIFY\*(C'\fR support has been compiled in, which is the default for non-minimal builds. It tries to go through all internal structures and checks them for validity. If anything @@ -1090,15 +1133,16 @@ In the following description, uppercase \f(CW\*(C`TYPE\*(C'\fR in names stands f watcher type, e.g. \f(CW\*(C`ev_TYPE_start\*(C'\fR can mean \f(CW\*(C`ev_timer_start\*(C'\fR for timer watchers and \f(CW\*(C`ev_io_start\*(C'\fR for I/O watchers. .PP -A watcher is a structure that you create and register to record your -interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to -become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: +A watcher is an opaque structure that you allocate and register to record +your interest in some event. To make a concrete example, imagine you want +to wait for \s-1STDIN\s0 to become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher +for that: .PP .Vb 5 \& static void my_cb (struct ev_loop *loop, ev_io *w, int revents) \& { \& ev_io_stop (w); -\& ev_unloop (loop, EVUNLOOP_ALL); +\& ev_break (loop, EVBREAK_ALL); \& } \& \& struct ev_loop *loop = ev_default_loop (0); @@ -1109,7 +1153,7 @@ become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: \& ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ); \& ev_io_start (loop, &stdin_watcher); \& -\& ev_loop (loop, 0); +\& ev_run (loop, 0); .Ve .PP As you can see, you are responsible for allocating the memory for your @@ -1119,11 +1163,11 @@ stack). Each watcher has an associated watcher structure (called \f(CW\*(C`struct ev_TYPE\*(C'\fR or simply \f(CW\*(C`ev_TYPE\*(C'\fR, as typedefs are provided for all watcher structs). .PP -Each watcher structure must be initialised by a call to \f(CW\*(C`ev_init -(watcher *, callback)\*(C'\fR, which expects a callback to be provided. This -callback gets invoked each time the event occurs (or, in the case of I/O -watchers, each time the event loop detects that the file descriptor given -is readable and/or writable). +Each watcher structure must be initialised by a call to \f(CW\*(C`ev_init (watcher +*, callback)\*(C'\fR, which expects a callback to be provided. This callback is +invoked each time the event occurs (or, in the case of I/O watchers, each +time the event loop detects that the file descriptor given is readable +and/or writable). .PP Each watcher type further has its own \f(CW\*(C`ev_TYPE_set (watcher *, ...)\*(C'\fR macro to configure it, with arguments specific to the watcher type. There @@ -1155,9 +1199,9 @@ are: .PD The file descriptor in the \f(CW\*(C`ev_io\*(C'\fR watcher has become readable and/or writable. -.ie n .IP """EV_TIMEOUT""" 4 -.el .IP "\f(CWEV_TIMEOUT\fR" 4 -.IX Item "EV_TIMEOUT" +.ie n .IP """EV_TIMER""" 4 +.el .IP "\f(CWEV_TIMER\fR" 4 +.IX Item "EV_TIMER" The \f(CW\*(C`ev_timer\*(C'\fR watcher has timed out. .ie n .IP """EV_PERIODIC""" 4 .el .IP "\f(CWEV_PERIODIC\fR" 4 @@ -1187,13 +1231,13 @@ The \f(CW\*(C`ev_idle\*(C'\fR watcher has determined that you have nothing bette .el .IP "\f(CWEV_CHECK\fR" 4 .IX Item "EV_CHECK" .PD -All \f(CW\*(C`ev_prepare\*(C'\fR watchers are invoked just \fIbefore\fR \f(CW\*(C`ev_loop\*(C'\fR starts +All \f(CW\*(C`ev_prepare\*(C'\fR watchers are invoked just \fIbefore\fR \f(CW\*(C`ev_run\*(C'\fR starts to gather new events, and all \f(CW\*(C`ev_check\*(C'\fR watchers are invoked just after -\&\f(CW\*(C`ev_loop\*(C'\fR has gathered them, but before it invokes any callbacks for any +\&\f(CW\*(C`ev_run\*(C'\fR has gathered them, but before it invokes any callbacks for any received events. Callbacks of both watcher types can start and stop as many watchers as they want, and all of them will be taken into account (for example, a \f(CW\*(C`ev_prepare\*(C'\fR watcher might start an idle watcher to keep -\&\f(CW\*(C`ev_loop\*(C'\fR from blocking). +\&\f(CW\*(C`ev_run\*(C'\fR from blocking). .ie n .IP """EV_EMBED""" 4 .el .IP "\f(CWEV_EMBED\fR" 4 .IX Item "EV_EMBED" @@ -1203,6 +1247,10 @@ The embedded event loop specified in the \f(CW\*(C`ev_embed\*(C'\fR watcher need .IX Item "EV_FORK" The event loop has been resumed in the child process after fork (see \&\f(CW\*(C`ev_fork\*(C'\fR). +.ie n .IP """EV_CLEANUP""" 4 +.el .IP "\f(CWEV_CLEANUP\fR" 4 +.IX Item "EV_CLEANUP" +The event loop is about to be destroyed (see \f(CW\*(C`ev_cleanup\*(C'\fR). .ie n .IP """EV_ASYNC""" 4 .el .IP "\f(CWEV_ASYNC\fR" 4 .IX Item "EV_ASYNC" @@ -1450,6 +1498,56 @@ programmers): \& (((char *)w) \- offsetof (struct my_biggy, t2)); \& } .Ve +.SS "\s-1WATCHER\s0 \s-1STATES\s0" +.IX Subsection "WATCHER STATES" +There are various watcher states mentioned throughout this manual \- +active, pending and so on. In this section these states and the rules to +transition between them will be described in more detail \- and while these +rules might look complicated, they usually do \*(L"the right thing\*(R". +.IP "initialiased" 4 +.IX Item "initialiased" +Before a watcher can be registered with the event looop it has to be +initialised. This can be done with a call to \f(CW\*(C`ev_TYPE_init\*(C'\fR, or calls to +\&\f(CW\*(C`ev_init\*(C'\fR followed by the watcher-specific \f(CW\*(C`ev_TYPE_set\*(C'\fR function. +.Sp +In this state it is simply some block of memory that is suitable for use +in an event loop. It can be moved around, freed, reused etc. at will. +.IP "started/running/active" 4 +.IX Item "started/running/active" +Once a watcher has been started with a call to \f(CW\*(C`ev_TYPE_start\*(C'\fR it becomes +property of the event loop, and is actively waiting for events. While in +this state it cannot be accessed (except in a few documented ways), moved, +freed or anything else \- the only legal thing is to keep a pointer to it, +and call libev functions on it that are documented to work on active watchers. +.IP "pending" 4 +.IX Item "pending" +If a watcher is active and libev determines that an event it is interested +in has occurred (such as a timer expiring), it will become pending. It will +stay in this pending state until either it is stopped or its callback is +about to be invoked, so it is not normally pending inside the watcher +callback. +.Sp +The watcher might or might not be active while it is pending (for example, +an expired non-repeating timer can be pending but no longer active). If it +is stopped, it can be freely accessed (e.g. by calling \f(CW\*(C`ev_TYPE_set\*(C'\fR), +but it is still property of the event loop at this time, so cannot be +moved, freed or reused. And if it is active the rules described in the +previous item still apply. +.Sp +It is also possible to feed an event on a watcher that is not active (e.g. +via \f(CW\*(C`ev_feed_event\*(C'\fR), in which case it becomes pending without being +active. +.IP "stopped" 4 +.IX Item "stopped" +A watcher can be stopped implicitly by libev (in which case it might still +be pending), or explicitly by calling its \f(CW\*(C`ev_TYPE_stop\*(C'\fR function. The +latter will clear any pending state the watcher might be in, regardless +of whether it was active or not, so stopping a watcher explicitly before +freeing it is often a good idea. +.Sp +While stopped (and not pending) the watcher is essentially in the +initialised state, that is it can be reused, moved, modified in any way +you wish. .SS "\s-1WATCHER\s0 \s-1PRIORITY\s0 \s-1MODELS\s0" .IX Subsection "WATCHER PRIORITY MODELS" Many event loops support \fIwatcher priorities\fR, which are usually small @@ -1501,7 +1599,7 @@ For example, to emulate how many other event libraries handle priorities, you can associate an \f(CW\*(C`ev_idle\*(C'\fR watcher to each such watcher, and in the normal watcher callback, you just start the idle watcher. The real processing is done in the idle watcher callback. This causes libev to -continously poll and process kernel event data for the watcher, but when +continuously poll and process kernel event data for the watcher, but when the lock-out case is known to be rare (which in turn is rare :), this is workable. .PP @@ -1526,7 +1624,7 @@ other events are pending: \& // are not yet ready to handle it. \& ev_io_stop (EV_A_ w); \& -\& // start the idle watcher to ahndle the actual event. +\& // start the idle watcher to handle the actual event. \& // it will not be executed as long as other watchers \& // with the default priority are receiving events. \& ev_idle_start (EV_A_ &idle); @@ -1588,7 +1686,7 @@ If you cannot use non-blocking mode, then force the use of a known-to-be-good backend (at the time of this writing, this includes only \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR). The same applies to file descriptors for which non-blocking operation makes no sense (such as -files) \- libev doesn't guarentee any specific behaviour in that case. +files) \- libev doesn't guarantee any specific behaviour in that case. .PP Another thing you have to watch out for is that it is quite easy to receive \*(L"spurious\*(R" readiness notifications, that is your callback might @@ -1667,6 +1765,46 @@ So when you encounter spurious, unexplained daemon exits, make sure you ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon somewhere, as that would have given you a big clue). .PP +\fIThe special problem of \fIaccept()\fIing when you can't\fR +.IX Subsection "The special problem of accept()ing when you can't" +.PP +Many implementations of the \s-1POSIX\s0 \f(CW\*(C`accept\*(C'\fR function (for example, +found in post\-2004 Linux) have the peculiar behaviour of not removing a +connection from the pending queue in all error cases. +.PP +For example, larger servers often run out of file descriptors (because +of resource limits), causing \f(CW\*(C`accept\*(C'\fR to fail with \f(CW\*(C`ENFILE\*(C'\fR but not +rejecting the connection, leading to libev signalling readiness on +the next iteration again (the connection still exists after all), and +typically causing the program to loop at 100% \s-1CPU\s0 usage. +.PP +Unfortunately, the set of errors that cause this issue differs between +operating systems, there is usually little the app can do to remedy the +situation, and no known thread-safe method of removing the connection to +cope with overload is known (to me). +.PP +One of the easiest ways to handle this situation is to just ignore it +\&\- when the program encounters an overload, it will just loop until the +situation is over. While this is a form of busy waiting, no \s-1OS\s0 offers an +event-based way to handle this situation, so it's the best one can do. +.PP +A better way to handle the situation is to log any errors other than +\&\f(CW\*(C`EAGAIN\*(C'\fR and \f(CW\*(C`EWOULDBLOCK\*(C'\fR, making sure not to flood the log with such +messages, and continue as usual, which at least gives the user an idea of +what could be wrong (\*(L"raise the ulimit!\*(R"). For extra points one could stop +the \f(CW\*(C`ev_io\*(C'\fR watcher on the listening fd \*(L"for a while\*(R", which reduces \s-1CPU\s0 +usage. +.PP +If your program is single-threaded, then you could also keep a dummy file +descriptor for overload situations (e.g. by opening \fI/dev/null\fR), and +when you run into \f(CW\*(C`ENFILE\*(C'\fR or \f(CW\*(C`EMFILE\*(C'\fR, close it, run \f(CW\*(C`accept\*(C'\fR, +close that fd, and create a new dummy fd. This will gracefully refuse +clients under typical overload conditions. +.PP +The last way to handle it is to simply log the error and \f(CW\*(C`exit\*(C'\fR, as +is often done with \f(CW\*(C`malloc\*(C'\fR failures, but this results in an easy +opportunity for a DoS attack. +.PP \fIWatcher-Specific Functions\fR .IX Subsection "Watcher-Specific Functions" .IP "ev_io_init (ev_io *, callback, int fd, int events)" 4 @@ -1705,7 +1843,7 @@ attempt to read a whole line in the callback. \& ev_io stdin_readable; \& ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ); \& ev_io_start (loop, &stdin_readable); -\& ev_loop (loop, 0); +\& ev_run (loop, 0); .Ve .ie n .SS """ev_timer"" \- relative and optionally repeating timeouts" .el .SS "\f(CWev_timer\fP \- relative and optionally repeating timeouts" @@ -1724,7 +1862,7 @@ passed (not \fIat\fR, so on systems with very low-resolution clocks this might introduce a small delay). If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones of the same priority with later time-out values (but this is -no longer true when a callback calls \f(CW\*(C`ev_loop\*(C'\fR recursively). +no longer true when a callback calls \f(CW\*(C`ev_run\*(C'\fR recursively). .PP \fIBe smart about timeouts\fR .IX Subsection "Be smart about timeouts" @@ -1828,7 +1966,7 @@ within the callback: \& // if last_activity + 60. is older than now, we did time out \& if (timeout < now) \& { -\& // timeout occured, take action +\& // timeout occurred, take action \& } \& else \& { @@ -1862,14 +2000,14 @@ callback, which will \*(L"do the right thing\*(R" and start the timer: .Vb 3 \& ev_init (timer, callback); \& last_activity = ev_now (loop); -\& callback (loop, timer, EV_TIMEOUT); +\& callback (loop, timer, EV_TIMER); .Ve .Sp And when there is some activity, simply store the current time in \&\f(CW\*(C`last_activity\*(C'\fR, no libev calls at all: .Sp .Vb 1 -\& last_actiivty = ev_now (loop); +\& last_activity = ev_now (loop); .Ve .Sp This technique is slightly more complex, but in most cases where the @@ -1916,7 +2054,7 @@ overkill :) .PP Establishing the current time is a costly operation (it usually takes at least two system calls): \s-1EV\s0 therefore updates its idea of the current -time only before and after \f(CW\*(C`ev_loop\*(C'\fR collects new events, which causes a +time only before and after \f(CW\*(C`ev_run\*(C'\fR collects new events, which causes a growing difference between \f(CW\*(C`ev_now ()\*(C'\fR and \f(CW\*(C`ev_time ()\*(C'\fR when handling lots of events in one iteration. .PP @@ -2005,7 +2143,7 @@ then this time is relative to the current event loop time, otherwise it's the timeout value currently configured. .Sp That is, after an \f(CW\*(C`ev_timer_set (w, 5, 7)\*(C'\fR, \f(CW\*(C`ev_timer_remaining\*(C'\fR returns -\&\f(CW5\fR. When the timer is started and one second passes, \f(CW\*(C`ev_timer_remain\*(C'\fR +\&\f(CW5\fR. When the timer is started and one second passes, \f(CW\*(C`ev_timer_remaining\*(C'\fR will return \f(CW4\fR. When the timer expires and is restarted, it will return roughly \f(CW7\fR (likely slightly less as callback invocation takes some time, too), and so on. @@ -2045,7 +2183,7 @@ inactivity. \& ev_timer mytimer; \& ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */ \& ev_timer_again (&mytimer); /* start timer */ -\& ev_loop (loop, 0); +\& ev_run (loop, 0); \& \& // and in some piece of code that gets executed on any "activity": \& // reset the timeout to start ticking again at 10 seconds @@ -2081,7 +2219,7 @@ As with timers, the callback is guaranteed to be invoked only when the point in time where it is supposed to trigger has passed. If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones with later time-out values -(but this is no longer true when a callback calls \f(CW\*(C`ev_loop\*(C'\fR recursively). +(but this is no longer true when a callback calls \f(CW\*(C`ev_run\*(C'\fR recursively). .PP \fIWatcher-Specific Functions and Data Members\fR .IX Subsection "Watcher-Specific Functions and Data Members" @@ -2218,7 +2356,7 @@ potentially a lot of jitter, but good long-term stability. .PP .Vb 5 \& static void -\& clock_cb (struct ev_loop *loop, ev_io *w, int revents) +\& clock_cb (struct ev_loop *loop, ev_periodic *w, int revents) \& { \& ... its now a full hour (UTC, or TAI or whatever your clock follows) \& } @@ -2332,7 +2470,7 @@ Example: Try to exit cleanly on \s-1SIGINT\s0. \& static void \& sigint_cb (struct ev_loop *loop, ev_signal *w, int revents) \& { -\& ev_unloop (loop, EVUNLOOP_ALL); +\& ev_break (loop, EVBREAK_ALL); \& } \& \& ev_signal signal_watcher; @@ -2727,7 +2865,7 @@ Prepare and check watchers are usually (but not always) used in pairs: prepare watchers get invoked before the process blocks and check watchers afterwards. .PP -You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter +You \fImust not\fR call \f(CW\*(C`ev_run\*(C'\fR or similar functions that enter the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR watchers. Other loops than the current one are fine, however. The rationale behind this is that you do not need to check for recursion in @@ -2909,7 +3047,7 @@ libglib event loop. \& // create/start timer \& \& // poll -\& ev_loop (EV_A_ 0); +\& ev_run (EV_A_ 0); \& \& // stop timer again \& if (timeout >= 0) @@ -2997,7 +3135,7 @@ if you do not want that, you need to temporarily stop the embed watcher). .IP "ev_embed_sweep (loop, ev_embed *)" 4 .IX Item "ev_embed_sweep (loop, ev_embed *)" Make a single, non-blocking sweep over the embedded loop. This works -similarly to \f(CW\*(C`ev_loop (embedded_loop, EVLOOP_NONBLOCK)\*(C'\fR, but in the most +similarly to \f(CW\*(C`ev_run (embedded_loop, EVRUN_NOWAIT)\*(C'\fR, but in the most appropriate way for embedded loops. .IP "struct ev_loop *other [read\-only]" 4 .IX Item "struct ev_loop *other [read-only]" @@ -3069,7 +3207,7 @@ handlers will be invoked, too, of course. \fIThe special problem of life after fork \- how is it possible?\fR .IX Subsection "The special problem of life after fork - how is it possible?" .PP -Most uses of \f(CW\*(C`fork()\*(C'\fR consist of forking, then some simple calls to ste +Most uses of \f(CW\*(C`fork()\*(C'\fR consist of forking, then some simple calls to set up/change the process environment, followed by a call to \f(CW\*(C`exec()\*(C'\fR. This sequence should be handled by libev without any problems. .PP @@ -3095,30 +3233,67 @@ signal watchers). .PP When this is not possible, or you want to use the default loop for other reasons, then in the process that wants to start \*(L"fresh\*(R", call -\&\f(CW\*(C`ev_default_destroy ()\*(C'\fR followed by \f(CW\*(C`ev_default_loop (...)\*(C'\fR. Destroying -the default loop will \*(L"orphan\*(R" (not stop) all registered watchers, so you -have to be careful not to execute code that modifies those watchers. Note -also that in that case, you have to re-register any signal watchers. +\&\f(CW\*(C`ev_loop_destroy (EV_DEFAULT)\*(C'\fR followed by \f(CW\*(C`ev_default_loop (...)\*(C'\fR. +Destroying the default loop will \*(L"orphan\*(R" (not stop) all registered +watchers, so you have to be careful not to execute code that modifies +those watchers. Note also that in that case, you have to re-register any +signal watchers. .PP \fIWatcher-Specific Functions and Data Members\fR .IX Subsection "Watcher-Specific Functions and Data Members" -.IP "ev_fork_init (ev_signal *, callback)" 4 -.IX Item "ev_fork_init (ev_signal *, callback)" +.IP "ev_fork_init (ev_fork *, callback)" 4 +.IX Item "ev_fork_init (ev_fork *, callback)" Initialises and configures the fork watcher \- it has no parameters of any kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless, -believe me. -.ie n .SS """ev_async"" \- how to wake up another event loop" -.el .SS "\f(CWev_async\fP \- how to wake up another event loop" -.IX Subsection "ev_async - how to wake up another event loop" -In general, you cannot use an \f(CW\*(C`ev_loop\*(C'\fR from multiple threads or other +really. +.ie n .SS """ev_cleanup"" \- even the best things end" +.el .SS "\f(CWev_cleanup\fP \- even the best things end" +.IX Subsection "ev_cleanup - even the best things end" +Cleanup watchers are called just before the event loop is being destroyed +by a call to \f(CW\*(C`ev_loop_destroy\*(C'\fR. +.PP +While there is no guarantee that the event loop gets destroyed, cleanup +watchers provide a convenient method to install cleanup hooks for your +program, worker threads and so on \- you just to make sure to destroy the +loop when you want them to be invoked. +.PP +Cleanup watchers are invoked in the same way as any other watcher. Unlike +all other watchers, they do not keep a reference to the event loop (which +makes a lot of sense if you think about it). Like all other watchers, you +can call libev functions in the callback, except \f(CW\*(C`ev_cleanup_start\*(C'\fR. +.PP +\fIWatcher-Specific Functions and Data Members\fR +.IX Subsection "Watcher-Specific Functions and Data Members" +.IP "ev_cleanup_init (ev_cleanup *, callback)" 4 +.IX Item "ev_cleanup_init (ev_cleanup *, callback)" +Initialises and configures the cleanup watcher \- it has no parameters of +any kind. There is a \f(CW\*(C`ev_cleanup_set\*(C'\fR macro, but using it is utterly +pointless, I assure you. +.PP +Example: Register an atexit handler to destroy the default loop, so any +cleanup functions are called. +.PP +.Vb 5 +\& static void +\& program_exits (void) +\& { +\& ev_loop_destroy (EV_DEFAULT_UC); +\& } +\& +\& ... +\& atexit (program_exits); +.Ve +.ie n .SS """ev_async"" \- how to wake up an event loop" +.el .SS "\f(CWev_async\fP \- how to wake up an event loop" +.IX Subsection "ev_async - how to wake up an event loop" +In general, you cannot use an \f(CW\*(C`ev_run\*(C'\fR from multiple threads or other asynchronous sources such as signal handlers (as opposed to multiple event loops \- those are of course safe to use in different threads). .PP -Sometimes, however, you need to wake up another event loop you do not -control, for example because it belongs to another thread. This is what -\&\f(CW\*(C`ev_async\*(C'\fR watchers do: as long as the \f(CW\*(C`ev_async\*(C'\fR watcher is active, you -can signal it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal -safe. +Sometimes, however, you need to wake up an event loop you do not control, +for example because it belongs to another thread. This is what \f(CW\*(C`ev_async\*(C'\fR +watchers do: as long as the \f(CW\*(C`ev_async\*(C'\fR watcher is active, you can signal +it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal safe. .PP This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals, too, are asynchronous in nature, and signals, too, will be compressed @@ -3270,9 +3445,9 @@ If \f(CW\*(C`timeout\*(C'\fR is less than 0, then no timeout watcher will be started. Otherwise an \f(CW\*(C`ev_timer\*(C'\fR watcher with after = \f(CW\*(C`timeout\*(C'\fR (and repeat = 0) will be started. \f(CW0\fR is a valid timeout. .Sp -The callback has the type \f(CW\*(C`void (*cb)(int revents, void *arg)\*(C'\fR and gets +The callback has the type \f(CW\*(C`void (*cb)(int revents, void *arg)\*(C'\fR and is passed an \f(CW\*(C`revents\*(C'\fR set like normal event callbacks (a combination of -\&\f(CW\*(C`EV_ERROR\*(C'\fR, \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or \f(CW\*(C`EV_TIMEOUT\*(C'\fR) and the \f(CW\*(C`arg\*(C'\fR +\&\f(CW\*(C`EV_ERROR\*(C'\fR, \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or \f(CW\*(C`EV_TIMER\*(C'\fR) and the \f(CW\*(C`arg\*(C'\fR value passed to \f(CW\*(C`ev_once\*(C'\fR. Note that it is possible to receive \fIboth\fR a timeout and an io event at the same time \- you probably should give io events precedence. @@ -3284,7 +3459,7 @@ Example: wait up to ten seconds for data to appear on \s-1STDIN_FILENO\s0. \& { \& if (revents & EV_READ) \& /* stdin might have data for us, joy! */; -\& else if (revents & EV_TIMEOUT) +\& else if (revents & EV_TIMER) \& /* doh, nothing entered */; \& } \& @@ -3419,8 +3594,6 @@ Example: simple class declaration and watcher initialisation .Ve .IP "w\->set (object *)" 4 .IX Item "w->set (object *)" -This is an \fBexperimental\fR feature that might go away in a future version. -.Sp This is a variation of a method callback \- leaving out the method to call will default the method to \f(CW\*(C`operator ()\*(C'\fR, which makes it possible to use functor objects without having to manually specify the \f(CW\*(C`operator ()\*(C'\fR all @@ -3470,14 +3643,19 @@ Associates a different \f(CW\*(C`struct ev_loop\*(C'\fR with this watcher. You c do this when the watcher is inactive (and not pending either). .IP "w\->set ([arguments])" 4 .IX Item "w->set ([arguments])" -Basically the same as \f(CW\*(C`ev_TYPE_set\*(C'\fR, with the same arguments. Must be -called at least once. Unlike the C counterpart, an active watcher gets -automatically stopped and restarted when reconfiguring it with this -method. +Basically the same as \f(CW\*(C`ev_TYPE_set\*(C'\fR, with the same arguments. Either this +method or a suitable start method must be called at least once. Unlike the +C counterpart, an active watcher gets automatically stopped and restarted +when reconfiguring it with this method. .IP "w\->start ()" 4 .IX Item "w->start ()" Starts the watcher. Note that there is no \f(CW\*(C`loop\*(C'\fR argument, as the constructor already stores the event loop. +.IP "w\->start ([arguments])" 4 +.IX Item "w->start ([arguments])" +Instead of calling \f(CW\*(C`set\*(C'\fR and \f(CW\*(C`start\*(C'\fR methods separately, it is often +convenient to wrap them in one call. Uses the same type of arguments as +the configure \f(CW\*(C`set\*(C'\fR method of the watcher. .IP "w\->stop ()" 4 .IX Item "w->stop ()" Stops the watcher if it is active. Again, no \f(CW\*(C`loop\*(C'\fR argument. @@ -3498,21 +3676,26 @@ Invokes \f(CW\*(C`ev_stat_stat\*(C'\fR. .RS 4 .RE .PP -Example: Define a class with an \s-1IO\s0 and idle watcher, start one of them in -the constructor. +Example: Define a class with two I/O and idle watchers, start the I/O +watchers in the constructor. .PP -.Vb 4 +.Vb 5 \& class myclass \& { \& ev::io io ; void io_cb (ev::io &w, int revents); +\& ev::io2 io2 ; void io2_cb (ev::io &w, int revents); \& ev::idle idle; void idle_cb (ev::idle &w, int revents); \& \& myclass (int fd) \& { \& io .set (this); +\& io2 .set (this); \& idle.set (this); \& -\& io.start (fd, ev::READ); +\& io.set (fd, ev::WRITE); // configure the watcher +\& io.start (); // start it whenever convenient +\& +\& io2.start (fd, ev::READ); // set + start in one call \& } \& }; .Ve @@ -3560,8 +3743,8 @@ Erkki Seppala has written Ocaml bindings for libev, to be found at . .IP "Lua" 4 .IX Item "Lua" -Brian Maher has written a partial interface to libev -for lua (only \f(CW\*(C`ev_io\*(C'\fR and \f(CW\*(C`ev_timer\*(C'\fR), to be found at +Brian Maher has written a partial interface to libev for lua (at the +time of this writing, only \f(CW\*(C`ev_io\*(C'\fR and \f(CW\*(C`ev_timer\*(C'\fR), to be found at . .SH "MACRO MAGIC" .IX Header "MACRO MAGIC" @@ -3581,7 +3764,7 @@ loop argument\*(R"). The \f(CW\*(C`EV_A\*(C'\fR form is used when this is the so .Vb 3 \& ev_unref (EV_A); \& ev_timer_add (EV_A_ watcher); -\& ev_loop (EV_A_ 0); +\& ev_run (EV_A_ 0); .Ve .Sp It assumes the variable \f(CW\*(C`loop\*(C'\fR of type \f(CW\*(C`struct ev_loop *\*(C'\fR is in scope, @@ -3633,7 +3816,7 @@ or not. \& ev_check check; \& ev_check_init (&check, check_cb); \& ev_check_start (EV_DEFAULT_ &check); -\& ev_loop (EV_DEFAULT_ 0); +\& ev_run (EV_DEFAULT_ 0); .Ve .SH "EMBEDDING" .IX Header "EMBEDDING" @@ -3738,10 +3921,32 @@ For this of course you need the m4 file: .SS "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" .IX Subsection "PREPROCESSOR SYMBOLS/MACROS" Libev can be configured via a variety of preprocessor symbols you have to -define before including any of its files. The default in the absence of -autoconf is documented for every option. -.IP "\s-1EV_STANDALONE\s0" 4 -.IX Item "EV_STANDALONE" +define before including (or compiling) any of its files. The default in +the absence of autoconf is documented for every option. +.PP +Symbols marked with \*(L"(h)\*(R" do not change the \s-1ABI\s0, and can have different +values when compiling libev vs. including \fIev.h\fR, so it is permissible +to redefine them before including \fIev.h\fR without breaking compatibility +to a compiled library. All other symbols change the \s-1ABI\s0, which means all +users of libev and the libev code itself must be compiled with compatible +settings. +.IP "\s-1EV_COMPAT3\s0 (h)" 4 +.IX Item "EV_COMPAT3 (h)" +Backwards compatibility is a major concern for libev. This is why this +release of libev comes with wrappers for the functions and symbols that +have been renamed between libev version 3 and 4. +.Sp +You can disable these wrappers (to test compatibility with future +versions) by defining \f(CW\*(C`EV_COMPAT3\*(C'\fR to \f(CW0\fR when compiling your +sources. This has the additional advantage that you can drop the \f(CW\*(C`struct\*(C'\fR +from \f(CW\*(C`struct ev_loop\*(C'\fR declarations, as libev will provide an \f(CW\*(C`ev_loop\*(C'\fR +typedef in that case. +.Sp +In some future version, the default for \f(CW\*(C`EV_COMPAT3\*(C'\fR will become \f(CW0\fR, +and in some even more future version the compatibility code will be +removed completely. +.IP "\s-1EV_STANDALONE\s0 (h)" 4 +.IX Item "EV_STANDALONE (h)" Must always be \f(CW1\fR if you do not use autoconf configuration, which keeps libev from including \fIconfig.h\fR, and it also defines dummy implementations for some libevent functions (such as logging, which is not @@ -3881,22 +4086,22 @@ as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR watchers. .Sp In the absence of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (from \fIsignal.h\fR), which is usually good enough on most platforms. -.IP "\s-1EV_H\s0" 4 -.IX Item "EV_H" +.IP "\s-1EV_H\s0 (h)" 4 +.IX Item "EV_H (h)" The name of the \fIev.h\fR header file used to include it. The default if undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be used to virtually rename the \fIev.h\fR header file in case of conflicts. -.IP "\s-1EV_CONFIG_H\s0" 4 -.IX Item "EV_CONFIG_H" +.IP "\s-1EV_CONFIG_H\s0 (h)" 4 +.IX Item "EV_CONFIG_H (h)" If \f(CW\*(C`EV_STANDALONE\*(C'\fR isn't \f(CW1\fR, this variable can be used to override \&\fIev.c\fR's idea of where to find the \fIconfig.h\fR file, similarly to \&\f(CW\*(C`EV_H\*(C'\fR, above. -.IP "\s-1EV_EVENT_H\s0" 4 -.IX Item "EV_EVENT_H" +.IP "\s-1EV_EVENT_H\s0 (h)" 4 +.IX Item "EV_EVENT_H (h)" Similarly to \f(CW\*(C`EV_H\*(C'\fR, this macro can be used to override \fIevent.c\fR's idea of how the \fIevent.h\fR header can be found, the default is \f(CW"event.h"\fR. -.IP "\s-1EV_PROTOTYPES\s0" 4 -.IX Item "EV_PROTOTYPES" +.IP "\s-1EV_PROTOTYPES\s0 (h)" 4 +.IX Item "EV_PROTOTYPES (h)" If defined to be \f(CW0\fR, then \fIev.h\fR will not define any function prototypes, but still define all the structs and other symbols. This is occasionally useful if you want to provide your own wrapper functions @@ -3926,70 +4131,125 @@ fine. .Sp If your embedding application does not need any priorities, defining these both to \f(CW0\fR will save some memory and \s-1CPU\s0. -.IP "\s-1EV_PERIODIC_ENABLE\s0" 4 -.IX Item "EV_PERIODIC_ENABLE" -If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If -defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of -code. -.IP "\s-1EV_IDLE_ENABLE\s0" 4 -.IX Item "EV_IDLE_ENABLE" -If undefined or defined to be \f(CW1\fR, then idle watchers are supported. If -defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of -code. -.IP "\s-1EV_EMBED_ENABLE\s0" 4 -.IX Item "EV_EMBED_ENABLE" -If undefined or defined to be \f(CW1\fR, then embed watchers are supported. If -defined to be \f(CW0\fR, then they are not. Embed watchers rely on most other -watcher types, which therefore must not be disabled. -.IP "\s-1EV_STAT_ENABLE\s0" 4 -.IX Item "EV_STAT_ENABLE" -If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If -defined to be \f(CW0\fR, then they are not. -.IP "\s-1EV_FORK_ENABLE\s0" 4 -.IX Item "EV_FORK_ENABLE" -If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If -defined to be \f(CW0\fR, then they are not. -.IP "\s-1EV_ASYNC_ENABLE\s0" 4 -.IX Item "EV_ASYNC_ENABLE" -If undefined or defined to be \f(CW1\fR, then async watchers are supported. If -defined to be \f(CW0\fR, then they are not. -.IP "\s-1EV_MINIMAL\s0" 4 -.IX Item "EV_MINIMAL" +.IP "\s-1EV_PERIODIC_ENABLE\s0, \s-1EV_IDLE_ENABLE\s0, \s-1EV_EMBED_ENABLE\s0, \s-1EV_STAT_ENABLE\s0, \s-1EV_PREPARE_ENABLE\s0, \s-1EV_CHECK_ENABLE\s0, \s-1EV_FORK_ENABLE\s0, \s-1EV_SIGNAL_ENABLE\s0, \s-1EV_ASYNC_ENABLE\s0, \s-1EV_CHILD_ENABLE\s0." 4 +.IX Item "EV_PERIODIC_ENABLE, EV_IDLE_ENABLE, EV_EMBED_ENABLE, EV_STAT_ENABLE, EV_PREPARE_ENABLE, EV_CHECK_ENABLE, EV_FORK_ENABLE, EV_SIGNAL_ENABLE, EV_ASYNC_ENABLE, EV_CHILD_ENABLE." +If undefined or defined to be \f(CW1\fR (and the platform supports it), then +the respective watcher type is supported. If defined to be \f(CW0\fR, then it +is not. Disabling watcher types mainly saves code size. +.IP "\s-1EV_FEATURES\s0" 4 +.IX Item "EV_FEATURES" If you need to shave off some kilobytes of code at the expense of some -speed (but with the full \s-1API\s0), define this symbol to \f(CW1\fR. Currently this -is used to override some inlining decisions, saves roughly 30% code size -on amd64. It also selects a much smaller 2\-heap for timer management over -the default 4\-heap. -.Sp -You can save even more by disabling watcher types you do not need -and setting \f(CW\*(C`EV_MAXPRI\*(C'\fR == \f(CW\*(C`EV_MINPRI\*(C'\fR. Also, disabling \f(CW\*(C`assert\*(C'\fR -(\f(CW\*(C`\-DNDEBUG\*(C'\fR) will usually reduce code size a lot. -.Sp -Defining \f(CW\*(C`EV_MINIMAL\*(C'\fR to \f(CW2\fR will additionally reduce the core \s-1API\s0 to -provide a bare-bones event library. See \f(CW\*(C`ev.h\*(C'\fR for details on what parts -of the \s-1API\s0 are still available, and do not complain if this subset changes -over time. +speed (but with the full \s-1API\s0), you can define this symbol to request +certain subsets of functionality. The default is to enable all features +that can be enabled on the platform. +.Sp +A typical way to use this symbol is to define it to \f(CW0\fR (or to a bitset +with some broad features you want) and then selectively re-enable +additional parts you want, for example if you want everything minimal, +but multiple event loop support, async and child watchers and the poll +backend, use this: +.Sp +.Vb 5 +\& #define EV_FEATURES 0 +\& #define EV_MULTIPLICITY 1 +\& #define EV_USE_POLL 1 +\& #define EV_CHILD_ENABLE 1 +\& #define EV_ASYNC_ENABLE 1 +.Ve +.Sp +The actual value is a bitset, it can be a combination of the following +values: +.RS 4 +.ie n .IP "1 \- faster/larger code" 4 +.el .IP "\f(CW1\fR \- faster/larger code" 4 +.IX Item "1 - faster/larger code" +Use larger code to speed up some operations. +.Sp +Currently this is used to override some inlining decisions (enlarging the +code size by roughly 30% on amd64). +.Sp +When optimising for size, use of compiler flags such as \f(CW\*(C`\-Os\*(C'\fR with +gcc is recommended, as well as \f(CW\*(C`\-DNDEBUG\*(C'\fR, as libev contains a number of +assertions. +.ie n .IP "2 \- faster/larger data structures" 4 +.el .IP "\f(CW2\fR \- faster/larger data structures" 4 +.IX Item "2 - faster/larger data structures" +Replaces the small 2\-heap for timer management by a faster 4\-heap, larger +hash table sizes and so on. This will usually further increase code size +and can additionally have an effect on the size of data structures at +runtime. +.ie n .IP "4 \- full \s-1API\s0 configuration" 4 +.el .IP "\f(CW4\fR \- full \s-1API\s0 configuration" 4 +.IX Item "4 - full API configuration" +This enables priorities (sets \f(CW\*(C`EV_MAXPRI\*(C'\fR=2 and \f(CW\*(C`EV_MINPRI\*(C'\fR=\-2), and +enables multiplicity (\f(CW\*(C`EV_MULTIPLICITY\*(C'\fR=1). +.ie n .IP "8 \- full \s-1API\s0" 4 +.el .IP "\f(CW8\fR \- full \s-1API\s0" 4 +.IX Item "8 - full API" +This enables a lot of the \*(L"lesser used\*(R" \s-1API\s0 functions. See \f(CW\*(C`ev.h\*(C'\fR for +details on which parts of the \s-1API\s0 are still available without this +feature, and do not complain if this subset changes over time. +.ie n .IP "16 \- enable all optional watcher types" 4 +.el .IP "\f(CW16\fR \- enable all optional watcher types" 4 +.IX Item "16 - enable all optional watcher types" +Enables all optional watcher types. If you want to selectively enable +only some watcher types other than I/O and timers (e.g. prepare, +embed, async, child...) you can enable them manually by defining +\&\f(CW\*(C`EV_watchertype_ENABLE\*(C'\fR to \f(CW1\fR instead. +.ie n .IP "32 \- enable all backends" 4 +.el .IP "\f(CW32\fR \- enable all backends" 4 +.IX Item "32 - enable all backends" +This enables all backends \- without this feature, you need to enable at +least one backend manually (\f(CW\*(C`EV_USE_SELECT\*(C'\fR is a good choice). +.ie n .IP "64 \- enable OS-specific ""helper"" APIs" 4 +.el .IP "\f(CW64\fR \- enable OS-specific ``helper'' APIs" 4 +.IX Item "64 - enable OS-specific helper APIs" +Enable inotify, eventfd, signalfd and similar OS-specific helper APIs by +default. +.RE +.RS 4 +.Sp +Compiling with \f(CW\*(C`gcc \-Os \-DEV_STANDALONE \-DEV_USE_EPOLL=1 \-DEV_FEATURES=0\*(C'\fR +reduces the compiled size of libev from 24.7Kb code/2.8Kb data to 6.5Kb +code/0.3Kb data on my GNU/Linux amd64 system, while still giving you I/O +watchers, timers and monotonic clock support. +.Sp +With an intelligent-enough linker (gcc+binutils are intelligent enough +when you use \f(CW\*(C`\-Wl,\-\-gc\-sections \-ffunction\-sections\*(C'\fR) functions unused by +your program might be left out as well \- a binary starting a timer and an +I/O watcher then might come out at only 5Kb. +.RE +.IP "\s-1EV_AVOID_STDIO\s0" 4 +.IX Item "EV_AVOID_STDIO" +If this is set to \f(CW1\fR at compiletime, then libev will avoid using stdio +functions (printf, scanf, perror etc.). This will increase the code size +somewhat, but if your program doesn't otherwise depend on stdio and your +libc allows it, this avoids linking in the stdio library which is quite +big. +.Sp +Note that error messages might become less precise when this option is +enabled. .IP "\s-1EV_NSIG\s0" 4 .IX Item "EV_NSIG" The highest supported signal number, +1 (or, the number of signals): Normally, libev tries to deduce the maximum number of signals automatically, but sometimes this fails, in which case it can be specified. Also, using a lower number than detected (\f(CW32\fR should be -good for about any system in existance) can save some memory, as libev +good for about any system in existence) can save some memory, as libev statically allocates some 12\-24 bytes per signal number. .IP "\s-1EV_PID_HASHSIZE\s0" 4 .IX Item "EV_PID_HASHSIZE" \&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by -pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more -than enough. If you need to manage thousands of children you might want to -increase this value (\fImust\fR be a power of two). +pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_FEATURES\*(C'\fR disabled), +usually more than enough. If you need to manage thousands of children you +might want to increase this value (\fImust\fR be a power of two). .IP "\s-1EV_INOTIFY_HASHSIZE\s0" 4 .IX Item "EV_INOTIFY_HASHSIZE" \&\f(CW\*(C`ev_stat\*(C'\fR watchers use a small hash table to distribute workload by -inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), -usually more than enough. If you need to manage thousands of \f(CW\*(C`ev_stat\*(C'\fR -watchers you might want to increase this value (\fImust\fR be a power of -two). +inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_FEATURES\*(C'\fR +disabled), usually more than enough. If you need to manage thousands of +\&\f(CW\*(C`ev_stat\*(C'\fR watchers you might want to increase this value (\fImust\fR be a +power of two). .IP "\s-1EV_USE_4HEAP\s0" 4 .IX Item "EV_USE_4HEAP" Heaps are not very cache-efficient. To improve the cache-efficiency of the @@ -3997,8 +4257,8 @@ timer and periodics heaps, libev uses a 4\-heap when this symbol is defined to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has noticeably faster performance with many (thousands) of watchers. .Sp -The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR -(disabled). +The default is \f(CW1\fR, unless \f(CW\*(C`EV_FEATURES\*(C'\fR overrides it, in which case it +will be \f(CW0\fR. .IP "\s-1EV_HEAP_CACHE_AT\s0" 4 .IX Item "EV_HEAP_CACHE_AT" Heaps are not very cache-efficient. To improve the cache-efficiency of the @@ -4008,11 +4268,11 @@ which uses 8\-12 bytes more per watcher and a few hundred bytes more code, but avoids random read accesses on heap changes. This improves performance noticeably with many (hundreds) of watchers. .Sp -The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR -(disabled). +The default is \f(CW1\fR, unless \f(CW\*(C`EV_FEATURES\*(C'\fR overrides it, in which case it +will be \f(CW0\fR. .IP "\s-1EV_VERIFY\s0" 4 .IX Item "EV_VERIFY" -Controls how much internal verification (see \f(CW\*(C`ev_loop_verify ()\*(C'\fR) will +Controls how much internal verification (see \f(CW\*(C`ev_verify ()\*(C'\fR) will be done: If set to \f(CW0\fR, no internal verification code will be compiled in. If set to \f(CW1\fR, then verification code will be compiled in, but not called. If set to \f(CW2\fR, then the internal verification code will be @@ -4020,12 +4280,12 @@ called once per loop, which can slow down libev. If set to \f(CW3\fR, then the verification code will be called very frequently, which will slow down libev considerably. .Sp -The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be -\&\f(CW0\fR. +The default is \f(CW1\fR, unless \f(CW\*(C`EV_FEATURES\*(C'\fR overrides it, in which case it +will be \f(CW0\fR. .IP "\s-1EV_COMMON\s0" 4 .IX Item "EV_COMMON" By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining -this macro to a something else you can include more and other types of +this macro to something else you can include more and other types of members. You have to define it each time you include one of the files, though, and it must be identical each time. .Sp @@ -4093,16 +4353,15 @@ file. The usage in rxvt-unicode is simpler. It has a \fIev_cpp.h\fR header file that everybody includes and which overrides some configure choices: .PP -.Vb 9 -\& #define EV_MINIMAL 1 -\& #define EV_USE_POLL 0 -\& #define EV_MULTIPLICITY 0 -\& #define EV_PERIODIC_ENABLE 0 -\& #define EV_STAT_ENABLE 0 -\& #define EV_FORK_ENABLE 0 +.Vb 8 +\& #define EV_FEATURES 8 +\& #define EV_USE_SELECT 1 +\& #define EV_PREPARE_ENABLE 1 +\& #define EV_IDLE_ENABLE 1 +\& #define EV_SIGNAL_ENABLE 1 +\& #define EV_CHILD_ENABLE 1 +\& #define EV_USE_STDEXCEPT 0 \& #define EV_CONFIG_H -\& #define EV_MINPRI 0 -\& #define EV_MAXPRI 0 \& \& #include "ev++.h" .Ve @@ -4249,7 +4508,7 @@ protecting the loop data, respectively. .Ve .PP The event loop thread first acquires the mutex, and then jumps straight -into \f(CW\*(C`ev_loop\*(C'\fR: +into \f(CW\*(C`ev_run\*(C'\fR: .PP .Vb 4 \& void * @@ -4259,7 +4518,7 @@ into \f(CW\*(C`ev_loop\*(C'\fR: \& \& l_acquire (EV_A); \& pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); -\& ev_loop (EV_A_ 0); +\& ev_run (EV_A_ 0); \& l_release (EV_A); \& \& return 0; @@ -4329,13 +4588,13 @@ watchers in the next event loop iteration. .PP Libev is very accommodating to coroutines (\*(L"cooperative threads\*(R"): libev fully supports nesting calls to its functions from different -coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two +coroutines (e.g. you can call \f(CW\*(C`ev_run\*(C'\fR on the same loop from two different coroutines, and switch freely between both coroutines running the loop, as long as you don't confuse yourself). The only exception is that you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks. .PP Care has been taken to ensure that libev does not keep local state inside -\&\f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow for coroutine switches as +\&\f(CW\*(C`ev_run\*(C'\fR, and other calls do not usually allow for coroutine switches as they do not call any callbacks. .SS "\s-1COMPILER\s0 \s-1WARNINGS\s0" .IX Subsection "COMPILER WARNINGS" @@ -4355,7 +4614,7 @@ maintainable. And of course, some compiler warnings are just plain stupid, or simply wrong (because they don't actually warn about the condition their message seems to warn about). For example, certain older gcc versions had some -warnings that resulted an extreme number of false positives. These have +warnings that resulted in an extreme number of false positives. These have been fixed, but some people still insist on making code warn-free with such buggy versions. .PP @@ -4399,19 +4658,111 @@ If you need, for some reason, empty reports from valgrind for your project I suggest using suppression lists. .SH "PORTABILITY NOTES" .IX Header "PORTABILITY NOTES" +.SS "\s-1GNU/LINUX\s0 32 \s-1BIT\s0 \s-1LIMITATIONS\s0" +.IX Subsection "GNU/LINUX 32 BIT LIMITATIONS" +GNU/Linux is the only common platform that supports 64 bit file/large file +interfaces but \fIdisables\fR them by default. +.PP +That means that libev compiled in the default environment doesn't support +files larger than 2GiB or so, which mainly affects \f(CW\*(C`ev_stat\*(C'\fR watchers. +.PP +Unfortunately, many programs try to work around this GNU/Linux issue +by enabling the large file \s-1API\s0, which makes them incompatible with the +standard libev compiled for their system. +.PP +Likewise, libev cannot enable the large file \s-1API\s0 itself as this would +suddenly make it incompatible to the default compile time environment, +i.e. all programs not using special compile switches. +.SS "\s-1OS/X\s0 \s-1AND\s0 \s-1DARWIN\s0 \s-1BUGS\s0" +.IX Subsection "OS/X AND DARWIN BUGS" +The whole thing is a bug if you ask me \- basically any system interface +you touch is broken, whether it is locales, poll, kqueue or even the +OpenGL drivers. +.PP +\fI\f(CI\*(C`kqueue\*(C'\fI is buggy\fR +.IX Subsection "kqueue is buggy" +.PP +The kqueue syscall is broken in all known versions \- most versions support +only sockets, many support pipes. +.PP +Libev tries to work around this by not using \f(CW\*(C`kqueue\*(C'\fR by default on this +rotten platform, but of course you can still ask for it when creating a +loop \- embedding a socket-only kqueue loop into a select-based one is +probably going to work well. +.PP +\fI\f(CI\*(C`poll\*(C'\fI is buggy\fR +.IX Subsection "poll is buggy" +.PP +Instead of fixing \f(CW\*(C`kqueue\*(C'\fR, Apple replaced their (working) \f(CW\*(C`poll\*(C'\fR +implementation by something calling \f(CW\*(C`kqueue\*(C'\fR internally around the 10.5.6 +release, so now \f(CW\*(C`kqueue\*(C'\fR \fIand\fR \f(CW\*(C`poll\*(C'\fR are broken. +.PP +Libev tries to work around this by not using \f(CW\*(C`poll\*(C'\fR by default on +this rotten platform, but of course you can still ask for it when creating +a loop. +.PP +\fI\f(CI\*(C`select\*(C'\fI is buggy\fR +.IX Subsection "select is buggy" +.PP +All that's left is \f(CW\*(C`select\*(C'\fR, and of course Apple found a way to fuck this +one up as well: On \s-1OS/X\s0, \f(CW\*(C`select\*(C'\fR actively limits the number of file +descriptors you can pass in to 1024 \- your program suddenly crashes when +you use more. +.PP +There is an undocumented \*(L"workaround\*(R" for this \- defining +\&\f(CW\*(C`_DARWIN_UNLIMITED_SELECT\*(C'\fR, which libev tries to use, so select \fIshould\fR +work on \s-1OS/X\s0. +.SS "\s-1SOLARIS\s0 \s-1PROBLEMS\s0 \s-1AND\s0 \s-1WORKAROUNDS\s0" +.IX Subsection "SOLARIS PROBLEMS AND WORKAROUNDS" +\fI\f(CI\*(C`errno\*(C'\fI reentrancy\fR +.IX Subsection "errno reentrancy" +.PP +The default compile environment on Solaris is unfortunately so +thread-unsafe that you can't even use components/libraries compiled +without \f(CW\*(C`\-D_REENTRANT\*(C'\fR in a threaded program, which, of course, isn't +defined by default. A valid, if stupid, implementation choice. +.PP +If you want to use libev in threaded environments you have to make sure +it's compiled with \f(CW\*(C`_REENTRANT\*(C'\fR defined. +.PP +\fIEvent port backend\fR +.IX Subsection "Event port backend" +.PP +The scalable event interface for Solaris is called \*(L"event +ports\*(R". Unfortunately, this mechanism is very buggy in all major +releases. If you run into high \s-1CPU\s0 usage, your program freezes or you get +a large number of spurious wakeups, make sure you have all the relevant +and latest kernel patches applied. No, I don't know which ones, but there +are multiple ones to apply, and afterwards, event ports actually work +great. +.PP +If you can't get it to work, you can try running the program by setting +the environment variable \f(CW\*(C`LIBEV_FLAGS=3\*(C'\fR to only allow \f(CW\*(C`poll\*(C'\fR and +\&\f(CW\*(C`select\*(C'\fR backends. +.SS "\s-1AIX\s0 \s-1POLL\s0 \s-1BUG\s0" +.IX Subsection "AIX POLL BUG" +\&\s-1AIX\s0 unfortunately has a broken \f(CW\*(C`poll.h\*(C'\fR header. Libev works around +this by trying to avoid the poll backend altogether (i.e. it's not even +compiled in), which normally isn't a big problem as \f(CW\*(C`select\*(C'\fR works fine +with large bitsets on \s-1AIX\s0, and \s-1AIX\s0 is dead anyway. .SS "\s-1WIN32\s0 \s-1PLATFORM\s0 \s-1LIMITATIONS\s0 \s-1AND\s0 \s-1WORKAROUNDS\s0" .IX Subsection "WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS" +\fIGeneral issues\fR +.IX Subsection "General issues" +.PP Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0 model. Libev still offers limited functionality on this platform in the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket descriptors. This only applies when using Win32 natively, not when using -e.g. cygwin. +e.g. cygwin. Actually, it only applies to the microsofts own compilers, +as every compielr comes with a slightly differently broken/incompatible +environment. .PP Lifting these limitations would basically require the full -re-implementation of the I/O system. If you are into these kinds of -things, then note that glib does exactly that for you in a very portable -way (note also that glib is the slowest event library known to man). +re-implementation of the I/O system. If you are into this kind of thing, +then note that glib does exactly that for you in a very portable way (note +also that glib is the slowest event library known to man). .PP There is no supported compilation method available on windows except embedding it into other applications. @@ -4452,8 +4803,10 @@ you do \fInot\fR compile the \fIev.c\fR or any other embedded source files!): \& #include "evwrap.h" \& #include "ev.c" .Ve -.IP "The winsocket select function" 4 -.IX Item "The winsocket select function" +.PP +\fIThe winsocket \f(CI\*(C`select\*(C'\fI function\fR +.IX Subsection "The winsocket select function" +.PP The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it requires socket \fIhandles\fR and not socket \fIfile descriptors\fR (it is also extremely buggy). This makes select very inefficient, and also @@ -4461,32 +4814,34 @@ requires a mapping from file descriptors to socket handles (the Microsoft C runtime provides the function \f(CW\*(C`_open_osfhandle\*(C'\fR for this). See the discussion of the \f(CW\*(C`EV_SELECT_USE_FD_SET\*(C'\fR, \f(CW\*(C`EV_SELECT_IS_WINSOCKET\*(C'\fR and \&\f(CW\*(C`EV_FD_TO_WIN32_HANDLE\*(C'\fR preprocessor symbols for more info. -.Sp +.PP The configuration for a \*(L"naked\*(R" win32 using the Microsoft runtime libraries and raw winsocket select is: -.Sp +.PP .Vb 2 \& #define EV_USE_SELECT 1 \& #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */ .Ve -.Sp +.PP Note that winsockets handling of fd sets is O(n), so you can easily get a complexity in the O(nA\*^X) range when using win32. -.IP "Limited number of file descriptors" 4 -.IX Item "Limited number of file descriptors" +.PP +\fILimited number of file descriptors\fR +.IX Subsection "Limited number of file descriptors" +.PP Windows has numerous arbitrary (and low) limits on things. -.Sp +.PP Early versions of winsocket's select only supported waiting for a maximum of \f(CW64\fR handles (probably owning to the fact that all windows kernels can only wait for \f(CW64\fR things at the same time internally; Microsoft recommends spawning a chain of threads and wait for 63 handles and the previous thread in each. Sounds great!). -.Sp +.PP Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select call (which might be in libev or elsewhere, for example, perl and many other interpreters do their own select emulation on windows). -.Sp +.PP Another limit is the number of file descriptors in the Microsoft runtime libraries, which by default is \f(CW64\fR (there must be a hidden \fI64\fR fetish or something like this inside Microsoft). You can increase this @@ -4508,6 +4863,10 @@ structure (guaranteed by \s-1POSIX\s0 but not by \s-1ISO\s0 C for example), but assumes that the same (machine) code can be used to call any watcher callback: The watcher callbacks have different type signatures, but libev calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally. +.IP "pointer accesses must be thread-atomic" 4 +.IX Item "pointer accesses must be thread-atomic" +Accessing a pointer value must be atomic, it must both be readable and +writable in one piece \- this is the case on all current architectures. .ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4 .el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4 .IX Item "sig_atomic_t volatile must be thread-atomic as well" @@ -4540,11 +4899,11 @@ watchers. .el .IP "\f(CWdouble\fR must hold a time value in seconds with enough accuracy" 4 .IX Item "double must hold a time value in seconds with enough accuracy" The type \f(CW\*(C`double\*(C'\fR is used to represent timestamps. It is required to -have at least 51 bits of mantissa (and 9 bits of exponent), which is good -enough for at least into the year 4000. This requirement is fulfilled by -implementations implementing \s-1IEEE\s0 754, which is basically all existing -ones. With \s-1IEEE\s0 754 doubles, you get microsecond accuracy until at least -2200. +have at least 51 bits of mantissa (and 9 bits of exponent), which is +good enough for at least into the year 4000 with millisecond accuracy +(the design goal for libev). This requirement is overfulfilled by +implementations using \s-1IEEE\s0 754, which is basically all existing ones. With +\&\s-1IEEE\s0 754 doubles, you get microsecond accuracy until at least 2200. .PP If you know of other additional requirements drop me a note. .SH "ALGORITHMIC COMPLEXITIES" @@ -4610,22 +4969,82 @@ watchers becomes O(1) with respect to priority handling. Sending involves a system call \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR calls in the current loop iteration. Checking for async and signal events involves iterating over all running async watchers or all signal numbers. +.SH "PORTING FROM LIBEV 3.X TO 4.X" +.IX Header "PORTING FROM LIBEV 3.X TO 4.X" +The major version 4 introduced some incompatible changes to the \s-1API\s0. +.PP +At the moment, the \f(CW\*(C`ev.h\*(C'\fR header file provides compatibility definitions +for all changes, so most programs should still compile. The compatibility +layer might be removed in later versions of libev, so better update to the +new \s-1API\s0 early than late. +.ie n .IP """EV_COMPAT3"" backwards compatibility mechanism" 4 +.el .IP "\f(CWEV_COMPAT3\fR backwards compatibility mechanism" 4 +.IX Item "EV_COMPAT3 backwards compatibility mechanism" +The backward compatibility mechanism can be controlled by +\&\f(CW\*(C`EV_COMPAT3\*(C'\fR. See \*(L"\s-1MACROS\s0\*(R" in \s-1PREPROCESSOR\s0 \s-1SYMBOLS\s0 in the \s-1EMBEDDING\s0 +section. +.ie n .IP """ev_default_destroy"" and ""ev_default_fork"" have been removed" 4 +.el .IP "\f(CWev_default_destroy\fR and \f(CWev_default_fork\fR have been removed" 4 +.IX Item "ev_default_destroy and ev_default_fork have been removed" +These calls can be replaced easily by their \f(CW\*(C`ev_loop_xxx\*(C'\fR counterparts: +.Sp +.Vb 2 +\& ev_loop_destroy (EV_DEFAULT_UC); +\& ev_loop_fork (EV_DEFAULT); +.Ve +.IP "function/symbol renames" 4 +.IX Item "function/symbol renames" +A number of functions and symbols have been renamed: +.Sp +.Vb 3 +\& ev_loop => ev_run +\& EVLOOP_NONBLOCK => EVRUN_NOWAIT +\& EVLOOP_ONESHOT => EVRUN_ONCE +\& +\& ev_unloop => ev_break +\& EVUNLOOP_CANCEL => EVBREAK_CANCEL +\& EVUNLOOP_ONE => EVBREAK_ONE +\& EVUNLOOP_ALL => EVBREAK_ALL +\& +\& EV_TIMEOUT => EV_TIMER +\& +\& ev_loop_count => ev_iteration +\& ev_loop_depth => ev_depth +\& ev_loop_verify => ev_verify +.Ve +.Sp +Most functions working on \f(CW\*(C`struct ev_loop\*(C'\fR objects don't have an +\&\f(CW\*(C`ev_loop_\*(C'\fR prefix, so it was removed; \f(CW\*(C`ev_loop\*(C'\fR, \f(CW\*(C`ev_unloop\*(C'\fR and +associated constants have been renamed to not collide with the \f(CW\*(C`struct +ev_loop\*(C'\fR anymore and \f(CW\*(C`EV_TIMER\*(C'\fR now follows the same naming scheme +as all other watcher types. Note that \f(CW\*(C`ev_loop_fork\*(C'\fR is still called +\&\f(CW\*(C`ev_loop_fork\*(C'\fR because it would otherwise clash with the \f(CW\*(C`ev_fork\*(C'\fR +typedef. +.ie n .IP """EV_MINIMAL"" mechanism replaced by ""EV_FEATURES""" 4 +.el .IP "\f(CWEV_MINIMAL\fR mechanism replaced by \f(CWEV_FEATURES\fR" 4 +.IX Item "EV_MINIMAL mechanism replaced by EV_FEATURES" +The preprocessor symbol \f(CW\*(C`EV_MINIMAL\*(C'\fR has been replaced by a different +mechanism, \f(CW\*(C`EV_FEATURES\*(C'\fR. Programs using \f(CW\*(C`EV_MINIMAL\*(C'\fR usually compile +and work, but the library code will of course be larger. .SH "GLOSSARY" .IX Header "GLOSSARY" .IP "active" 4 .IX Item "active" -A watcher is active as long as it has been started (has been attached to -an event loop) but not yet stopped (disassociated from the event loop). +A watcher is active as long as it has been started and not yet stopped. +See \*(L"\s-1WATCHER\s0 \s-1STATES\s0\*(R" for details. .IP "application" 4 .IX Item "application" In this document, an application is whatever is using libev. +.IP "backend" 4 +.IX Item "backend" +The part of the code dealing with the operating system interfaces. .IP "callback" 4 .IX Item "callback" The address of a function that is called when some event has been detected. Callbacks are being passed the event loop, the watcher that received the event, and the actual event bitset. -.IP "callback invocation" 4 -.IX Item "callback invocation" +.IP "callback/watcher invocation" 4 +.IX Item "callback/watcher invocation" The act of calling the callback associated with a watcher. .IP "event" 4 .IX Item "event" @@ -4634,7 +5053,7 @@ for reading on a file descriptor, time having passed or simply not having any other events happening anymore. .Sp In libev, events are represented as single bits (such as \f(CW\*(C`EV_READ\*(C'\fR or -\&\f(CW\*(C`EV_TIMEOUT\*(C'\fR). +\&\f(CW\*(C`EV_TIMER\*(C'\fR). .IP "event library" 4 .IX Item "event library" A software package implementing an event model and loop. @@ -4648,12 +5067,8 @@ The model used to describe how an event loop handles and processes watchers and events. .IP "pending" 4 .IX Item "pending" -A watcher is pending as soon as the corresponding event has been detected, -and stops being pending as soon as the watcher will be invoked or its -pending status is explicitly cleared by the application. -.Sp -A watcher can be pending, but not active. Stopping a watcher also clears -its pending status. +A watcher is pending as soon as the corresponding event has been +detected. See \*(L"\s-1WATCHER\s0 \s-1STATES\s0\*(R" for details. .IP "real time" 4 .IX Item "real time" The physical time that is observed. It is apparently strictly monotonic :) @@ -4666,9 +5081,7 @@ clock. .IX Item "watcher" A data structure that describes interest in certain events. Watchers need to be started (attached to an event loop) before they can receive events. -.IP "watcher invocation" 4 -.IX Item "watcher invocation" -The act of calling the callback associated with a watcher. .SH "AUTHOR" .IX Header "AUTHOR" -Marc Lehmann , with repeated corrections by Mikael Magnusson. +Marc Lehmann , with repeated corrections by Mikael +Magnusson and Emanuele Giaquinta. -- cgit v1.2.3