--- libev/ev.pod 2007/12/12 22:26:37 1.84 +++ libev/ev.pod 2007/12/31 01:32:59 1.115 @@ -6,7 +6,7 @@ #include -=head1 EXAMPLE PROGRAM +=head2 EXAMPLE PROGRAM #include @@ -55,7 +55,7 @@ time: L. Libev is an event loop: you register interest in certain events (such as a -file descriptor being readable or a timeout occuring), and it will manage +file descriptor being readable or a timeout occurring), and it will manage these event sources and provide your program with events. To do this, it must take more or less complete control over your process @@ -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 @@ -888,12 +992,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). @@ -915,7 +1013,7 @@ =head3 The special problem of disappearing file descriptors -Some backends (e.g kqueue, epoll) need to be told about closing a file +Some backends (e.g. kqueue, epoll) need to be told about closing a file descriptor (either by calling C explicitly or by any other means, such as C). The reason is that you register interest in some file descriptor, but when it goes away, the operating system will silently drop @@ -934,6 +1032,28 @@ the libev application should not optimise around libev but should leave optimisations to libev. +=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 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 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 + +Some backends (epoll, kqueue) do not support C at all or exhibit +useless behaviour. Libev fully supports fork, but needs to be told about +it in the child. + +To support fork in your programs, you either have to call +C or C after a fork in the child, +enable C, or resort to C or +C. + =head3 Watcher-Specific Functions @@ -957,6 +1077,8 @@ =back +=head3 Examples + Example: Call C when STDIN_FILENO has become, well readable, but only once. Since it is likely line-buffered, you could attempt to read a whole line in the callback. @@ -1063,6 +1185,8 @@ =back +=head3 Examples + Example: Create a timer that fires after 60 seconds. static void @@ -1222,8 +1346,15 @@ 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 + Example: Call a callback every hour, or, more precisely, whenever the system clock is divisible by 3600. The callback invocation times have potentially a lot of jittering, but good long-term stability. @@ -1325,6 +1456,8 @@ =back +=head3 Examples + Example: Try to exit cleanly on SIGINT and SIGTERM. static void @@ -1374,6 +1507,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 @@ -1421,6 +1587,8 @@ =back +=head3 Examples + Example: Watch C for attribute changes. static void @@ -1442,9 +1610,37 @@ ... 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... @@ -1479,6 +1675,8 @@ =back +=head3 Examples + Example: Dynamically allocate an C watcher, start it, and in the callback, free it. Also, use no error checking, as usual. @@ -1539,11 +1737,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 @@ -1559,6 +1757,8 @@ =back +=head3 Examples + There are a number of principal ways to embed other event loops or modules into libev. Here are some ideas on how to include libadns into libev (there is a Perl module named C that does this, which you could @@ -1736,26 +1936,7 @@ So when you want to use this feature you will always have to be prepared that you cannot get an embeddable loop. The recommended way to get around this is to have a separate variables for your embeddable loop, try to -create it, and if that fails, use the normal loop for everything: - - struct ev_loop *loop_hi = ev_default_init (0); - struct ev_loop *loop_lo = 0; - struct ev_embed embed; - - // see if there is a chance of getting one that works - // (remember that a flags value of 0 means autodetection) - loop_lo = ev_embeddable_backends () & ev_recommended_backends () - ? ev_loop_new (ev_embeddable_backends () & ev_recommended_backends ()) - : 0; - - // if we got one, then embed it, otherwise default to loop_hi - if (loop_lo) - { - ev_embed_init (&embed, 0, loop_lo); - ev_embed_start (loop_hi, &embed); - } - else - loop_lo = loop_hi; +create it, and if that fails, use the normal loop for everything. =head3 Watcher-Specific Functions and Data Members @@ -1777,12 +1958,60 @@ similarly to C, but in the most apropriate way for embedded loops. -=item struct ev_loop *loop [read-only] +=item struct ev_loop *other [read-only] The embedded event loop. =back +=head3 Examples + +Example: Try to get an embeddable event loop and embed it into the default +event loop. If that is not possible, use the default loop. The default +loop is stored in C, while the mebeddable loop is stored in +C (which is C in the acse no embeddable loop can be +used). + + struct ev_loop *loop_hi = ev_default_init (0); + struct ev_loop *loop_lo = 0; + struct ev_embed embed; + + // see if there is a chance of getting one that works + // (remember that a flags value of 0 means autodetection) + loop_lo = ev_embeddable_backends () & ev_recommended_backends () + ? ev_loop_new (ev_embeddable_backends () & ev_recommended_backends ()) + : 0; + + // if we got one, then embed it, otherwise default to loop_hi + if (loop_lo) + { + ev_embed_init (&embed, 0, loop_lo); + ev_embed_start (loop_hi, &embed); + } + else + loop_lo = loop_hi; + +Example: Check if kqueue is available but not recommended and create +a kqueue backend for use with sockets (which usually work with any +kqueue implementation). Store the kqueue/socket-only event loop in +C. (One might optionally use C, too). + + struct ev_loop *loop = ev_default_init (0); + struct ev_loop *loop_socket = 0; + struct ev_embed embed; + + if (ev_supported_backends () & ~ev_recommended_backends () & EVBACKEND_KQUEUE) + if ((loop_socket = ev_loop_new (EVBACKEND_KQUEUE)) + { + ev_embed_init (&embed, 0, loop_socket); + ev_embed_start (loop, &embed); + } + + if (!loop_socket) + loop_socket = loop; + + // now use loop_socket for all sockets, and loop for everything else + =head2 C - the audacity to resume the event loop after a fork @@ -2126,7 +2355,7 @@ Game Server, the EV perl module, the GNU Virtual Private Ethernet (gvpe) and rxvt-unicode. -The goal is to enable you to just copy the neecssary files into your +The goal is to enable you to just copy the necessary files into your source directory without having to change even a single line in them, so you can easily upgrade by simply copying (or having a checked-out copy of libev somewhere in your source tree). @@ -2226,7 +2455,7 @@ monotonic clock option at both compiletime 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, althoguh you have +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>). @@ -2236,8 +2465,13 @@ realtime clock option at compiletime (and assume its availability at runtime if successful). Otherwise no use of the realtime clock option will be attempted. This effectively replaces C by C and will not normally affect correctness. See tzhe note about libraries -in the description of C, though. +(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 function doesn't follow POSIX in that it requires +socket I and not socket I. This makes select +very inefficient, and also requires a mapping from file descriptors +to socket handles. See the discussion of the C, +C and C preprocessor +symbols for more info. + +The configuration for a "naked" win32 using the microsoft runtime +libraries and raw winsocket select is: + + #define EV_USE_SELECT 1 + #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */ + +Note that winsockets handling of fd sets is O(n), so you can easily get a +complexity in the O(n²) range when using win32. + +=item Limited number of file descriptors + +Windows has numerous arbitrary (and low) limits on things. Early versions +of winsocket's select only supported waiting for a max. of C<64> handles +(probably owning to the fact that all windows kernels can only wait for +C<64> things at the same time internally; microsoft recommends spawning a +chain of threads and wait for 63 handles and the previous thread in each). + +Newer versions support more handles, but you need to define C +to some high number (e.g. C<2048>) before compiling the winsocket select +call (which might be in libev or elsewhere, for example, perl does its own +select emulation on windows). + +Another limit is the number of file descriptors in the microsoft runtime +libraries, which by default is C<64> (there must be a hidden I<64> fetish +or something like this inside microsoft). You can increase this by calling +C<_setmaxstdio>, which can increase this limit to C<2048> (another +arbitrary limit), but is broken in many versions of the microsoft runtime +libraries. + +This might get you to about C<512> or C<2048> sockets (depending on +windows version and/or the phase of the moon). To get more, you need to +wrap all I/O functions and provide your own fd management, but the cost of +calling select (O(n²)) will likely make this unworkable. =back