diff options
-rw-r--r-- | ev.3 | 87 | ||||
-rw-r--r-- | ev.html | 68 | ||||
-rw-r--r-- | ev.pod | 67 |
3 files changed, 214 insertions, 8 deletions
@@ -129,7 +129,7 @@ .\" ======================================================================== .\" .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" @@ -1244,10 +1244,21 @@ Prepare and check watchers are usually (but not always) used in tandem: 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 @@ -1276,7 +1287,77 @@ Initialises and configures the prepare or check watcher \- they have no 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 8 +\& static void +\& io_cb (ev_loop *loop, ev_io *w, int revents) +\& { +\& // set the relevant poll flags +\& 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..." @@ -6,7 +6,7 @@ <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 Nov 24 17:57:37 2007" /> + <meta name="created" content="Mon Nov 26 10:52:13 2007" /> <meta name="generator" content="Pod::Xhtml 1.57" /> <link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head> <body> @@ -1128,10 +1128,20 @@ callback, free it. Alos, use no error checking, as usual.</p> <p>Prepare and check watchers are usually (but not always) used in tandem: prepare watchers get invoked before the process blocks and check watchers afterwards.</p> +<p>You <i>must not</i> call <code>ev_loop</code> or similar functions that enter +the current event loop from either <code>ev_prepare</code> or <code>ev_check</code> +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 <code>ev_prepare</code>, blocking, +<code>ev_check</code> so if you have one watcher of each kind they will always be +called in pairs bracketing the blocking call.</p> <p>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.</p> +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 <code>XFlush ()</code> in an <code>ev_prepare</code> +watcher).</p> <p>This is done by examining in each prepare call which file descriptors need to be watched by the other library, registering <code>ev_io</code> watchers for them and starting an <code>ev_timer</code> watcher for any timeouts (many libraries @@ -1157,11 +1167,63 @@ parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check macros, but using them is utterly, utterly and completely pointless.</p> </dd> </dl> -<p>Example: *TODO*.</p> +<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> +<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 + 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 + 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 ())); + + /* 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); + + // 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))); + + fds [i].revents = 0; + iow [i].data = fds + i; + ev_io_start (loop, iow + i); + } + } + // stop all watchers after blocking + static void + adns_check_cb (ev_loop *loop, ev_check *w, int revents) + { + ev_timer_stop (loop, &tw); + for (int i = 0; i < nfd; ++i) + ev_io_stop (loop, iow + i); + adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); + } + + + + +</pre> </div> <h2 id="code_ev_embed_code_when_one_backend_"><code>ev_embed</code> - when one backend isn't enough...</h2> @@ -1104,10 +1104,21 @@ Prepare and check watchers are usually (but not always) used in tandem: prepare watchers get invoked before the process blocks and check watchers afterwards. +You I<must not> call C<ev_loop> or similar functions that enter +the current event loop from either C<ev_prepare> or C<ev_check> +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 C<ev_prepare>, blocking, +C<ev_check> so if you have one watcher of each kind they will always be +called in pairs bracketing the blocking call. + 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 C<XFlush ()> in an C<ev_prepare> +watcher). This is done by examining in each prepare call which file descriptors need to be watched by the other library, registering C<ev_io> watchers for @@ -1139,7 +1150,59 @@ macros, but using them is utterly, utterly and completely pointless. =back -Example: *TODO*. +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: + + 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 + 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 + 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 ())); + + /* 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); + + // 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))); + + fds [i].revents = 0; + iow [i].data = fds + i; + ev_io_start (loop, iow + i); + } + } + + // stop all watchers after blocking + static void + adns_check_cb (ev_loop *loop, ev_check *w, int revents) + { + ev_timer_stop (loop, &tw); + + for (int i = 0; i < nfd; ++i) + ev_io_stop (loop, iow + i); + + adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); + } =head2 C<ev_embed> - when one backend isn't enough... |