… | |
… | |
645 | *)\*(C'\fR), and you can stop watching for events at any time by calling the |
645 | *)\*(C'\fR), and you can stop watching for events at any time by calling the |
646 | corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR. |
646 | corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR. |
647 | .PP |
647 | .PP |
648 | As long as your watcher is active (has been started but not stopped) you |
648 | As long as your watcher is active (has been started but not stopped) you |
649 | must not touch the values stored in it. Most specifically you must never |
649 | must not touch the values stored in it. Most specifically you must never |
650 | reinitialise it or call its set macro. |
650 | reinitialise it or call its \f(CW\*(C`set\*(C'\fR macro. |
651 | .PP |
|
|
652 | You can check whether an event is active by calling the \f(CW\*(C`ev_is_active |
|
|
653 | (watcher *)\*(C'\fR macro. To see whether an event is outstanding (but the |
|
|
654 | callback for it has not been called yet) you can use the \f(CW\*(C`ev_is_pending |
|
|
655 | (watcher *)\*(C'\fR macro. |
|
|
656 | .PP |
651 | .PP |
657 | Each and every callback receives the event loop pointer as first, the |
652 | Each and every callback receives the event loop pointer as first, the |
658 | registered watcher structure as second, and a bitset of received events as |
653 | registered watcher structure as second, and a bitset of received events as |
659 | third argument. |
654 | third argument. |
660 | .PP |
655 | .PP |
… | |
… | |
718 | Libev will usually signal a few \*(L"dummy\*(R" events together with an error, |
713 | Libev will usually signal a few \*(L"dummy\*(R" events together with an error, |
719 | for example it might indicate that a fd is readable or writable, and if |
714 | for example it might indicate that a fd is readable or writable, and if |
720 | your callbacks is well-written it can just attempt the operation and cope |
715 | your callbacks is well-written it can just attempt the operation and cope |
721 | with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multithreaded |
716 | with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multithreaded |
722 | programs, though, so beware. |
717 | programs, though, so beware. |
|
|
718 | .Sh "\s-1SUMMARY\s0 \s-1OF\s0 \s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0" |
|
|
719 | .IX Subsection "SUMMARY OF GENERIC WATCHER FUNCTIONS" |
|
|
720 | In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type, |
|
|
721 | e.g. \f(CW\*(C`timer\*(C'\fR for \f(CW\*(C`ev_timer\*(C'\fR watchers and \f(CW\*(C`io\*(C'\fR for \f(CW\*(C`ev_io\*(C'\fR watchers. |
|
|
722 | .ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4 |
|
|
723 | .el .IP "\f(CWev_init\fR (ev_TYPE *watcher, callback)" 4 |
|
|
724 | .IX Item "ev_init (ev_TYPE *watcher, callback)" |
|
|
725 | This macro initialises the generic portion of a watcher. The contents |
|
|
726 | of the watcher object can be arbitrary (so \f(CW\*(C`malloc\*(C'\fR will do). Only |
|
|
727 | the generic parts of the watcher are initialised, you \fIneed\fR to call |
|
|
728 | the type-specific \f(CW\*(C`ev_TYPE_set\*(C'\fR macro afterwards to initialise the |
|
|
729 | type-specific parts. For each type there is also a \f(CW\*(C`ev_TYPE_init\*(C'\fR macro |
|
|
730 | which rolls both calls into one. |
|
|
731 | .Sp |
|
|
732 | You can reinitialise a watcher at any time as long as it has been stopped |
|
|
733 | (or never started) and there are no pending events outstanding. |
|
|
734 | .Sp |
|
|
735 | The callbakc is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher, |
|
|
736 | int revents)\*(C'\fR. |
|
|
737 | .ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4 |
|
|
738 | .el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4 |
|
|
739 | .IX Item "ev_TYPE_set (ev_TYPE *, [args])" |
|
|
740 | This macro initialises the type-specific parts of a watcher. You need to |
|
|
741 | call \f(CW\*(C`ev_init\*(C'\fR at least once before you call this macro, but you can |
|
|
742 | call \f(CW\*(C`ev_TYPE_set\*(C'\fR any number of times. You must not, however, call this |
|
|
743 | macro on a watcher that is active (it can be pending, however, which is a |
|
|
744 | difference to the \f(CW\*(C`ev_init\*(C'\fR macro). |
|
|
745 | .Sp |
|
|
746 | Although some watcher types do not have type-specific arguments |
|
|
747 | (e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro. |
|
|
748 | .ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4 |
|
|
749 | .el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4 |
|
|
750 | .IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])" |
|
|
751 | This convinience macro rolls both \f(CW\*(C`ev_init\*(C'\fR and \f(CW\*(C`ev_TYPE_set\*(C'\fR macro |
|
|
752 | calls into a single call. This is the most convinient method to initialise |
|
|
753 | a watcher. The same limitations apply, of course. |
|
|
754 | .ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4 |
|
|
755 | .el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4 |
|
|
756 | .IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)" |
|
|
757 | Starts (activates) the given watcher. Only active watchers will receive |
|
|
758 | events. If the watcher is already active nothing will happen. |
|
|
759 | .ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4 |
|
|
760 | .el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4 |
|
|
761 | .IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)" |
|
|
762 | Stops the given watcher again (if active) and clears the pending |
|
|
763 | status. It is possible that stopped watchers are pending (for example, |
|
|
764 | non-repeating timers are being stopped when they become pending), but |
|
|
765 | \&\f(CW\*(C`ev_TYPE_stop\*(C'\fR ensures that the watcher is neither active nor pending. If |
|
|
766 | you want to free or reuse the memory used by the watcher it is therefore a |
|
|
767 | good idea to always call its \f(CW\*(C`ev_TYPE_stop\*(C'\fR function. |
|
|
768 | .IP "bool ev_is_active (ev_TYPE *watcher)" 4 |
|
|
769 | .IX Item "bool ev_is_active (ev_TYPE *watcher)" |
|
|
770 | Returns a true value iff the watcher is active (i.e. it has been started |
|
|
771 | and not yet been stopped). As long as a watcher is active you must not modify |
|
|
772 | it. |
|
|
773 | .IP "bool ev_is_pending (ev_TYPE *watcher)" 4 |
|
|
774 | .IX Item "bool ev_is_pending (ev_TYPE *watcher)" |
|
|
775 | Returns a true value iff the watcher is pending, (i.e. it has outstanding |
|
|
776 | events but its callback has not yet been invoked). As long as a watcher |
|
|
777 | is pending (but not active) you must not call an init function on it (but |
|
|
778 | \&\f(CW\*(C`ev_TYPE_set\*(C'\fR is safe) and you must make sure the watcher is available to |
|
|
779 | libev (e.g. you cnanot \f(CW\*(C`free ()\*(C'\fR it). |
|
|
780 | .IP "callback = ev_cb (ev_TYPE *watcher)" 4 |
|
|
781 | .IX Item "callback = ev_cb (ev_TYPE *watcher)" |
|
|
782 | Returns the callback currently set on the watcher. |
|
|
783 | .IP "ev_cb_set (ev_TYPE *watcher, callback)" 4 |
|
|
784 | .IX Item "ev_cb_set (ev_TYPE *watcher, callback)" |
|
|
785 | Change the callback. You can change the callback at virtually any time |
|
|
786 | (modulo threads). |
723 | .Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" |
787 | .Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" |
724 | .IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" |
788 | .IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" |
725 | Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change |
789 | Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change |
726 | and read at any time, libev will completely ignore it. This can be used |
790 | and read at any time, libev will completely ignore it. This can be used |
727 | to associate arbitrary data with your watcher. If you need more data and |
791 | to associate arbitrary data with your watcher. If you need more data and |
… | |
… | |
1205 | Example: *TODO*. |
1269 | Example: *TODO*. |
1206 | .ie n .Sh """ev_embed"" \- when one backend isn't enough" |
1270 | .ie n .Sh """ev_embed"" \- when one backend isn't enough" |
1207 | .el .Sh "\f(CWev_embed\fP \- when one backend isn't enough" |
1271 | .el .Sh "\f(CWev_embed\fP \- when one backend isn't enough" |
1208 | .IX Subsection "ev_embed - when one backend isn't enough" |
1272 | .IX Subsection "ev_embed - when one backend isn't enough" |
1209 | This is a rather advanced watcher type that lets you embed one event loop |
1273 | This is a rather advanced watcher type that lets you embed one event loop |
1210 | into another. |
1274 | into another (currently only \f(CW\*(C`ev_io\*(C'\fR events are supported in the embedded |
|
|
1275 | loop, other types of watchers might be handled in a delayed or incorrect |
|
|
1276 | fashion and must not be used). |
1211 | .PP |
1277 | .PP |
1212 | There are primarily two reasons you would want that: work around bugs and |
1278 | There are primarily two reasons you would want that: work around bugs and |
1213 | prioritise I/O. |
1279 | prioritise I/O. |
1214 | .PP |
1280 | .PP |
1215 | As an example for a bug workaround, the kqueue backend might only support |
1281 | As an example for a bug workaround, the kqueue backend might only support |
… | |
… | |
1223 | As for prioritising I/O: rarely you have the case where some fds have |
1289 | As for prioritising I/O: rarely you have the case where some fds have |
1224 | to be watched and handled very quickly (with low latency), and even |
1290 | to be watched and handled very quickly (with low latency), and even |
1225 | priorities and idle watchers might have too much overhead. In this case |
1291 | priorities and idle watchers might have too much overhead. In this case |
1226 | you would put all the high priority stuff in one loop and all the rest in |
1292 | you would put all the high priority stuff in one loop and all the rest in |
1227 | a second one, and embed the second one in the first. |
1293 | a second one, and embed the second one in the first. |
|
|
1294 | .PP |
|
|
1295 | As long as the watcher is active, the callback will be invoked every time |
|
|
1296 | there might be events pending in the embedded loop. The callback must then |
|
|
1297 | call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke |
|
|
1298 | their callbacks (you could also start an idle watcher to give the embedded |
|
|
1299 | loop strictly lower priority for example). You can also set the callback |
|
|
1300 | to \f(CW0\fR, in which case the embed watcher will automatically execute the |
|
|
1301 | embedded loop sweep. |
1228 | .PP |
1302 | .PP |
1229 | As long as the watcher is started it will automatically handle events. The |
1303 | As long as the watcher is started it will automatically handle events. The |
1230 | callback will be invoked whenever some events have been handled. You can |
1304 | callback will be invoked whenever some events have been handled. You can |
1231 | set the callback to \f(CW0\fR to avoid having to specify one if you are not |
1305 | set the callback to \f(CW0\fR to avoid having to specify one if you are not |
1232 | interested in that. |
1306 | interested in that. |
… | |
… | |
1267 | \& ev_embed_start (loop_hi, &embed); |
1341 | \& ev_embed_start (loop_hi, &embed); |
1268 | \& } |
1342 | \& } |
1269 | \& else |
1343 | \& else |
1270 | \& loop_lo = loop_hi; |
1344 | \& loop_lo = loop_hi; |
1271 | .Ve |
1345 | .Ve |
1272 | .IP "ev_embed_init (ev_embed *, callback, struct ev_loop *loop)" 4 |
1346 | .IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4 |
1273 | .IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *loop)" |
1347 | .IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" |
1274 | .PD 0 |
1348 | .PD 0 |
1275 | .IP "ev_embed_set (ev_embed *, callback, struct ev_loop *loop)" 4 |
1349 | .IP "ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop)" 4 |
1276 | .IX Item "ev_embed_set (ev_embed *, callback, struct ev_loop *loop)" |
1350 | .IX Item "ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop)" |
1277 | .PD |
1351 | .PD |
1278 | Configures the watcher to embed the given loop, which must be embeddable. |
1352 | Configures the watcher to embed the given loop, which must be |
|
|
1353 | embeddable. If the callback is \f(CW0\fR, then \f(CW\*(C`ev_embed_sweep\*(C'\fR will be |
|
|
1354 | invoked automatically, otherwise it is the responsibility of the callback |
|
|
1355 | to invoke it (it will continue to be called until the sweep has been done, |
|
|
1356 | if you do not want thta, you need to temporarily stop the embed watcher). |
|
|
1357 | .IP "ev_embed_sweep (loop, ev_embed *)" 4 |
|
|
1358 | .IX Item "ev_embed_sweep (loop, ev_embed *)" |
|
|
1359 | Make a single, non-blocking sweep over the embedded loop. This works |
|
|
1360 | similarly to \f(CW\*(C`ev_loop (embedded_loop, EVLOOP_NONBLOCK)\*(C'\fR, but in the most |
|
|
1361 | apropriate way for embedded loops. |
1279 | .SH "OTHER FUNCTIONS" |
1362 | .SH "OTHER FUNCTIONS" |
1280 | .IX Header "OTHER FUNCTIONS" |
1363 | .IX Header "OTHER FUNCTIONS" |
1281 | There are some other functions of possible interest. Described. Here. Now. |
1364 | There are some other functions of possible interest. Described. Here. Now. |
1282 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
1365 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
1283 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
1366 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
… | |
… | |
1312 | .Ve |
1395 | .Ve |
1313 | .Sp |
1396 | .Sp |
1314 | .Vb 1 |
1397 | .Vb 1 |
1315 | \& ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
1398 | \& ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); |
1316 | .Ve |
1399 | .Ve |
1317 | .IP "ev_feed_event (loop, watcher, int events)" 4 |
1400 | .IP "ev_feed_event (ev_loop *, watcher *, int revents)" 4 |
1318 | .IX Item "ev_feed_event (loop, watcher, int events)" |
1401 | .IX Item "ev_feed_event (ev_loop *, watcher *, int revents)" |
1319 | Feeds the given event set into the event loop, as if the specified event |
1402 | Feeds the given event set into the event loop, as if the specified event |
1320 | had happened for the specified watcher (which must be a pointer to an |
1403 | had happened for the specified watcher (which must be a pointer to an |
1321 | initialised but not necessarily started event watcher). |
1404 | initialised but not necessarily started event watcher). |
1322 | .IP "ev_feed_fd_event (loop, int fd, int revents)" 4 |
1405 | .IP "ev_feed_fd_event (ev_loop *, int fd, int revents)" 4 |
1323 | .IX Item "ev_feed_fd_event (loop, int fd, int revents)" |
1406 | .IX Item "ev_feed_fd_event (ev_loop *, int fd, int revents)" |
1324 | Feed an event on the given fd, as if a file descriptor backend detected |
1407 | Feed an event on the given fd, as if a file descriptor backend detected |
1325 | the given events it. |
1408 | the given events it. |
1326 | .IP "ev_feed_signal_event (loop, int signum)" 4 |
1409 | .IP "ev_feed_signal_event (ev_loop *loop, int signum)" 4 |
1327 | .IX Item "ev_feed_signal_event (loop, int signum)" |
1410 | .IX Item "ev_feed_signal_event (ev_loop *loop, int signum)" |
1328 | Feed an event as if the given signal occured (loop must be the default loop!). |
1411 | Feed an event as if the given signal occured (\f(CW\*(C`loop\*(C'\fR must be the default |
|
|
1412 | loop!). |
1329 | .SH "LIBEVENT EMULATION" |
1413 | .SH "LIBEVENT EMULATION" |
1330 | .IX Header "LIBEVENT EMULATION" |
1414 | .IX Header "LIBEVENT EMULATION" |
1331 | Libev offers a compatibility emulation layer for libevent. It cannot |
1415 | Libev offers a compatibility emulation layer for libevent. It cannot |
1332 | emulate the internals of libevent, so here are some usage hints: |
1416 | emulate the internals of libevent, so here are some usage hints: |
1333 | .IP "* Use it by including <event.h>, as usual." 4 |
1417 | .IP "* Use it by including <event.h>, as usual." 4 |