.\" ========================================================================
.\"
.IX Title ""<STANDARD INPUT>" 1"
-.TH "<STANDARD INPUT>" 1 "2007-11-24" "perl v5.8.8" "User Contributed Perl Documentation"
+.TH "<STANDARD INPUT>" 1 "2007-11-26" "perl v5.8.8" "User Contributed Perl Documentation"
.SH "NAME"
libev \- a high performance full\-featured event loop written in C
.SH "SYNOPSIS"
prepare watchers get invoked before the process blocks and check watchers
afterwards.
.PP
+You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter
+the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR
+watchers. Other loops than the current one are fine, however. The
+rationale behind this is that you do not need to check for recursion in
+those watchers, i.e. the sequence will always be \f(CW\*(C`ev_prepare\*(C'\fR, blocking,
+\&\f(CW\*(C`ev_check\*(C'\fR so if you have one watcher of each kind they will always be
+called in pairs bracketing the blocking call.
+.PP
Their main purpose is to integrate other event mechanisms into libev and
their use is somewhat advanced. This could be used, for example, to track
variable changes, implement your own watchers, integrate net-snmp or a
-coroutine library and lots more.
+coroutine library and lots more. They are also occasionally useful if
+you cache some data and want to flush it before blocking (for example,
+in X programs you might want to do an \f(CW\*(C`XFlush ()\*(C'\fR in an \f(CW\*(C`ev_prepare\*(C'\fR
+watcher).
.PP
This is done by examining in each prepare call which file descriptors need
to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for
parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR
macros, but using them is utterly, utterly and completely pointless.
.PP
-Example: *TODO*.
+Example: To include a library such as adns, you would add \s-1IO\s0 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:
+.PP
+.Vb 2
+\& static ev_io iow [nfd];
+\& static ev_timer tw;
+.Ve
+.PP
+.Vb 9
+\& 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;
+\& }
+.Ve
+.PP
+.Vb 7
+\& // create io watchers for each fd and a timer before blocking
+\& static void
+\& adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents)
+\& {
+\& int timeout = 3600000;truct pollfd fds [nfd];
+\& // actual code will need to loop here and realloc etc.
+\& adns_beforepoll (ads, fds, &nfd, &timeout, timeval_from (ev_time ()));
+.Ve
+.PP
+.Vb 3
+\& /* the callback is illegal, but won't be called as we stop during check */
+\& ev_timer_init (&tw, 0, timeout * 1e-3);
+\& ev_timer_start (loop, &tw);
+.Ve
+.PP
+.Vb 6
+\& // create on ev_io per pollfd
+\& for (int i = 0; i < nfd; ++i)
+\& {
+\& ev_io_init (iow + i, io_cb, fds [i].fd,
+\& ((fds [i].events & POLLIN ? EV_READ : 0)
+\& | (fds [i].events & POLLOUT ? EV_WRITE : 0)));
+.Ve
+.PP
+.Vb 5
+\& fds [i].revents = 0;
+\& iow [i].data = fds + i;
+\& ev_io_start (loop, iow + i);
+\& }
+\& }
+.Ve
+.PP
+.Vb 5
+\& // stop all watchers after blocking
+\& static void
+\& adns_check_cb (ev_loop *loop, ev_check *w, int revents)
+\& {
+\& ev_timer_stop (loop, &tw);
+.Ve
+.PP
+.Vb 2
+\& for (int i = 0; i < nfd; ++i)
+\& ev_io_stop (loop, iow + i);
+.Ve
+.PP
+.Vb 2
+\& adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop));
+\& }
+.Ve
.ie n .Sh """ev_embed"" \- when one backend isn't enough..."
.el .Sh "\f(CWev_embed\fP \- when one backend isn't enough..."
.IX Subsection "ev_embed - when one backend isn't enough..."
.Ve
.PP
.Vb 5
-\& ev_select.c only when select backend is enabled (which is is by default)
+\& ev_select.c only when select backend is enabled (which is by default)
\& ev_poll.c only when poll backend is enabled (disabled by default)
\& ev_epoll.c only when the epoll backend is enabled (disabled by default)
\& ev_kqueue.c only when the kqueue backend is enabled (disabled by default)
.Ve
.PP
\&\fIev.c\fR includes the backend files directly when enabled, so you only need
-to compile a single file.
+to compile this single file.
.PP
\fI\s-1LIBEVENT\s0 \s-1COMPATIBILITY\s0 \s-1API\s0\fR
.IX Subsection "LIBEVENT COMPATIBILITY API"
.PP
Instead of using \f(CW\*(C`EV_STANDALONE=1\*(C'\fR and providing your config in
whatever way you want, you can also \f(CW\*(C`m4_include([libev.m4])\*(C'\fR in your
-\&\fIconfigure.ac\fR and leave \f(CW\*(C`EV_STANDALONE\*(C'\fR off. \fIev.c\fR will then include
-\&\fIconfig.h\fR and configure itself accordingly.
+\&\fIconfigure.ac\fR and leave \f(CW\*(C`EV_STANDALONE\*(C'\fR undefined. \fIev.c\fR will then
+include \fIconfig.h\fR and configure itself accordingly.
.PP
For this of course you need the m4 file:
.PP
\& SV *self; /* contains this struct */ \e
\& SV *cb_sv, *fh /* note no trailing ";" */
.Ve
-.IP "\s-1EV_CB_DECLARE\s0(type)" 4
-.IX Item "EV_CB_DECLARE(type)"
+.IP "\s-1EV_CB_DECLARE\s0 (type)" 4
+.IX Item "EV_CB_DECLARE (type)"
.PD 0
-.IP "\s-1EV_CB_INVOKE\s0(watcher,revents)" 4
-.IX Item "EV_CB_INVOKE(watcher,revents)"
-.IP "ev_set_cb(ev,cb)" 4
-.IX Item "ev_set_cb(ev,cb)"
+.IP "\s-1EV_CB_INVOKE\s0 (watcher, revents)" 4
+.IX Item "EV_CB_INVOKE (watcher, revents)"
+.IP "ev_set_cb (ev, cb)" 4
+.IX Item "ev_set_cb (ev, cb)"
.PD
Can be used to change the callback member declaration in each watcher,
and the way callbacks are invoked and set. Must expand to a struct member
definition and a statement, respectively. See the \fIev.v\fR header file for
their default definitions. One possible use for overriding these is to
-avoid the ev_loop pointer as first argument in all cases, or to use method
-calls instead of plain function calls in \*(C+.
+avoid the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument in all cases, or to use
+method calls instead of plain function calls in \*(C+.
.Sh "\s-1EXAMPLES\s0"
.IX Subsection "EXAMPLES"
For a real-world example of a program the includes libev
\& #include "ev_cpp.h"
\& #include "ev.c"
.Ve
+.SH "COMPLEXITIES"
+.IX Header "COMPLEXITIES"
+In this section the complexities of (many of) the algorithms used inside
+libev will be explained. For complexity discussions about backends see the
+documentation for \f(CW\*(C`ev_default_init\*(C'\fR.
+.RS 4
+.IP "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" 4
+.IX Item "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)"
+.PD 0
+.IP "Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)" 4
+.IX Item "Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)"
+.IP "Starting io/check/prepare/idle/signal/child watchers: O(1)" 4
+.IX Item "Starting io/check/prepare/idle/signal/child watchers: O(1)"
+.IP "Stopping check/prepare/idle watchers: O(1)" 4
+.IX Item "Stopping check/prepare/idle watchers: O(1)"
+.IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % 16))" 4
+.IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % 16))"
+.IP "Finding the next timer per loop iteration: O(1)" 4
+.IX Item "Finding the next timer per loop iteration: O(1)"
+.IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4
+.IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)"
+.IP "Activating one watcher: O(1)" 4
+.IX Item "Activating one watcher: O(1)"
+.RE
+.RS 4
+.PD
.SH "AUTHOR"
.IX Header "AUTHOR"
Marc Lehmann <libev@schmorp.de>.