ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.html
(Generate patch)

Comparing libev/ev.html (file contents):
Revision 1.45 by root, Sat Nov 24 16:57:39 2007 UTC vs.
Revision 1.46 by root, Mon Nov 26 09:52:14 2007 UTC

4<head> 4<head>
5 <title>libev</title> 5 <title>libev</title>
6 <meta name="description" content="Pod documentation for libev" /> 6 <meta name="description" content="Pod documentation for libev" />
7 <meta name="inputfile" content="&lt;standard input&gt;" /> 7 <meta name="inputfile" content="&lt;standard input&gt;" />
8 <meta name="outputfile" content="&lt;standard output&gt;" /> 8 <meta name="outputfile" content="&lt;standard output&gt;" />
9 <meta name="created" content="Sat Nov 24 17:57:37 2007" /> 9 <meta name="created" content="Mon Nov 26 10:52:13 2007" />
10 <meta name="generator" content="Pod::Xhtml 1.57" /> 10 <meta name="generator" content="Pod::Xhtml 1.57" />
11<link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head> 11<link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head>
12<body> 12<body>
13<div class="pod"> 13<div class="pod">
14<!-- INDEX START --> 14<!-- INDEX START -->
1126<h2 id="code_ev_prepare_code_and_code_ev_che"><code>ev_prepare</code> and <code>ev_check</code> - customise your event loop!</h2> 1126<h2 id="code_ev_prepare_code_and_code_ev_che"><code>ev_prepare</code> and <code>ev_check</code> - customise your event loop!</h2>
1127<div id="code_ev_prepare_code_and_code_ev_che-2"> 1127<div id="code_ev_prepare_code_and_code_ev_che-2">
1128<p>Prepare and check watchers are usually (but not always) used in tandem: 1128<p>Prepare and check watchers are usually (but not always) used in tandem:
1129prepare watchers get invoked before the process blocks and check watchers 1129prepare watchers get invoked before the process blocks and check watchers
1130afterwards.</p> 1130afterwards.</p>
1131<p>You <i>must not</i> call <code>ev_loop</code> or similar functions that enter
1132the current event loop from either <code>ev_prepare</code> or <code>ev_check</code>
1133watchers. Other loops than the current one are fine, however. The
1134rationale behind this is that you do not need to check for recursion in
1135those watchers, i.e. the sequence will always be <code>ev_prepare</code>, blocking,
1136<code>ev_check</code> so if you have one watcher of each kind they will always be
1137called in pairs bracketing the blocking call.</p>
1131<p>Their main purpose is to integrate other event mechanisms into libev and 1138<p>Their main purpose is to integrate other event mechanisms into libev and
1132their use is somewhat advanced. This could be used, for example, to track 1139their use is somewhat advanced. This could be used, for example, to track
1133variable changes, implement your own watchers, integrate net-snmp or a 1140variable changes, implement your own watchers, integrate net-snmp or a
1134coroutine library and lots more.</p> 1141coroutine library and lots more. They are also occasionally useful if
1142you cache some data and want to flush it before blocking (for example,
1143in X programs you might want to do an <code>XFlush ()</code> in an <code>ev_prepare</code>
1144watcher).</p>
1135<p>This is done by examining in each prepare call which file descriptors need 1145<p>This is done by examining in each prepare call which file descriptors need
1136to be watched by the other library, registering <code>ev_io</code> watchers for 1146to be watched by the other library, registering <code>ev_io</code> watchers for
1137them and starting an <code>ev_timer</code> watcher for any timeouts (many libraries 1147them and starting an <code>ev_timer</code> watcher for any timeouts (many libraries
1138provide just this functionality). Then, in the check watcher you check for 1148provide just this functionality). Then, in the check watcher you check for
1139any events that occured (by checking the pending status of all watchers 1149any events that occured (by checking the pending status of all watchers
1155 <p>Initialises and configures the prepare or check watcher - they have no 1165 <p>Initialises and configures the prepare or check watcher - they have no
1156parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check_set</code> 1166parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check_set</code>
1157macros, but using them is utterly, utterly and completely pointless.</p> 1167macros, but using them is utterly, utterly and completely pointless.</p>
1158 </dd> 1168 </dd>
1159</dl> 1169</dl>
1160<p>Example: *TODO*.</p> 1170<p>Example: To include a library such as adns, you would add IO watchers
1171and a timeout watcher in a prepare handler, as required by libadns, and
1172in a check watcher, destroy them and call into libadns. What follows is
1173pseudo-code only of course:</p>
1174<pre> static ev_io iow [nfd];
1175 static ev_timer tw;
1161 1176
1177 static void
1178 io_cb (ev_loop *loop, ev_io *w, int revents)
1179 {
1180 // set the relevant poll flags
1181 struct pollfd *fd = (struct pollfd *)w-&gt;data;
1182 if (revents &amp; EV_READ ) fd-&gt;revents |= fd-&gt;events &amp; POLLIN;
1183 if (revents &amp; EV_WRITE) fd-&gt;revents |= fd-&gt;events &amp; POLLOUT;
1184 }
1162 1185
1186 // create io watchers for each fd and a timer before blocking
1187 static void
1188 adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents)
1189 {
1190 int timeout = 3600000;truct pollfd fds [nfd];
1191 // actual code will need to loop here and realloc etc.
1192 adns_beforepoll (ads, fds, &amp;nfd, &amp;timeout, timeval_from (ev_time ()));
1163 1193
1194 /* the callback is illegal, but won't be called as we stop during check */
1195 ev_timer_init (&amp;tw, 0, timeout * 1e-3);
1196 ev_timer_start (loop, &amp;tw);
1164 1197
1198 // create on ev_io per pollfd
1199 for (int i = 0; i &lt; nfd; ++i)
1200 {
1201 ev_io_init (iow + i, io_cb, fds [i].fd,
1202 ((fds [i].events &amp; POLLIN ? EV_READ : 0)
1203 | (fds [i].events &amp; POLLOUT ? EV_WRITE : 0)));
1204
1205 fds [i].revents = 0;
1206 iow [i].data = fds + i;
1207 ev_io_start (loop, iow + i);
1208 }
1209 }
1210
1211 // stop all watchers after blocking
1212 static void
1213 adns_check_cb (ev_loop *loop, ev_check *w, int revents)
1214 {
1215 ev_timer_stop (loop, &amp;tw);
1216
1217 for (int i = 0; i &lt; nfd; ++i)
1218 ev_io_stop (loop, iow + i);
1219
1220 adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop));
1221 }
1222
1223
1224
1225
1226</pre>
1165 1227
1166</div> 1228</div>
1167<h2 id="code_ev_embed_code_when_one_backend_"><code>ev_embed</code> - when one backend isn't enough...</h2> 1229<h2 id="code_ev_embed_code_when_one_backend_"><code>ev_embed</code> - when one backend isn't enough...</h2>
1168<div id="code_ev_embed_code_when_one_backend_-2"> 1230<div id="code_ev_embed_code_when_one_backend_-2">
1169<p>This is a rather advanced watcher type that lets you embed one event loop 1231<p>This is a rather advanced watcher type that lets you embed one event loop

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines