If you don't know what event loop to use, use the one returned from this
function.
+The default loop is the only loop that can handle C<ev_signal> and
+C<ev_child> watchers, and to do this, it always registers a handler
+for C<SIGCHLD>. If this is a problem for your app you can either
+create a dynamic loop with C<ev_loop_new> that doesn't do that, or you
+can simply overwrite the C<SIGCHLD> signal handler I<after> calling
+C<ev_default_init>.
+
The flags argument can be used to specify special behaviour or specific
backends to use, and is usually specified as C<0> (or C<EVFLAG_AUTO>).
descriptors a "slow" C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> backend
might perform better.
+On the positive side, ignoring the spurious readyness notifications, this
+backend actually performed to specification in all tests and is fully
+embeddable, which is a rare feat among the OS-specific backends.
+
=item C<EVBACKEND_ALL>
Try all backends (even potentially broken ones that wouldn't be tried
=back
If one or more of these are ored into the flags value, then only these
-backends will be tried (in the reverse order as given here). If none are
-specified, most compiled-in backend will be tried, usually in reverse
-order of their flag values :)
+backends will be tried (in the reverse order as listed here). If none are
+specified, all backends in C<ev_recommended_backends ()> will be tried.
The most typical usage is like this:
=item ev_default_fork ()
-This function reinitialises the kernel state for backends that have
-one. Despite the name, you can call it anytime, but it makes most sense
-after forking, in either the parent or child process (or both, but that
-again makes little sense).
+This function sets a flag that causes subsequent C<ev_loop> iterations
+to reinitialise the kernel state for backends that have one. Despite the
+name, you can call it anytime, but it makes most sense after forking, in
+the child process (or both child and parent, but that again makes little
+sense). You I<must> call it in the child before using any of the libev
+functions, and it will only take effect at the next C<ev_loop> iteration.
-You I<must> call this function in the child process after forking if and
-only if you want to use the event library in both processes. If you just
-fork+exec, you don't have to call it.
+On the other hand, you only need to call this function in the child
+process if and only if you want to use the event library in the child. If
+you just fork+exec, you don't have to call it at all.
The function itself is quite fast and it's usually not a problem to call
it just in case after a fork. To make this easy, the function will fit in
pthread_atfork (0, 0, ev_default_fork);
-At the moment, C<EVBACKEND_SELECT> and C<EVBACKEND_POLL> are safe to use
-without calling this function, so if you force one of those backends you
-do not need to care.
-
=item ev_loop_fork (loop)
Like C<ev_default_fork>, but acts on an event loop created by
Here are the gory details of what C<ev_loop> 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.
- 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
C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or
C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return.
+This "unloop state" will be cleared when entering C<ev_loop> again.
+
=item ev_ref (loop)
=item ev_unref (loop)
visible to the libev user and should not keep C<ev_loop> from exiting if
no event watchers registered by it are active. It is also an excellent
way to do this for generic recurring timers or from within third-party
-libraries. Just remember to I<unref after start> and I<ref before stop>.
+libraries. Just remember to I<unref after start> and I<ref before stop>
+(but only if the watcher wasn't active before, or was active before,
+respectively).
Example: Create a signal watcher, but keep it from keeping C<ev_loop>
running when nothing else is active.
=back
+=head3 Examples
+
Example: Call C<stdin_readable_cb> 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.
=back
+=head3 Examples
+
Example: Create a timer that fires after 60 seconds.
static void
=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.
=back
+=head3 Examples
+
Example: Try to exit cleanly on SIGINT and SIGTERM.
static void
=back
+=head3 Examples
+
Example: Dynamically allocate an C<ev_idle> watcher, start it, and in the
callback, free it. Also, use no error checking, as usual.
=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<EV::ADNS> that does this, which you could
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
=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<loop_hi>, while the mebeddable loop is stored in
+C<loop_lo> (which is C<loop_hi> 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<loop_socket>. (One might optionally use C<EVFLAG_NOENV>, 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<ev_fork> - the audacity to resume the event loop after a fork
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<EV_SELECT_IS_WINSOCKET> 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<poll>(2)
=item EV_H
The name of the F<ev.h> header file used to include it. The default if
-undefined is C<< <ev.h> >> in F<event.h> and C<"ev.h"> in F<ev.c>. This
-can be used to virtually rename the F<ev.h> header file in case of conflicts.
+undefined is C<"ev.h"> in F<event.h>, F<ev.c> and F<ev++.h>. This can be
+used to virtually rename the F<ev.h> header file in case of conflicts.
=item EV_CONFIG_H
=item EV_EVENT_H
Similarly to C<EV_H>, this macro can be used to override F<event.c>'s idea
-of how the F<event.h> header can be found.
+of how the F<event.h> header can be found, the default is C<"event.h">.
=item EV_PROTOTYPES
=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<EVBACKEND_SELECT> 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<select> function doesn't follow POSIX in that it requires
+socket I<handles> and not socket I<file descriptors>. This makes select
+very inefficient, and also requires a mapping from file descriptors
+to socket handles. See the discussion of the C<EV_SELECT_USE_FD_SET>,
+C<EV_SELECT_IS_WINSOCKET> and C<EV_FD_TO_WIN32_HANDLE> 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<FD_SETSIZE>
+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
+
+
=head1 AUTHOR
Marc Lehmann <libev@schmorp.de>.