--- libev/ev.pod 2007/12/31 01:30:53 1.113 +++ libev/ev.pod 2008/05/21 12:51:38 1.158 @@ -8,51 +8,65 @@ =head2 EXAMPLE PROGRAM + // a single header file is required #include + // every watcher type has its own typedef'd struct + // with the name ev_ ev_io stdin_watcher; ev_timer timeout_watcher; - /* called when data readable on stdin */ + // 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) { - /* puts ("stdin ready"); */ - ev_io_stop (EV_A_ w); /* just a syntax example */ - ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ + puts ("stdin ready"); + // for one-shot events, one must manually stop the watcher + // with its corresponding stop function. + ev_io_stop (EV_A_ w); + + // this causes all nested ev_loop's to stop iterating + ev_unloop (EV_A_ EVUNLOOP_ALL); } + // another callback, this time for a time-out static void timeout_cb (EV_P_ struct ev_timer *w, int revents) { - /* puts ("timeout"); */ - ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ + puts ("timeout"); + // this causes the innermost ev_loop to stop iterating + ev_unloop (EV_A_ EVUNLOOP_ONE); } int main (void) { + // use the default event loop unless you have special needs struct ev_loop *loop = ev_default_loop (0); - /* initialise an io watcher, then start it */ + // initialise an io watcher, then start it + // this one will watch for stdin to become readable ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); ev_io_start (loop, &stdin_watcher); - /* simple non-repeating 5.5 second timeout */ + // initialise a timer watcher, then start it + // simple non-repeating 5.5 second timeout ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); ev_timer_start (loop, &timeout_watcher); - /* loop till timeout or data ready */ + // now wait for events to arrive ev_loop (loop, 0); + // unloop was called, so exit return 0; } =head1 DESCRIPTION -The newest version of this document is also available as a html-formatted +The newest version of this document is also available as an html-formatted web page you might find easier to navigate when reading it for the first -time: L. +time: L. Libev is an event loop: you register interest in certain events (such as a file descriptor being readable or a timeout occurring), and it will manage @@ -86,12 +100,13 @@ =head2 CONVENTIONS -Libev is very configurable. In this manual the default configuration will -be described, which supports multiple event loops. For 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 this argument. +Libev is very configurable. In this manual the default (and most common) +configuration will be described, which supports multiple event loops. For +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 +this argument. =head2 TIME REPRESENTATION @@ -183,18 +198,21 @@ =item ev_set_allocator (void *(*cb)(void *ptr, long size)) Sets the allocation function to use (the prototype is similar - the -semantics is identical - to the realloc C function). It is used to -allocate and free memory (no surprises here). If it returns zero when -memory needs to be allocated, the library might abort or take some -potentially destructive action. The default is your system realloc -function. +semantics are identical to the C C89/SuS/POSIX function). It is +used to allocate and free memory (no surprises here). If it returns zero +when memory needs to be allocated (C), the library might abort +or take some potentially destructive action. + +Since some systems (at least OpenBSD and Darwin) fail to implement +correct C semantics, libev will use a wrapper around the system +C and C functions by default. You could override this function in high-availability programs to, say, free some memory if it cannot allocate memory, to use a special allocator, or even to sleep a while and retry until some memory is available. Example: Replace the libev allocator with one that waits a bit and then -retries). +retries (example requires a standards-compliant C). static void * persistent_realloc (void *ptr, size_t size) @@ -243,13 +261,6 @@ types of such loops, the I loop, which supports signals and child events, and dynamically created loops which do not. -If you use threads, a common model is to run the default event loop -in your main thread (or in a separate thread) and for each thread you -create, you also create another event loop. Libev itself does no locking -whatsoever, so if you mix calls to the same event loop in different -threads, make sure you lock (this is usually a bad idea, though, even if -done correctly, because it's hideous and inefficient). - =over 4 =item struct ev_loop *ev_default_loop (unsigned int flags) @@ -262,6 +273,17 @@ If you don't know what event loop to use, use the one returned from this function. +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). + +The default loop is the only loop that can handle C and +C watchers, and to do this, it always registers a handler +for C. If this is a problem for your app you can either +create a dynamic loop with C that doesn't do that, or you +can simply overwrite the C signal handler I calling +C. + The flags argument can be used to specify special behaviour or specific backends to use, and is usually specified as C<0> (or C). @@ -292,8 +314,8 @@ This works by calling C on every iteration of the loop, and thus this might slow down your event loop if you do a lot of loop iterations and little real work, but is usually not noticeable (on my -Linux system for example, C is actually a simple 5-insn sequence -without a syscall and thus I fast, but my Linux system also has +GNU/Linux system for example, C is actually a simple 5-insn sequence +without a syscall and thus I fast, but my GNU/Linux system also has C which is even faster). The big advantage of this flag is that you can forget about fork (and @@ -316,7 +338,7 @@ writing a server, you should C in a loop to accept as many connections as possible during one iteration. You might also want to have a look at C to increase the amount of -readyness notifications you get per iteration. +readiness notifications you get per iteration. =item C (value 2, poll backend, available everywhere except on windows) @@ -334,7 +356,7 @@ 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 rewiring a syscall per fd change, no fork support and bad +cases and requiring a syscall per fd change, no fork support and bad support for dup. While stopping, setting and starting an I/O watcher in the same iteration @@ -405,6 +427,10 @@ descriptors a "slow" C or C backend might perform better. +On the positive side, ignoring the spurious readiness notifications, this +backend actually performed to specification in all tests and is fully +embeddable, which is a rare feat among the OS-specific backends. + =item C Try all backends (even potentially broken ones that wouldn't be tried @@ -416,9 +442,8 @@ =back If one or more of these are ored into the flags value, then only these -backends will be tried (in the reverse order as given here). If none are -specified, most compiled-in backend will be tried, usually in reverse -order of their flag values :) +backends will be tried (in the reverse order as listed here). If none are +specified, all backends in C will be tried. The most typical usage is like this: @@ -443,6 +468,10 @@ handle signal and child watchers, and attempts to do so will be greeted by undefined behaviour (or a failed assertion if assertions are enabled). +Note that this function I thread-safe, and the recommended way to use +libev with threads is indeed to create one loop per thread, and using the +default loop in the "main" or "initial" thread. + Example: Try to create a event loop that uses epoll and nothing else. struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); @@ -475,14 +504,16 @@ =item ev_default_fork () -This function reinitialises the kernel state for backends that have -one. Despite the name, you can call it anytime, but it makes most sense -after forking, in either the parent or child process (or both, but that -again makes little sense). - -You I call this function in the child process after forking if and -only if you want to use the event library in both processes. If you just -fork+exec, you don't have to call it. +This function sets a flag that causes subsequent C iterations +to reinitialise the kernel state for backends that have one. Despite the +name, you can call it anytime, but it makes most sense after forking, in +the child process (or both child and parent, but that again makes little +sense). You I call it in the child before using any of the libev +functions, and it will only take effect at the next C iteration. + +On the other hand, you only need to call this function in the child +process if and only if you want to use the event library in the child. If +you just fork+exec, you don't have to call it at all. The function itself is quite fast and it's usually not a problem to call it just in case after a fork. To make this easy, the function will fit in @@ -490,16 +521,16 @@ pthread_atfork (0, 0, ev_default_fork); -At the moment, C and C are safe to use -without calling this function, so if you force one of those backends you -do not need to care. - =item ev_loop_fork (loop) Like C, but acts on an event loop created by C. Yes, you have to call this on every allocated event loop after fork, and how you do this is entirely your own problem. +=item int ev_is_default_loop (loop) + +Returns true when the given loop actually is the default loop, false otherwise. + =item unsigned int ev_loop_count (loop) Returns the count of loop iterations for the loop, which is identical to @@ -577,7 +608,7 @@ were used, or there are no active watchers, return, otherwise continue with step *. -Example: Queue some jobs and then loop until no events are outsanding +Example: Queue some jobs and then loop until no events are outstanding anymore. ... queue jobs here, make sure they register event watchers as long @@ -592,6 +623,8 @@ C, which will make the innermost C call return, or C, which will make all nested C calls return. +This "unloop state" will be cleared when entering C again. + =item ev_ref (loop) =item ev_unref (loop) @@ -605,7 +638,9 @@ visible to the libev user and should not keep C from exiting if no event watchers registered by it are active. It is also an excellent way to do this for generic recurring timers or from within third-party -libraries. Just remember to I and I. +libraries. Just remember to I and I +(but only if the watcher wasn't active before, or was active before, +respectively). Example: Create a signal watcher, but keep it from keeping C running when nothing else is active. @@ -764,6 +799,10 @@ The event loop has been resumed in the child process after fork (see C). +=item C + +The given async watcher has been asynchronously notified (see C). + =item C An unspecified error has occured, the watcher has been stopped. This might @@ -995,7 +1034,7 @@ C). Another thing you have to watch out for is that it is quite easy to -receive "spurious" readyness notifications, that is your callback might +receive "spurious" readiness notifications, that is your callback might be called with C but a subsequent C(2) will actually block because there is no data. Not only are some backends known to create a lot of those (for example solaris ports), it is very easy to get into @@ -1052,6 +1091,18 @@ enable C, or resort to C or C. +=head3 The special problem of SIGPIPE + +While not really specific to libev, it is easy to forget about SIGPIPE: +when reading from a pipe whose other end has been closed, your program +gets send a SIGPIPE, which, by default, aborts your program. For most +programs this is sensible behaviour, for daemons, this is usually +undesirable. + +So when you encounter spurious, unexplained daemon exits, make sure you +ignore SIGPIPE (and maybe make sure you log the exit status of your daemon +somewhere, as that would have given you a big clue). + =head3 Watcher-Specific Functions @@ -1102,8 +1153,8 @@ given time, and optionally repeating in regular intervals after that. The timers are based on real time, that is, if you register an event that -times out after an hour and you reset your system clock to last years -time, it will still time out after (roughly) and hour. "Roughly" because +times out after an hour and you reset your system clock to january last +year, it will still time out after (roughly) and hour. "Roughly" because detecting time jumps is hard, and some inaccuracies are unavoidable (the monotonic clock option helps a lot here). @@ -1115,7 +1166,7 @@ ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); -The callback is guarenteed to be invoked only when its timeout has passed, +The callback is guarenteed to be invoked only after its timeout has passed, but if multiple timers become ready during the same loop iteration then order of execution is undefined. @@ -1127,18 +1178,19 @@ =item ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat) -Configure the timer to trigger after C seconds. If C is -C<0.>, then it will automatically be stopped. If it is positive, then the -timer will automatically be configured to trigger again C seconds -later, again, and again, until stopped manually. - -The timer itself will do a best-effort at avoiding drift, that is, if you -configure a timer to trigger every 10 seconds, then it will trigger at -exactly 10 second intervals. If, however, your program cannot keep up with -the timer (because it takes longer than those 10 seconds to do stuff) the -timer will not fire more than once per event loop iteration. +Configure the timer to trigger after C seconds. If C +is C<0.>, then it will automatically be stopped once the timeout is +reached. If it is positive, then the timer will automatically be +configured to trigger again C seconds later, again, and again, +until stopped manually. + +The timer itself will do a best-effort at avoiding drift, that is, if +you configure a timer to trigger every 10 seconds, then it will normally +trigger at exactly 10 second intervals. If, however, your program cannot +keep up with the timer (because it takes longer than those 10 seconds to +do stuff) the timer will not fire more than once per event loop iteration. -=item ev_timer_again (loop) +=item ev_timer_again (loop, ev_timer *) This will act as if the timer timed out and restart it again if it is repeating. The exact semantics are: @@ -1223,18 +1275,19 @@ Unlike C's, they are not based on real time (or relative time) but on wallclock time (absolute time). You can tell a periodic watcher -to trigger "at" some specific point in time. For example, if you tell a +to trigger after some specific point in time. For example, if you tell a periodic watcher to trigger in 10 seconds (by specifiying e.g. C) and then reset your system clock to the last year, then it will -take a year to trigger the event (unlike an C, which would trigger -roughly 10 seconds later). - -They can also be used to implement vastly more complex timers, such as -triggering an event on each midnight, local time or other, complicated, -rules. ++ 10.>, that is, an absolute time not a delay) and then reset your system +clock to january of the previous year, then it will take more than year +to trigger the event (unlike an C, which would still trigger +roughly 10 seconds later as it uses a relative timeout). + +Cs can also be used to implement vastly more complex timers, +such as triggering an event on each "midnight, local time", or other +complicated, rules. As with timers, the callback is guarenteed to be invoked only when the -time (C) has been passed, but if multiple periodic timers become ready +time (C) has passed, but if multiple periodic timers become ready during the same loop iteration then order of execution is undefined. =head3 Watcher-Specific Functions and Data Members @@ -1252,19 +1305,20 @@ =item * absolute timer (at = time, interval = reschedule_cb = 0) -In this configuration the watcher triggers an event at the wallclock time -C and doesn't repeat. It will not adjust when a time jump occurs, -that is, if it is to be run at January 1st 2011 then it will run when the -system time reaches or surpasses this time. +In this configuration the watcher triggers an event after the wallclock +time C has passed and doesn't repeat. It will not adjust when a time +jump occurs, that is, if it is to be run at January 1st 2011 then it will +run when the system time reaches or surpasses this time. -=item * non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) +=item * repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) In this mode the watcher will always be scheduled to time out at the next C time (for some integer N, which can also be negative) and then repeat, regardless of any time jumps. This can be used to create timers that do not drift with respect to system -time: +time, for example, here is a C that triggers each hour, on +the hour: ev_periodic_set (&periodic, 0., 3600., 0); @@ -1279,7 +1333,12 @@ For numerical stability it is preferable that the C value is near C (the current time), but there is no range requirement for -this value. +this value, and in fact is often specified as zero. + +Note also that there is an upper limit to how often a timer can fire (cpu +speed for example), so if C is very small then timing stability +will of course detoriate. Libev itself tries to be exact to be about one +millisecond (if the OS supports it and the machine is fast enough). =item * manual reschedule mode (at and interval ignored, reschedule_cb = callback) @@ -1289,12 +1348,14 @@ current time as second argument. NOTE: I. 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 legal). +ever, or make ANY event loop modifications whatsoever>. -Its prototype is C, e.g.: +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) { @@ -1306,11 +1367,11 @@ will usually be called just before the callback will be triggered, but might be called at other times, too. -NOTE: I<< This callback must always return a time that is later than the -passed C value >>. Not even C itself will do, it I be larger. +NOTE: I<< This callback must always return a time that is higher than or +equal to the passed C value >>. This can be used to create very complex timers, such as a timer that -triggers on each midnight, local time. To do this, you would calculate the +triggers on "next midnight, local time". To do this, you would calculate the next midnight after C and return the timestamp value for this. How you do this is, again, up to you (but it is not trivial, which is the main reason I omitted it as an example). @@ -1324,6 +1385,11 @@ a different time than the last time it was called (e.g. in a crond like program when the crontabs have changed). +=item ev_tstamp ev_periodic_at (ev_periodic *) + +When active, returns the absolute time that the watcher is supposed to +trigger next. + =item ev_tstamp offset [read-write] When repeating, this contains the offset value, otherwise this is the @@ -1344,11 +1410,6 @@ switched off. Can be changed any time, but changes only take effect when the periodic timer fires or C is being called. -=item ev_tstamp at [read-only] - -When active, contains the absolute time that the watcher is supposed to -trigger next. - =back =head3 Examples @@ -1401,6 +1462,12 @@ watcher for a signal is stopped libev will reset the signal handler to SIG_DFL (regardless of what it was set to before). +If possible and supported, libev will install its handlers with +C behaviour enabled, so syscalls should not be unduly +interrupted. If you have a problem with syscalls getting interrupted by +signals you can block all signals in an C watcher and unblock +them in an C watcher. + =head3 Watcher-Specific Functions and Data Members =over 4 @@ -1418,26 +1485,67 @@ =back +=head3 Examples + +Example: Try to exit cleanly on SIGINT and SIGTERM. + + static void + sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) + { + ev_unloop (loop, EVUNLOOP_ALL); + } + + struct ev_signal signal_watcher; + ev_signal_init (&signal_watcher, sigint_cb, SIGINT); + ev_signal_start (loop, &sigint_cb); + =head2 C - watch out for process status changes Child watchers trigger when your process receives a SIGCHLD in response to -some child status changes (most typically when a child of yours dies). +some child status changes (most typically when a child of yours dies). It +is permissible to install a child watcher I the child has been +forked (which implies it might have already exited), as long as the event +loop isn't entered (or is continued from a watcher). + +Only the default event loop is capable of handling signals, and therefore +you can only rgeister child watchers in the default event loop. + +=head3 Process Interaction + +Libev grabs C as soon as the default event loop is +initialised. This is necessary to guarantee proper behaviour even if +the first child watcher is started after the child exits. The occurance +of C is recorded asynchronously, but child reaping is done +synchronously as part of the event loop processing. Libev always reaps all +children, even ones not watched. + +=head3 Overriding the Built-In Processing + +Libev offers no special support for overriding the built-in child +processing, but if your application collides with libev's default child +handler, you can override it easily by installing your own handler for +C after initialising the default loop, and making sure the +default loop never gets destroyed. You are encouraged, however, to use an +event-based approach to child reaping and thus use libev's support for +that, so other libev users can use C watchers freely. =head3 Watcher-Specific Functions and Data Members =over 4 -=item ev_child_init (ev_child *, callback, int pid) +=item ev_child_init (ev_child *, callback, int pid, int trace) -=item ev_child_set (ev_child *, int pid) +=item ev_child_set (ev_child *, int pid, int trace) Configures the watcher to wait for status changes of process C (or I process if C is specified as C<0>). The callback can look at the C member of the C watcher structure to see the status word (use the macros from C and see your systems C documentation). The C member contains the pid of the -process causing the status change. +process causing the status change. C must be either C<0> (only +activate the watcher when the process terminates) or C<1> (additionally +activate the watcher when the process is stopped or continued). =item int pid [read-only] @@ -1456,17 +1564,32 @@ =head3 Examples -Example: Try to exit cleanly on SIGINT and SIGTERM. +Example: C a new process and install a child handler to wait for +its completion. + + ev_child cw; static void - sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) + child_cb (EV_P_ struct ev_child *w, int revents) { - ev_unloop (loop, EVUNLOOP_ALL); + ev_child_stop (EV_A_ w); + printf ("process %d exited with status %x\n", w->rpid, w->rstatus); } - struct ev_signal signal_watcher; - ev_signal_init (&signal_watcher, sigint_cb, SIGINT); - ev_signal_start (loop, &sigint_cb); + pid_t pid = fork (); + + if (pid < 0) + // error + else if (pid == 0) + { + // the forked child executes here + exit (1); + } + else + { + ev_child_init (&cw, child_cb, pid, 0); + ev_child_start (EV_DEFAULT_ &cw); + } =head2 C - did the file attributes just change? @@ -1499,11 +1622,23 @@ At the time of this writing, only the Linux inotify interface is implemented (implementing kqueue support is left as an exercise for the -reader). Inotify will be used to give hints only and should not change the -semantics of C watchers, which means that libev sometimes needs -to fall back to regular polling again even with inotify, but changes are -usually detected immediately, and if the file exists there will be no -polling. +reader, note, however, that the author sees no way of implementing ev_stat +semantics with kqueue). Inotify will be used to give hints only and should +not change the semantics of C watchers, which means that libev +sometimes needs to fall back to regular polling again even with inotify, +but changes are usually detected immediately, and if the file exists there +will be no polling. + +=head3 ABI Issues (Largefile Support) + +Libev by default (unless the user overrides this) uses the default +compilation environment, which means that on systems with optionally +disabled large file support, you get the 32 bit version of the stat +structure. When using the library from programs that change the ABI to +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 noticably with ev_stat and largefile support. =head3 Inotify @@ -1512,9 +1647,9 @@ change detection where possible. The inotify descriptor will be created lazily when the first C watcher is being started. -Inotify presense does not change the semantics of C watchers +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 presense of inotify support +making regular C calls. Even in the presence of inotify support there are many cases where libev has to resort to regular C polling. (There is no support for kqueue, as apparently it cannot be used to @@ -1527,16 +1662,25 @@ even on systems where the resolution is higher, many filesystems still only support whole seconds. -That means that, if the time is the only thing that changes, you might -miss updates: on the first update, C detects a change and calls -your callback, which does something. When there is another update within -the same second, C will be unable to detect it. - -The solution to this is to delay acting on a change for a second (or till -the next second boundary), using a roughly one-second delay C -(C). The C<.01> -is added to work around small timing inconsistencies of some operating -systems. +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 +calls your callback, which does something. When there is another update +within the same second, C will be unable to detect it as the stat +data does not change. + +The solution to this is to delay acting on a change for slightly more +than a second (or till slightly after the next full second boundary), using +a roughly one-second-delay C (e.g. C). + +The C<.02> offset is added to work around small timing inconsistencies +of some operating systems (where the second counter of the current time +might be be delayed. One such system is the Linux kernel, where a call to +C might return a timestamp with a full second later than +a subsequent C