--- libev/ev.pod 2007/12/21 05:10:01 1.95 +++ libev/ev.pod 2007/12/25 07:05:45 1.110 @@ -6,7 +6,7 @@ #include -=head1 EXAMPLE PROGRAM +=head2 EXAMPLE PROGRAM #include @@ -67,7 +67,7 @@ details of the event, and then hand it over to libev by I the watcher. -=head1 FEATURES +=head2 FEATURES Libev supports C which have a high +overhead for the actual polling but can deliver many events at once. + +By setting a higher I you allow libev to spend more +time collecting I/O events, so you can handle more events per iteration, +at the cost of increasing latency. Timeouts (both C and +C) will be not affected. Setting this to a non-null value will +introduce an additional C call into most loop iterations. + +Likewise, by setting a higher I you allow libev +to spend more time collecting timeouts, at the expense of increased +latency (the watcher callback will be called later). C watchers +will not be affected. Setting this to a non-null value will not introduce +any overhead in libev. + +Many (busy) programs can usually benefit by setting the io collect +interval to a value near C<0.1> or so, which is often enough for +interactive servers (of course not for games), likewise for timeouts. It +usually doesn't make much sense to set it to a lower value than C<0.01>, +as this approsaches the timing granularity of most systems. + =back @@ -905,12 +985,6 @@ descriptors to non-blocking mode is also usually a good idea (but not required if you know what you are doing). -You have to be careful with dup'ed file descriptors, though. Some backends -(the linux epoll backend is a notable example) cannot handle dup'ed file -descriptors correctly if you register interest in two or more fds pointing -to the same underlying file/socket/etc. description (that is, they share -the same underlying "file open"). - If you must do this, then force the use of a known-to-be-good backend (at the time of this writing, this includes only C and C). @@ -954,12 +1028,12 @@ =head3 The special problem of dup'ed file descriptors Some backends (e.g. epoll), cannot register events for file descriptors, -but only events for the underlying file descriptions. That menas when you -have C'ed file descriptors and register events for them, only one -file descriptor might actually receive events. +but only events for the underlying file descriptions. That means when you +have C'ed file descriptors or weirder constellations, and register +events for them, only one file descriptor might actually receive events. -There is no workaorund possible except not registering events -for potentially C'ed file descriptors or to resort to +There is no workaround possible except not registering events +for potentially C'ed file descriptors, or to resort to C or C. =head3 The special problem of fork @@ -1418,6 +1492,39 @@ usually detected immediately, and if the file exists there will be no polling. +=head3 Inotify + +When C support has been compiled into libev (generally only +available on 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. + +Inotify presense 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 +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 +implement this functionality, due to the requirement of having a file +descriptor open on the object at all times). + +=head3 The special problem of stat time resolution + +The C syscall only supports full-second resolution portably, and +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. + =head3 Watcher-Specific Functions and Data Members =over 4 @@ -1465,6 +1572,8 @@ =back +=head3 Examples + Example: Watch C for attribute changes. static void @@ -1486,8 +1595,36 @@ ... ev_stat passwd; - ev_stat_init (&passwd, passwd_cb, "/etc/passwd"); + ev_stat_init (&passwd, passwd_cb, "/etc/passwd", 0.); + ev_stat_start (loop, &passwd); + +Example: Like above, but additionally use a one-second delay so we do not +miss updates (however, frequent updates will delay processing, too, so +one might do the work both on C callback invocation I on +C callback invocation). + + static ev_stat passwd; + static ev_timer timer; + + static void + timer_cb (EV_P_ ev_timer *w, int revents) + { + ev_timer_stop (EV_A_ w); + + /* now it's one second after the most recent passwd change */ + } + + static void + stat_cb (EV_P_ ev_stat *w, int revents) + { + /* reset the one-second timer */ + ev_timer_again (EV_A_ &timer); + } + + ... + ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.); ev_stat_start (loop, &passwd); + ev_timer_init (&timer, timer_cb, 0., 1.01); =head2 C - when you've got nothing better to do... @@ -1583,11 +1720,11 @@ priority, to ensure that they are being run before any other watchers after the poll. Also, C watchers (and C watchers, too) should not activate ("feed") events into libev. While libev fully -supports this, they will be called before other C watchers did -their job. As C watchers are often used to embed other event -loops those other event loops might be in an unusable state until their -C watcher ran (always remind yourself to coexist peacefully with -others). +supports this, they will be called before other C watchers +did their job. As C watchers are often used to embed other +(non-libev) event loops those other event loops might be in an unusable +state until their C watcher ran (always remind yourself to +coexist peacefully with others). =head3 Watcher-Specific Functions and Data Members @@ -1736,7 +1873,7 @@ This is a rather advanced watcher type that lets you embed one event loop into another (currently only C events are supported in the embedded loop, other types of watchers might be handled in a delayed or incorrect -fashion and must not be used). (See portability notes, below). +fashion and must not be used). There are primarily two reasons you would want that: work around bugs and prioritise I/O. @@ -1801,22 +1938,6 @@ else loop_lo = loop_hi; -=head2 Portability notes - -Kqueue is nominally embeddable, but this is broken on all BSDs that I -tried, in various ways. Usually the embedded event loop will simply never -receive events, sometimes it will only trigger a few times, sometimes in a -loop. Epoll is also nominally embeddable, but many Linux kernel versions -will always eport the epoll fd as ready, even when no events are pending. - -While libev allows embedding these backends (they are contained in -C), take extreme care that it will actually -work. - -When in doubt, create a dynamic event loop forced to use sockets (this -usually works) and possibly another thread and a pipe or so to report to -your main event loop. - =head3 Watcher-Specific Functions and Data Members =over 4 @@ -2299,6 +2420,11 @@ (CLOCK_REALTIME, ...)> and will not normally affect correctness. See the note about libraries in the description of C, though. +=item EV_USE_NANOSLEEP + +If defined to be C<1>, libev will assume that C is available +and will use it for delays. Otherwise it will use C