#include <ev.h>
+=head1 EXAMPLE PROGRAM
+
+ #include <ev.h>
+
+ ev_io stdin_watcher;
+ ev_timer timeout_watcher;
+
+ /* called when data 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 */
+ }
+
+ static void
+ timeout_cb (EV_P_ struct ev_timer *w, int revents)
+ {
+ /* puts ("timeout"); */
+ ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */
+ }
+
+ int
+ main (void)
+ {
+ struct ev_loop *loop = ev_default_loop (0);
+
+ /* initialise an io watcher, then start it */
+ 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 */
+ ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
+ ev_timer_start (loop, &timeout_watcher);
+
+ /* loop till timeout or data ready */
+ ev_loop (loop, 0);
+
+ return 0;
+ }
+
=head1 DESCRIPTION
Libev is an event loop: you register interest in certain events (such as a
=head1 FEATURES
-Libev supports select, poll, the linux-specific epoll and the bsd-specific
-kqueue mechanisms for file descriptor events, relative timers, absolute
-timers with customised rescheduling, signal events, process status change
-events (related to SIGCHLD), and event watchers dealing with the event
-loop mechanism itself (idle, prepare and check watchers). It also is quite
-fast (see this L<benchmark|http://libev.schmorp.de/bench.html> comparing
-it to libevent for example).
+Libev supports C<select>, C<poll>, the linux-specific C<epoll>, the
+bsd-specific C<kqueue> and the solaris-specific event port mechanisms
+for file descriptor events (C<ev_io>), relative timers (C<ev_timer>),
+absolute timers with customised rescheduling (C<ev_periodic>), synchronous
+signals (C<ev_signal>), process status change events (C<ev_child>), and
+event watchers dealing with the event loop mechanism itself (C<ev_idle>,
+C<ev_embed>, C<ev_prepare> and C<ev_check> watchers) as well as
+file watchers (C<ev_stat>) and even limited support for fork events
+(C<ev_fork>).
+
+It also is quite fast (see this
+L<benchmark|http://libev.schmorp.de/bench.html> comparing it to libevent
+for example).
=head1 CONVENTIONS
-Libev is very configurable. In this manual the default configuration
-will be described, which supports multiple event loops. For more info
-about various configuration options please have a look at the file
-F<README.embed> in the libev distribution. If libev was configured without
-support for multiple event loops, then all functions taking an initial
-argument of name C<loop> (which is always of type C<struct ev_loop *>)
-will not have this argument.
+Libev is very configurable. In this manual the default configuration will
+be described, which supports multiple event loops. For more info about
+various configuration options please have a look at B<EMBED> section in
+this manual. If libev was configured without support for multiple event
+loops, then all functions taking an initial argument of name C<loop>
+(which is always of type C<struct ev_loop *>) will not have this argument.
=head1 TIME REPRESENTATION
to the C<double> type in C, and when you need to do any calculations on
it, you should treat it as such.
-
=head1 GLOBAL FUNCTIONS
These functions can be called anytime, even before initialising the
compatible to older versions, so a larger minor version alone is usually
not a problem.
-Example: make sure we haven't accidentally been linked against the wrong
-version:
+Example: Make sure we haven't accidentally been linked against the wrong
+version.
assert (("libev version mismatch",
ev_version_major () == EV_VERSION_MAJOR
See the description of C<ev_embed> watchers for more info.
-=item ev_set_allocator (void *(*cb)(void *ptr, long size))
+=item ev_set_allocator (void *(*cb)(void *ptr, size_t size))
-Sets the allocation function to use (the prototype is similar to the
-realloc C function, the semantics are identical). It is used to allocate
-and free memory (no surprises here). If it returns zero when memory
-needs to be allocated, the library might abort or take some potentially
-destructive action. The default is your system realloc function.
+Sets the allocation function to use (the prototype and semantics are
+identical to the realloc C function). It is used to allocate and free
+memory (no surprises here). If it returns zero when memory needs to be
+allocated, the library might abort or take some potentially destructive
+action. The default is your system realloc function.
You could override this function in high-availability programs to, say,
free some memory if it cannot allocate memory, to use a special allocator,
or even to sleep a while and retry until some memory is available.
-Example: replace the libev allocator with one that waits a bit and then
-retries: better than mine).
+Example: Replace the libev allocator with one that waits a bit and then
+retries).
static void *
- persistent_realloc (void *ptr, long size)
+ persistent_realloc (void *ptr, size_t size)
{
for (;;)
{
requested operation, or, if the condition doesn't go away, do bad stuff
(such as abort).
-Example: do the same thing as libev does internally:
+Example: This is basically the same thing that libev does internally, too.
static void
fatal_error (const char *msg)
handle signal and child watchers, and attempts to do so will be greeted by
undefined behaviour (or a failed assertion if assertions are enabled).
-Example: try to create a event loop that uses epoll and nothing else.
+Example: Try to create a event loop that uses epoll and nothing else.
struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
if (!epoller)
- If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK
were used, 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 outsanding
anymore.
... queue jobs here, make sure they register event watchers as long
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>.
-Example: create a signal watcher, but keep it from keeping C<ev_loop>
+Example: Create a signal watcher, but keep it from keeping C<ev_loop>
running when nothing else is active.
- struct dv_signal exitsig;
+ struct ev_signal exitsig;
ev_signal_init (&exitsig, sig_cb, SIGINT);
- ev_signal_start (myloop, &exitsig);
- evf_unref (myloop);
+ ev_signal_start (loop, &exitsig);
+ evf_unref (loop);
-Example: for some weird reason, unregister the above signal handler again.
+Example: For some weird reason, unregister the above signal handler again.
- ev_ref (myloop);
- ev_signal_stop (myloop, &exitsig);
+ ev_ref (loop);
+ ev_signal_stop (loop, &exitsig);
=back
(for example, a C<ev_prepare> watcher might start an idle watcher to keep
C<ev_loop> from blocking).
+=item C<EV_EMBED>
+
+The embedded event loop specified in the C<ev_embed> watcher needs attention.
+
+=item C<EV_FORK>
+
+The event loop has been resumed in the child process after fork (see
+C<ev_fork>).
+
=item C<EV_ERROR>
An unspecified error has occured, the watcher has been stopped. This might
C<ev_TYPE_set> is safe) and you must make sure the watcher is available to
libev (e.g. you cnanot C<free ()> it).
-=item callback = ev_cb (ev_TYPE *watcher)
+=item callback ev_cb (ev_TYPE *watcher)
Returns the callback currently set on the watcher.
...
}
-More interesting and less C-conformant ways of catsing your callback type
-have been omitted....
+More interesting and less C-conformant ways of casting your callback type
+instead have been omitted.
+
+Another common scenario is having some data structure with multiple
+watchers:
+
+ struct my_biggy
+ {
+ int some_data;
+ ev_timer t1;
+ ev_timer t2;
+ }
+
+In this case getting the pointer to C<my_biggy> is a bit more complicated,
+you need to use C<offsetof>:
+
+ #include <stddef.h>
+
+ static void
+ t1_cb (EV_P_ struct ev_timer *w, int revents)
+ {
+ struct my_biggy big = (struct my_biggy *
+ (((char *)w) - offsetof (struct my_biggy, t1));
+ }
+
+ static void
+ t2_cb (EV_P_ struct ev_timer *w, int revents)
+ {
+ struct my_biggy big = (struct my_biggy *
+ (((char *)w) - offsetof (struct my_biggy, t2));
+ }
=head1 WATCHER TYPES
=back
-Example: call C<stdin_readable_cb> when STDIN_FILENO has become, well
+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:
+attempt to read a whole line in the callback.
static void
stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents)
=back
-Example: create a timer that fires after 60 seconds.
+Example: Create a timer that fires after 60 seconds.
static void
one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents)
ev_timer_init (&mytimer, one_minute_cb, 60., 0.);
ev_timer_start (loop, &mytimer);
-Example: create a timeout timer that times out after 10 seconds of
+Example: Create a timeout timer that times out after 10 seconds of
inactivity.
static void
=back
-Example: call a callback every hour, or, more precisely, whenever the
+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.
ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0);
ev_periodic_start (loop, &hourly_tick);
-Example: the same as above, but use a reschedule callback to do it:
+Example: The same as above, but use a reschedule callback to do it:
#include <math.h>
ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb);
-Example: call a callback every hour, starting now:
+Example: Call a callback every hour, starting now:
struct ev_periodic hourly_tick;
ev_periodic_init (&hourly_tick, clock_cb,
=back
-Example: try to exit cleanly on SIGINT and SIGTERM.
+Example: Try to exit cleanly on SIGINT and SIGTERM.
static void
sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
=back
-Example: dynamically allocate an C<ev_idle>, start it, and in the
-callback, free it. Alos, use no error checking, as usual.
+Example: Dynamically allocate an C<ev_idle> watcher, start it, and in the
+callback, free it. Also, use no error checking, as usual.
static void
idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents)
=back
+=head2 C<ev_fork> - the audacity to resume the event loop after a fork
+
+Fork watchers are called when a C<fork ()> was detected (usually because
+whoever is a good citizen cared to tell libev about it by calling
+C<ev_default_fork> or C<ev_loop_fork>). The invocation is done before the
+event loop blocks next and before C<ev_check> watchers are being called,
+and only in the child after the fork. If whoever good citizen calling
+C<ev_default_fork> cheats and calls it in the wrong process, the fork
+handlers will be invoked, too, of course.
+
+=over 4
+
+=item ev_fork_init (ev_signal *, callback)
+
+Initialises and configures the fork watcher - it has no parameters of any
+kind. There is a C<ev_fork_set> macro, but using it is utterly pointless,
+believe me.
+
+=back
+
+
=head1 OTHER FUNCTIONS
There are some other functions of possible interest. Described. Here. Now.
Invokes C<ev_embed_sweep>.
+=item w->update () C<ev::stat> only
+
+Invokes C<ev_stat_stat>.
+
=back
=back
io.start (fd, ev::READ);
}
+
+=head1 MACRO MAGIC
+
+Libev can be compiled with a variety of options, the most fundemantal is
+C<EV_MULTIPLICITY>. This option determines wether (most) functions and
+callbacks have an initial C<struct ev_loop *> argument.
+
+To make it easier to write programs that cope with either variant, the
+following macros are defined:
+
+=over 4
+
+=item C<EV_A>, C<EV_A_>
+
+This provides the loop I<argument> for functions, if one is required ("ev
+loop argument"). The C<EV_A> form is used when this is the sole argument,
+C<EV_A_> is used when other arguments are following. Example:
+
+ ev_unref (EV_A);
+ ev_timer_add (EV_A_ watcher);
+ ev_loop (EV_A_ 0);
+
+It assumes the variable C<loop> of type C<struct ev_loop *> is in scope,
+which is often provided by the following macro.
+
+=item C<EV_P>, C<EV_P_>
+
+This provides the loop I<parameter> for functions, if one is required ("ev
+loop parameter"). The C<EV_P> form is used when this is the sole parameter,
+C<EV_P_> is used when other parameters are following. Example:
+
+ // this is how ev_unref is being declared
+ static void ev_unref (EV_P);
+
+ // this is how you can declare your typical callback
+ static void cb (EV_P_ ev_timer *w, int revents)
+
+It declares a parameter C<loop> of type C<struct ev_loop *>, quite
+suitable for use with C<EV_A>.
+
+=item C<EV_DEFAULT>, C<EV_DEFAULT_>
+
+Similar to the other two macros, this gives you the value of the default
+loop, if multiple loops are supported ("ev loop default").
+
+=back
+
+Example: Declare and initialise a check watcher, working regardless of
+wether multiple loops are supported or not.
+
+ static void
+ check_cb (EV_P_ ev_timer *w, int revents)
+ {
+ ev_check_stop (EV_A_ w);
+ }
+
+ ev_check check;
+ ev_check_init (&check, check_cb);
+ ev_check_start (EV_DEFAULT_ &check);
+ ev_loop (EV_DEFAULT_ 0);
+
+
=head1 EMBEDDING
Libev can (and often is) directly embedded into host
If undefined or defined to be C<1>, then stat watchers are supported. If
defined to be C<0>, then they are not.
+=item EV_FORK_ENABLE
+
+If undefined or defined to be C<1>, then fork watchers are supported. If
+defined to be C<0>, then they are not.
+
=item EV_MINIMAL
If you need to shave off some kilobytes of code at the expense of some
speed, define this symbol to C<1>. Currently only used for gcc to override
some inlining decisions, saves roughly 30% codesize of amd64.
+=item EV_PID_HASHSIZE
+
+C<ev_child> watchers use a small hash table to distribute workload by
+pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more
+than enough. If you need to manage thousands of children you might want to
+increase this value.
+
=item EV_COMMON
By default, all watchers have a C<void *data> member. By redefining