X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/5466167e0504f6fd929074dda89e770d4abfd3c3..71b44ee4e9677f9ddc2b7f91f32654cfd1bd2f73:/ev.pod diff --git a/ev.pod b/ev.pod index 3ad52c0..6d8fe82 100644 --- a/ev.pod +++ b/ev.pod @@ -6,7 +6,7 @@ libev - a high performance full-featured event loop written in C #include -=head1 EXAMPLE PROGRAM +=head2 EXAMPLE PROGRAM #include @@ -55,7 +55,7 @@ web page you might find easier to navigate when reading it for the first 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 @@ watchers>, which are relatively small C structures you initialise with the 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 @@ -885,12 +1004,6 @@ fd as you want (as long as you don't confuse yourself). Setting all file 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). @@ -910,6 +1023,52 @@ whether a file descriptor is really ready with a known-to-be good interface such as poll (fortunately in our Xlib example, Xlib already does this on its own, so its quite safe to use). +=head3 The special problem of disappearing file descriptors + +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 +this interest. If another file descriptor with the same number then is +registered with libev, there is no efficient way to see that this is, in +fact, a different file descriptor. + +To avoid having to explicitly tell libev about such cases, libev follows +the following policy: Each time C is being called, libev +will assume that this is potentially a new file descriptor, otherwise +it is assumed that the file descriptor stays the same. That means that +you I to call C (or C) when you change the +descriptor even if the file descriptor number itself did not change. + +This is how one would do it normally anyway, the important point is that +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 + =over 4 =item ev_io_init (ev_io *, callback, int fd, int events) @@ -930,6 +1089,8 @@ The events being watched. =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. @@ -972,6 +1133,8 @@ The callback is guarenteed to be invoked only when its timeout has passed, but if multiple timers become ready during the same loop iteration then order of execution is undefined. +=head3 Watcher-Specific Functions and Data Members + =over 4 =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) @@ -1034,6 +1197,8 @@ which is also when any modifications are taken into account. =back +=head3 Examples + Example: Create a timer that fires after 60 seconds. static void @@ -1086,6 +1251,8 @@ 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 during the same loop iteration then order of execution is undefined. +=head3 Watcher-Specific Functions and Data Members + =over 4 =item ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb) @@ -1191,8 +1358,15 @@ The current reschedule callback, or C<0>, if this functionality is 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. @@ -1241,6 +1415,8 @@ as you don't register any with libev). Similarly, when the last signal watcher for a signal is stopped libev will reset the signal handler to SIG_DFL (regardless of what it was set to before). +=head3 Watcher-Specific Functions and Data Members + =over 4 =item ev_signal_init (ev_signal *, callback, int signum) @@ -1262,6 +1438,8 @@ The signal the watcher watches out for. Child watchers trigger when your process receives a SIGCHLD in response to some child status changes (most typically when a child of yours dies). +=head3 Watcher-Specific Functions and Data Members + =over 4 =item ev_child_init (ev_child *, callback, int pid) @@ -1290,6 +1468,8 @@ C and C documentation for details). =back +=head3 Examples + Example: Try to exit cleanly on SIGINT and SIGTERM. static void @@ -1339,6 +1519,41 @@ 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 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 =item ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval) @@ -1384,6 +1599,8 @@ The filesystem path that is being watched. =back +=head3 Examples + Example: Watch C for attribute changes. static void @@ -1405,8 +1622,36 @@ Example: Watch C for attribute changes. ... 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... @@ -1430,6 +1675,8 @@ effect on its own sometimes), idle watchers are a good place to do "pseudo-background processing", or delay processing stuff to after the event loop has handled all outstanding events. +=head3 Watcher-Specific Functions and Data Members + =over 4 =item ev_idle_init (ev_signal *, callback) @@ -1440,6 +1687,8 @@ believe me. =back +=head3 Examples + Example: Dynamically allocate an C watcher, start it, and in the callback, free it. Also, use no error checking, as usual. @@ -1500,11 +1749,13 @@ It is recommended to give C watchers highest (C) 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 =over 4 @@ -1518,6 +1769,8 @@ macros, but using them is utterly, utterly and completely pointless. =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 @@ -1695,26 +1948,9 @@ portable one. 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; +create it, and if that fails, use the normal loop for everything. - // 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; +=head3 Watcher-Specific Functions and Data Members =over 4 @@ -1734,12 +1970,60 @@ Make a single, non-blocking sweep over the embedded loop. This works 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 @@ -1751,6 +2035,8 @@ and only in the child after the fork. If whoever good citizen calling C cheats and calls it in the wrong process, the fork handlers will be invoked, too, of course. +=head3 Watcher-Specific Functions and Data Members + =over 4 =item ev_fork_init (ev_signal *, callback) @@ -1976,16 +2262,16 @@ constructor already stores the event loop. Stops the watcher if it is active. Again, no C argument. -=item w->again () C, C only +=item w->again () (C, C only) For C and C, this invokes the corresponding C function. -=item w->sweep () C only +=item w->sweep () (C only) Invokes C. -=item w->update () C only +=item w->update () (C only) Invokes C. @@ -2015,9 +2301,9 @@ the constructor. =head1 MACRO MAGIC -Libev can be compiled with a variety of options, the most fundemantal is -C. This option determines whether (most) functions and -callbacks have an initial C argument. +Libev can be compiled with a variety of options, the most fundamantal +of which is C. This option determines whether (most) +functions and callbacks have an initial C argument. To make it easier to write programs that cope with either variant, the following macros are defined: @@ -2081,7 +2367,7 @@ applications. Examples of applications that embed it include the Deliantra 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). @@ -2181,7 +2467,7 @@ If defined to be C<1>, libev will try to detect the availability of the 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>). @@ -2191,8 +2477,13 @@ If defined to be C<1>, libev will try to detect the availability of the 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