… | |
… | |
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="<standard input>" /> |
7 | <meta name="inputfile" content="<standard input>" /> |
8 | <meta name="outputfile" content="<standard output>" /> |
8 | <meta name="outputfile" content="<standard output>" /> |
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: |
1129 | prepare watchers get invoked before the process blocks and check watchers |
1129 | prepare watchers get invoked before the process blocks and check watchers |
1130 | afterwards.</p> |
1130 | afterwards.</p> |
|
|
1131 | <p>You <i>must not</i> call <code>ev_loop</code> or similar functions that enter |
|
|
1132 | the current event loop from either <code>ev_prepare</code> or <code>ev_check</code> |
|
|
1133 | watchers. Other loops than the current one are fine, however. The |
|
|
1134 | rationale behind this is that you do not need to check for recursion in |
|
|
1135 | those 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 |
|
|
1137 | called 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 |
1132 | their use is somewhat advanced. This could be used, for example, to track |
1139 | their use is somewhat advanced. This could be used, for example, to track |
1133 | variable changes, implement your own watchers, integrate net-snmp or a |
1140 | variable changes, implement your own watchers, integrate net-snmp or a |
1134 | coroutine library and lots more.</p> |
1141 | coroutine library and lots more. They are also occasionally useful if |
|
|
1142 | you cache some data and want to flush it before blocking (for example, |
|
|
1143 | in X programs you might want to do an <code>XFlush ()</code> in an <code>ev_prepare</code> |
|
|
1144 | watcher).</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 |
1136 | to be watched by the other library, registering <code>ev_io</code> watchers for |
1146 | to be watched by the other library, registering <code>ev_io</code> watchers for |
1137 | them and starting an <code>ev_timer</code> watcher for any timeouts (many libraries |
1147 | them and starting an <code>ev_timer</code> watcher for any timeouts (many libraries |
1138 | provide just this functionality). Then, in the check watcher you check for |
1148 | provide just this functionality). Then, in the check watcher you check for |
1139 | any events that occured (by checking the pending status of all watchers |
1149 | any 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 |
1156 | parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check_set</code> |
1166 | parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check_set</code> |
1157 | macros, but using them is utterly, utterly and completely pointless.</p> |
1167 | macros, 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 |
|
|
1171 | and a timeout watcher in a prepare handler, as required by libadns, and |
|
|
1172 | in a check watcher, destroy them and call into libadns. What follows is |
|
|
1173 | pseudo-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->data; |
|
|
1182 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1183 | if (revents & EV_WRITE) fd->revents |= fd->events & 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, &nfd, &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 (&tw, 0, timeout * 1e-3); |
|
|
1196 | ev_timer_start (loop, &tw); |
1164 | |
1197 | |
|
|
1198 | // create on ev_io per pollfd |
|
|
1199 | for (int i = 0; i < nfd; ++i) |
|
|
1200 | { |
|
|
1201 | ev_io_init (iow + i, io_cb, fds [i].fd, |
|
|
1202 | ((fds [i].events & POLLIN ? EV_READ : 0) |
|
|
1203 | | (fds [i].events & 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, &tw); |
|
|
1216 | |
|
|
1217 | for (int i = 0; i < 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 |