… | |
… | |
127 | .\} |
127 | .\} |
128 | .rm #[ #] #H #V #F C |
128 | .rm #[ #] #H #V #F C |
129 | .\" ======================================================================== |
129 | .\" ======================================================================== |
130 | .\" |
130 | .\" |
131 | .IX Title ""<STANDARD INPUT>" 1" |
131 | .IX Title ""<STANDARD INPUT>" 1" |
132 | .TH "<STANDARD INPUT>" 1 "2007-11-24" "perl v5.8.8" "User Contributed Perl Documentation" |
132 | .TH "<STANDARD INPUT>" 1 "2007-11-26" "perl v5.8.8" "User Contributed Perl Documentation" |
133 | .SH "NAME" |
133 | .SH "NAME" |
134 | libev \- a high performance full\-featured event loop written in C |
134 | libev \- a high performance full\-featured event loop written in C |
135 | .SH "SYNOPSIS" |
135 | .SH "SYNOPSIS" |
136 | .IX Header "SYNOPSIS" |
136 | .IX Header "SYNOPSIS" |
137 | .Vb 1 |
137 | .Vb 1 |
… | |
… | |
1242 | .IX Subsection "ev_prepare and ev_check - customise your event loop!" |
1242 | .IX Subsection "ev_prepare and ev_check - customise your event loop!" |
1243 | Prepare and check watchers are usually (but not always) used in tandem: |
1243 | Prepare and check watchers are usually (but not always) used in tandem: |
1244 | prepare watchers get invoked before the process blocks and check watchers |
1244 | prepare watchers get invoked before the process blocks and check watchers |
1245 | afterwards. |
1245 | afterwards. |
1246 | .PP |
1246 | .PP |
|
|
1247 | You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter |
|
|
1248 | the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR |
|
|
1249 | watchers. Other loops than the current one are fine, however. The |
|
|
1250 | rationale behind this is that you do not need to check for recursion in |
|
|
1251 | those watchers, i.e. the sequence will always be \f(CW\*(C`ev_prepare\*(C'\fR, blocking, |
|
|
1252 | \&\f(CW\*(C`ev_check\*(C'\fR so if you have one watcher of each kind they will always be |
|
|
1253 | called in pairs bracketing the blocking call. |
|
|
1254 | .PP |
1247 | Their main purpose is to integrate other event mechanisms into libev and |
1255 | Their main purpose is to integrate other event mechanisms into libev and |
1248 | their use is somewhat advanced. This could be used, for example, to track |
1256 | their use is somewhat advanced. This could be used, for example, to track |
1249 | variable changes, implement your own watchers, integrate net-snmp or a |
1257 | variable changes, implement your own watchers, integrate net-snmp or a |
1250 | coroutine library and lots more. |
1258 | coroutine library and lots more. They are also occasionally useful if |
|
|
1259 | you cache some data and want to flush it before blocking (for example, |
|
|
1260 | in X programs you might want to do an \f(CW\*(C`XFlush ()\*(C'\fR in an \f(CW\*(C`ev_prepare\*(C'\fR |
|
|
1261 | watcher). |
1251 | .PP |
1262 | .PP |
1252 | This is done by examining in each prepare call which file descriptors need |
1263 | This is done by examining in each prepare call which file descriptors need |
1253 | to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for |
1264 | to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for |
1254 | them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries |
1265 | them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries |
1255 | provide just this functionality). Then, in the check watcher you check for |
1266 | provide just this functionality). Then, in the check watcher you check for |
… | |
… | |
1274 | .PD |
1285 | .PD |
1275 | Initialises and configures the prepare or check watcher \- they have no |
1286 | Initialises and configures the prepare or check watcher \- they have no |
1276 | parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR |
1287 | parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR |
1277 | macros, but using them is utterly, utterly and completely pointless. |
1288 | macros, but using them is utterly, utterly and completely pointless. |
1278 | .PP |
1289 | .PP |
1279 | Example: *TODO*. |
1290 | Example: To include a library such as adns, you would add \s-1IO\s0 watchers |
|
|
1291 | and a timeout watcher in a prepare handler, as required by libadns, and |
|
|
1292 | in a check watcher, destroy them and call into libadns. What follows is |
|
|
1293 | pseudo-code only of course: |
|
|
1294 | .PP |
|
|
1295 | .Vb 2 |
|
|
1296 | \& static ev_io iow [nfd]; |
|
|
1297 | \& static ev_timer tw; |
|
|
1298 | .Ve |
|
|
1299 | .PP |
|
|
1300 | .Vb 9 |
|
|
1301 | \& static void |
|
|
1302 | \& io_cb (ev_loop *loop, ev_io *w, int revents) |
|
|
1303 | \& { |
|
|
1304 | \& // set the relevant poll flags |
|
|
1305 | \& // could also call adns_processreadable etc. here |
|
|
1306 | \& struct pollfd *fd = (struct pollfd *)w->data; |
|
|
1307 | \& if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1308 | \& if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
|
|
1309 | \& } |
|
|
1310 | .Ve |
|
|
1311 | .PP |
|
|
1312 | .Vb 7 |
|
|
1313 | \& // create io watchers for each fd and a timer before blocking |
|
|
1314 | \& static void |
|
|
1315 | \& adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
|
|
1316 | \& { |
|
|
1317 | \& int timeout = 3600000;truct pollfd fds [nfd]; |
|
|
1318 | \& // actual code will need to loop here and realloc etc. |
|
|
1319 | \& adns_beforepoll (ads, fds, &nfd, &timeout, timeval_from (ev_time ())); |
|
|
1320 | .Ve |
|
|
1321 | .PP |
|
|
1322 | .Vb 3 |
|
|
1323 | \& /* the callback is illegal, but won't be called as we stop during check */ |
|
|
1324 | \& ev_timer_init (&tw, 0, timeout * 1e-3); |
|
|
1325 | \& ev_timer_start (loop, &tw); |
|
|
1326 | .Ve |
|
|
1327 | .PP |
|
|
1328 | .Vb 6 |
|
|
1329 | \& // create on ev_io per pollfd |
|
|
1330 | \& for (int i = 0; i < nfd; ++i) |
|
|
1331 | \& { |
|
|
1332 | \& ev_io_init (iow + i, io_cb, fds [i].fd, |
|
|
1333 | \& ((fds [i].events & POLLIN ? EV_READ : 0) |
|
|
1334 | \& | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
|
|
1335 | .Ve |
|
|
1336 | .PP |
|
|
1337 | .Vb 5 |
|
|
1338 | \& fds [i].revents = 0; |
|
|
1339 | \& iow [i].data = fds + i; |
|
|
1340 | \& ev_io_start (loop, iow + i); |
|
|
1341 | \& } |
|
|
1342 | \& } |
|
|
1343 | .Ve |
|
|
1344 | .PP |
|
|
1345 | .Vb 5 |
|
|
1346 | \& // stop all watchers after blocking |
|
|
1347 | \& static void |
|
|
1348 | \& adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
|
|
1349 | \& { |
|
|
1350 | \& ev_timer_stop (loop, &tw); |
|
|
1351 | .Ve |
|
|
1352 | .PP |
|
|
1353 | .Vb 2 |
|
|
1354 | \& for (int i = 0; i < nfd; ++i) |
|
|
1355 | \& ev_io_stop (loop, iow + i); |
|
|
1356 | .Ve |
|
|
1357 | .PP |
|
|
1358 | .Vb 2 |
|
|
1359 | \& adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
|
|
1360 | \& } |
|
|
1361 | .Ve |
1280 | .ie n .Sh """ev_embed"" \- when one backend isn't enough..." |
1362 | .ie n .Sh """ev_embed"" \- when one backend isn't enough..." |
1281 | .el .Sh "\f(CWev_embed\fP \- when one backend isn't enough..." |
1363 | .el .Sh "\f(CWev_embed\fP \- when one backend isn't enough..." |
1282 | .IX Subsection "ev_embed - when one backend isn't enough..." |
1364 | .IX Subsection "ev_embed - when one backend isn't enough..." |
1283 | This is a rather advanced watcher type that lets you embed one event loop |
1365 | This is a rather advanced watcher type that lets you embed one event loop |
1284 | into another (currently only \f(CW\*(C`ev_io\*(C'\fR events are supported in the embedded |
1366 | into another (currently only \f(CW\*(C`ev_io\*(C'\fR events are supported in the embedded |
… | |
… | |
1599 | .Vb 1 |
1681 | .Vb 1 |
1600 | \& ev_win32.c required on win32 platforms only |
1682 | \& ev_win32.c required on win32 platforms only |
1601 | .Ve |
1683 | .Ve |
1602 | .PP |
1684 | .PP |
1603 | .Vb 5 |
1685 | .Vb 5 |
1604 | \& ev_select.c only when select backend is enabled (which is is by default) |
1686 | \& ev_select.c only when select backend is enabled (which is by default) |
1605 | \& ev_poll.c only when poll backend is enabled (disabled by default) |
1687 | \& ev_poll.c only when poll backend is enabled (disabled by default) |
1606 | \& ev_epoll.c only when the epoll backend is enabled (disabled by default) |
1688 | \& ev_epoll.c only when the epoll backend is enabled (disabled by default) |
1607 | \& ev_kqueue.c only when the kqueue backend is enabled (disabled by default) |
1689 | \& ev_kqueue.c only when the kqueue backend is enabled (disabled by default) |
1608 | \& ev_port.c only when the solaris port backend is enabled (disabled by default) |
1690 | \& ev_port.c only when the solaris port backend is enabled (disabled by default) |
1609 | .Ve |
1691 | .Ve |
1610 | .PP |
1692 | .PP |
1611 | \&\fIev.c\fR includes the backend files directly when enabled, so you only need |
1693 | \&\fIev.c\fR includes the backend files directly when enabled, so you only need |
1612 | to compile a single file. |
1694 | to compile this single file. |
1613 | .PP |
1695 | .PP |
1614 | \fI\s-1LIBEVENT\s0 \s-1COMPATIBILITY\s0 \s-1API\s0\fR |
1696 | \fI\s-1LIBEVENT\s0 \s-1COMPATIBILITY\s0 \s-1API\s0\fR |
1615 | .IX Subsection "LIBEVENT COMPATIBILITY API" |
1697 | .IX Subsection "LIBEVENT COMPATIBILITY API" |
1616 | .PP |
1698 | .PP |
1617 | To include the libevent compatibility \s-1API\s0, also include: |
1699 | To include the libevent compatibility \s-1API\s0, also include: |
… | |
… | |
1638 | \fI\s-1AUTOCONF\s0 \s-1SUPPORT\s0\fR |
1720 | \fI\s-1AUTOCONF\s0 \s-1SUPPORT\s0\fR |
1639 | .IX Subsection "AUTOCONF SUPPORT" |
1721 | .IX Subsection "AUTOCONF SUPPORT" |
1640 | .PP |
1722 | .PP |
1641 | Instead of using \f(CW\*(C`EV_STANDALONE=1\*(C'\fR and providing your config in |
1723 | Instead of using \f(CW\*(C`EV_STANDALONE=1\*(C'\fR and providing your config in |
1642 | whatever way you want, you can also \f(CW\*(C`m4_include([libev.m4])\*(C'\fR in your |
1724 | whatever way you want, you can also \f(CW\*(C`m4_include([libev.m4])\*(C'\fR in your |
1643 | \&\fIconfigure.ac\fR and leave \f(CW\*(C`EV_STANDALONE\*(C'\fR off. \fIev.c\fR will then include |
1725 | \&\fIconfigure.ac\fR and leave \f(CW\*(C`EV_STANDALONE\*(C'\fR undefined. \fIev.c\fR will then |
1644 | \&\fIconfig.h\fR and configure itself accordingly. |
1726 | include \fIconfig.h\fR and configure itself accordingly. |
1645 | .PP |
1727 | .PP |
1646 | For this of course you need the m4 file: |
1728 | For this of course you need the m4 file: |
1647 | .PP |
1729 | .PP |
1648 | .Vb 1 |
1730 | .Vb 1 |
1649 | \& libev.m4 |
1731 | \& libev.m4 |
… | |
… | |
1775 | .Vb 3 |
1857 | .Vb 3 |
1776 | \& #define EV_COMMON \e |
1858 | \& #define EV_COMMON \e |
1777 | \& SV *self; /* contains this struct */ \e |
1859 | \& SV *self; /* contains this struct */ \e |
1778 | \& SV *cb_sv, *fh /* note no trailing ";" */ |
1860 | \& SV *cb_sv, *fh /* note no trailing ";" */ |
1779 | .Ve |
1861 | .Ve |
1780 | .IP "\s-1EV_CB_DECLARE\s0(type)" 4 |
1862 | .IP "\s-1EV_CB_DECLARE\s0 (type)" 4 |
1781 | .IX Item "EV_CB_DECLARE(type)" |
1863 | .IX Item "EV_CB_DECLARE (type)" |
1782 | .PD 0 |
1864 | .PD 0 |
1783 | .IP "\s-1EV_CB_INVOKE\s0(watcher,revents)" 4 |
1865 | .IP "\s-1EV_CB_INVOKE\s0 (watcher, revents)" 4 |
1784 | .IX Item "EV_CB_INVOKE(watcher,revents)" |
1866 | .IX Item "EV_CB_INVOKE (watcher, revents)" |
1785 | .IP "ev_set_cb(ev,cb)" 4 |
1867 | .IP "ev_set_cb (ev, cb)" 4 |
1786 | .IX Item "ev_set_cb(ev,cb)" |
1868 | .IX Item "ev_set_cb (ev, cb)" |
1787 | .PD |
1869 | .PD |
1788 | Can be used to change the callback member declaration in each watcher, |
1870 | Can be used to change the callback member declaration in each watcher, |
1789 | and the way callbacks are invoked and set. Must expand to a struct member |
1871 | and the way callbacks are invoked and set. Must expand to a struct member |
1790 | definition and a statement, respectively. See the \fIev.v\fR header file for |
1872 | definition and a statement, respectively. See the \fIev.v\fR header file for |
1791 | their default definitions. One possible use for overriding these is to |
1873 | their default definitions. One possible use for overriding these is to |
1792 | avoid the ev_loop pointer as first argument in all cases, or to use method |
1874 | avoid the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument in all cases, or to use |
1793 | calls instead of plain function calls in \*(C+. |
1875 | method calls instead of plain function calls in \*(C+. |
1794 | .Sh "\s-1EXAMPLES\s0" |
1876 | .Sh "\s-1EXAMPLES\s0" |
1795 | .IX Subsection "EXAMPLES" |
1877 | .IX Subsection "EXAMPLES" |
1796 | For a real-world example of a program the includes libev |
1878 | For a real-world example of a program the includes libev |
1797 | verbatim, you can have a look at the \s-1EV\s0 perl module |
1879 | verbatim, you can have a look at the \s-1EV\s0 perl module |
1798 | (<http://software.schmorp.de/pkg/EV.html>). It has the libev files in |
1880 | (<http://software.schmorp.de/pkg/EV.html>). It has the libev files in |
… | |
… | |
1819 | .Sp |
1901 | .Sp |
1820 | .Vb 2 |
1902 | .Vb 2 |
1821 | \& #include "ev_cpp.h" |
1903 | \& #include "ev_cpp.h" |
1822 | \& #include "ev.c" |
1904 | \& #include "ev.c" |
1823 | .Ve |
1905 | .Ve |
|
|
1906 | .SH "COMPLEXITIES" |
|
|
1907 | .IX Header "COMPLEXITIES" |
|
|
1908 | In this section the complexities of (many of) the algorithms used inside |
|
|
1909 | libev will be explained. For complexity discussions about backends see the |
|
|
1910 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
|
|
1911 | .RS 4 |
|
|
1912 | .IP "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" 4 |
|
|
1913 | .IX Item "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" |
|
|
1914 | .PD 0 |
|
|
1915 | .IP "Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)" 4 |
|
|
1916 | .IX Item "Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)" |
|
|
1917 | .IP "Starting io/check/prepare/idle/signal/child watchers: O(1)" 4 |
|
|
1918 | .IX Item "Starting io/check/prepare/idle/signal/child watchers: O(1)" |
|
|
1919 | .IP "Stopping check/prepare/idle watchers: O(1)" 4 |
|
|
1920 | .IX Item "Stopping check/prepare/idle watchers: O(1)" |
|
|
1921 | .IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % 16))" 4 |
|
|
1922 | .IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % 16))" |
|
|
1923 | .IP "Finding the next timer per loop iteration: O(1)" 4 |
|
|
1924 | .IX Item "Finding the next timer per loop iteration: O(1)" |
|
|
1925 | .IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 |
|
|
1926 | .IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" |
|
|
1927 | .IP "Activating one watcher: O(1)" 4 |
|
|
1928 | .IX Item "Activating one watcher: O(1)" |
|
|
1929 | .RE |
|
|
1930 | .RS 4 |
|
|
1931 | .PD |
1824 | .SH "AUTHOR" |
1932 | .SH "AUTHOR" |
1825 | .IX Header "AUTHOR" |
1933 | .IX Header "AUTHOR" |
1826 | Marc Lehmann <libev@schmorp.de>. |
1934 | Marc Lehmann <libev@schmorp.de>. |