|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
|
|
|
|
<head>
|
|
|
|
<title>libev</title>
|
|
|
|
<meta name="description" content="Pod documentation for libev" />
|
|
|
|
<meta name="inputfile" content="<standard input>" />
|
|
|
|
<meta name="outputfile" content="<standard output>" />
|
|
|
|
<meta name="created" content="Thu Nov 29 18:28:02 2007" />
|
|
|
|
<meta name="generator" content="Pod::Xhtml 1.57" />
|
|
|
|
<link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head>
|
|
|
|
<body>
|
|
|
|
<div class="pod">
|
|
|
|
<!-- INDEX START -->
|
|
|
|
<h3 id="TOP">Index</h3>
|
|
|
|
|
|
|
|
<ul><li><a href="#NAME">NAME</a></li>
|
|
|
|
<li><a href="#SYNOPSIS">SYNOPSIS</a></li>
|
|
|
|
<li><a href="#EXAMPLE_PROGRAM">EXAMPLE PROGRAM</a></li>
|
|
|
|
<li><a href="#DESCRIPTION">DESCRIPTION</a></li>
|
|
|
|
<li><a href="#FEATURES">FEATURES</a></li>
|
|
|
|
<li><a href="#CONVENTIONS">CONVENTIONS</a></li>
|
|
|
|
<li><a href="#TIME_REPRESENTATION">TIME REPRESENTATION</a></li>
|
|
|
|
<li><a href="#GLOBAL_FUNCTIONS">GLOBAL FUNCTIONS</a></li>
|
|
|
|
<li><a href="#FUNCTIONS_CONTROLLING_THE_EVENT_LOOP">FUNCTIONS CONTROLLING THE EVENT LOOP</a></li>
|
|
|
|
<li><a href="#ANATOMY_OF_A_WATCHER">ANATOMY OF A WATCHER</a>
|
|
|
|
<ul><li><a href="#GENERIC_WATCHER_FUNCTIONS">GENERIC WATCHER FUNCTIONS</a></li>
|
|
|
|
<li><a href="#ASSOCIATING_CUSTOM_DATA_WITH_A_WATCH">ASSOCIATING CUSTOM DATA WITH A WATCHER</a></li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li><a href="#WATCHER_TYPES">WATCHER TYPES</a>
|
|
|
|
<ul><li><a href="#code_ev_io_code_is_this_file_descrip"><code>ev_io</code> - is this file descriptor readable or writable?</a></li>
|
|
|
|
<li><a href="#code_ev_timer_code_relative_and_opti"><code>ev_timer</code> - relative and optionally repeating timeouts</a></li>
|
|
|
|
<li><a href="#code_ev_periodic_code_to_cron_or_not"><code>ev_periodic</code> - to cron or not to cron?</a></li>
|
|
|
|
<li><a href="#code_ev_signal_code_signal_me_when_a"><code>ev_signal</code> - signal me when a signal gets signalled!</a></li>
|
|
|
|
<li><a href="#code_ev_child_code_watch_out_for_pro"><code>ev_child</code> - watch out for process status changes</a></li>
|
|
|
|
<li><a href="#code_ev_stat_code_did_the_file_attri"><code>ev_stat</code> - did the file attributes just change?</a></li>
|
|
|
|
<li><a href="#code_ev_idle_code_when_you_ve_got_no"><code>ev_idle</code> - when you've got nothing better to do...</a></li>
|
|
|
|
<li><a href="#code_ev_prepare_code_and_code_ev_che"><code>ev_prepare</code> and <code>ev_check</code> - customise your event loop!</a></li>
|
|
|
|
<li><a href="#code_ev_embed_code_when_one_backend_"><code>ev_embed</code> - when one backend isn't enough...</a></li>
|
|
|
|
<li><a href="#code_ev_fork_code_the_audacity_to_re"><code>ev_fork</code> - the audacity to resume the event loop after a fork</a></li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li><a href="#OTHER_FUNCTIONS">OTHER FUNCTIONS</a></li>
|
|
|
|
<li><a href="#LIBEVENT_EMULATION">LIBEVENT EMULATION</a></li>
|
|
|
|
<li><a href="#C_SUPPORT">C++ SUPPORT</a></li>
|
|
|
|
<li><a href="#MACRO_MAGIC">MACRO MAGIC</a></li>
|
|
|
|
<li><a href="#EMBEDDING">EMBEDDING</a>
|
|
|
|
<ul><li><a href="#FILESETS">FILESETS</a>
|
|
|
|
<ul><li><a href="#CORE_EVENT_LOOP">CORE EVENT LOOP</a></li>
|
|
|
|
<li><a href="#LIBEVENT_COMPATIBILITY_API">LIBEVENT COMPATIBILITY API</a></li>
|
|
|
|
<li><a href="#AUTOCONF_SUPPORT">AUTOCONF SUPPORT</a></li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li><a href="#PREPROCESSOR_SYMBOLS_MACROS">PREPROCESSOR SYMBOLS/MACROS</a></li>
|
|
|
|
<li><a href="#EXAMPLES">EXAMPLES</a></li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li><a href="#COMPLEXITIES">COMPLEXITIES</a></li>
|
|
|
|
<li><a href="#AUTHOR">AUTHOR</a>
|
|
|
|
</li>
|
|
|
|
</ul><hr />
|
|
|
|
<!-- INDEX END -->
|
|
|
|
|
|
|
|
<h1 id="NAME">NAME</h1>
|
|
|
|
<div id="NAME_CONTENT">
|
|
|
|
<p>libev - a high performance full-featured event loop written in C</p>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="SYNOPSIS">SYNOPSIS</h1>
|
|
|
|
<div id="SYNOPSIS_CONTENT">
|
|
|
|
<pre> #include <ev.h>
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="EXAMPLE_PROGRAM">EXAMPLE PROGRAM</h1>
|
|
|
|
<div id="EXAMPLE_PROGRAM_CONTENT">
|
|
|
|
<pre> #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;
|
|
|
|
}
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="DESCRIPTION">DESCRIPTION</h1>
|
|
|
|
<div id="DESCRIPTION_CONTENT">
|
|
|
|
<p>Libev is an event loop: you register interest in certain events (such as a
|
|
|
|
file descriptor being readable or a timeout occuring), and it will manage
|
|
|
|
these event sources and provide your program with events.</p>
|
|
|
|
<p>To do this, it must take more or less complete control over your process
|
|
|
|
(or thread) by executing the <i>event loop</i> handler, and will then
|
|
|
|
communicate events via a callback mechanism.</p>
|
|
|
|
<p>You register interest in certain events by registering so-called <i>event
|
|
|
|
watchers</i>, which are relatively small C structures you initialise with the
|
|
|
|
details of the event, and then hand it over to libev by <i>starting</i> the
|
|
|
|
watcher.</p>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="FEATURES">FEATURES</h1>
|
|
|
|
<div id="FEATURES_CONTENT">
|
|
|
|
<p>Libev supports <code>select</code>, <code>poll</code>, the Linux-specific <code>epoll</code>, the
|
|
|
|
BSD-specific <code>kqueue</code> and the Solaris-specific event port mechanisms
|
|
|
|
for file descriptor events (<code>ev_io</code>), the Linux <code>inotify</code> interface
|
|
|
|
(for <code>ev_stat</code>), relative timers (<code>ev_timer</code>), absolute timers
|
|
|
|
with customised rescheduling (<code>ev_periodic</code>), synchronous signals
|
|
|
|
(<code>ev_signal</code>), process status change events (<code>ev_child</code>), and event
|
|
|
|
watchers dealing with the event loop mechanism itself (<code>ev_idle</code>,
|
|
|
|
<code>ev_embed</code>, <code>ev_prepare</code> and <code>ev_check</code> watchers) as well as
|
|
|
|
file watchers (<code>ev_stat</code>) and even limited support for fork events
|
|
|
|
(<code>ev_fork</code>).</p>
|
|
|
|
<p>It also is quite fast (see this
|
|
|
|
<a href="http://libev.schmorp.de/bench.html">benchmark</a> comparing it to libevent
|
|
|
|
for example).</p>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="CONVENTIONS">CONVENTIONS</h1>
|
|
|
|
<div id="CONVENTIONS_CONTENT">
|
|
|
|
<p>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 <strong>EMBED</strong> section in
|
|
|
|
this manual. If libev was configured without support for multiple event
|
|
|
|
loops, then all functions taking an initial argument of name <code>loop</code>
|
|
|
|
(which is always of type <code>struct ev_loop *</code>) will not have this argument.</p>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="TIME_REPRESENTATION">TIME REPRESENTATION</h1>
|
|
|
|
<div id="TIME_REPRESENTATION_CONTENT">
|
|
|
|
<p>Libev represents time as a single floating point number, representing the
|
|
|
|
(fractional) number of seconds since the (POSIX) epoch (somewhere near
|
|
|
|
the beginning of 1970, details are complicated, don't ask). This type is
|
|
|
|
called <code>ev_tstamp</code>, which is what you should use too. It usually aliases
|
|
|
|
to the <code>double</code> type in C, and when you need to do any calculations on
|
|
|
|
it, you should treat it as such.</p>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="GLOBAL_FUNCTIONS">GLOBAL FUNCTIONS</h1>
|
|
|
|
<div id="GLOBAL_FUNCTIONS_CONTENT">
|
|
|
|
<p>These functions can be called anytime, even before initialising the
|
|
|
|
library in any way.</p>
|
|
|
|
<dl>
|
|
|
|
<dt>ev_tstamp ev_time ()</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Returns the current time as libev would use it. Please note that the
|
|
|
|
<code>ev_now</code> function is usually faster and also often returns the timestamp
|
|
|
|
you actually want to know.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>int ev_version_major ()</dt>
|
|
|
|
<dt>int ev_version_minor ()</dt>
|
|
|
|
<dd>
|
|
|
|
<p>You can find out the major and minor version numbers of the library
|
|
|
|
you linked against by calling the functions <code>ev_version_major</code> and
|
|
|
|
<code>ev_version_minor</code>. If you want, you can compare against the global
|
|
|
|
symbols <code>EV_VERSION_MAJOR</code> and <code>EV_VERSION_MINOR</code>, which specify the
|
|
|
|
version of the library your program was compiled against.</p>
|
|
|
|
<p>Usually, it's a good idea to terminate if the major versions mismatch,
|
|
|
|
as this indicates an incompatible change. Minor versions are usually
|
|
|
|
compatible to older versions, so a larger minor version alone is usually
|
|
|
|
not a problem.</p>
|
|
|
|
<p>Example: Make sure we haven't accidentally been linked against the wrong
|
|
|
|
version.</p>
|
|
|
|
<pre> assert (("libev version mismatch",
|
|
|
|
ev_version_major () == EV_VERSION_MAJOR
|
|
|
|
&& ev_version_minor () >= EV_VERSION_MINOR));
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
<dt>unsigned int ev_supported_backends ()</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Return the set of all backends (i.e. their corresponding <code>EV_BACKEND_*</code>
|
|
|
|
value) compiled into this binary of libev (independent of their
|
|
|
|
availability on the system you are running on). See <code>ev_default_loop</code> for
|
|
|
|
a description of the set values.</p>
|
|
|
|
<p>Example: make sure we have the epoll method, because yeah this is cool and
|
|
|
|
a must have and can we have a torrent of it please!!!11</p>
|
|
|
|
<pre> assert (("sorry, no epoll, no sex",
|
|
|
|
ev_supported_backends () & EVBACKEND_EPOLL));
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
<dt>unsigned int ev_recommended_backends ()</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Return the set of all backends compiled into this binary of libev and also
|
|
|
|
recommended for this platform. This set is often smaller than the one
|
|
|
|
returned by <code>ev_supported_backends</code>, as for example kqueue is broken on
|
|
|
|
most BSDs and will not be autodetected unless you explicitly request it
|
|
|
|
(assuming you know what you are doing). This is the set of backends that
|
|
|
|
libev will probe for if you specify no backends explicitly.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>unsigned int ev_embeddable_backends ()</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Returns the set of backends that are embeddable in other event loops. This
|
|
|
|
is the theoretical, all-platform, value. To find which backends
|
|
|
|
might be supported on the current system, you would need to look at
|
|
|
|
<code>ev_embeddable_backends () & ev_supported_backends ()</code>, likewise for
|
|
|
|
recommended ones.</p>
|
|
|
|
<p>See the description of <code>ev_embed</code> watchers for more info.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_set_allocator (void *(*cb)(void *ptr, long size))</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Sets the allocation function to use (the prototype is similar - the
|
|
|
|
semantics is 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.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
<p>Example: Replace the libev allocator with one that waits a bit and then
|
|
|
|
retries).</p>
|
|
|
|
<pre> static void *
|
|
|
|
persistent_realloc (void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
void *newptr = realloc (ptr, size);
|
|
|
|
|
|
|
|
if (newptr)
|
|
|
|
return newptr;
|
|
|
|
|
|
|
|
sleep (60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
...
|
|
|
|
ev_set_allocator (persistent_realloc);
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_set_syserr_cb (void (*cb)(const char *msg));</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Set the callback function to call on a retryable syscall error (such
|
|
|
|
as failed select, poll, epoll_wait). The message is a printable string
|
|
|
|
indicating the system call or subsystem causing the problem. If this
|
|
|
|
callback is set, then libev will expect it to remedy the sitution, no
|
|
|
|
matter what, when it returns. That is, libev will generally retry the
|
|
|
|
requested operation, or, if the condition doesn't go away, do bad stuff
|
|
|
|
(such as abort).</p>
|
|
|
|
<p>Example: This is basically the same thing that libev does internally, too.</p>
|
|
|
|
<pre> static void
|
|
|
|
fatal_error (const char *msg)
|
|
|
|
{
|
|
|
|
perror (msg);
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
...
|
|
|
|
ev_set_syserr_cb (fatal_error);
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="FUNCTIONS_CONTROLLING_THE_EVENT_LOOP">FUNCTIONS CONTROLLING THE EVENT LOOP</h1>
|
|
|
|
<div id="FUNCTIONS_CONTROLLING_THE_EVENT_LOOP-2">
|
|
|
|
<p>An event loop is described by a <code>struct ev_loop *</code>. The library knows two
|
|
|
|
types of such loops, the <i>default</i> loop, which supports signals and child
|
|
|
|
events, and dynamically created loops which do not.</p>
|
|
|
|
<p>If you use threads, a common model is to run the default event loop
|
|
|
|
in your main thread (or in a separate thread) and for each thread you
|
|
|
|
create, you also create another event loop. Libev itself does no locking
|
|
|
|
whatsoever, so if you mix calls to the same event loop in different
|
|
|
|
threads, make sure you lock (this is usually a bad idea, though, even if
|
|
|
|
done correctly, because it's hideous and inefficient).</p>
|
|
|
|
<dl>
|
|
|
|
<dt>struct ev_loop *ev_default_loop (unsigned int flags)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>This will initialise the default event loop if it hasn't been initialised
|
|
|
|
yet and return it. If the default loop could not be initialised, returns
|
|
|
|
false. If it already was initialised it simply returns it (and ignores the
|
|
|
|
flags. If that is troubling you, check <code>ev_backend ()</code> afterwards).</p>
|
|
|
|
<p>If you don't know what event loop to use, use the one returned from this
|
|
|
|
function.</p>
|
|
|
|
<p>The flags argument can be used to specify special behaviour or specific
|
|
|
|
backends to use, and is usually specified as <code>0</code> (or <code>EVFLAG_AUTO</code>).</p>
|
|
|
|
<p>The following flags are supported:</p>
|
|
|
|
<p>
|
|
|
|
<dl>
|
|
|
|
<dt><code>EVFLAG_AUTO</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The default flags value. Use this if you have no clue (it's the right
|
|
|
|
thing, believe me).</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVFLAG_NOENV</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>If this flag bit is ored into the flag value (or the program runs setuid
|
|
|
|
or setgid) then libev will <i>not</i> look at the environment variable
|
|
|
|
<code>LIBEV_FLAGS</code>. Otherwise (the default), this environment variable will
|
|
|
|
override the flags completely if it is found in the environment. This is
|
|
|
|
useful to try out specific backends to test their performance, or to work
|
|
|
|
around bugs.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVFLAG_FORKCHECK</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>Instead of calling <code>ev_default_fork</code> or <code>ev_loop_fork</code> manually after
|
|
|
|
a fork, you can also make libev check for a fork in each iteration by
|
|
|
|
enabling this flag.</p>
|
|
|
|
<p>This works by calling <code>getpid ()</code> on every iteration of the loop,
|
|
|
|
and thus this might slow down your event loop if you do a lot of loop
|
|
|
|
iterations and little real work, but is usually not noticable (on my
|
|
|
|
Linux system for example, <code>getpid</code> is actually a simple 5-insn sequence
|
|
|
|
without a syscall and thus <i>very</i> fast, but my Linux system also has
|
|
|
|
<code>pthread_atfork</code> which is even faster).</p>
|
|
|
|
<p>The big advantage of this flag is that you can forget about fork (and
|
|
|
|
forget about forgetting to tell libev about forking) when you use this
|
|
|
|
flag.</p>
|
|
|
|
<p>This flag setting cannot be overriden or specified in the <code>LIBEV_FLAGS</code>
|
|
|
|
environment variable.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVBACKEND_SELECT</code> (value 1, portable select backend)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>This is your standard select(2) backend. Not <i>completely</i> standard, as
|
|
|
|
libev tries to roll its own fd_set with no limits on the number of fds,
|
|
|
|
but if that fails, expect a fairly low limit on the number of fds when
|
|
|
|
using this backend. It doesn't scale too well (O(highest_fd)), but its usually
|
|
|
|
the fastest backend for a low number of fds.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVBACKEND_POLL</code> (value 2, poll backend, available everywhere except on windows)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>And this is your standard poll(2) backend. It's more complicated than
|
|
|
|
select, but handles sparse fds better and has no artificial limit on the
|
|
|
|
number of fds you can use (except it will slow down considerably with a
|
|
|
|
lot of inactive fds). It scales similarly to select, i.e. O(total_fds).</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVBACKEND_EPOLL</code> (value 4, Linux)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>For few fds, this backend is a bit little slower than poll and select,
|
|
|
|
but it scales phenomenally better. While poll and select usually scale like
|
|
|
|
O(total_fds) where n is the total number of fds (or the highest fd), epoll scales
|
|
|
|
either O(1) or O(active_fds).</p>
|
|
|
|
<p>While stopping and starting an I/O watcher in the same iteration will
|
|
|
|
result in some caching, there is still a syscall per such incident
|
|
|
|
(because the fd could point to a different file description now), so its
|
|
|
|
best to avoid that. Also, dup()ed file descriptors might not work very
|
|
|
|
well if you register events for both fds.</p>
|
|
|
|
<p>Please note that epoll sometimes generates spurious notifications, so you
|
|
|
|
need to use non-blocking I/O or other means to avoid blocking when no data
|
|
|
|
(or space) is available.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVBACKEND_KQUEUE</code> (value 8, most BSD clones)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Kqueue deserves special mention, as at the time of this writing, it
|
|
|
|
was broken on all BSDs except NetBSD (usually it doesn't work with
|
|
|
|
anything but sockets and pipes, except on Darwin, where of course its
|
|
|
|
completely useless). For this reason its not being "autodetected"
|
|
|
|
unless you explicitly specify it explicitly in the flags (i.e. using
|
|
|
|
<code>EVBACKEND_KQUEUE</code>).</p>
|
|
|
|
<p>It scales in the same way as the epoll backend, but the interface to the
|
|
|
|
kernel is more efficient (which says nothing about its actual speed, of
|
|
|
|
course). While starting and stopping an I/O watcher does not cause an
|
|
|
|
extra syscall as with epoll, it still adds up to four event changes per
|
|
|
|
incident, so its best to avoid that.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVBACKEND_DEVPOLL</code> (value 16, Solaris 8)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>This is not implemented yet (and might never be).</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVBACKEND_PORT</code> (value 32, Solaris 10)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>This uses the Solaris 10 port mechanism. As with everything on Solaris,
|
|
|
|
it's really slow, but it still scales very well (O(active_fds)).</p>
|
|
|
|
<p>Please note that solaris ports can result in a lot of spurious
|
|
|
|
notifications, so you need to use non-blocking I/O or other means to avoid
|
|
|
|
blocking when no data (or space) is available.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EVBACKEND_ALL</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>Try all backends (even potentially broken ones that wouldn't be tried
|
|
|
|
with <code>EVFLAG_AUTO</code>). Since this is a mask, you can do stuff such as
|
|
|
|
<code>EVBACKEND_ALL & ~EVBACKEND_KQUEUE</code>.</p>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
|
|
<p>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 :)</p>
|
|
|
|
<p>The most typical usage is like this:</p>
|
|
|
|
<pre> if (!ev_default_loop (0))
|
|
|
|
fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>Restrict libev to the select and poll backends, and do not allow
|
|
|
|
environment settings to be taken into account:</p>
|
|
|
|
<pre> ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>Use whatever libev has to offer, but make sure that kqueue is used if
|
|
|
|
available (warning, breaks stuff, best use only with your own private
|
|
|
|
event loop and only if you know the OS supports your types of fds):</p>
|
|
|
|
<pre> ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE);
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
<dt>struct ev_loop *ev_loop_new (unsigned int flags)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Similar to <code>ev_default_loop</code>, but always creates a new event loop that is
|
|
|
|
always distinct from the default loop. Unlike the default loop, it cannot
|
|
|
|
handle signal and child watchers, and attempts to do so will be greeted by
|
|
|
|
undefined behaviour (or a failed assertion if assertions are enabled).</p>
|
|
|
|
<p>Example: Try to create a event loop that uses epoll and nothing else.</p>
|
|
|
|
<pre> struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
|
|
|
|
if (!epoller)
|
|
|
|
fatal ("no epoll found here, maybe it hides under your chair");
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_default_destroy ()</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Destroys the default loop again (frees all memory and kernel state
|
|
|
|
etc.). None of the active event watchers will be stopped in the normal
|
|
|
|
sense, so e.g. <code>ev_is_active</code> might still return true. It is your
|
|
|
|
responsibility to either stop all watchers cleanly yoursef <i>before</i>
|
|
|
|
calling this function, or cope with the fact afterwards (which is usually
|
|
|
|
the easiest thing, youc na just ignore the watchers and/or <code>free ()</code> them
|
|
|
|
for example).</p>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_loop_destroy (loop)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Like <code>ev_default_destroy</code>, but destroys an event loop created by an
|
|
|
|
earlier call to <code>ev_loop_new</code>.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_default_fork ()</dt>
|
|
|
|
<dd>
|
|
|
|
<p>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).</p>
|
|
|
|
<p>You <i>must</i> 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.</p>
|
|
|
|
<p>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
|
|
|
|
quite nicely into a call to <code>pthread_atfork</code>:</p>
|
|
|
|
<pre> pthread_atfork (0, 0, ev_default_fork);
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>At the moment, <code>EVBACKEND_SELECT</code> and <code>EVBACKEND_POLL</code> are safe to use
|
|
|
|
without calling this function, so if you force one of those backends you
|
|
|
|
do not need to care.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_loop_fork (loop)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Like <code>ev_default_fork</code>, but acts on an event loop created by
|
|
|
|
<code>ev_loop_new</code>. Yes, you have to call this on every allocated event loop
|
|
|
|
after fork, and how you do this is entirely your own problem.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>unsigned int ev_backend (loop)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Returns one of the <code>EVBACKEND_*</code> flags indicating the event backend in
|
|
|
|
use.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_tstamp ev_now (loop)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Returns the current "event loop time", which is the time the event loop
|
|
|
|
received events and started processing them. This timestamp does not
|
|
|
|
change as long as callbacks are being processed, and this is also the base
|
|
|
|
time used for relative timers. You can treat it as the timestamp of the
|
|
|
|
event occuring (or more correctly, libev finding out about it).</p>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_loop (loop, int flags)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Finally, this is it, the event handler. This function usually is called
|
|
|
|
after you initialised all your watchers and you want to start handling
|
|
|
|
events.</p>
|
|
|
|
<p>If the flags argument is specified as <code>0</code>, it will not return until
|
|
|
|
either no event watchers are active anymore or <code>ev_unloop</code> was called.</p>
|
|
|
|
<p>Please note that an explicit <code>ev_unloop</code> is usually better than
|
|
|
|
relying on all watchers to be stopped when deciding when a program has
|
|
|
|
finished (especially in interactive programs), but having a program that
|
|
|
|
automatically loops as long as it has to and no longer by virtue of
|
|
|
|
relying on its watchers stopping correctly is a thing of beauty.</p>
|
|
|
|
<p>A flags value of <code>EVLOOP_NONBLOCK</code> will look for new events, will handle
|
|
|
|
those events and any outstanding ones, but will not block your process in
|
|
|
|
case there are no events and will return after one iteration of the loop.</p>
|
|
|
|
<p>A flags value of <code>EVLOOP_ONESHOT</code> will look for new events (waiting if
|
|
|
|
neccessary) and will handle those and any outstanding ones. It will block
|
|
|
|
your process until at least one new event arrives, and will return after
|
|
|
|
one iteration of the loop. This is useful if you are waiting for some
|
|
|
|
external event in conjunction with something not expressible using other
|
|
|
|
libev watchers. However, a pair of <code>ev_prepare</code>/<code>ev_check</code> watchers is
|
|
|
|
usually a better approach for this kind of thing.</p>
|
|
|
|
<p>Here are the gory details of what <code>ev_loop</code> does:</p>
|
|
|
|
<pre> * If there are no active watchers (reference count is zero), return.
|
|
|
|
- Queue prepare watchers and then call all outstanding 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.
|
|
|
|
- Block the process, waiting for any events.
|
|
|
|
- Queue all outstanding I/O (fd) events.
|
|
|
|
- Update the "event loop time" and do time jump handling.
|
|
|
|
- Queue all outstanding timers.
|
|
|
|
- Queue all outstanding periodics.
|
|
|
|
- If no events are pending now, queue all idle watchers.
|
|
|
|
- Queue all check watchers.
|
|
|
|
- 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 *.
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>Example: Queue some jobs and then loop until no events are outsanding
|
|
|
|
anymore.</p>
|
|
|
|
<pre> ... queue jobs here, make sure they register event watchers as long
|
|
|
|
... as they still have work to do (even an idle watcher will do..)
|
|
|
|
ev_loop (my_loop, 0);
|
|
|
|
... jobs done. yeah!
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_unloop (loop, how)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Can be used to make a call to <code>ev_loop</code> return early (but only after it
|
|
|
|
has processed all outstanding events). The <code>how</code> argument must be either
|
|
|
|
<code>EVUNLOOP_ONE</code>, which will make the innermost <code>ev_loop</code> call return, or
|
|
|
|
<code>EVUNLOOP_ALL</code>, which will make all nested <code>ev_loop</code> calls return.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_ref (loop)</dt>
|
|
|
|
<dt>ev_unref (loop)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Ref/unref can be used to add or remove a reference count on the event
|
|
|
|
loop: Every watcher keeps one reference, and as long as the reference
|
|
|
|
count is nonzero, <code>ev_loop</code> will not return on its own. If you have
|
|
|
|
a watcher you never unregister that should not keep <code>ev_loop</code> from
|
|
|
|
returning, ev_unref() after starting, and ev_ref() before stopping it. For
|
|
|
|
example, libev itself uses this for its internal signal pipe: It is not
|
|
|
|
visible to the libev user and should not keep <code>ev_loop</code> 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</i> and <i>ref before stop</i>.</p>
|
|
|
|
<p>Example: Create a signal watcher, but keep it from keeping <code>ev_loop</code>
|
|
|
|
running when nothing else is active.</p>
|
|
|
|
<pre> struct ev_signal exitsig;
|
|
|
|
ev_signal_init (&exitsig, sig_cb, SIGINT);
|
|
|
|
ev_signal_start (loop, &exitsig);
|
|
|
|
evf_unref (loop);
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>Example: For some weird reason, unregister the above signal handler again.</p>
|
|
|
|
<pre> ev_ref (loop);
|
|
|
|
ev_signal_stop (loop, &exitsig);
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="ANATOMY_OF_A_WATCHER">ANATOMY OF A WATCHER</h1>
|
|
|
|
<div id="ANATOMY_OF_A_WATCHER_CONTENT">
|
|
|
|
<p>A watcher is a structure that you create and register to record your
|
|
|
|
interest in some event. For instance, if you want to wait for STDIN to
|
|
|
|
become readable, you would create an <code>ev_io</code> watcher for that:</p>
|
|
|
|
<pre> static void my_cb (struct ev_loop *loop, struct ev_io *w, int revents)
|
|
|
|
{
|
|
|
|
ev_io_stop (w);
|
|
|
|
ev_unloop (loop, EVUNLOOP_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ev_loop *loop = ev_default_loop (0);
|
|
|
|
struct ev_io stdin_watcher;
|
|
|
|
ev_init (&stdin_watcher, my_cb);
|
|
|
|
ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ);
|
|
|
|
ev_io_start (loop, &stdin_watcher);
|
|
|
|
ev_loop (loop, 0);
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>As you can see, you are responsible for allocating the memory for your
|
|
|
|
watcher structures (and it is usually a bad idea to do this on the stack,
|
|
|
|
although this can sometimes be quite valid).</p>
|
|
|
|
<p>Each watcher structure must be initialised by a call to <code>ev_init
|
|
|
|
(watcher *, callback)</code>, which expects a callback to be provided. This
|
|
|
|
callback gets invoked each time the event occurs (or, in the case of io
|
|
|
|
watchers, each time the event loop detects that the file descriptor given
|
|
|
|
is readable and/or writable).</p>
|
|
|
|
<p>Each watcher type has its own <code>ev_<type>_set (watcher *, ...)</code> macro
|
|
|
|
with arguments specific to this watcher type. There is also a macro
|
|
|
|
to combine initialisation and setting in one call: <code>ev_<type>_init
|
|
|
|
(watcher *, callback, ...)</code>.</p>
|
|
|
|
<p>To make the watcher actually watch out for events, you have to start it
|
|
|
|
with a watcher-specific start function (<code>ev_<type>_start (loop, watcher
|
|
|
|
*)</code>), and you can stop watching for events at any time by calling the
|
|
|
|
corresponding stop function (<code>ev_<type>_stop (loop, watcher *)</code>.</p>
|
|
|
|
<p>As long as your watcher is active (has been started but not stopped) you
|
|
|
|
must not touch the values stored in it. Most specifically you must never
|
|
|
|
reinitialise it or call its <code>set</code> macro.</p>
|
|
|
|
<p>Each and every callback receives the event loop pointer as first, the
|
|
|
|
registered watcher structure as second, and a bitset of received events as
|
|
|
|
third argument.</p>
|
|
|
|
<p>The received events usually include a single bit per event type received
|
|
|
|
(you can receive multiple events at the same time). The possible bit masks
|
|
|
|
are:</p>
|
|
|
|
<dl>
|
|
|
|
<dt><code>EV_READ</code></dt>
|
|
|
|
<dt><code>EV_WRITE</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The file descriptor in the <code>ev_io</code> watcher has become readable and/or
|
|
|
|
writable.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_TIMEOUT</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The <code>ev_timer</code> watcher has timed out.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_PERIODIC</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The <code>ev_periodic</code> watcher has timed out.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_SIGNAL</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The signal specified in the <code>ev_signal</code> watcher has been received by a thread.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_CHILD</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The pid specified in the <code>ev_child</code> watcher has received a status change.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_STAT</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The path specified in the <code>ev_stat</code> watcher changed its attributes somehow.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_IDLE</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The <code>ev_idle</code> watcher has determined that you have nothing better to do.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_PREPARE</code></dt>
|
|
|
|
<dt><code>EV_CHECK</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>All <code>ev_prepare</code> watchers are invoked just <i>before</i> <code>ev_loop</code> starts
|
|
|
|
to gather new events, and all <code>ev_check</code> watchers are invoked just after
|
|
|
|
<code>ev_loop</code> has gathered them, but before it invokes any callbacks for any
|
|
|
|
received events. Callbacks of both watcher types can start and stop as
|
|
|
|
many watchers as they want, and all of them will be taken into account
|
|
|
|
(for example, a <code>ev_prepare</code> watcher might start an idle watcher to keep
|
|
|
|
<code>ev_loop</code> from blocking).</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_EMBED</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The embedded event loop specified in the <code>ev_embed</code> watcher needs attention.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_FORK</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>The event loop has been resumed in the child process after fork (see
|
|
|
|
<code>ev_fork</code>).</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>EV_ERROR</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p>An unspecified error has occured, the watcher has been stopped. This might
|
|
|
|
happen because the watcher could not be properly started because libev
|
|
|
|
ran out of memory, a file descriptor was found to be closed or any other
|
|
|
|
problem. You best act on it by reporting the problem and somehow coping
|
|
|
|
with the watcher being stopped.</p>
|
|
|
|
<p>Libev will usually signal a few "dummy" events together with an error,
|
|
|
|
for example it might indicate that a fd is readable or writable, and if
|
|
|
|
your callbacks is well-written it can just attempt the operation and cope
|
|
|
|
with the error from read() or write(). This will not work in multithreaded
|
|
|
|
programs, though, so beware.</p>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h2 id="GENERIC_WATCHER_FUNCTIONS">GENERIC WATCHER FUNCTIONS</h2>
|
|
|
|
<div id="GENERIC_WATCHER_FUNCTIONS_CONTENT">
|
|
|
|
<p>In the following description, <code>TYPE</code> stands for the watcher type,
|
|
|
|
e.g. <code>timer</code> for <code>ev_timer</code> watchers and <code>io</code> for <code>ev_io</code> watchers.</p>
|
|
|
|
<dl>
|
|
|
|
<dt><code>ev_init</code> (ev_TYPE *watcher, callback)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>This macro initialises the generic portion of a watcher. The contents
|
|
|
|
of the watcher object can be arbitrary (so <code>malloc</code> will do). Only
|
|
|
|
the generic parts of the watcher are initialised, you <i>need</i> to call
|
|
|
|
the type-specific <code>ev_TYPE_set</code> macro afterwards to initialise the
|
|
|
|
type-specific parts. For each type there is also a <code>ev_TYPE_init</code> macro
|
|
|
|
which rolls both calls into one.</p>
|
|
|
|
<p>You can reinitialise a watcher at any time as long as it has been stopped
|
|
|
|
(or never started) and there are no pending events outstanding.</p>
|
|
|
|
<p>The callback is always of type <code>void (*)(ev_loop *loop, ev_TYPE *watcher,
|
|
|
|
int revents)</code>.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>ev_TYPE_set</code> (ev_TYPE *, [args])</dt>
|
|
|
|
<dd>
|
|
|
|
<p>This macro initialises the type-specific parts of a watcher. You need to
|
|
|
|
call <code>ev_init</code> at least once before you call this macro, but you can
|
|
|
|
call <code>ev_TYPE_set</code> any number of times. You must not, however, call this
|
|
|
|
macro on a watcher that is active (it can be pending, however, which is a
|
|
|
|
difference to the <code>ev_init</code> macro).</p>
|
|
|
|
<p>Although some watcher types do not have type-specific arguments
|
|
|
|
(e.g. <code>ev_prepare</code>) you still need to call its <code>set</code> macro.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>ev_TYPE_init</code> (ev_TYPE *watcher, callback, [args])</dt>
|
|
|
|
<dd>
|
|
|
|
<p>This convinience macro rolls both <code>ev_init</code> and <code>ev_TYPE_set</code> macro
|
|
|
|
calls into a single call. This is the most convinient method to initialise
|
|
|
|
a watcher. The same limitations apply, of course.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>ev_TYPE_start</code> (loop *, ev_TYPE *watcher)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Starts (activates) the given watcher. Only active watchers will receive
|
|
|
|
events. If the watcher is already active nothing will happen.</p>
|
|
|
|
</dd>
|
|
|
|
<dt><code>ev_TYPE_stop</code> (loop *, ev_TYPE *watcher)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Stops the given watcher again (if active) and clears the pending
|
|
|
|
status. It is possible that stopped watchers are pending (for example,
|
|
|
|
non-repeating timers are being stopped when they become pending), but
|
|
|
|
<code>ev_TYPE_stop</code> ensures that the watcher is neither active nor pending. If
|
|
|
|
you want to free or reuse the memory used by the watcher it is therefore a
|
|
|
|
good idea to always call its <code>ev_TYPE_stop</code> function.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>bool ev_is_active (ev_TYPE *watcher)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Returns a true value iff the watcher is active (i.e. it has been started
|
|
|
|
and not yet been stopped). As long as a watcher is active you must not modify
|
|
|
|
it.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>bool ev_is_pending (ev_TYPE *watcher)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Returns a true value iff the watcher is pending, (i.e. it has outstanding
|
|
|
|
events but its callback has not yet been invoked). As long as a watcher
|
|
|
|
is pending (but not active) you must not call an init function on it (but
|
|
|
|
<code>ev_TYPE_set</code> is safe) and you must make sure the watcher is available to
|
|
|
|
libev (e.g. you cnanot <code>free ()</code> it).</p>
|
|
|
|
</dd>
|
|
|
|
<dt>callback ev_cb (ev_TYPE *watcher)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Returns the callback currently set on the watcher.</p>
|
|
|
|
</dd>
|
|
|
|
<dt>ev_cb_set (ev_TYPE *watcher, callback)</dt>
|
|
|
|
<dd>
|
|
|
|
<p>Change the callback. You can change the callback at virtually any time
|
|
|
|
(modulo threads).</p>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h2 id="ASSOCIATING_CUSTOM_DATA_WITH_A_WATCH">ASSOCIATING CUSTOM DATA WITH A WATCHER</h2>
|
|
|
|
<div id="ASSOCIATING_CUSTOM_DATA_WITH_A_WATCH-2">
|
|
|
|
<p>Each watcher has, by default, a member <code>void *data</code> that you can change
|
|
|
|
and read at any time, libev will completely ignore it. This can be used
|
|
|
|
to associate arbitrary data with your watcher. If you need more data and
|
|
|
|
don't want to allocate memory and store a pointer to it in that data
|
|
|
|
member, you can also "subclass" the watcher type and provide your own
|
|
|
|
data:</p>
|
|
|
|
<pre> struct my_io
|
|
|
|
{
|
|
|
|
struct ev_io io;
|
|
|
|
int otherfd;
|
|
|
|
void *somedata;
|
|
|
|
struct whatever *mostinteresting;
|
|
|
|
}
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>And since your callback will be called with a pointer to the watcher, you
|
|
|
|
can cast it back to your own type:</p>
|
|
|
|
<pre> static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents)
|
|
|
|
{
|
|
|
|
struct my_io *w = (struct my_io *)w_;
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>More interesting and less C-conformant ways of casting your callback type
|
|
|
|
instead have been omitted.</p>
|
|
|
|
<p>Another common scenario is having some data structure with multiple
|
|
|
|
watchers:</p>
|
|
|
|
<pre> struct my_biggy
|
|
|
|
{
|
|
|
|
int some_data;
|
|
|
|
ev_timer t1;
|
|
|
|
ev_timer t2;
|
|
|
|
}
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
<p>In this case getting the pointer to <code>my_biggy</code> is a bit more complicated,
|
|
|
|
you need to use <code>offsetof</code>:</p>
|
|
|
|
<pre> #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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h1 id="WATCHER_TYPES">WATCHER TYPES</h1>
|
|
|
|
<div id="WATCHER_TYPES_CONTENT">
|
|
|
|
<p>This section describes each watcher in detail, but will not repeat
|
|
|
|
information given in the last section. Any initialisation/set macros,
|
|
|
|
functions and members specific to the watcher type are explained.</p>
|
|
|
|
<p>Members are additionally marked with either <i>[read-only]</i>, meaning that,
|
|
|
|
while the watcher is active, you can look at the member and expect some
|
|
|
|
sensible content, but you must not modify it (you can modify it while the
|
|
|
|
watcher is stopped to your hearts content), or <i>[read-write]</i>, which
|
|
|
|
means you can expect it to have some sensible content while the watcher
|
|
|
|
is active, but you can also modify it. Modifying it may not do something
|
|
|
|
sensible or take immediate effect (or do anything at all), but libev will
|
|
|
|
not crash or malfunction in any way.</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<h2 id="code_ev_io_code_is_this_file_descrip"><code>ev_io</code> - is this file descriptor readable or writable?</h2>
|
|
|
|
<div id="code_ev_io_code_is_this_file_descrip-2">
|
|
|
|
<p>I/O watchers check whether a file descriptor is readable or writable
|
|
|
|
in each iteration of the event loop, or, more precisely, when reading
|
|
|
|
would not block the process and writing would at least be able to write
|
|
|
|
some data. This behaviour is called level-triggering because you keep
|
|
|
|
receiving events as long as the condition persists. Remember you can stop
|
|
|
|
the watcher if you don't want to act on the event and neither want to
|
|
|
|
receive future events.</p>
|
|
|