--- libev/ev.pod 2007/12/24 04:34:00 1.107 +++ libev/ev.pod 2007/12/31 01:31:30 1.114 @@ -553,12 +553,16 @@ Here are the gory details of what C does: - Before the first iteration, call any pending watchers. - * If there are no active watchers (reference count is zero), return. - - Queue all prepare watchers and then call all outstanding watchers. + * If EVFLAG_FORKCHECK was used, check for a fork. + - If a fork was detected, queue and call all fork watchers. + - Queue and call all prepare watchers. - If we have been forked, recreate the kernel state. - Update the kernel state with all outstanding changes. - Update the "event loop time". - - Calculate for how long to block. + - Calculate for how long to sleep or block, if at all + (active idle watchers, EVLOOP_NONBLOCK or not having + any active watchers at all will result in not sleeping). + - Sleep if the I/O and timer collect interval say so. - Block the process, waiting for any events. - Queue all outstanding I/O (fd) events. - Update the "event loop time" and do time jump handling. @@ -569,10 +573,11 @@ - Call all queued watchers in reverse order (i.e. check watchers first). Signals and child watchers are implemented as I/O watchers, and will be handled here by queueing them when their watcher gets executed. - - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK - were used, return, otherwise continue with step *. + - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK + 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 @@ -985,12 +990,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). @@ -1035,8 +1034,8 @@ 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 and register events for them, only one -file descriptor might actually receive events. +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 @@ -1076,6 +1075,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. @@ -1182,6 +1183,8 @@ =back +=head3 Examples + Example: Create a timer that fires after 60 seconds. static void @@ -1348,6 +1351,8 @@ =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. @@ -1449,6 +1454,8 @@ =back +=head3 Examples + Example: Try to exit cleanly on SIGINT and SIGTERM. static void @@ -1498,6 +1505,22 @@ 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 @@ -1562,6 +1585,8 @@ =back +=head3 Examples + Example: Watch C for attribute changes. static void @@ -1648,6 +1673,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. @@ -1728,6 +1755,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 @@ -1905,26 +1934,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 @@ -1952,6 +1962,54 @@ =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 @@ -2440,6 +2498,14 @@ it is assumed that all these functions actually work on fds, even on win32. Should not be defined on non-win32 platforms. +=item EV_FD_TO_WIN32_HANDLE + +If C is enabled, then libev needs a way to map +file descriptors to socket handles. When not defining this symbol (the +default), then libev will call C<_get_osfhandle>, which is usually +correct. In some cases, programs use their own file descriptor management, +in which case they can provide this function to map fds to socket handles. + =item EV_USE_POLL If defined to be C<1>, libev will compile in support for the C(2) @@ -2485,8 +2551,8 @@ =item EV_H The name of the F header file used to include it. The default if -undefined is C<< >> in F and C<"ev.h"> in F. This -can be used to virtually rename the F header file in case of conflicts. +undefined is C<"ev.h"> in F and F. This can be used to +virtually rename the F header file in case of conflicts. =item EV_CONFIG_H @@ -2497,7 +2563,7 @@ =item EV_EVENT_H Similarly to C, this macro can be used to override F's idea -of how the F header can be found. +of how the F header can be found, the dfeault is C<"event.h">. =item EV_PROTOTYPES @@ -2720,6 +2786,73 @@ =back + +=head1 Win32 platform limitations and workarounds + +Win32 doesn't support any of the standards (e.g. POSIX) that libev +requires, and its I/O model is fundamentally incompatible with the POSIX +model. Libev still offers limited functionality on this platform in +the form of the C backend, and only supports socket +descriptors. This only applies when using Win32 natively, not when using +e.g. cygwin. + +There is no supported compilation method available on windows except +embedding it into other applications. + +Due to the many, low, and arbitrary limits on the win32 platform and the +abysmal performance of winsockets, using a large number of sockets is not +recommended (and not reasonable). If your program needs to use more than +a hundred or so sockets, then likely it needs to use a totally different +implementation for windows, as libev offers the POSIX model, which cannot +be implemented efficiently on windows (microsoft monopoly games). + +=over 4 + +=item The winsocket select function + +The winsocket C