--- libev/ev.pod 2007/12/22 05:48:02 1.97 +++ libev/ev.pod 2008/04/02 06:34:51 1.140 @@ -6,51 +6,65 @@ #include -=head1 EXAMPLE PROGRAM +=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. @@ -67,7 +81,7 @@ details of the event, and then hand it over to libev by I the watcher. -=head1 FEATURES +=head2 FEATURES Libev supports 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