… | |
… | |
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:33:21 2007" /> |
9 | <meta name="created" content="Mon Nov 26 11:20:35 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 --> |
… | |
… | |
50 | </li> |
50 | </li> |
51 | <li><a href="#PREPROCESSOR_SYMBOLS_MACROS">PREPROCESSOR SYMBOLS/MACROS</a></li> |
51 | <li><a href="#PREPROCESSOR_SYMBOLS_MACROS">PREPROCESSOR SYMBOLS/MACROS</a></li> |
52 | <li><a href="#EXAMPLES">EXAMPLES</a></li> |
52 | <li><a href="#EXAMPLES">EXAMPLES</a></li> |
53 | </ul> |
53 | </ul> |
54 | </li> |
54 | </li> |
|
|
55 | <li><a href="#COMPLEXITIES">COMPLEXITIES</a></li> |
55 | <li><a href="#AUTHOR">AUTHOR</a> |
56 | <li><a href="#AUTHOR">AUTHOR</a> |
56 | </li> |
57 | </li> |
57 | </ul><hr /> |
58 | </ul><hr /> |
58 | <!-- INDEX END --> |
59 | <!-- INDEX END --> |
59 | |
60 | |
… | |
… | |
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 | <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"> |
1128 | <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: |
1129 | <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 |
1130 | prepare watchers get invoked before the process blocks and check watchers |
1130 | afterwards.</p> |
1131 | afterwards.</p> |
|
|
1132 | <p>You <i>must not</i> call <code>ev_loop</code> or similar functions that enter |
|
|
1133 | the current event loop from either <code>ev_prepare</code> or <code>ev_check</code> |
|
|
1134 | watchers. Other loops than the current one are fine, however. The |
|
|
1135 | rationale behind this is that you do not need to check for recursion in |
|
|
1136 | those watchers, i.e. the sequence will always be <code>ev_prepare</code>, blocking, |
|
|
1137 | <code>ev_check</code> so if you have one watcher of each kind they will always be |
|
|
1138 | called in pairs bracketing the blocking call.</p> |
1131 | <p>Their main purpose is to integrate other event mechanisms into libev and |
1139 | <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 |
1140 | 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 |
1141 | variable changes, implement your own watchers, integrate net-snmp or a |
1134 | coroutine library and lots more.</p> |
1142 | coroutine library and lots more. They are also occasionally useful if |
|
|
1143 | you cache some data and want to flush it before blocking (for example, |
|
|
1144 | in X programs you might want to do an <code>XFlush ()</code> in an <code>ev_prepare</code> |
|
|
1145 | watcher).</p> |
1135 | <p>This is done by examining in each prepare call which file descriptors need |
1146 | <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 |
1147 | 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 |
1148 | 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 |
1149 | 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 |
1150 | 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 |
1166 | <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> |
1167 | 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> |
1168 | macros, but using them is utterly, utterly and completely pointless.</p> |
1158 | </dd> |
1169 | </dd> |
1159 | </dl> |
1170 | </dl> |
1160 | <p>Example: *TODO*.</p> |
1171 | <p>Example: To include a library such as adns, you would add IO watchers |
|
|
1172 | and a timeout watcher in a prepare handler, as required by libadns, and |
|
|
1173 | in a check watcher, destroy them and call into libadns. What follows is |
|
|
1174 | pseudo-code only of course:</p> |
|
|
1175 | <pre> static ev_io iow [nfd]; |
|
|
1176 | static ev_timer tw; |
1161 | |
1177 | |
|
|
1178 | static void |
|
|
1179 | io_cb (ev_loop *loop, ev_io *w, int revents) |
|
|
1180 | { |
|
|
1181 | // set the relevant poll flags |
|
|
1182 | // could also call adns_processreadable etc. here |
|
|
1183 | struct pollfd *fd = (struct pollfd *)w->data; |
|
|
1184 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1185 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
|
|
1186 | } |
1162 | |
1187 | |
|
|
1188 | // create io watchers for each fd and a timer before blocking |
|
|
1189 | static void |
|
|
1190 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
|
|
1191 | { |
|
|
1192 | int timeout = 3600000;truct pollfd fds [nfd]; |
|
|
1193 | // actual code will need to loop here and realloc etc. |
|
|
1194 | adns_beforepoll (ads, fds, &nfd, &timeout, timeval_from (ev_time ())); |
1163 | |
1195 | |
|
|
1196 | /* the callback is illegal, but won't be called as we stop during check */ |
|
|
1197 | ev_timer_init (&tw, 0, timeout * 1e-3); |
|
|
1198 | ev_timer_start (loop, &tw); |
1164 | |
1199 | |
|
|
1200 | // create on ev_io per pollfd |
|
|
1201 | for (int i = 0; i < nfd; ++i) |
|
|
1202 | { |
|
|
1203 | ev_io_init (iow + i, io_cb, fds [i].fd, |
|
|
1204 | ((fds [i].events & POLLIN ? EV_READ : 0) |
|
|
1205 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
|
|
1206 | |
|
|
1207 | fds [i].revents = 0; |
|
|
1208 | iow [i].data = fds + i; |
|
|
1209 | ev_io_start (loop, iow + i); |
|
|
1210 | } |
|
|
1211 | } |
|
|
1212 | |
|
|
1213 | // stop all watchers after blocking |
|
|
1214 | static void |
|
|
1215 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
|
|
1216 | { |
|
|
1217 | ev_timer_stop (loop, &tw); |
|
|
1218 | |
|
|
1219 | for (int i = 0; i < nfd; ++i) |
|
|
1220 | ev_io_stop (loop, iow + i); |
|
|
1221 | |
|
|
1222 | adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
|
|
1223 | } |
|
|
1224 | |
|
|
1225 | |
|
|
1226 | |
|
|
1227 | |
|
|
1228 | </pre> |
1165 | |
1229 | |
1166 | </div> |
1230 | </div> |
1167 | <h2 id="code_ev_embed_code_when_one_backend_"><code>ev_embed</code> - when one backend isn't enough...</h2> |
1231 | <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"> |
1232 | <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 |
1233 | <p>This is a rather advanced watcher type that lets you embed one event loop |
… | |
… | |
1656 | SV *self; /* contains this struct */ \ |
1720 | SV *self; /* contains this struct */ \ |
1657 | SV *cb_sv, *fh /* note no trailing ";" */ |
1721 | SV *cb_sv, *fh /* note no trailing ";" */ |
1658 | |
1722 | |
1659 | </pre> |
1723 | </pre> |
1660 | </dd> |
1724 | </dd> |
1661 | <dt>EV_CB_DECLARE(type)</dt> |
1725 | <dt>EV_CB_DECLARE (type)</dt> |
1662 | <dt>EV_CB_INVOKE(watcher,revents)</dt> |
1726 | <dt>EV_CB_INVOKE (watcher, revents)</dt> |
1663 | <dt>ev_set_cb(ev,cb)</dt> |
1727 | <dt>ev_set_cb (ev, cb)</dt> |
1664 | <dd> |
1728 | <dd> |
1665 | <p>Can be used to change the callback member declaration in each watcher, |
1729 | <p>Can be used to change the callback member declaration in each watcher, |
1666 | and the way callbacks are invoked and set. Must expand to a struct member |
1730 | and the way callbacks are invoked and set. Must expand to a struct member |
1667 | definition and a statement, respectively. See the <cite>ev.v</cite> header file for |
1731 | definition and a statement, respectively. See the <cite>ev.v</cite> header file for |
1668 | their default definitions. One possible use for overriding these is to |
1732 | their default definitions. One possible use for overriding these is to |
1669 | avoid the ev_loop pointer as first argument in all cases, or to use method |
1733 | avoid the <code>struct ev_loop *</code> as first argument in all cases, or to use |
1670 | calls instead of plain function calls in C++.</p> |
1734 | method calls instead of plain function calls in C++.</p> |
1671 | |
1735 | |
1672 | </div> |
1736 | </div> |
1673 | <h2 id="EXAMPLES">EXAMPLES</h2> |
1737 | <h2 id="EXAMPLES">EXAMPLES</h2> |
1674 | <div id="EXAMPLES_CONTENT"> |
1738 | <div id="EXAMPLES_CONTENT"> |
1675 | <p>For a real-world example of a program the includes libev |
1739 | <p>For a real-world example of a program the includes libev |
… | |
… | |
1691 | </pre> |
1755 | </pre> |
1692 | <p>And a <cite>ev_cpp.C</cite> implementation file that contains libev proper and is compiled:</p> |
1756 | <p>And a <cite>ev_cpp.C</cite> implementation file that contains libev proper and is compiled:</p> |
1693 | <pre> #include "ev_cpp.h" |
1757 | <pre> #include "ev_cpp.h" |
1694 | #include "ev.c" |
1758 | #include "ev.c" |
1695 | |
1759 | |
|
|
1760 | |
|
|
1761 | |
|
|
1762 | |
1696 | </pre> |
1763 | </pre> |
|
|
1764 | |
|
|
1765 | </div> |
|
|
1766 | <h1 id="COMPLEXITIES">COMPLEXITIES</h1><p><a href="#TOP" class="toplink">Top</a></p> |
|
|
1767 | <div id="COMPLEXITIES_CONTENT"> |
|
|
1768 | <p>In this section the complexities of (many of) the algorithms used inside |
|
|
1769 | libev will be explained. For complexity discussions about backends see the |
|
|
1770 | documentation for <code>ev_default_init</code>.</p> |
|
|
1771 | <p> |
|
|
1772 | <dl> |
|
|
1773 | <dt>Starting and stopping timer/periodic watchers: O(log skipped_other_timers)</dt> |
|
|
1774 | <dt>Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)</dt> |
|
|
1775 | <dt>Starting io/check/prepare/idle/signal/child watchers: O(1)</dt> |
|
|
1776 | <dt>Stopping check/prepare/idle watchers: O(1)</dt> |
|
|
1777 | <dt>Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % 16))</dt> |
|
|
1778 | <dt>Finding the next timer per loop iteration: O(1)</dt> |
|
|
1779 | <dt>Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)</dt> |
|
|
1780 | <dt>Activating one watcher: O(1)</dt> |
|
|
1781 | </dl> |
|
|
1782 | </p> |
|
|
1783 | |
|
|
1784 | |
|
|
1785 | |
|
|
1786 | |
1697 | |
1787 | |
1698 | </div> |
1788 | </div> |
1699 | <h1 id="AUTHOR">AUTHOR</h1><p><a href="#TOP" class="toplink">Top</a></p> |
1789 | <h1 id="AUTHOR">AUTHOR</h1><p><a href="#TOP" class="toplink">Top</a></p> |
1700 | <div id="AUTHOR_CONTENT"> |
1790 | <div id="AUTHOR_CONTENT"> |
1701 | <p>Marc Lehmann <libev@schmorp.de>.</p> |
1791 | <p>Marc Lehmann <libev@schmorp.de>.</p> |