--- libev/ev.pod 2009/07/08 04:14:34 1.248 +++ libev/ev.pod 2010/11/10 19:50:09 1.347 @@ -28,8 +28,8 @@ // with its corresponding stop function. ev_io_stop (EV_A_ w); - // this causes all nested ev_loop's to stop iterating - ev_unloop (EV_A_ EVUNLOOP_ALL); + // this causes all nested ev_run's to stop iterating + ev_break (EV_A_ EVBREAK_ALL); } // another callback, this time for a time-out @@ -37,15 +37,15 @@ 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 @@ -58,7 +58,7 @@ 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; @@ -77,9 +77,17 @@ on event-based programming, nor will it introduce event-based programming with libev. -Familarity with event based programming techniques in general is assumed +Familiarity with event based programming techniques in general is assumed throughout this document. +=head1 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, then the L above and +look up the missing functions in L and the C and +C sections in L. + =head1 ABOUT LIBEV Libev is an event loop: you register interest in certain events (such as a @@ -100,13 +108,14 @@ Libev supports C (files, many character devices...). + +Epoll is truly the train wreck analog among event poll mechanisms. 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 @@ -520,37 +586,10 @@ =back -If one or more of these are or'ed into the flags value, then only these -backends will be tried (in the reverse order as listed here). If none are -specified, all backends in C will be tried. - -Example: This is the most typical usage. - - if (!ev_default_loop (0)) - fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); - -Example: Restrict libev to the select and poll backends, and do not allow -environment settings to be taken into account: - - ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); - -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 OS supports your types of -fds): - - ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); - -=item struct ev_loop *ev_loop_new (unsigned int flags) - -Similar to C, 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). - -Note that this function I 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 "main" or "initial" thread. +If one or more of the backend flags are or'ed into the flags value, +then only these backends will be tried (in the reverse order as listed +here). If none are specified, all backends in C will be tried. Example: Try to create a event loop that uses epoll and nothing else. @@ -558,9 +597,14 @@ if (!epoller) fatal ("no epoll found here, maybe it hides under your chair"); -=item ev_default_destroy () +Example: Use whatever libev has to offer, but make sure that kqueue is +used if available. + + struct ev_loop *loop = ev_loop_new (ev_recommended_backends () | EVBACKEND_KQUEUE); -Destroys the default loop again (frees all memory and kernel state +=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. C might still return true. It is your responsibility to either stop all watchers cleanly yourself I @@ -572,68 +616,79 @@ handlers), will not be freed by this function, and related watchers (such as signal and child watchers) would need to be stopped manually. -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 -C and C). +This function is normally used on loop objects allocated by +C, but it can also be used on the default loop returned by +C, in which case it is not thread-safe. + +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 its resources. +If you need dynamically allocated loops it is better to use C +and C. -=item ev_loop_destroy (loop) - -Like C, but destroys an event loop created by an -earlier call to C. - -=item ev_default_fork () +=item ev_loop_fork (loop) -This function sets a flag that causes subsequent C iterations -to reinitialise the kernel state for backends that have one. Despite the +This function sets a flag that causes subsequent C 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 I call it in the child before using any of the libev -functions, and it will only take effect at the next C iteration. +the child process. You I call it (or use C) in the +child before resuming or calling C. + +Again, you I to call it on I loop that you want to re-use after +a fork, I. This is +because some kernel interfaces *cough* I *cough* do funny things +during fork. 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, C 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). 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 C: +it just in case after a fork. - pthread_atfork (0, 0, ev_default_fork); +Example: Automate calling C on the default loop when +using pthreads. -=item ev_loop_fork (loop) + static void + post_fork_child (void) + { + ev_loop_fork (EV_DEFAULT); + } -Like C, but acts on an event loop created by -C. 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. + ... + pthread_atfork (0, 0, post_fork_child); =item int ev_is_default_loop (loop) Returns true when the given loop is, in fact, the default loop, and false otherwise. -=item unsigned int ev_loop_count (loop) +=item unsigned int ev_iteration (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 C<0> and -happily wraps around with enough iterations. +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 C<0> +and happily wraps around with enough iterations. This value can sometimes be useful as a generation counter of sorts (it "ticks" the number of loop iterations), as it roughly corresponds with -C and C calls. +C and C calls - and is incremented between the +prepare and check phases. -=item unsigned int ev_loop_depth (loop) +=item unsigned int ev_depth (loop) -Returns the number of times C was entered minus the number of -times C was exited, in other words, the recursion depth. +Returns the number of times C was entered minus the number of +times C was exited normally, in other words, the recursion depth. -Outside C, this number is zero. In a callback, this number is -C<1>, unless C was invoked recursively (or from another thread), +Outside C, this number is zero. In a callback, this number is +C<1>, unless C was invoked recursively (or from another thread), in which case it is higher. -Leaving C abnormally (setjmp/longjmp, cancelling the thread -etc.), doesn't count as exit. +Leaving C abnormally (setjmp/longjmp, cancelling the thread, +throwing an exception etc.), doesn't count as "exit" - consider this +as a hint to avoid such ungentleman-like behaviour unless it's really +convenient, in which case it is fully supported. =item unsigned int ev_backend (loop) @@ -652,7 +707,7 @@ Establishes the current time by querying the kernel, updating the time returned by C in the progress. This is a costly operation and -is usually done automatically within C. +is usually done automatically within C. 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 @@ -664,8 +719,8 @@ =item ev_resume (loop) -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. A typical use case would be an interactive program such as a game: When the user presses C<^Z> to suspend the game and resumes it an hour later it @@ -677,7 +732,7 @@ Effectively, all C watchers will be delayed by the time spend between C and C, and all C watchers will be rescheduled (that is, they will lose any events that would have -occured while suspended). +occurred while suspended). After calling C you B call I function on the given loop other than C, and you B call C @@ -686,28 +741,37 @@ Calling C/C has the side effect of updating the event loop time (see C). -=item ev_loop (loop, int flags) +=item ev_run (loop, int flags) Finally, this is it, the event handler. This function usually is called -after you initialised all your watchers and you want to start handling -events. +after you have initialised all your watchers and you want to start +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 I. -If the flags argument is specified as C<0>, it will not return until -either no event watchers are active anymore or C was called. +If the flags argument is specified as C<0>, it will keep handling events +until either no event watchers are active anymore or C was +called. -Please note that an explicit C is usually better than +Please note that an explicit C 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. -A flags value of C 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. +This function is also I exception-safe - you can break out of +a C call by calling C in a callback, throwing a C++ +exception and so on. This does not decrement the C value, nor +will it clear any outstanding C breaks. + +A flags value of C 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. -A flags value of C will look for new events (waiting if +A flags value of C 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 @@ -716,55 +780,65 @@ 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 C"). However, a pair of C/C watchers is +own C"). However, a pair of C/C watchers is usually a better approach for this kind of thing. -Here are the gory details of what C does: +Here are the gory details of what C does: + - 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. Example: Queue some jobs and then loop until no events are outstanding anymore. ... 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! -=item ev_unloop (loop, how) +=item ev_break (loop, how) -Can be used to make a call to C return early (but only after it +Can be used to make a call to C return early (but only after it has processed all outstanding events). The C argument must be either -C, which will make the innermost C call return, or -C, which will make all nested C calls return. +C, which will make the innermost C call return, or +C, which will make all nested C calls return. -This "unloop state" will be cleared when entering C again. +This "break state" will be cleared on the next call to C. -It is safe to call C from otuside any C calls. +It is safe to call C from outside any C calls, too, in +which case it will have no effect. =item ev_ref (loop) @@ -772,14 +846,15 @@ 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, C will not return on its own. +count is nonzero, C will not return on its own. -If you have a watcher you never unregister that should not keep C -from returning, call ev_unref() after starting, and ev_ref() before -stopping it. +This is useful when you have a watcher that you never intend to +unregister, but that nevertheless should not keep C from +returning. In such a case, call C after starting, and C +before stopping it. As an example, libev itself uses this for its internal signal pipe: It -is not visible to the libev user and should not keep C from +is not visible to the libev user and should not keep C 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 I and I in the callback). -Example: Create a signal watcher, but keep it from keeping C +Example: Create a signal watcher, but keep it from keeping C running when nothing else is active. ev_signal exitsig; @@ -843,7 +918,7 @@ 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). Setting the I can improve the opportunity for saving power, as the program will "bundle" timer callback invocations that @@ -858,7 +933,76 @@ ev_set_timeout_collect_interval (EV_DEFAULT_UC_ 0.1); ev_set_io_collect_interval (EV_DEFAULT_UC_ 0.01); -=item ev_loop_verify (loop) +=item ev_invoke_pending (loop) + +This call will simply invoke all pending watchers while resetting their +pending state. Normally, C 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 C or C of course). + +=item int ev_pending_count (loop) + +Returns the number of pending watchers - zero indicates that no watchers +are pending. + +=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, C will call +this callback instead. This is useful, for example, when you want to +invoke the actual watchers inside another context (another thread etc.). + +If you want to reset the callback, use C as new +callback. + +=item ev_set_loop_release_cb (loop, void (*release)(EV_P), void (*acquire)(EV_P)) + +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. + +However, C 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 C and C, another way is to set these +I and I callbacks on the loop. + +When set, then C will be called just before the thread is +suspended waiting for new events, and C is called just +afterwards. + +Ideally, C will just call your mutex_unlock function, and +C will just call the mutex_lock function again. + +While event loop modifications are allowed between invocations of +C and C (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 C watcher to wake up C when you want it +to take note of any changes you made. + +In theory, threads executing C will be async-cancel safe between +invocations of C and C. + +See also the locking example in the C section later in this +document. + +=item ev_set_userdata (loop, void *data) + +=item void *ev_userdata (loop) + +Set and retrieve a single C associated with a loop. When +C has never been called, then C returns +C<0>. + +These two functions can be used to associate arbitrary data with a loop, +and are intended solely for the C, C and +C callbacks described above, but of course can be (ab-)used for +any other purpose as well. + +=item ev_verify (loop) This function only does something when C support has been compiled in, which is the default for non-minimal builds. It tries to go @@ -879,14 +1023,15 @@ watcher type, e.g. C can mean C for timer watchers and C for I/O watchers. -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 STDIN to -become readable, you would create an C 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 STDIN to become readable, you would create an C watcher +for that: 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); @@ -897,7 +1042,7 @@ ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ); ev_io_start (loop, &stdin_watcher); - ev_loop (loop, 0); + ev_run (loop, 0); As you can see, you are responsible for allocating the memory for your watcher structures (and it is I a bad idea to do this on the @@ -906,11 +1051,11 @@ Each watcher has an associated watcher structure (called C or simply C, as typedefs are provided for all watcher structs). -Each watcher structure must be initialised by a call to C, 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 C, 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). Each watcher type further has its own C<< ev_TYPE_set (watcher *, ...) >> macro to configure it, with arguments specific to the watcher type. There @@ -943,7 +1088,7 @@ The file descriptor in the C watcher has become readable and/or writable. -=item C +=item C The C watcher has timed out. @@ -971,13 +1116,13 @@ =item C -All C watchers are invoked just I C starts +All C watchers are invoked just I C starts to gather new events, and all C watchers are invoked just after -C has gathered them, but before it invokes any callbacks for any +C 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 C watcher might start an idle watcher to keep -C from blocking). +C from blocking). =item C @@ -988,6 +1133,10 @@ The event loop has been resumed in the child process after fork (see C). +=item C + +The event loop is about to be destroyed (see C). + =item C The given async watcher has been asynchronously notified (see C). @@ -1043,7 +1192,7 @@ ev_init (&w, my_cb); ev_io_set (&w, STDIN_FILENO, EV_READ); -=item C (ev_TYPE *, [args]) +=item C (ev_TYPE *watcher, [args]) This macro initialises the type-specific parts of a watcher. You need to call C at least once before you call this macro, but you can @@ -1066,7 +1215,7 @@ ev_io_init (&w, my_cb, STDIN_FILENO, EV_READ); -=item C (loop *, ev_TYPE *watcher) +=item C (loop, ev_TYPE *watcher) Starts (activates) the given watcher. Only active watchers will receive events. If the watcher is already active nothing will happen. @@ -1076,7 +1225,7 @@ ev_io_start (EV_DEFAULT_UC, &w); -=item C (loop *, ev_TYPE *watcher) +=item C (loop, ev_TYPE *watcher) Stops the given watcher if active, and clears the pending status (whether the watcher was active or not). @@ -1111,7 +1260,7 @@ Change the callback. You can change the callback at virtually any time (modulo threads). -=item ev_set_priority (ev_TYPE *watcher, priority) +=item ev_set_priority (ev_TYPE *watcher, int priority) =item int ev_priority (ev_TYPE *watcher) @@ -1153,8 +1302,21 @@ Sometimes it can be useful to "poll" a watcher instead of waiting for its callback to be invoked, which can be accomplished with this function. -=back +=item ev_feed_event (loop, ev_TYPE *watcher, int revents) + +Feeds the given event set into the event loop, as if the specified event +had happened for the specified watcher (which must be a pointer to an +initialised but not necessarily started event watcher). Obviously you must +not free the watcher as long as it has pending events. + +Stopping the watcher, letting libev invoke it, or calling +C will clear the pending event, even if the watcher was +not started in the first place. + +See also C and C for related +functions that do not need a watcher. +=back =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER @@ -1221,6 +1383,65 @@ (((char *)w) - offsetof (struct my_biggy, t2)); } +=head2 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 "the right thing". + +=over 4 + +=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 C, or calls to +C followed by the watcher-specific C function. + +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. + +=item started/running/active + +Once a watcher has been started with a call to C 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. + +=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. + +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 C), +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. + +It is also possible to feed an event on a watcher that is not active (e.g. +via C), in which case it becomes pending without being +active. + +=item stopped + +A watcher can be stopped implicitly by libev (in which case it might still +be pending), or explicitly by calling its C 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. + +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. + +=back + =head2 WATCHER PRIORITY MODELS Many event loops support I, which are usually small @@ -1272,7 +1493,7 @@ you can associate an C 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. @@ -1296,7 +1517,7 @@ // 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); @@ -1360,7 +1581,7 @@ known-to-be-good backend (at the time of this writing, this includes only C and C). 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. Another thing you have to watch out for is that it is quite easy to receive "spurious" readiness notifications, that is your callback might @@ -1435,6 +1656,44 @@ ignore SIGPIPE (and maybe make sure you log the exit status of your daemon somewhere, as that would have given you a big clue). +=head3 The special problem of accept()ing when you can't + +Many implementations of the POSIX C 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. + +For example, larger servers often run out of file descriptors (because +of resource limits), causing C to fail with C 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% CPU usage. + +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). + +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 OS offers an +event-based way to handle this situation, so it's the best one can do. + +A better way to handle the situation is to log any errors other than +C and C, 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 ("raise the ulimit!"). For extra points one could stop +the C watcher on the listening fd "for a while", which reduces CPU +usage. + +If your program is single-threaded, then you could also keep a dummy file +descriptor for overload situations (e.g. by opening F), and +when you run into C or C, close it, run C, +close that fd, and create a new dummy fd. This will gracefully refuse +clients under typical overload conditions. + +The last way to handle it is to simply log the error and C, as +is often done with C failures, but this results in an easy +opportunity for a DoS attack. =head3 Watcher-Specific Functions @@ -1476,7 +1735,7 @@ 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); =head2 C - relative and optionally repeating timeouts @@ -1495,7 +1754,7 @@ 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 C recursively). +no longer true when a callback calls C recursively). =head3 Be smart about timeouts @@ -1591,7 +1850,7 @@ // if last_activity + 60. is older than now, we did time out if (timeout < now) { - // timeout occured, take action + // timeout occurred, take action } else { @@ -1623,12 +1882,12 @@ ev_init (timer, callback); last_activity = ev_now (loop); - callback (loop, timer, EV_TIMEOUT); + callback (loop, timer, EV_TIMER); And when there is some activity, simply store the current time in C, no libev calls at all: - last_actiivty = ev_now (loop); + last_activity = ev_now (loop); This technique is slightly more complex, but in most cases where the time-out is unlikely to be triggered, much more efficient. @@ -1676,7 +1935,7 @@ Establishing the current time is a costly operation (it usually takes at least two system calls): EV therefore updates its idea of the current -time only before and after C collects new events, which causes a +time only before and after C collects new events, which causes a growing difference between C and C when handling lots of events in one iteration. @@ -1692,6 +1951,36 @@ update of the time returned by C by calling C. +=head3 The special problems of suspended animation + +When you leave the server world it is quite customary to hit machines that +can suspend/hibernate - what happens to the clocks during such a suspend? + +Some quick tests made with a Linux 2.6.28 indicate that a suspend freezes +all processes, while the clocks (C, C) continue +to run until the system is suspended, but they will not advance while the +system is suspended. That means, on resume, it will be as if the program +was frozen for a few seconds, but the suspend time will not be counted +towards C when a monotonic clock source is used. The real time +clock advanced as expected, but if it is used as sole clocksource, then a +long suspend would be detected as a time jump by libev, and timers would +be adjusted accordingly. + +I would not be surprised to see different behaviour in different between +operating systems, OS versions or even different hardware. + +The other form of suspend (job control, or sending a SIGSTOP) will see a +time jump in the monotonic clocks and the realtime clock. If the program +is suspended for a very long time, and monotonic clock sources are in use, +then you can expect Cs to expire as the full suspension time +will be counted towards the timers. When no monotonic clock source is in +use, then libev will again assume a timejump and adjust accordingly. + +It might be beneficial for this latter case to call C +and C in code that handles C, to at least get +deterministic behaviour in this case (you can do nothing against +C). + =head3 Watcher-Specific Functions and Data Members =over 4 @@ -1727,6 +2016,18 @@ This sounds a bit complicated, see L, above, for a usage example. +=item ev_tstamp ev_timer_remaining (loop, ev_timer *) + +Returns the remaining time until a timer fires. If the timer is active, +then this time is relative to the current event loop time, otherwise it's +the timeout value currently configured. + +That is, after an C, C returns +C<5>. When the timer is started and one second passes, C +will return C<4>. When the timer expires and is restarted, it will return +roughly C<7> (likely slightly less as callback invocation takes some time, +too), and so on. + =item ev_tstamp repeat [read-write] The current C value. Will be used each time the watcher times out @@ -1761,7 +2062,7 @@ 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 @@ -1797,7 +2098,7 @@ 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 C recursively). +(but this is no longer true when a callback calls C recursively). =head3 Watcher-Specific Functions and Data Members @@ -1935,7 +2236,7 @@ potentially a lot of jitter, but good long-term stability. 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) } @@ -1968,25 +2269,58 @@ Signal watchers will trigger an event when the process receives a specific signal one or more times. Even though signals are very asynchronous, libev -will try it's best to deliver signals synchronously, i.e. as part of the +will try its best to deliver signals synchronously, i.e. as part of the normal event processing, like any other event. -If you want signals asynchronously, just use C as you would -do without libev and forget about sharing the signal. You can even use -C from a signal handler to synchronously wake up an event loop. +If you want signals to be delivered truly asynchronously, just use +C as you would do without libev and forget about sharing +the signal. You can even use C from a signal handler to +synchronously wake up an event loop. + +You can configure as many watchers as you like for the same signal, but +only within the same loop, i.e. you can watch for C in your +default loop and for C in another loop, but you cannot watch for +C in both the default loop and another loop at the same time. At +the moment, C is permanently tied to the default loop. -You can configure as many watchers as you like per signal. Only when the -first watcher gets started will libev actually register a signal handler +When the first watcher gets started will libev actually register something with the kernel (thus it coexists with your own signal handlers as long as -you don't register any with libev for the same signal). Similarly, when -the last signal watcher for a signal is stopped, libev will reset the -signal handler to SIG_DFL (regardless of what it was set to before). +you don't register any with libev for the same signal). If possible and supported, libev will install its handlers with -C behaviour enabled, so system calls should not be unduly -interrupted. If you have a problem with system calls getting interrupted by -signals you can block all signals in an C watcher and unblock -them in an C watcher. +C (or equivalent) behaviour enabled, so system calls should +not be unduly interrupted. If you have a problem with system calls getting +interrupted by signals you can block all signals in an C watcher +and unblock them in an C watcher. + +=head3 The special problem of inheritance over fork/execve/pthread_create + +Both the signal mask (C) and the signal disposition +(C) are unspecified after starting a signal watcher (and after +stopping it again), that is, libev might or might not block the signal, +and might or might not set or restore the installed signal handler. + +While this does not matter for the signal disposition (libev never +sets signals to C, so handlers will be reset to C on +C), this matters for the signal mask: many programs do not expect +certain signals to be blocked. + +This means that before calling C (from the child) you should reset +the signal mask to whatever "default" you expect (all clear is a good +choice usually). + +The simplest way to ensure that the signal mask is reset in the child is +to install a fork handler with C that resets it. That will +catch fork calls done by libraries (such as the libc) as well. + +In current versions of libev, the signal will not be blocked indefinitely +unless you use the C API (C). While this reduces +the window of opportunity for problems, it will not go away, as libev +I to modify the signal mask, at least temporarily. + +So I can't stress this enough: I. This +is not a libev-specific thing, this is true for most event libraries. =head3 Watcher-Specific Functions and Data Members @@ -2012,7 +2346,7 @@ 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; @@ -2035,13 +2369,14 @@ you can only register child watchers in the default event loop. Due to some design glitches inside libev, child watchers will always be -handled at maximum priority (their priority is set to EV_MAXPRI by libev) +handled at maximum priority (their priority is set to C by +libev) =head3 Process Interaction Libev grabs C as soon as the default event loop is -initialised. This is necessary to guarantee proper behaviour even if -the first child watcher is started after the child exits. The occurrence +initialised. This is necessary to guarantee proper behaviour even if the +first child watcher is started after the child exits. The occurrence of C is recorded asynchronously, but child reaping is done synchronously as part of the event loop processing. Libev always reaps all children, even ones not watched. @@ -2061,7 +2396,8 @@ Currently, the child watcher never gets stopped, even when the child terminates, so normally one needs to stop the watcher in the callback. Future versions of libev might stop the watcher automatically -when a child exit is detected. +when a child exit is detected (calling C twice is not a +problem). =head3 Watcher-Specific Functions and Data Members @@ -2406,7 +2742,7 @@ prepare watchers get invoked before the process blocks and check watchers afterwards. -You I call C or similar functions that enter +You I call C or similar functions that enter the current event loop from either C or C 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 @@ -2584,7 +2920,7 @@ // create/start timer // poll - ev_loop (EV_A_ 0); + ev_run (EV_A_ 0); // stop timer again if (timeout >= 0) @@ -2672,7 +3008,7 @@ =item ev_embed_sweep (loop, ev_embed *) Make a single, non-blocking sweep over the embedded loop. This works -similarly to C, but in the most +similarly to C, but in the most appropriate way for embedded loops. =item struct ev_loop *other [read-only] @@ -2742,7 +3078,7 @@ =head3 The special problem of life after fork - how is it possible? -Most uses of C consist of forking, then some simple calls to ste +Most uses of C consist of forking, then some simple calls to set up/change the process environment, followed by a call to C. This sequence should be handled by libev without any problems. @@ -2768,35 +3104,75 @@ When this is not possible, or you want to use the default loop for other reasons, then in the process that wants to start "fresh", call -C followed by C. Destroying -the default loop will "orphan" (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. +C followed by C. +Destroying the default loop will "orphan" (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. =head3 Watcher-Specific Functions and Data Members =over 4 -=item ev_fork_init (ev_signal *, callback) +=item ev_fork_init (ev_fork *, callback) Initialises and configures the fork watcher - it has no parameters of any kind. There is a C macro, but using it is utterly pointless, -believe me. +really. + +=back + + +=head2 C - even the best things end + +Cleanup watchers are called just before the event loop is being destroyed +by a call to C. + +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. + +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 C. + +=head3 Watcher-Specific Functions and Data Members + +=over 4 + +=item ev_cleanup_init (ev_cleanup *, callback) + +Initialises and configures the cleanup watcher - it has no parameters of +any kind. There is a C macro, but using it is utterly +pointless, I assure you. =back +Example: Register an atexit handler to destroy the default loop, so any +cleanup functions are called. + + static void + program_exits (void) + { + ev_loop_destroy (EV_DEFAULT_UC); + } + + ... + atexit (program_exits); -=head2 C - how to wake up another event loop -In general, you cannot use an C from multiple threads or other +=head2 C - how to wake up an event loop + +In general, you cannot use an C 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). -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 -C watchers do: as long as the C watcher is active, you -can signal it by calling C, 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 C +watchers do: as long as the C watcher is active, you can signal +it by calling C, which is thread- and signal safe. This functionality is very similar to C watchers, as signals, too, are asynchronous in nature, and signals, too, will be compressed @@ -2811,7 +3187,8 @@ C does not support queueing of data in any way. The reason is that the author does not know of a simple (or any) algorithm for a multiple-writer-single-reader queue that works in all cases and doesn't -need elaborate support such as pthreads. +need elaborate support such as pthreads or unportable memory access +semantics. That means that if you want to queue data, you have to provide your own queue. But at least I can tell you how to implement locking around your @@ -2960,9 +3337,9 @@ started. Otherwise an C watcher with after = C (and repeat = 0) will be started. C<0> is a valid timeout. -The callback has the type C and gets +The callback has the type C and is passed an C set like normal event callbacks (a combination of -C, C, C or C) and the C +C, C, C or C) and the C value passed to C. Note that it is possible to receive I a timeout and an io event at the same time - you probably should give io events precedence. @@ -2973,24 +3350,18 @@ { if (revents & EV_READ) /* stdin might have data for us, joy! */; - else if (revents & EV_TIMEOUT) + else if (revents & EV_TIMER) /* doh, nothing entered */; } ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); -=item ev_feed_event (struct ev_loop *, watcher *, int revents) - -Feeds the given event set into the event loop, as if the specified event -had happened for the specified watcher (which must be a pointer to an -initialised but not necessarily started event watcher). - -=item ev_feed_fd_event (struct ev_loop *, int fd, int revents) +=item ev_feed_fd_event (loop, int fd, int revents) Feed an event on the given fd, as if a file descriptor backend detected the given events it. -=item ev_feed_signal_event (struct ev_loop *loop, int signum) +=item ev_feed_signal_event (loop, int signum) Feed an event as if the given signal occurred (C must be the default loop!). @@ -2998,6 +3369,56 @@ =back +=head1 COMMON OR USEFUL IDIOMS (OR BOTH) + +This section explains some common idioms that are not immediately +obvious. Note that examples are sprinkled over the whole manual, and this +section only contains stuff that wouldn't fit anywhere else. + +=over 4 + +=item Model/nested event loop invocations and exit conditions. + +Often (especially in GUI toolkits) there are places where you have +I interaction, which is most easily implemented by recursively +invoking C. + +This brings the problem of exiting - a callback might want to finish the +main C call, but not the nested one (e.g. user clicked "Quit", but +a modal "Are you sure?" dialog is still waiting), or just the nested one +and not the main one (e.g. user clocked "Ok" in a modal dialog), or some +other combination: In these cases, C will not work alone. + +The solution is to maintain "break this loop" variable for each C +invocation, and use a loop around C until the condition is +triggered, using C: + + // main loop + int exit_main_loop = 0; + + while (!exit_main_loop) + ev_run (EV_DEFAULT_ EVRUN_ONCE); + + // in a model watcher + int exit_nested_loop = 0; + + while (!exit_nested_loop) + ev_run (EV_A_ EVRUN_ONCE); + +To exit from any of these loops, just set the corresponding exit variable: + + // exit modal loop + exit_nested_loop = 1; + + // exit main program, after modal loop is finished + exit_main_loop = 1; + + // exit both + exit_main_loop = exit_nested_loop = 1; + +=back + + =head1 LIBEVENT EMULATION Libev offers a compatibility emulation layer for libevent. It cannot @@ -3005,6 +3426,11 @@ =over 4 +=item * Only the libevent-1.4.1-beta API is being emulated. + +This was the newest libevent version available when libev was implemented, +and is still mostly unchanged in 2010. + =item * Use it by including , as usual. =item * The following members are fully supported: ev_base, ev_callback, @@ -3019,7 +3445,7 @@ is an ev_pri field. =item * In libevent, the last base created gets the signals, in libev, the -first base created (== the default loop) gets the signals. +base that registered the signal gets the signals. =item * Other members are not supported. @@ -3048,11 +3474,11 @@ that the watcher is associated with (or no additional members at all if you disable C when embedding libev). -Currently, functions, and static and non-static member functions can be -used as callbacks. Other types should be easy to add as long as they only -need one additional pointer for context. If you need support for other -types of functors please contact the author (preferably after implementing -it). +Currently, functions, static and non-static member functions and classes +with C can be used as callbacks. Other types should be easy +to add as long as they only need one additional pointer for context. If +you need support for other types of functors please contact the author +(preferably after implementing it). Here is a list of things available in the C namespace: @@ -3080,7 +3506,7 @@ =item ev::TYPE::TYPE () -=item ev::TYPE::TYPE (struct ev_loop *) +=item ev::TYPE::TYPE (loop) =item ev::TYPE::~TYPE @@ -3124,8 +3550,6 @@ =item w->set (object *) -This is an B feature that might go away in a future version. - This is a variation of a method callback - leaving out the method to call will default the method to C, which makes it possible to use functor objects without having to manually specify the C all @@ -3167,23 +3591,29 @@ static void io_cb (ev::io &w, int revents) { } iow.set (); -=item w->set (struct ev_loop *) +=item w->set (loop) Associates a different C with this watcher. You can only do this when the watcher is inactive (and not pending either). =item w->set ([arguments]) -Basically the same as C, 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 C, 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. =item w->start () Starts the watcher. Note that there is no C argument, as the constructor already stores the event loop. +=item w->start ([arguments]) + +Instead of calling C and C methods separately, it is often +convenient to wrap them in one call. Uses the same type of arguments as +the configure C method of the watcher. + =item w->stop () Stops the watcher if it is active. Again, no C argument. @@ -3205,20 +3635,25 @@ =back -Example: Define a class with an IO 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. 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 } }; @@ -3274,6 +3709,12 @@ Erkki Seppala has written Ocaml bindings for libev, to be found at L. +=item Lua + +Brian Maher has written a partial interface to libev for lua (at the +time of this writing, only C and C), to be found at +L. + =back @@ -3296,7 +3737,7 @@ ev_unref (EV_A); ev_timer_add (EV_A_ watcher); - ev_loop (EV_A_ 0); + ev_run (EV_A_ 0); It assumes the variable C of type C is in scope, which is often provided by the following macro. @@ -3346,7 +3787,7 @@ 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); =head1 EMBEDDING @@ -3436,12 +3877,35 @@ =head2 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. +define before including (or compiling) any of its files. The default in +the absence of autoconf is documented for every option. + +Symbols marked with "(h)" do not change the ABI, and can have different +values when compiling libev vs. including F, so it is permissible +to redefine them before including F without breaking compatibility +to a compiled library. All other symbols change the ABI, which means all +users of libev and the libev code itself must be compiled with compatible +settings. =over 4 -=item EV_STANDALONE +=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. + +You can disable these wrappers (to test compatibility with future +versions) by defining C to C<0> when compiling your +sources. This has the additional advantage that you can drop the C +from C declarations, as libev will provide an C +typedef in that case. + +In some future version, the default for C will become C<0>, +and in some even more future version the compatibility code will be +removed completely. + +=item EV_STANDALONE (h) Must always be C<1> if you do not use autoconf configuration, which keeps libev from including F, and it also defines dummy @@ -3449,7 +3913,7 @@ supported). It will also not define any of the structs usually found in F that are not directly supported by the libev core alone. -In stanbdalone mode, libev will still try to automatically deduce the +In standalone mode, libev will still try to automatically deduce the configuration, but has to be more conservative. =item EV_USE_MONOTONIC @@ -3524,7 +3988,7 @@ it is assumed that all these functions actually work on fds, even on win32. Should not be defined on non-win32 platforms. -=item EV_FD_TO_WIN32_HANDLE +=item EV_FD_TO_WIN32_HANDLE(fd) If C is enabled, then libev needs a way to map file descriptors to socket handles. When not defining this symbol (the @@ -3532,6 +3996,20 @@ correct. In some cases, programs use their own file descriptor management, in which case they can provide this function to map fds to socket handles. +=item EV_WIN32_HANDLE_TO_FD(handle) + +If C then libev maps handles to file descriptors +using the standard C<_open_osfhandle> function. For programs implementing +their own fd to handle mapping, overwriting this function makes it easier +to do so. This can be done by defining this macro to an appropriate value. + +=item EV_WIN32_CLOSE_FD(fd) + +If programs implement their own fd to handle mapping on win32, then this +macro can be used to override the C function, useful to unregister +file descriptors again. Note that the replacement function has to close +the underlying OS handle. + =item EV_USE_POLL If defined to be C<1>, libev will compile in support for the C(2) @@ -3587,24 +4065,24 @@ In the absence of this define, libev will use C (from F), which is usually good enough on most platforms. -=item EV_H +=item EV_H (h) The name of the F header file used to include it. The default if undefined is C<"ev.h"> in F, F and F. This can be used to virtually rename the F header file in case of conflicts. -=item EV_CONFIG_H +=item EV_CONFIG_H (h) If C isn't C<1>, this variable can be used to override F's idea of where to find the F file, similarly to C, above. -=item EV_EVENT_H +=item EV_EVENT_H (h) Similarly to C, this macro can be used to override F's idea of how the F header can be found, the default is C<"event.h">. -=item EV_PROTOTYPES +=item EV_PROTOTYPES (h) If defined to be C<0>, then F will not define any function prototypes, but still define all the structs and other symbols. This is @@ -3636,60 +4114,130 @@ If your embedding application does not need any priorities, defining these both to C<0> will save some memory and CPU. -=item EV_PERIODIC_ENABLE +=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 C<1> (and the platform supports it), then +the respective watcher type is supported. If defined to be C<0>, then it +is not. Disabling watcher types mainly saves code size. -If undefined or defined to be C<1>, then periodic timers are supported. If -defined to be C<0>, then they are not. Disabling them saves a few kB of -code. +=item EV_FEATURES -=item EV_IDLE_ENABLE +If you need to shave off some kilobytes of code at the expense of some +speed (but with the full API), 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. + +A typical way to use this symbol is to define it to C<0> (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: + + #define EV_FEATURES 0 + #define EV_MULTIPLICITY 1 + #define EV_USE_POLL 1 + #define EV_CHILD_ENABLE 1 + #define EV_ASYNC_ENABLE 1 -If undefined or defined to be C<1>, then idle watchers are supported. If -defined to be C<0>, then they are not. Disabling them saves a few kB of -code. +The actual value is a bitset, it can be a combination of the following +values: -=item EV_EMBED_ENABLE +=over 4 -If undefined or defined to be C<1>, then embed watchers are supported. If -defined to be C<0>, then they are not. Embed watchers rely on most other -watcher types, which therefore must not be disabled. +=item C<1> - faster/larger code -=item EV_STAT_ENABLE +Use larger code to speed up some operations. -If undefined or defined to be C<1>, then stat watchers are supported. If -defined to be C<0>, then they are not. +Currently this is used to override some inlining decisions (enlarging the +code size by roughly 30% on amd64). -=item EV_FORK_ENABLE +When optimising for size, use of compiler flags such as C<-Os> with +gcc is recommended, as well as C<-DNDEBUG>, as libev contains a number of +assertions. -If undefined or defined to be C<1>, then fork watchers are supported. If -defined to be C<0>, then they are not. +=item C<2> - faster/larger data structures -=item EV_ASYNC_ENABLE +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. -If undefined or defined to be C<1>, then async watchers are supported. If -defined to be C<0>, then they are not. +=item C<4> - full API configuration -=item EV_MINIMAL +This enables priorities (sets C=2 and C=-2), and +enables multiplicity (C=1). -If you need to shave off some kilobytes of code at the expense of some -speed, define this symbol to C<1>. 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. +=item C<8> - full API + +This enables a lot of the "lesser used" API functions. See C for +details on which parts of the API are still available without this +feature, and do not complain if this subset changes over time. + +=item C<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 +C to C<1> instead. + +=item C<32> - enable all backends + +This enables all backends - without this feature, you need to enable at +least one backend manually (C is a good choice). + +=item C<64> - enable OS-specific "helper" APIs + +Enable inotify, eventfd, signalfd and similar OS-specific helper APIs by +default. + +=back + +Compiling with C +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. + +With an intelligent-enough linker (gcc+binutils are intelligent enough +when you use C<-Wl,--gc-sections -ffunction-sections>) 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. + +=item EV_AVOID_STDIO + +If this is set to C<1> 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. + +Note that error messages might become less precise when this option is +enabled. + +=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 (C<32> should be +good for about any system in existence) can save some memory, as libev +statically allocates some 12-24 bytes per signal number. =item EV_PID_HASHSIZE C watchers use a small hash table to distribute workload by -pid. The default size is C<16> (or C<1> with C), usually more -than enough. If you need to manage thousands of children you might want to -increase this value (I be a power of two). +pid. The default size is C<16> (or C<1> with C disabled), +usually more than enough. If you need to manage thousands of children you +might want to increase this value (I be a power of two). =item EV_INOTIFY_HASHSIZE C watchers use a small hash table to distribute workload by -inotify watch id. The default size is C<16> (or C<1> with C), -usually more than enough. If you need to manage thousands of C -watchers you might want to increase this value (I be a power of -two). +inotify watch id. The default size is C<16> (or C<1> with C +disabled), usually more than enough. If you need to manage thousands of +C watchers you might want to increase this value (I be a +power of two). =item EV_USE_4HEAP @@ -3698,8 +4246,8 @@ to C<1>. The 4-heap uses more complicated (longer) code but has noticeably faster performance with many (thousands) of watchers. -The default is C<1> unless C is set in which case it is C<0> -(disabled). +The default is C<1>, unless C overrides it, in which case it +will be C<0>. =item EV_HEAP_CACHE_AT @@ -3710,12 +4258,12 @@ but avoids random read accesses on heap changes. This improves performance noticeably with many (hundreds) of watchers. -The default is C<1> unless C is set in which case it is C<0> -(disabled). +The default is C<1>, unless C overrides it, in which case it +will be C<0>. =item EV_VERIFY -Controls how much internal verification (see C) will +Controls how much internal verification (see C) will be done: If set to C<0>, no internal verification code will be compiled in. If set to C<1>, then verification code will be compiled in, but not called. If set to C<2>, then the internal verification code will be @@ -3723,13 +4271,13 @@ verification code will be called very frequently, which will slow down libev considerably. -The default is C<1>, unless C is set, in which case it will be -C<0>. +The default is C<1>, unless C overrides it, in which case it +will be C<0>. =item EV_COMMON By default, all watchers have a C 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. @@ -3792,15 +4340,14 @@ The usage in rxvt-unicode is simpler. It has a F header file that everybody includes and which overrides some configure choices: - #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 + #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" @@ -3870,17 +4417,155 @@ =back +=head4 THREAD LOCKING EXAMPLE + +Here is a fictitious example of how to run an event loop in a different +thread than where callbacks are being invoked and watchers are +created/added/removed. + +For a real-world example, see the C perl module, +which uses exactly this technique (which is suited for many high-level +languages). + +The example uses a pthread mutex to protect the loop data, a condition +variable to wait for callback invocations, an async watcher to notify the +event loop thread and an unspecified mechanism to wake up the main thread. + +First, you need to associate some data with the event loop: + + typedef struct { + mutex_t lock; /* global loop lock */ + ev_async async_w; + thread_t tid; + cond_t invoke_cv; + } userdata; + + void prepare_loop (EV_P) + { + // for simplicity, we use a static userdata struct. + static userdata u; + + ev_async_init (&u->async_w, async_cb); + ev_async_start (EV_A_ &u->async_w); + + pthread_mutex_init (&u->lock, 0); + pthread_cond_init (&u->invoke_cv, 0); + + // now associate this with the loop + ev_set_userdata (EV_A_ u); + ev_set_invoke_pending_cb (EV_A_ l_invoke); + ev_set_loop_release_cb (EV_A_ l_release, l_acquire); + + // then create the thread running ev_loop + pthread_create (&u->tid, 0, l_run, EV_A); + } + +The callback for the C watcher does nothing: the watcher is used +solely to wake up the event loop so it takes notice of any new watchers +that might have been added: + + static void + async_cb (EV_P_ ev_async *w, int revents) + { + // just used for the side effects + } + +The C and C callbacks simply unlock/lock the mutex +protecting the loop data, respectively. + + static void + l_release (EV_P) + { + userdata *u = ev_userdata (EV_A); + pthread_mutex_unlock (&u->lock); + } + + static void + l_acquire (EV_P) + { + userdata *u = ev_userdata (EV_A); + pthread_mutex_lock (&u->lock); + } + +The event loop thread first acquires the mutex, and then jumps straight +into C: + + void * + l_run (void *thr_arg) + { + struct ev_loop *loop = (struct ev_loop *)thr_arg; + + l_acquire (EV_A); + pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); + ev_run (EV_A_ 0); + l_release (EV_A); + + return 0; + } + +Instead of invoking all pending watchers, the C callback will +signal the main thread via some unspecified mechanism (signals? pipe +writes? C?) and then waits until all pending watchers +have been called (in a while loop because a) spurious wakeups are possible +and b) skipping inter-thread-communication when there are no pending +watchers is very beneficial): + + static void + l_invoke (EV_P) + { + userdata *u = ev_userdata (EV_A); + + while (ev_pending_count (EV_A)) + { + wake_up_other_thread_in_some_magic_or_not_so_magic_way (); + pthread_cond_wait (&u->invoke_cv, &u->lock); + } + } + +Now, whenever the main thread gets told to invoke pending watchers, it +will grab the lock, call C and then signal the loop +thread to continue: + + static void + real_invoke_pending (EV_P) + { + userdata *u = ev_userdata (EV_A); + + pthread_mutex_lock (&u->lock); + ev_invoke_pending (EV_A); + pthread_cond_signal (&u->invoke_cv); + pthread_mutex_unlock (&u->lock); + } + +Whenever you want to start/stop a watcher or do other modifications to an +event loop, you will now have to lock: + + ev_timer timeout_watcher; + userdata *u = ev_userdata (EV_A); + + ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); + + pthread_mutex_lock (&u->lock); + ev_timer_start (EV_A_ &timeout_watcher); + ev_async_send (EV_A_ &u->async_w); + pthread_mutex_unlock (&u->lock); + +Note that sending the C watcher is required because otherwise +an event loop currently blocking in the kernel will have no knowledge +about the newly added timer. By waking up the loop it will pick up any new +watchers in the next event loop iteration. + =head3 COROUTINES Libev is very accommodating to coroutines ("cooperative threads"): libev fully supports nesting calls to its functions from different -coroutines (e.g. you can call C 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 C reschedule callbacks. +coroutines (e.g. you can call C 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 C reschedule callbacks. Care has been taken to ensure that libev does not keep local state inside -C, and other calls do not usually allow for coroutine switches as +C, and other calls do not usually allow for coroutine switches as they do not call any callbacks. =head2 COMPILER WARNINGS @@ -3901,7 +4586,7 @@ 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. @@ -3947,19 +4632,109 @@ =head1 PORTABILITY NOTES +=head2 GNU/LINUX 32 BIT LIMITATIONS + +GNU/Linux is the only common platform that supports 64 bit file/large file +interfaces but I them by default. + +That means that libev compiled in the default environment doesn't support +files larger than 2GiB or so, which mainly affects C watchers. + +Unfortunately, many programs try to work around this GNU/Linux issue +by enabling the large file API, which makes them incompatible with the +standard libev compiled for their system. + +Likewise, libev cannot enable the large file API itself as this would +suddenly make it incompatible to the default compile time environment, +i.e. all programs not using special compile switches. + +=head2 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. + +=head3 C is buggy + +The kqueue syscall is broken in all known versions - most versions support +only sockets, many support pipes. + +Libev tries to work around this by not using C 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. + +=head3 C is buggy + +Instead of fixing C, Apple replaced their (working) C +implementation by something calling C internally around the 10.5.6 +release, so now C I C are broken. + +Libev tries to work around this by not using C by default on +this rotten platform, but of course you can still ask for it when creating +a loop. + +=head3 C, and of course Apple found a way to fuck this +one up as well: On OS/X, C backends. + +=head2 AIX POLL BUG + +AIX unfortunately has a broken C 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 C function The winsocket C