<meta name="description" content="Pod documentation for libev" />
<meta name="inputfile" content="<standard input>" />
<meta name="outputfile" content="<standard output>" />
- <meta name="created" content="Sat Dec 8 15:31:35 2007" />
+ <meta name="created" content="Sun Dec 9 20:47:27 2007" />
<meta name="generator" content="Pod::Xhtml 1.57" />
<link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head>
<body>
<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
+ <p>You can find out the major and minor ABI 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>These version numbers refer to the ABI version of the library, not the
+release version.</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
+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
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.
+<pre> - 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 we have been forked, recreate the kernel state.
- Update the kernel state with all outstanding changes.
- Update the "event loop time".
periodic watcher to trigger in 10 seconds (by specifiying e.g. <code>ev_now ()
+ 10.</code>) and then reset your system clock to the last year, then it will
take a year to trigger the event (unlike an <code>ev_timer</code>, which would trigger
-roughly 10 seconds later and of course not if you reset your system time
-again).</p>
+roughly 10 seconds later).</p>
<p>They can also be used to implement vastly more complex timers, such as
-triggering an event on eahc midnight, local time.</p>
+triggering an event on each midnight, local time or other, complicated,
+rules.</p>
<p>As with timers, the callback is guarenteed to be invoked only when the
time (<code>at</code>) has been passed, but if multiple periodic timers become ready
during the same loop iteration then order of execution is undefined.</p>
operation, and we will explain them from simplest to complex:</p>
<p>
<dl>
- <dt>* absolute timer (interval = reschedule_cb = 0)</dt>
+ <dt>* absolute timer (at = time, interval = reschedule_cb = 0)</dt>
<dd>
<p>In this configuration the watcher triggers an event at the wallclock time
<code>at</code> and doesn't repeat. It will not adjust when a time jump occurs,
that is, if it is to be run at January 1st 2011 then it will run when the
system time reaches or surpasses this time.</p>
</dd>
- <dt>* non-repeating interval timer (interval > 0, reschedule_cb = 0)</dt>
+ <dt>* non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)</dt>
<dd>
<p>In this mode the watcher will always be scheduled to time out at the next
-<code>at + N * interval</code> time (for some integer N) and then repeat, regardless
-of any time jumps.</p>
+<code>at + N * interval</code> time (for some integer N, which can also be negative)
+and then repeat, regardless of any time jumps.</p>
<p>This can be used to create timers that do not drift with respect to system
time:</p>
<pre> ev_periodic_set (&periodic, 0., 3600., 0);
<p>Another way to think about it (for the mathematically inclined) is that
<code>ev_periodic</code> will try to run the callback in this mode at the next possible
time where <code>time = at (mod interval)</code>, regardless of any time jumps.</p>
+ <p>For numerical stability it is preferable that the <code>at</code> value is near
+<code>ev_now ()</code> (the current time), but there is no range requirement for
+this value.</p>
</dd>
- <dt>* manual reschedule mode (reschedule_cb = callback)</dt>
+ <dt>* manual reschedule mode (at and interval ignored, reschedule_cb = callback)</dt>
<dd>
<p>In this mode the values for <code>interval</code> and <code>at</code> are both being
ignored. Instead, each time the periodic watcher gets scheduled, the
<p>NOTE: <i>This callback MUST NOT stop or destroy any periodic watcher,
ever, or make any event loop modifications</i>. If you need to stop it,
return <code>now + 1e30</code> (or so, fudge fudge) and stop it afterwards (e.g. by
-starting a prepare watcher).</p>
+starting an <code>ev_prepare</code> watcher, which is legal).</p>
<p>Its prototype is <code>ev_tstamp (*reschedule_cb)(struct ev_periodic *w,
ev_tstamp now)</code>, e.g.:</p>
<pre> static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now)
when you changed some parameters or the reschedule callback would return
a different time than the last time it was called (e.g. in a crond like
program when the crontabs have changed).</p>
+ </dd>
+ <dt>ev_tstamp offset [read-write]</dt>
+ <dd>
+ <p>When repeating, this contains the offset value, otherwise this is the
+absolute point in time (the <code>at</code> value passed to <code>ev_periodic_set</code>).</p>
+ <p>Can be modified any time, but changes only take effect when the periodic
+timer fires or <code>ev_periodic_again</code> is being called.</p>
</dd>
<dt>ev_tstamp interval [read-write]</dt>
<dd>
of lower priority, but only once, using idle watchers to keep the event
loop from blocking if lower-priority coroutines are active, thus mapping
low-priority coroutines to idle/background tasks).</p>
+<p>It is recommended to give <code>ev_check</code> watchers highest (<code>EV_MAXPRI</code>)
+priority, to ensure that they are being run before any other watchers
+after the poll. Also, <code>ev_check</code> watchers (and <code>ev_prepare</code> watchers,
+too) should not activate ("feed") events into libev. While libev fully
+supports this, they will be called before other <code>ev_check</code> watchers did
+their job. As <code>ev_check</code> watchers are often used to embed other event
+loops those other event loops might be in an unusable state until their
+<code>ev_check</code> watcher ran (always remind yourself to coexist peacefully with
+others).</p>
<dl>
<dt>ev_prepare_init (ev_prepare *, callback)</dt>
<dt>ev_check_init (ev_check *, callback)</dt>
macros, but using them is utterly, utterly and completely pointless.</p>
</dd>
</dl>
-<p>Example: To include a library such as adns, you would add IO watchers
-and a timeout watcher in a prepare handler, as required by libadns, and
-in a check watcher, destroy them and call into libadns. What follows is
-pseudo-code only of course:</p>
+<p>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 <code>EV::ADNS</code> that does this, which you could
+use for an actually working example. Another Perl module named <code>EV::Glib</code>
+embeds a Glib main context into libev, and finally, <code>Glib::EV</code> embeds EV
+into the Glib event loop).</p>
+<p>Method 1: Add IO watchers and a timeout watcher in a prepare handler,
+and in a check watcher, destroy them and call into libadns. What follows
+is pseudo-code only of course. This requires you to either use a low
+priority for the check watcher or use <code>ev_clear_pending</code> explicitly, as
+the callbacks for the IO/timeout watchers might not have been called yet.</p>
<pre> static ev_io iow [nfd];
static ev_timer tw;
static void
io_cb (ev_loop *loop, ev_io *w, int revents)
{
- // set the relevant poll flags
- // could also call adns_processreadable etc. here
- struct pollfd *fd = (struct pollfd *)w->data;
- if (revents & EV_READ ) fd->revents |= fd->events & POLLIN;
- if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT;
}
// create io watchers for each fd and a timer before blocking
ev_timer_init (&tw, 0, timeout * 1e-3);
ev_timer_start (loop, &tw);
- // create on ev_io per pollfd
+ // create one ev_io per pollfd
for (int i = 0; i < nfd; ++i)
{
ev_io_init (iow + i, io_cb, fds [i].fd,
| (fds [i].events & POLLOUT ? EV_WRITE : 0)));
fds [i].revents = 0;
- iow [i].data = fds + i;
ev_io_start (loop, iow + i);
}
}
ev_timer_stop (loop, &tw);
for (int i = 0; i < nfd; ++i)
- ev_io_stop (loop, iow + i);
+ {
+ // set the relevant poll flags
+ // could also call adns_processreadable etc. here
+ struct pollfd *fd = fds + i;
+ int revents = ev_clear_pending (iow + i);
+ if (revents & EV_READ ) fd->revents |= fd->events & POLLIN;
+ if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT;
+
+ // now stop the watcher
+ ev_io_stop (loop, iow + i);
+ }
adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop));
}
+</pre>
+<p>Method 2: This would be just like method 1, but you run <code>adns_afterpoll</code>
+in the prepare watcher and would dispose of the check watcher.</p>
+<p>Method 3: If the module to be embedded supports explicit event
+notification (adns does), you can also make use of the actual watcher
+callbacks, and only destroy/create the watchers in the prepare watcher.</p>
+<pre> static void
+ timer_cb (EV_P_ ev_timer *w, int revents)
+ {
+ adns_state ads = (adns_state)w->data;
+ update_now (EV_A);
+
+ adns_processtimeouts (ads, &tv_now);
+ }
+
+ static void
+ io_cb (EV_P_ ev_io *w, int revents)
+ {
+ adns_state ads = (adns_state)w->data;
+ update_now (EV_A);
+
+ if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now);
+ if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now);
+ }
+
+ // do not ever call adns_afterpoll
+
+</pre>
+<p>Method 4: Do not use a prepare or check watcher because the module you
+want to embed is too inflexible to support it. Instead, youc na override
+their poll function. The drawback with this solution is that the main
+loop is now no longer controllable by EV. The <code>Glib::EV</code> module does
+this.</p>
+<pre> static gint
+ event_poll_func (GPollFD *fds, guint nfds, gint timeout)
+ {
+ int got_events = 0;
+
+ for (n = 0; n < nfds; ++n)
+ // create/start io watcher that sets the relevant bits in fds[n] and increment got_events
+
+ if (timeout >= 0)
+ // create/start timer
+
+ // poll
+ ev_loop (EV_A_ 0);
+
+ // stop timer again
+ if (timeout >= 0)
+ ev_timer_stop (EV_A_ &to);
+
+ // stop io watchers again - their callbacks should have set
+ for (n = 0; n < nfds; ++n)
+ ev_io_stop (EV_A_ iow [n]);
+
+ return got_events;
+ }
+