--- libev/ev.pod 2008/09/30 19:33:33 1.189 +++ libev/ev.pod 2009/03/04 12:51:37 1.226 @@ -11,15 +11,17 @@ // a single header file is required #include + #include // for puts + // 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 +34,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 @@ -105,7 +107,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 +280,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 @@ -296,7 +302,7 @@ Note that this function is I thread-safe, so if you want to use it from multiple threads, you have to lock (note also that this is unlikely, -as loops cannot bes hared easily between threads anyway). +as loops cannot be shared easily between threads anyway). The default loop is the only loop that can handle C and C watchers, and to do this, it always registers a handler @@ -382,26 +388,43 @@ For few fds, this backend is a bit little slower than poll and select, but it scales phenomenally better. While poll and select usually scale like O(total_fds) where n is the total number of fds (or the highest fd), -epoll scales either O(1) or O(active_fds). The epoll design has a number -of shortcomings, such as silently dropping events in some hard-to-detect -cases and requiring a system call per fd change, no fork support and bad -support for dup. +epoll scales either O(1) or O(active_fds). + +The epoll mechanism deserves honorable mention as the most misdesigned +of the more advanced event mechanisms: mere annoyances include silently +dropping file descriptors, requiring a system call per change per file +descriptor (and unnecessary guessing of parameters), problems with dup and +so on. The biggest issue is fork races, however - if a program forks then +I parent and child process have to recreate the epoll set, which can +take considerable time (one syscall per file descriptor) and is of course +hard to detect. + +Epoll is also notoriously buggy - embedding epoll fds I work, but +of course I, and epoll just loves to report events for totally +I file descriptors (even already closed ones, so one cannot +even remove them from the set) than registered in the set (especially +on SMP systems). Libev tries to counter these spurious notifications by +employing an additional generation counter and comparing that against the +events to filter out spurious ones, recreating the set when required. 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 incident -(because the fd could point to a different file description now), so its -best to avoid that. Also, C'ed file descriptors might not work -very well if you register events for both fds. - -Please note that epoll sometimes generates spurious notifications, so you -need to use non-blocking I/O or other means to avoid blocking when no data -(or space) is available. +will result in some caching, there is still a system call per such +incident (because the same I could point to a different +I now), so its best to avoid that. Also, C'ed +file descriptors might not work very well if you register events for both +file descriptors. Best performance from this backend is achieved by not unregistering all watchers for a file descriptor until it has been closed, if possible, i.e. keep at least one watcher active per fd at all times. Stopping and starting a watcher (without re-setting it) also usually doesn't cause -extra overhead. +extra overhead. A fork can both result in spurious notifications as well +as in libev having to destroy and recreate the epoll object, which can +take considerable time and thus should be avoided. + +All this means that, in practice, C can be as fast or +faster than epoll for maybe up to a hundred file descriptors, depending on +the usage. So sad. While nominally embeddable in other event loops, this feature is broken in all kernel versions tested so far. @@ -411,12 +434,15 @@ =item C (value 8, most BSD clones) -Kqueue deserves special mention, as at the time of this writing, it was -broken on all BSDs except NetBSD (usually it doesn't work reliably with -anything but sockets and pipes, except on Darwin, where of course it's -completely useless). For this reason it's not being "auto-detected" unless -you explicitly specify it in the flags (i.e. using C) or -libev was compiled on a known-to-be-good (-enough) system like NetBSD. +Kqueue deserves special mention, as at the time of this writing, it +was broken on all BSDs except NetBSD (usually it doesn't work reliably +with anything but sockets and pipes, except on Darwin, where of course +it's completely useless). Unlike epoll, however, whose brokenness +is by design, these kqueue bugs can (and eventually will) be fixed +without API changes to existing programs. For this reason it's not being +"auto-detected" unless you explicitly specify it in the flags (i.e. using +C) or libev was compiled on a known-to-be-good (-enough) +system like NetBSD. You still can embed kqueue into a normal poll or select backend and use it only for sockets (after having made sure that sockets work with kqueue on @@ -426,8 +452,9 @@ kernel is more efficient (which says nothing about its actual speed, of course). While stopping, setting and starting an I/O watcher does never cause an extra system call as with C, it still adds up to -two event changes per incident. Support for C is very bad and it -drops fds silently in similarly hard-to-detect cases. +two event changes per incident. Support for C is very bad (but +sane, unlike epoll) and it drops fds silently in similarly hard-to-detect +cases This backend usually performs well under most conditions. @@ -435,8 +462,8 @@ everywhere, so you might need to test for this. And since it is broken almost everywhere, you should only use it when you have a lot of sockets (for which it usually works), by embedding it into another event loop -(e.g. C or C) and, did I mention it, -using it only for sockets. +(e.g. C or C (but C is of course +also broken on OS X)) and, did I mention it, using it only for sockets. This backend maps C into an C kevent with C, and C into an C kevent with @@ -466,7 +493,7 @@ On the positive side, with the exception of the spurious readiness notifications, this backend actually performed fully to specification in all tests and is fully embeddable, which is a rare feat among the -OS-specific backends. +OS-specific backends (I vastly prefer correctness over speed hacks). This backend maps C and C in the same way as C. @@ -529,9 +556,9 @@ the easiest thing, you can just ignore the watchers and/or C them for example). -Note that certain global state, such as signal state, will not be freed by -this function, and related watchers (such as signal and child watchers) -would need to be stopped manually. +Note that certain global state, such as signal state (and installed signal +handlers), will not be freed by this function, and related watchers (such +as signal and child watchers) would need to be stopped manually. 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 @@ -633,7 +660,7 @@ 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 guarentee that a +be an event internal to libev itself, so there is no guarantee that a user-registered callback will be called), and will return after one iteration of the loop. @@ -687,6 +714,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 +739,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 +797,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 +811,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 +848,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 +933,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 +951,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 +965,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 +1009,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 +1068,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 +1100,7 @@ struct my_io { - struct ev_io io; + ev_io io; int otherfd; void *somedata; struct whatever *mostinteresting; @@ -1071,7 +1113,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 +1141,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 +1293,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 +1301,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 +1322,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->repeat = 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 +1549,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 +1565,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 +1578,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); @@ -1432,7 +1621,7 @@ =item ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb) -=item ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb) +=item ev_periodic_set (ev_periodic *, ev_tstamp at, ev_tstamp interval, reschedule_cb) Lots of arguments, lets sort it out... There are basically three modes of operation, and we will explain them from simplest to most complex: @@ -1484,16 +1673,17 @@ current time as second argument. NOTE: I. +ever, or make ANY other event loop modifications whatsoever>. If you need to stop it, return C (or so, fudge fudge) and stop 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 +1730,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 +1745,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 +1759,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 +1768,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); @@ -1630,12 +1820,12 @@ 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, &signal_watcher); @@ -1720,7 +1910,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); @@ -1745,35 +1935,38 @@ =head2 C - did the file attributes just change? This watches a file system path for attribute changes. That is, it calls -C regularly (or when the OS says it changed) and sees if it changed -compared to the last time, invoking the callback if it did. +C on that path in regular intervals (or when the OS says it changed) +and sees if it changed compared to the last time, invoking the callback if +it did. The path does not need to exist: changing from "path exists" to "path does -not exist" is a status change like any other. The condition "path does -not exist" is signified by the C field being zero (which is -otherwise always forced to be at least one) and all the other fields of -the stat buffer having unspecified contents. - -The path I be absolute and I end in a slash. If it is -relative and your working directory changes, the behaviour is undefined. - -Since there is no standard kernel interface to do this, the portable -implementation simply calls C regularly on the path to see if -it changed somehow. You can specify a recommended polling interval for -this case. If you specify a polling interval of C<0> (highly recommended!) -then a I value will be used (which -you can expect to be around five seconds, although this might change -dynamically). Libev will also impose a minimum interval which is currently -around C<0.1>, but thats usually overkill. +not exist" is a status change like any other. The condition "path does not +exist" (or more correctly "path cannot be stat'ed") is signified by the +C field being zero (which is otherwise always forced to be at +least one) and all the other fields of the stat buffer having unspecified +contents. + +The path I end in a slash or contain special components such as +C<.> or C<..>. The path I be absolute: If it is relative and +your working directory changes, then the behaviour is undefined. + +Since there is no portable change notification interface available, the +portable implementation simply calls C regularly on the path +to see if it changed somehow. You can specify a recommended polling +interval for this case. If you specify a polling interval of C<0> (highly +recommended!) then a I value will be used +(which you can expect to be around five seconds, although this might +change dynamically). Libev will also impose a minimum interval which is +currently around C<0.1>, but that's usually overkill. This watcher type is not meant for massive numbers of stat watchers, as even with OS-supported change notifications, this can be resource-intensive. At the time of this writing, the only OS-specific interface implemented -is the Linux inotify interface (implementing kqueue support is left as -an exercise for the reader. Note, however, that the author sees no way -of implementing C semantics with kqueue). +is the Linux inotify interface (implementing kqueue support is left as an +exercise for the reader. Note, however, that the author sees no way of +implementing C semantics with kqueue, except as a hint). =head3 ABI Issues (Largefile Support) @@ -1784,7 +1977,7 @@ use 64 bit file offsets the programs will fail. In that case you have to compile libev with the same flags to get binary compatibility. This is obviously the case with any flags that change the ABI, but the problem is -most noticeably disabled with ev_stat and large file support. +most noticeably displayed with ev_stat and large file support. The solution for this is to lobby your distribution maker to make large file interfaces available by default (as e.g. FreeBSD does) and not @@ -1794,27 +1987,48 @@ =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 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 making regular C calls. Even in the presence of inotify support there are many cases where libev has to resort to regular C polling, -but as long as the path exists, libev usually gets away without polling. +but as long as kernel 2.6.25 or newer is used (2.6.24 and older have too +many bugs), the path exists (i.e. stat succeeds), and the path resides on +a local filesystem (libev currently assumes only ext2/3, jfs, reiserfs and +xfs are fully working) libev usually gets away without polling. There is no support for kqueue, as apparently it cannot be used to implement this functionality, due to the requirement of having a file descriptor open on the object at all times, and detecting renames, unlinks etc. is difficult. +=head3 C is a synchronous operation + +Libev doesn't normally do any kind of I/O itself, and so is not blocking +the process. The exception are C watchers - those call C, which is a synchronous operation. + +For local paths, this usually doesn't matter: unless the system is very +busy or the intervals between stat's are large, a stat call will be fast, +as the path data is usually in memory already (except when starting the +watcher). + +For networked file systems, calling C can block an indefinite +time due to network issues, and even under good conditions, a stat call +often takes multiple milliseconds. + +Therefore, it is best to avoid using C watchers on networked +paths, although this is fully supported by libev. + =head3 The special problem of stat time resolution -The C system call only supports full-second resolution portably, and -even on systems where the resolution is higher, most file systems still -only support whole seconds. +The C system call only supports full-second resolution portably, +and even on systems where the resolution is higher, most file systems +still only support whole seconds. That means that, if the time is the only thing that changes, you can easily miss updates: on the first update, C detects a change and @@ -1967,7 +2181,7 @@ =over 4 -=item ev_idle_init (ev_signal *, callback) +=item ev_idle_init (ev_idle *, callback) Initialises and configures the idle watcher - it has no parameters of any kind. There is a C macro, but using it is utterly pointless, @@ -1981,14 +2195,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 +2293,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 +2324,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); @@ -2216,24 +2430,20 @@ this case you would put all the high priority stuff in one loop and all the rest in a second one, and embed the second one in the first. -As long as the watcher is active, the callback will be invoked every time -there might be events pending in the embedded loop. The callback must then -call C to make a single sweep and invoke -their callbacks (you could also start an idle watcher to give the embedded -loop strictly lower priority for example). You can also set the callback -to C<0>, in which case the embed watcher will automatically execute the -embedded loop sweep. - -As long as the watcher is started it will automatically handle events. The -callback will be invoked whenever some events have been handled. You can -set the callback to C<0> to avoid having to specify one if you are not -interested in that. - -Also, there have not currently been made special provisions for forking: -when you fork, you not only have to call C on both loops, -but you will also have to stop and restart any C watchers -yourself - but you can use a fork watcher to handle this automatically, -and future versions of libev might do just that. +As long as the watcher is active, the callback will be invoked every +time there might be events pending in the embedded loop. The callback +must then call C to make a single +sweep and invoke their callbacks (the callback doesn't need to invoke the +C function directly, it could also start an idle watcher +to give the embedded loop strictly lower priority for example). + +You can also set the callback to C<0>, in which case the embed watcher +will automatically execute the embedded loop sweep whenever necessary. + +Fork detection will be handled transparently while the C watcher +is active, i.e., the embedded loop will automatically be forked when the +embedding loop forks. In other cases, the user is responsible for calling +C on the embedded loop. Unfortunately, not all backends are embeddable: only the ones returned by C are, which, unfortunately, does not include any @@ -2288,7 +2498,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 +2522,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 +2596,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; @@ -2463,7 +2673,7 @@ =item ev_async_init (ev_async *, callback) Initialises and configures the async watcher - it has no parameters of any -kind. There is a C macro, but using it is utterly pointless, +kind. There is a C macro, but using it is utterly pointless, trust me. =item ev_async_send (loop, ev_async *) @@ -2504,47 +2714,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!). @@ -2676,6 +2889,36 @@ ev::io iow; iow.set (&obj); +=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 +the time. Incidentally, you can then also leave out the template argument +list. + +The C method prototype must be C. + +See the method-C above for more details. + +Example: use a functor object as callback. + + struct myfunctor + { + void operator() (ev::io &w, int revents) + { + ... + } + } + + myfunctor f; + + ev::io w; + w.set (&f); + =item w->set (void *data = 0) Also sets a callback, but uses a static method or plain function as @@ -2784,11 +3027,19 @@ more on top of it. It can be found via gem servers. Its homepage is at L. +Roger Pack reports that using the link order C<-lws2_32 -lmsvcrt-ruby-190> +makes rev work even on mingw. + =item D 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 @@ -2898,7 +3149,7 @@ #include "ev.h" Both header files and implementation files can be compiled with a C++ -compiler (at least, thats a stated goal, and breakage will be treated +compiler (at least, that's a stated goal, and breakage will be treated as a bug). You need the following files in your source tree, or in a directory @@ -2964,24 +3215,40 @@ 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 +configuration, but has to be more conservative. + =item EV_USE_MONOTONIC If defined to be C<1>, libev will try to detect the availability of the -monotonic clock option at both compile time and runtime. Otherwise no use -of the monotonic clock option will be attempted. If you enable this, you -usually have to link against librt or something similar. Enabling it when -the functionality isn't available is safe, though, although you have +monotonic clock option at both compile time and runtime. Otherwise no +use of the monotonic clock option will be attempted. If you enable this, +you usually have to link against librt or something similar. Enabling it +when the functionality isn't available is safe, though, although you have to make sure you link against any libraries where the C -function is hiding in (often F<-lrt>). +function is hiding in (often F<-lrt>). See also C. =item EV_USE_REALTIME If defined to be C<1>, libev will try to detect the availability of the -real-time clock option at compile time (and assume its availability at -runtime if successful). Otherwise no use of the real-time clock option will -be attempted. This effectively replaces C by C and will not normally affect correctness. See the -note about libraries in the description of C, though. +real-time clock option at compile time (and assume its availability +at runtime if successful). Otherwise no use of the real-time clock +option will be attempted. This effectively replaces C +by C and will not normally affect +correctness. See the note about libraries in the description of +C, though. Defaults to the opposite value of +C. + +=item EV_USE_CLOCK_SYSCALL + +If defined to be C<1>, libev will try to use a direct syscall instead +of calling the system-provided C function. This option +exists because on GNU/Linux, C is in C, but C +unconditionally pulls in C, slowing down single-threaded +programs needlessly. Using a direct syscall is slightly slower (in +theory), because no optimised vdso implementation can be used, but avoids +the pthread dependency. Defaults to C<1> on GNU/Linux with glibc 2.x or +higher, as it simplifies linking (no need for C<-lrt>). =item EV_USE_NANOSLEEP @@ -3007,11 +3274,11 @@ If defined to C<1>, then the select backend will use the system C structure. This is useful if libev doesn't compile due to a missing -C or C definition or it mis-guesses the bitset layout on -exotic systems. This usually limits the range of file descriptors to some -low limit such as 1024 or might have other limitations (winsocket only -allows 64 sockets). The C macro, set before compilation, might -influence the size of the C used. +C or C definition or it mis-guesses the bitset layout +on exotic systems. This usually limits the range of file descriptors to +some low limit such as 1024 or might have other limitations (winsocket +only allows 64 sockets). The C macro, set before compilation, +configures the maximum size of the C. =item EV_SELECT_IS_WINSOCKET @@ -3315,11 +3582,11 @@ =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 @@ -3371,15 +3638,16 @@ =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. +C, and other calls do not usually allow for coroutine switches as +they do not call any callbacks. =head2 COMPILER WARNINGS @@ -3410,7 +3678,7 @@ warnings, not errors, or proof of bugs. -=head1 VALGRIND +=head2 VALGRIND Valgrind has a special section here because it is a popular tool that is highly useful. Unfortunately, valgrind reports are very hard to interpret. @@ -3423,7 +3691,7 @@ ==2274== still reachable: 256 bytes in 1 blocks. 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. +is not a memleak - the memory is still being referenced, and didn't leak. 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, @@ -3443,78 +3711,7 @@ I suggest using suppression lists. - -=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) - -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. - -=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. - -=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)) - -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). - -=item Finding the next timer in each loop iteration: O(1) - -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 PORTABILITY +=head1 PORTABILITY NOTES =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS @@ -3669,7 +3866,78 @@ If you know of other additional requirements drop me a note. +=head1 ALGORITHMIC COMPLEXITIES + +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. + +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. + +=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) + +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. + +=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. + +=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)) + +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). + +=item Finding the next timer in each loop iteration: O(1) + +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 -Marc Lehmann . +Marc Lehmann , with repeated corrections by Mikael Magnusson.