--- libev/ev.pod 2008/09/29 03:31:14 1.187 +++ libev/ev.pod 2008/10/24 08:30:01 1.202 @@ -12,14 +12,14 @@ #include // every watcher type has its own typedef'd struct - // with the name ev_ + // with the name ev_TYPE ev_io stdin_watcher; ev_timer timeout_watcher; // all watcher callbacks have a similar signature // this callback is called when data is readable on stdin static void - stdin_cb (EV_P_ struct ev_io *w, int revents) + stdin_cb (EV_P_ ev_io *w, int revents) { puts ("stdin ready"); // for one-shot events, one must manually stop the watcher @@ -32,7 +32,7 @@ // another callback, this time for a time-out static void - timeout_cb (EV_P_ struct ev_timer *w, int revents) + timeout_cb (EV_P_ ev_timer *w, int revents) { puts ("timeout"); // this causes the innermost ev_loop to stop iterating @@ -43,7 +43,7 @@ main (void) { // use the default event loop unless you have special needs - struct ev_loop *loop = ev_default_loop (0); + ev_loop *loop = ev_default_loop (0); // initialise an io watcher, then start it // this one will watch for stdin to become readable @@ -105,7 +105,7 @@ more info about various configuration options please have a look at B section in this manual. If libev was configured without support for multiple event loops, then all functions taking an initial argument of -name C (which is always of type C) will not have +name C (which is always of type C) will not have this argument. =head2 TIME REPRESENTATION @@ -278,9 +278,13 @@ =head1 FUNCTIONS CONTROLLING THE EVENT LOOP -An event loop is described by a C. The library knows two -types of such loops, the I loop, which supports signals and child -events, and dynamically created loops which do not. +An event loop is described by a C (the C +is I optional in this case, as there is also an C +I). + +The library knows two types of such loops, the I loop, which +supports signals and child events, and dynamically created loops which do +not. =over 4 @@ -687,6 +691,8 @@ This "unloop state" will be cleared when entering C again. +It is safe to call C from otuside any C calls. + =item ev_ref (loop) =item ev_unref (loop) @@ -710,7 +716,7 @@ Example: Create a signal watcher, but keep it from keeping C running when nothing else is active. - struct ev_signal exitsig; + ev_signal exitsig; ev_signal_init (&exitsig, sig_cb, SIGINT); ev_signal_start (loop, &exitsig); evf_unref (loop); @@ -768,7 +774,7 @@ =item ev_loop_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 +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 is found to be inconsistent, it will print an error message to standard error and call C. @@ -782,26 +788,36 @@ =head1 ANATOMY OF A WATCHER +In the following description, uppercase C in names stands for the +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: - static void my_cb (struct ev_loop *loop, struct ev_io *w, int revents) + static void my_cb (struct ev_loop *loop, ev_io *w, int revents) { ev_io_stop (w); ev_unloop (loop, EVUNLOOP_ALL); } struct ev_loop *loop = ev_default_loop (0); - struct ev_io stdin_watcher; + + ev_io stdin_watcher; + ev_init (&stdin_watcher, my_cb); ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ); ev_io_start (loop, &stdin_watcher); + ev_loop (loop, 0); As you can see, you are responsible for allocating the memory for your -watcher structures (and it is usually a bad idea to do this on the stack, -although this can sometimes be quite valid). +watcher structures (and it is I a bad idea to do this on the +stack). + +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 @@ -809,19 +825,19 @@ watchers, each time the event loop detects that the file descriptor given is readable and/or writable). -Each watcher type has its own C<< ev__set (watcher *, ...) >> macro -with arguments specific to this watcher type. There is also a macro -to combine initialisation and setting in one call: C<< ev__init -(watcher *, callback, ...) >>. +Each watcher type further has its own C<< ev_TYPE_set (watcher *, ...) >> +macro to configure it, with arguments specific to the watcher type. There +is also a macro to combine initialisation and setting in one call: C<< +ev_TYPE_init (watcher *, callback, ...) >>. To make the watcher actually watch out for events, you have to start it -with a watcher-specific start function (C<< ev__start (loop, watcher +with a watcher-specific start function (C<< ev_TYPE_start (loop, watcher *) >>), and you can stop watching for events at any time by calling the -corresponding stop function (C<< ev__stop (loop, watcher *) >>. +corresponding stop function (C<< ev_TYPE_stop (loop, watcher *) >>. As long as your watcher is active (has been started but not stopped) you must not touch the values stored in it. Most specifically you must never -reinitialise it or call its C macro. +reinitialise it or call its C macro. Each and every callback receives the event loop pointer as first, the registered watcher structure as second, and a bitset of received events as @@ -894,8 +910,12 @@ An unspecified error has occurred, the watcher has been stopped. This might happen because the watcher could not be properly started because libev ran out of memory, a file descriptor was found to be closed or any other -problem. You best act on it by reporting the problem and somehow coping -with the watcher being stopped. +problem. Libev considers these application bugs. + +You best act on it by reporting the problem and somehow coping with the +watcher being stopped. Note that well-written programs should not receive +an error ever, so when your watcher receives it, this usually indicates a +bug in your program. Libev will usually signal a few "dummy" events together with an error, for example it might indicate that a fd is readable or writable, and if your @@ -908,9 +928,6 @@ =head2 GENERIC WATCHER FUNCTIONS -In the following description, C stands for the watcher type, -e.g. C for C watchers and C for C watchers. - =over 4 =item C (ev_TYPE *watcher, callback) @@ -925,7 +942,7 @@ You can reinitialise a watcher at any time as long as it has been stopped (or never started) and there are no pending events outstanding. -The callback is always of type C. Example: Initialise an C watcher in two steps. @@ -969,12 +986,14 @@ =item C (loop *, ev_TYPE *watcher) -Stops the given watcher again (if active) and clears the pending -status. It is possible that stopped watchers are pending (for example, -non-repeating timers are being stopped when they become pending), but -C ensures that the watcher is neither active nor pending. If -you want to free or reuse the memory used by the watcher it is therefore a -good idea to always call its C function. +Stops the given watcher if active, and clears the pending status (whether +the watcher was active or not). + +It is possible that stopped watchers are pending - for example, +non-repeating timers are being stopped when they become pending - but +calling C ensures that the watcher is neither active nor +pending. If you want to free or reuse the memory used by the watcher it is +therefore a good idea to always call its C function. =item bool ev_is_active (ev_TYPE *watcher) @@ -1026,7 +1045,7 @@ Setting a priority outside the range of C to C is fine, as long as you do not mind that the priority value you query might -or might not have been adjusted to be within valid range. +or might not have been clamped to the valid range. =item ev_invoke (loop, ev_TYPE *watcher, int revents) @@ -1058,7 +1077,7 @@ struct my_io { - struct ev_io io; + ev_io io; int otherfd; void *somedata; struct whatever *mostinteresting; @@ -1071,7 +1090,7 @@ And since your callback will be called with a pointer to the watcher, you can cast it back to your own type: - static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) + static void my_cb (struct ev_loop *loop, ev_io *w_, int revents) { struct my_io *w = (struct my_io *)w_; ... @@ -1099,14 +1118,14 @@ #include static void - t1_cb (EV_P_ struct ev_timer *w, int revents) + t1_cb (EV_P_ ev_timer *w, int revents) { struct my_biggy big = (struct my_biggy * (((char *)w) - offsetof (struct my_biggy, t1)); } static void - t2_cb (EV_P_ struct ev_timer *w, int revents) + t2_cb (EV_P_ ev_timer *w, int revents) { struct my_biggy big = (struct my_biggy * (((char *)w) - offsetof (struct my_biggy, t2)); @@ -1251,7 +1270,7 @@ attempt to read a whole line in the callback. static void - stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) + stdin_readable_cb (struct ev_loop *loop, ev_io *w, int revents) { ev_io_stop (loop, w); .. read from stdin here (or from w->fd) and handle any I/O errors @@ -1259,7 +1278,7 @@ ... struct ev_loop *loop = ev_default_init (0); - struct ev_io stdin_readable; + 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); @@ -1280,6 +1299,181 @@ passed, but if multiple timers become ready during the same loop iteration then order of execution is undefined. +=head3 Be smart about timeouts + +Many real-world problems involve some kind of timeout, usually for error +recovery. A typical example is an HTTP request - if the other side hangs, +you want to raise some error after a while. + +What follows are some ways to handle this problem, from obvious and +inefficient to smart and efficient. + +In the following, a 60 second activity timeout is assumed - a timeout that +gets reset to 60 seconds each time there is activity (e.g. each time some +data or other life sign was received). + +=over 4 + +=item 1. Use a timer and stop, reinitialise and start it on activity. + +This is the most obvious, but not the most simple way: In the beginning, +start the watcher: + + ev_timer_init (timer, callback, 60., 0.); + ev_timer_start (loop, timer); + +Then, each time there is some activity, C it, initialise it +and start it again: + + ev_timer_stop (loop, timer); + ev_timer_set (timer, 60., 0.); + ev_timer_start (loop, timer); + +This is relatively simple to implement, but means that each time there is +some activity, libev will first have to remove the timer from its internal +data structure and then add it again. Libev tries to be fast, but it's +still not a constant-time operation. + +=item 2. Use a timer and re-start it with C inactivity. + +This is the easiest way, and involves using C instead of +C. + +To implement this, configure an C with a C value +of C<60> and then call C at start and each time you +successfully read or write some data. If you go into an idle state where +you do not expect data to travel on the socket, you can C +the timer, and C will automatically restart it if need be. + +That means you can ignore both the C function and the +C argument to C, and only ever use the C +member and C. + +At start: + + ev_timer_init (timer, callback); + timer->repeat = 60.; + ev_timer_again (loop, timer); + +Each time there is some activity: + + ev_timer_again (loop, timer); + +It is even possible to change the time-out on the fly, regardless of +whether the watcher is active or not: + + timer->repeat = 30.; + ev_timer_again (loop, timer); + +This is slightly more efficient then stopping/starting the timer each time +you want to modify its timeout value, as libev does not have to completely +remove and re-insert the timer from/into its internal data structure. + +It is, however, even simpler than the "obvious" way to do it. + +=item 3. Let the timer time out, but then re-arm it as required. + +This method is more tricky, but usually most efficient: Most timeouts are +relatively long compared to the intervals between other activity - in +our example, within 60 seconds, there are usually many I/O events with +associated activity resets. + +In this case, it would be more efficient to leave the C alone, +but remember the time of last activity, and check for a real timeout only +within the callback: + + ev_tstamp last_activity; // time of last activity + + static void + callback (EV_P_ ev_timer *w, int revents) + { + ev_tstamp now = ev_now (EV_A); + ev_tstamp timeout = last_activity + 60.; + + // if last_activity + 60. is older than now, we did time out + if (timeout < now) + { + // timeout occured, take action + } + else + { + // callback was invoked, but there was some activity, re-arm + // the watcher to fire in last_activity + 60, which is + // guaranteed to be in the future, so "again" is positive: + w->again = timeout - now; + ev_timer_again (EV_A_ w); + } + } + +To summarise the callback: first calculate the real timeout (defined +as "60 seconds after the last activity"), then check if that time has +been reached, which means something I, in fact, time out. Otherwise +the callback was invoked too early (C is in the future), so +re-schedule the timer to fire at that future time, to see if maybe we have +a timeout then. + +Note how C is used, taking advantage of the +C optimisation when the timer is already running. + +This scheme causes more callback invocations (about one every 60 seconds +minus half the average time between activity), but virtually no calls to +libev to change the timeout. + +To start the timer, simply initialise the watcher and set C +to the current time (meaning we just have some activity :), then call the +callback, which will "do the right thing" and start the timer: + + ev_timer_init (timer, callback); + last_activity = ev_now (loop); + callback (loop, timer, EV_TIMEOUT); + +And when there is some activity, simply store the current time in +C, no libev calls at all: + + last_actiivty = 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. + +Changing the timeout is trivial as well (if it isn't hard-coded in the +callback :) - just change the timeout and invoke the callback, which will +fix things for you. + +=item 4. Wee, just use a double-linked list for your timeouts. + +If there is not one request, but many thousands (millions...), all +employing some kind of timeout with the same timeout value, then one can +do even better: + +When starting the timeout, calculate the timeout value and put the timeout +at the I of the list. + +Then use an C to fire when the timeout at the I of +the list is expected to fire (for example, using the technique #3). + +When there is some activity, remove the timer from the list, recalculate +the timeout, append it to the end of the list again, and make sure to +update the C if it was taken from the beginning of the list. + +This way, one can manage an unlimited number of timeouts in O(1) time for +starting, stopping and updating the timers, at the expense of a major +complication, and having to use a constant timeout. The constant timeout +ensures that the list stays sorted. + +=back + +So which method the best? + +Method #2 is a simple no-brain-required solution that is adequate in most +situations. Method #3 requires a bit more thinking, but handles many cases +better, and isn't very complicated either. In most case, choosing either +one is fine, with #3 being better in typical situations. + +Method #1 is almost always a bad idea, and buys you nothing. Method #4 is +rather complicated, but extremely efficient, something that really pays +off after the first million or so of active timers, i.e. it's usually +overkill :) + =head3 The special problem of time updates Establishing the current time is a costly operation (it usually takes at @@ -1332,36 +1526,8 @@ If the timer is repeating, either start it if necessary (with the C value), or reset the running timer to the C value. -This sounds a bit complicated, but here is a useful and typical -example: Imagine you have a TCP connection and you want a so-called idle -timeout, that is, you want to be called when there have been, say, 60 -seconds of inactivity on the socket. The easiest way to do this is to -configure an C with a C value of C<60> and then call -C each time you successfully read or write some data. If -you go into an idle state where you do not expect data to travel on the -socket, you can C the timer, and C will -automatically restart it if need be. - -That means you can ignore the C value and C -altogether and only ever use the C value and C: - - ev_timer_init (timer, callback, 0., 5.); - ev_timer_again (loop, timer); - ... - timer->again = 17.; - ev_timer_again (loop, timer); - ... - timer->again = 10.; - ev_timer_again (loop, timer); - -This is more slightly efficient then stopping/starting the timer each time -you want to modify its timeout value. - -Note, however, that it is often even more efficient to remember the -time of the last activity and let the timer time-out naturally. In the -callback, you then check whether the time-out is real, or, if there was -some activity, you reschedule the watcher to time-out in "last_activity + -timeout - ev_now ()" seconds. +This sounds a bit complicated, see "Be smart about timeouts", above, for a +usage example. =item ev_tstamp repeat [read-write] @@ -1376,12 +1542,12 @@ Example: Create a timer that fires after 60 seconds. static void - one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents) + one_minute_cb (struct ev_loop *loop, ev_timer *w, int revents) { .. one minute over, w is actually stopped right here } - struct ev_timer mytimer; + ev_timer mytimer; ev_timer_init (&mytimer, one_minute_cb, 60., 0.); ev_timer_start (loop, &mytimer); @@ -1389,12 +1555,12 @@ inactivity. static void - timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents) + timeout_cb (struct ev_loop *loop, ev_timer *w, int revents) { .. ten seconds without any activity } - struct ev_timer mytimer; + 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); @@ -1490,10 +1656,11 @@ it afterwards (e.g. by starting an C watcher, which is the only event loop modification you are allowed to do). -The callback prototype is C, e.g.: - static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) + static ev_tstamp + my_rescheduler (ev_periodic *w, ev_tstamp now) { return now + 60.; } @@ -1540,7 +1707,7 @@ take effect when the periodic timer fires or C is being called. -=item ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write] +=item ev_tstamp (*reschedule_cb)(ev_periodic *w, ev_tstamp now) [read-write] The current reschedule callback, or C<0>, if this functionality is switched off. Can be changed any time, but changes only take effect when @@ -1555,12 +1722,12 @@ potentially a lot of jitter, but good long-term stability. static void - clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) + clock_cb (struct ev_loop *loop, ev_io *w, int revents) { ... its now a full hour (UTC, or TAI or whatever your clock follows) } - struct ev_periodic hourly_tick; + ev_periodic hourly_tick; ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0); ev_periodic_start (loop, &hourly_tick); @@ -1569,7 +1736,7 @@ #include static ev_tstamp - my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) + my_scheduler_cb (ev_periodic *w, ev_tstamp now) { return now + (3600. - fmod (now, 3600.)); } @@ -1578,7 +1745,7 @@ Example: Call a callback every hour, starting now: - struct ev_periodic hourly_tick; + ev_periodic hourly_tick; ev_periodic_init (&hourly_tick, clock_cb, fmod (ev_now (loop), 3600.), 3600., 0); ev_periodic_start (loop, &hourly_tick); @@ -1627,17 +1794,17 @@ =head3 Examples -Example: Try to exit cleanly on SIGINT and SIGTERM. +Example: Try to exit cleanly on SIGINT. static void - sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) + sigint_cb (struct ev_loop *loop, ev_signal *w, int revents) { ev_unloop (loop, EVUNLOOP_ALL); } - struct ev_signal signal_watcher; + ev_signal signal_watcher; ev_signal_init (&signal_watcher, sigint_cb, SIGINT); - ev_signal_start (loop, &sigint_cb); + ev_signal_start (loop, &signal_watcher); =head2 C - watch out for process status changes @@ -1720,7 +1887,7 @@ ev_child cw; static void - child_cb (EV_P_ struct ev_child *w, int revents) + child_cb (EV_P_ ev_child *w, int revents) { ev_child_stop (EV_A_ w); printf ("process %d exited with status %x\n", w->rpid, w->rstatus); @@ -1794,10 +1961,11 @@ =head3 Inotify and Kqueue -When C support has been compiled into libev (generally only -available with Linux) and present at runtime, it will be used to speed up -change detection where possible. The inotify descriptor will be created lazily -when the first C watcher is being started. +When C support has been compiled into libev (generally +only available with Linux 2.6.25 or above due to bugs in earlier +implementations) and present at runtime, it will be used to speed up +change detection where possible. The inotify descriptor will be created +lazily when the first C watcher is being started. Inotify presence does not change the semantics of C watchers except that changes might be detected earlier, and in some cases, to avoid @@ -1981,14 +2149,14 @@ callback, free it. Also, use no error checking, as usual. static void - idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) + idle_cb (struct ev_loop *loop, ev_idle *w, int revents) { free (w); // now do something you wanted to do when the program has // no longer anything immediate to do. } - struct ev_idle *idle_watcher = malloc (sizeof (struct ev_idle)); + ev_idle *idle_watcher = malloc (sizeof (ev_idle)); ev_idle_init (idle_watcher, idle_cb); ev_idle_start (loop, idle_cb); @@ -2079,13 +2247,13 @@ static ev_timer tw; static void - io_cb (ev_loop *loop, ev_io *w, int revents) + io_cb (struct ev_loop *loop, ev_io *w, int revents) { } // create io watchers for each fd and a timer before blocking static void - adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) + adns_prepare_cb (struct ev_loop *loop, ev_prepare *w, int revents) { int timeout = 3600000; struct pollfd fds [nfd]; @@ -2110,7 +2278,7 @@ // stop all watchers after blocking static void - adns_check_cb (ev_loop *loop, ev_check *w, int revents) + adns_check_cb (struct ev_loop *loop, ev_check *w, int revents) { ev_timer_stop (loop, &tw); @@ -2288,7 +2456,7 @@ struct ev_loop *loop_hi = ev_default_init (0); struct ev_loop *loop_lo = 0; - struct ev_embed embed; + ev_embed embed; // see if there is a chance of getting one that works // (remember that a flags value of 0 means autodetection) @@ -2312,7 +2480,7 @@ struct ev_loop *loop = ev_default_init (0); struct ev_loop *loop_socket = 0; - struct ev_embed embed; + ev_embed embed; if (ev_supported_backends () & ~ev_recommended_backends () & EVBACKEND_KQUEUE) if ((loop_socket = ev_loop_new (EVBACKEND_KQUEUE)) @@ -2386,8 +2554,8 @@ =item queueing from a signal handler context To implement race-free queueing, you simply add to the queue in the signal -handler but you block the signal handler in the watcher callback. Here is an example that does that for -some fictitious SIGUSR1 handler: +handler but you block the signal handler in the watcher callback. Here is +an example that does that for some fictitious SIGUSR1 handler: static ev_async mysig; @@ -2504,47 +2672,50 @@ =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) This function combines a simple timer and an I/O watcher, calls your -callback on whichever event happens first and automatically stop both +callback on whichever event happens first and automatically stops both watchers. This is useful if you want to wait for a single event on an fd or timeout without having to allocate/configure/start/stop/free one or more watchers yourself. -If C is less than 0, then no I/O watcher will be started and events -is being ignored. Otherwise, an C watcher for the given C and -C set will be created and started. +If C is less than 0, then no I/O watcher will be started and the +C argument is being ignored. Otherwise, an C watcher for +the given C and C set will be created and started. If C is less than 0, then no timeout watcher will be started. Otherwise an C watcher with after = C (and -repeat = 0) will be started. While C<0> is a valid timeout, it is of -dubious value. +repeat = 0) will be started. C<0> is a valid timeout. The callback has the type C and gets passed an C set like normal event callbacks (a combination of C, C, C or C) and the C -value passed to 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. + +Example: wait up to ten seconds for data to appear on STDIN_FILENO. static void stdin_ready (int revents, void *arg) { - if (revents & EV_TIMEOUT) - /* doh, nothing entered */; - else if (revents & EV_READ) + if (revents & EV_READ) /* stdin might have data for us, joy! */; + else if (revents & EV_TIMEOUT) + /* doh, nothing entered */; } ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); -=item ev_feed_event (ev_loop *, watcher *, int revents) +=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 (ev_loop *, int fd, int revents) +=item ev_feed_fd_event (struct ev_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 (ev_loop *loop, int signum) +=item ev_feed_signal_event (struct ev_loop *loop, int signum) Feed an event as if the given signal occurred (C must be the default loop!). @@ -2789,6 +2960,11 @@ Leandro Lucarella has written a D language binding (F) for libev, to be found at L. +=item Ocaml + +Erkki Seppala has written Ocaml bindings for libev, to be found at +L. + =back @@ -3308,17 +3484,18 @@ #include "ev_cpp.h" #include "ev.c" +=head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES -=head1 THREADS AND COROUTINES +=head2 THREADS AND COROUTINES -=head2 THREADS +=head3 THREADS All libev functions are reentrant and thread-safe unless explicitly -documented otherwise, but it uses no locking itself. This means that you -can use as many loops as you want in parallel, as long as there are no -concurrent calls into any libev function with the same loop parameter -(C calls have an implicit default loop parameter, of -course): libev guarantees that different event loops share no data +documented otherwise, but libev implements no locking itself. This means +that you can use as many loops as you want in parallel, as long as there +are no concurrent calls into any libev function with the same loop +parameter (C calls have an implicit default loop parameter, +of course): libev guarantees that different event loops share no data structures that need any locking. Or to put it differently: calls with different loop parameters can be done @@ -3368,90 +3545,84 @@ =back -=head2 COROUTINES +=head3 COROUTINES -Libev is much more accommodating to coroutines ("cooperative threads"): -libev fully supports nesting calls to it's functions from different +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 +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 coroutine switches. - - -=head1 COMPLEXITIES - -In this section the complexities of (many of) the algorithms used inside -libev will be explained. For complexity discussions about backends see the -documentation for C. - -All of the following are about amortised time: If an array needs to be -extended, libev needs to realloc and move the whole array, but this -happens asymptotically never with higher number of elements, so O(1) might -mean it might do a lengthy realloc operation in rare cases, but on average -it is much faster and asymptotically approaches constant time. - -=over 4 - -=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) - -This means that, when you have a watcher that triggers in one hour and -there are 100 watchers that would trigger before that then inserting will -have to skip roughly seven (C) of these watchers. - -=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) +C, and other calls do not usually allow for coroutine switches as +they do not clal any callbacks. -That means that changing a timer costs less than removing/adding them -as only the relative motion in the event queue has to be paid for. +=head2 COMPILER WARNINGS -=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) - -These just add the watcher into an array or at the head of a list. +Depending on your compiler and compiler settings, you might get no or a +lot of warnings when compiling libev code. Some people are apparently +scared by this. -=item Stopping check/prepare/idle/fork/async watchers: O(1) +However, these are unavoidable for many reasons. For one, each compiler +has different warnings, and each user has different tastes regarding +warning options. "Warn-free" code therefore cannot be a goal except when +targeting a specific compiler and compiler-version. -=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) +Another reason is that some compiler warnings require elaborate +workarounds, or other changes to the code that make it less clear and less +maintainable. -These watchers are stored in lists then need to be walked to find the -correct watcher to remove. The lists are usually short (you don't usually -have many watchers waiting for the same fd or signal). +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 +been fixed, but some people still insist on making code warn-free with +such buggy versions. -=item Finding the next timer in each loop iteration: O(1) +While libev is written to generate as few warnings as possible, +"warn-free" code is not a goal, and it is recommended not to build libev +with any compiler warnings enabled unless you are prepared to cope with +them (e.g. by ignoring them). Remember that warnings are just that: +warnings, not errors, or proof of bugs. -By virtue of using a binary or 4-heap, the next timer is always found at a -fixed position in the storage array. -=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) +=head2 VALGRIND -A change means an I/O watcher gets started or stopped, which requires -libev to recalculate its status (and possibly tell the kernel, depending -on backend and whether C was used). +Valgrind has a special section here because it is a popular tool that is +highly useful. Unfortunately, valgrind reports are very hard to interpret. -=item Activating one watcher (putting it into the pending state): O(1) +If you think you found a bug (memory leak, uninitialised data access etc.) +in libev, then check twice: If valgrind reports something like: -=item Priority handling: O(number_of_priorities) + ==2274== definitely lost: 0 bytes in 0 blocks. + ==2274== possibly lost: 0 bytes in 0 blocks. + ==2274== still reachable: 256 bytes in 1 blocks. -Priorities are implemented by allocating some space for each -priority. When doing priority-based operations, libev usually has to -linearly search all the priorities, but starting/stopping and activating -watchers becomes O(1) with respect to priority handling. +Then there is no memory leak, just as memory accounted to global variables +is not a memleak - the memory is still being refernced, and didn't leak. -=item Sending an ev_async: O(1) +Similarly, under some circumstances, valgrind might report kernel bugs +as if it were a bug in libev (e.g. in realloc or in the poll backend, +although an acceptable workaround has been found here), or it might be +confused. -=item Processing ev_async_send: O(number_of_async_watchers) +Keep in mind that valgrind is a very good tool, but only a tool. Don't +make it into some kind of religion. -=item Processing signals: O(max_signal_number) +If you are unsure about something, feel free to contact the mailing list +with the full valgrind report and an explanation on why you think this +is a bug in libev (best check the archives, too :). However, don't be +annoyed when you get a brisk "this is no bug" answer and take the chance +of learning how to interpret valgrind properly. -Sending involves a system call I there were no other C -calls in the current loop iteration. Checking for async and signal events -involves iterating over all running async watchers or all signal numbers. +If you need, for some reason, empty reports from valgrind for your project +I suggest using suppression lists. -=back +=head1 PORTABILITY NOTES -=head1 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS +=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS Win32 doesn't support any of the standards (e.g. POSIX) that libev requires, and its I/O model is fundamentally incompatible with the POSIX @@ -3548,11 +3719,10 @@ =back +=head2 PORTABILITY REQUIREMENTS -=head1 PORTABILITY REQUIREMENTS - -In addition to a working ISO-C implementation, libev relies on a few -additional extensions: +In addition to a working ISO-C implementation and of course the +backend-specific APIs, libev relies on a few additional extensions: =over 4 @@ -3587,11 +3757,11 @@ =item C must be large enough for common memory allocation sizes -To improve portability and simplify using libev, libev uses C -internally instead of C when allocating its data structures. On -non-POSIX systems (Microsoft...) this might be unexpectedly low, but -is still at least 31 bits everywhere, which is enough for hundreds of -millions of watchers. +To improve portability and simplify its API, libev uses C internally +instead of C when allocating its data structures. On non-POSIX +systems (Microsoft...) this might be unexpectedly low, but is still at +least 31 bits everywhere, which is enough for hundreds of millions of +watchers. =item C must hold a time value in seconds with enough accuracy @@ -3605,56 +3775,75 @@ If you know of other additional requirements drop me a note. -=head1 COMPILER WARNINGS +=head1 ALGORITHMIC COMPLEXITIES -Depending on your compiler and compiler settings, you might get no or a -lot of warnings when compiling libev code. Some people are apparently -scared by this. +In this section the complexities of (many of) the algorithms used inside +libev will be documented. For complexity discussions about backends see +the documentation for C. -However, these are unavoidable for many reasons. For one, each compiler -has different warnings, and each user has different tastes regarding -warning options. "Warn-free" code therefore cannot be a goal except when -targeting a specific compiler and compiler-version. +All of the following are about amortised time: If an array needs to be +extended, libev needs to realloc and move the whole array, but this +happens asymptotically rarer with higher number of elements, so O(1) might +mean that libev does a lengthy realloc operation in rare cases, but on +average it is much faster and asymptotically approaches constant time. -Another reason is that some compiler warnings require elaborate -workarounds, or other changes to the code that make it less clear and less -maintainable. +=over 4 -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). +=item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) -While libev is written to generate as few warnings as possible, -"warn-free" code is not a goal, and it is recommended not to build libev -with any compiler warnings enabled unless you are prepared to cope with -them (e.g. by ignoring them). Remember that warnings are just that: -warnings, not errors, or proof of bugs. +This means that, when you have a watcher that triggers in one hour and +there are 100 watchers that would trigger before that, then inserting will +have to skip roughly seven (C) of these watchers. +=item Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers) -=head1 VALGRIND +That means that changing a timer costs less than removing/adding them, +as only the relative motion in the event queue has to be paid for. -Valgrind has a special section here because it is a popular tool that is -highly useful, but valgrind reports are very hard to interpret. +=item Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1) -If you think you found a bug (memory leak, uninitialised data access etc.) -in libev, then check twice: If valgrind reports something like: +These just add the watcher into an array or at the head of a list. - ==2274== definitely lost: 0 bytes in 0 blocks. - ==2274== possibly lost: 0 bytes in 0 blocks. - ==2274== still reachable: 256 bytes in 1 blocks. +=item Stopping check/prepare/idle/fork/async watchers: O(1) + +=item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) -Then there is no memory leak. Similarly, under some circumstances, -valgrind might report kernel bugs as if it were a bug in libev, or it -might be confused (it is a very good tool, but only a tool). +These watchers are stored in lists, so they need to be walked to find the +correct watcher to remove. The lists are usually short (you don't usually +have many watchers waiting for the same fd or signal: one is typical, two +is rare). -If you are unsure about something, feel free to contact the mailing list -with the full valgrind report and an explanation on why you think this is -a bug in libev. However, don't be annoyed when you get a brisk "this is -no bug" answer and take the chance of learning how to interpret valgrind -properly. +=item Finding the next timer in each loop iteration: O(1) -If you need, for some reason, empty reports from valgrind for your project -I suggest using suppression lists. +By virtue of using a binary or 4-heap, the next timer is always found at a +fixed position in the storage array. + +=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) + +A change means an I/O watcher gets started or stopped, which requires +libev to recalculate its status (and possibly tell the kernel, depending +on backend and whether C was used). + +=item Activating one watcher (putting it into the pending state): O(1) + +=item Priority handling: O(number_of_priorities) + +Priorities are implemented by allocating some space for each +priority. When doing priority-based operations, libev usually has to +linearly search all the priorities, but starting/stopping and activating +watchers becomes O(1) with respect to priority handling. + +=item Sending an ev_async: O(1) + +=item Processing ev_async_send: O(number_of_async_watchers) + +=item Processing signals: O(max_signal_number) + +Sending involves a system call I there were no other C +calls in the current loop iteration. Checking for async and signal events +involves iterating over all running async watchers or all signal numbers. + +=back =head1 AUTHOR