<meta name="description" content="Pod documentation for libev" />
<meta name="inputfile" content="<standard input>" />
<meta name="outputfile" content="<standard output>" />
- <meta name="created" content="Tue Nov 27 09:11:42 2007" />
+ <meta name="created" content="Tue Nov 27 21:14:27 2007" />
<meta name="generator" content="Pod::Xhtml 1.57" />
<link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head>
<body>
<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>
</div>
<h1 id="SYNOPSIS">SYNOPSIS</h1><p><a href="#TOP" class="toplink">Top</a></p>
<div id="SYNOPSIS_CONTENT">
-<pre> #include <ev.h>
+<pre> /* this is the only header you need */
+ #include <ev.h>
+
+ /* what follows is a fully working example program */
+ 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>
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><p><a href="#TOP" class="toplink">Top</a></p>
<div id="GLOBAL_FUNCTIONS_CONTENT">
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>
+ <dt>ev_set_allocator (void *(*cb)(void *ptr, size_t size))</dt>
<dd>
- <p>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.</p>
+ <p>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.</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: better than mine).</p>
<pre> static void *
- persistent_realloc (void *ptr, long size)
+ persistent_realloc (void *ptr, size_t size)
{
for (;;)
{
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>
+</div>
+<h2 id="code_ev_fork_code_the_audacity_to_re"><code>ev_fork</code> - the audacity to resume the event loop after a fork</h2>
+<div id="code_ev_fork_code_the_audacity_to_re-2">
+<p>Fork watchers are called when a <code>fork ()</code> was detected (usually because
+whoever is a good citizen cared to tell libev about it by calling
+<code>ev_default_fork</code> or <code>ev_loop_fork</code>). The invocation is done before the
+event loop blocks next and before <code>ev_check</code> watchers are being called,
+and only in the child after the fork. If whoever good citizen calling
+<code>ev_default_fork</code> cheats and calls it in the wrong process, the fork
+handlers will be invoked, too, of course.</p>
+<dl>
+ <dt>ev_fork_init (ev_signal *, callback)</dt>
+ <dd>
+ <p>Initialises and configures the fork watcher - it has no parameters of any
+kind. There is a <code>ev_fork_set</code> macro, but using it is utterly pointless,
+believe me.</p>
+ </dd>
+</dl>
+
+
+
+
+
</div>
<h1 id="OTHER_FUNCTIONS">OTHER FUNCTIONS</h1><p><a href="#TOP" class="toplink">Top</a></p>
<div id="OTHER_FUNCTIONS_CONTENT">
<dd>
<p>Invokes <code>ev_embed_sweep</code>.</p>
</dd>
+ <dt>w->update () <code>ev::stat</code> only</dt>
+ <dd>
+ <p>Invokes <code>ev_stat_stat</code>.</p>
+ </dd>
</dl>
</p>
</dd>
io.start (fd, ev::READ);
}
+
+
+
+</pre>
+
+</div>
+<h1 id="MACRO_MAGIC">MACRO MAGIC</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="MACRO_MAGIC_CONTENT">
+<p>Libev can be compiled with a variety of options, the most fundemantal is
+<code>EV_MULTIPLICITY</code>. This option determines wether (most) functions and
+callbacks have an initial <code>struct ev_loop *</code> argument.</p>
+<p>To make it easier to write programs that cope with either variant, the
+following macros are defined:</p>
+<dl>
+ <dt><code>EV_A</code>, <code>EV_A_</code></dt>
+ <dd>
+ <p>This provides the loop <i>argument</i> for functions, if one is required ("ev
+loop argument"). The <code>EV_A</code> form is used when this is the sole argument,
+<code>EV_A_</code> is used when other arguments are following. Example:</p>
+<pre> ev_unref (EV_A);
+ ev_timer_add (EV_A_ watcher);
+ ev_loop (EV_A_ 0);
+
+</pre>
+ <p>It assumes the variable <code>loop</code> of type <code>struct ev_loop *</code> is in scope,
+which is often provided by the following macro.</p>
+ </dd>
+ <dt><code>EV_P</code>, <code>EV_P_</code></dt>
+ <dd>
+ <p>This provides the loop <i>parameter</i> for functions, if one is required ("ev
+loop parameter"). The <code>EV_P</code> form is used when this is the sole parameter,
+<code>EV_P_</code> is used when other parameters are following. Example:</p>
+<pre> // 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)
+
+</pre>
+ <p>It declares a parameter <code>loop</code> of type <code>struct ev_loop *</code>, quite
+suitable for use with <code>EV_A</code>.</p>
+ </dd>
+ <dt><code>EV_DEFAULT</code>, <code>EV_DEFAULT_</code></dt>
+ <dd>
+ <p>Similar to the other two macros, this gives you the value of the default
+loop, if multiple loops are supported ("ev loop default").</p>
+ </dd>
+</dl>
+<p>Example: Declare and initialise a check watcher, working regardless of
+wether multiple loops are supported or not.</p>
+<pre> 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);
+
+
+
+
</pre>
</div>
<dt>EV_STAT_ENABLE</dt>
<dd>
<p>If undefined or defined to be <code>1</code>, then stat watchers are supported. If
+defined to be <code>0</code>, then they are not.</p>
+ </dd>
+ <dt>EV_FORK_ENABLE</dt>
+ <dd>
+ <p>If undefined or defined to be <code>1</code>, then fork watchers are supported. If
defined to be <code>0</code>, then they are not.</p>
</dd>
<dt>EV_MINIMAL</dt>
<p>If you need to shave off some kilobytes of code at the expense of some
speed, define this symbol to <code>1</code>. Currently only used for gcc to override
some inlining decisions, saves roughly 30% codesize of amd64.</p>
+ </dd>
+ <dt>EV_PID_HASHSIZE</dt>
+ <dd>
+ <p><code>ev_child</code> watchers use a small hash table to distribute workload by
+pid. The default size is <code>16</code> (or <code>1</code> with <code>EV_MINIMAL</code>), usually more
+than enough. If you need to manage thousands of children you might want to
+increase this value.</p>
</dd>
<dt>EV_COMMON</dt>
<dd>