=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