… | |
… | |
130 | .\} |
130 | .\} |
131 | .rm #[ #] #H #V #F C |
131 | .rm #[ #] #H #V #F C |
132 | .\" ======================================================================== |
132 | .\" ======================================================================== |
133 | .\" |
133 | .\" |
134 | .IX Title "LIBEV 3" |
134 | .IX Title "LIBEV 3" |
135 | .TH LIBEV 3 "2009-02-06" "libev-3.53" "libev - high performance full featured event loop" |
135 | .TH LIBEV 3 "2009-04-25" "libev-3.6" "libev - high performance full featured event loop" |
136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
137 | .\" way too many mistakes in technical documents. |
137 | .\" way too many mistakes in technical documents. |
138 | .if n .ad l |
138 | .if n .ad l |
139 | .nh |
139 | .nh |
140 | .SH "NAME" |
140 | .SH "NAME" |
… | |
… | |
201 | \& |
201 | \& |
202 | \& // unloop was called, so exit |
202 | \& // unloop was called, so exit |
203 | \& return 0; |
203 | \& return 0; |
204 | \& } |
204 | \& } |
205 | .Ve |
205 | .Ve |
206 | .SH "DESCRIPTION" |
206 | .SH "ABOUT THIS DOCUMENT" |
207 | .IX Header "DESCRIPTION" |
207 | .IX Header "ABOUT THIS DOCUMENT" |
|
|
208 | This document documents the libev software package. |
|
|
209 | .PP |
208 | The newest version of this document is also available as an html-formatted |
210 | The newest version of this document is also available as an html-formatted |
209 | web page you might find easier to navigate when reading it for the first |
211 | web page you might find easier to navigate when reading it for the first |
210 | time: <http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>. |
212 | time: <http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>. |
211 | .PP |
213 | .PP |
|
|
214 | While this document tries to be as complete as possible in documenting |
|
|
215 | libev, its usage and the rationale behind its design, it is not a tutorial |
|
|
216 | on event-based programming, nor will it introduce event-based programming |
|
|
217 | with libev. |
|
|
218 | .PP |
|
|
219 | Familarity with event based programming techniques in general is assumed |
|
|
220 | throughout this document. |
|
|
221 | .SH "ABOUT LIBEV" |
|
|
222 | .IX Header "ABOUT LIBEV" |
212 | Libev is an event loop: you register interest in certain events (such as a |
223 | Libev is an event loop: you register interest in certain events (such as a |
213 | file descriptor being readable or a timeout occurring), and it will manage |
224 | file descriptor being readable or a timeout occurring), and it will manage |
214 | these event sources and provide your program with events. |
225 | these event sources and provide your program with events. |
215 | .PP |
226 | .PP |
216 | To do this, it must take more or less complete control over your process |
227 | To do this, it must take more or less complete control over your process |
… | |
… | |
246 | for multiple event loops, then all functions taking an initial argument of |
257 | for multiple event loops, then all functions taking an initial argument of |
247 | name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`ev_loop *\*(C'\fR) will not have |
258 | name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`ev_loop *\*(C'\fR) will not have |
248 | this argument. |
259 | this argument. |
249 | .Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0" |
260 | .Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0" |
250 | .IX Subsection "TIME REPRESENTATION" |
261 | .IX Subsection "TIME REPRESENTATION" |
251 | Libev represents time as a single floating point number, representing the |
262 | Libev represents time as a single floating point number, representing |
252 | (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near |
263 | the (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere |
253 | the beginning of 1970, details are complicated, don't ask). This type is |
264 | near the beginning of 1970, details are complicated, don't ask). This |
254 | called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases |
265 | type is called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually |
255 | to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on |
266 | aliases to the \f(CW\*(C`double\*(C'\fR type in C. When you need to do any calculations |
256 | it, you should treat it as some floating point value. Unlike the name |
267 | on it, you should treat it as some floating point value. Unlike the name |
257 | component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences |
268 | component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences |
258 | throughout libev. |
269 | throughout libev. |
259 | .SH "ERROR HANDLING" |
270 | .SH "ERROR HANDLING" |
260 | .IX Header "ERROR HANDLING" |
271 | .IX Header "ERROR HANDLING" |
261 | Libev knows three classes of errors: operating system errors, usage errors |
272 | Libev knows three classes of errors: operating system errors, usage errors |
… | |
… | |
760 | This function is rarely useful, but when some event callback runs for a |
771 | This function is rarely useful, but when some event callback runs for a |
761 | very long time without entering the event loop, updating libev's idea of |
772 | very long time without entering the event loop, updating libev's idea of |
762 | the current time is a good idea. |
773 | the current time is a good idea. |
763 | .Sp |
774 | .Sp |
764 | See also \*(L"The special problem of time updates\*(R" in the \f(CW\*(C`ev_timer\*(C'\fR section. |
775 | See also \*(L"The special problem of time updates\*(R" in the \f(CW\*(C`ev_timer\*(C'\fR section. |
|
|
776 | .IP "ev_suspend (loop)" 4 |
|
|
777 | .IX Item "ev_suspend (loop)" |
|
|
778 | .PD 0 |
|
|
779 | .IP "ev_resume (loop)" 4 |
|
|
780 | .IX Item "ev_resume (loop)" |
|
|
781 | .PD |
|
|
782 | These two functions suspend and resume a loop, for use when the loop is |
|
|
783 | not used for a while and timeouts should not be processed. |
|
|
784 | .Sp |
|
|
785 | A typical use case would be an interactive program such as a game: When |
|
|
786 | the user presses \f(CW\*(C`^Z\*(C'\fR to suspend the game and resumes it an hour later it |
|
|
787 | would be best to handle timeouts as if no time had actually passed while |
|
|
788 | the program was suspended. This can be achieved by calling \f(CW\*(C`ev_suspend\*(C'\fR |
|
|
789 | in your \f(CW\*(C`SIGTSTP\*(C'\fR handler, sending yourself a \f(CW\*(C`SIGSTOP\*(C'\fR and calling |
|
|
790 | \&\f(CW\*(C`ev_resume\*(C'\fR directly afterwards to resume timer processing. |
|
|
791 | .Sp |
|
|
792 | Effectively, all \f(CW\*(C`ev_timer\*(C'\fR watchers will be delayed by the time spend |
|
|
793 | between \f(CW\*(C`ev_suspend\*(C'\fR and \f(CW\*(C`ev_resume\*(C'\fR, and all \f(CW\*(C`ev_periodic\*(C'\fR watchers |
|
|
794 | will be rescheduled (that is, they will lose any events that would have |
|
|
795 | occured while suspended). |
|
|
796 | .Sp |
|
|
797 | After calling \f(CW\*(C`ev_suspend\*(C'\fR you \fBmust not\fR call \fIany\fR function on the |
|
|
798 | given loop other than \f(CW\*(C`ev_resume\*(C'\fR, and you \fBmust not\fR call \f(CW\*(C`ev_resume\*(C'\fR |
|
|
799 | without a previous call to \f(CW\*(C`ev_suspend\*(C'\fR. |
|
|
800 | .Sp |
|
|
801 | Calling \f(CW\*(C`ev_suspend\*(C'\fR/\f(CW\*(C`ev_resume\*(C'\fR has the side effect of updating the |
|
|
802 | event loop time (see \f(CW\*(C`ev_now_update\*(C'\fR). |
765 | .IP "ev_loop (loop, int flags)" 4 |
803 | .IP "ev_loop (loop, int flags)" 4 |
766 | .IX Item "ev_loop (loop, int flags)" |
804 | .IX Item "ev_loop (loop, int flags)" |
767 | Finally, this is it, the event handler. This function usually is called |
805 | Finally, this is it, the event handler. This function usually is called |
768 | after you initialised all your watchers and you want to start handling |
806 | after you initialised all your watchers and you want to start handling |
769 | events. |
807 | events. |
… | |
… | |
856 | .Sp |
894 | .Sp |
857 | If you have a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR |
895 | If you have a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR |
858 | from returning, call \fIev_unref()\fR after starting, and \fIev_ref()\fR before |
896 | from returning, call \fIev_unref()\fR after starting, and \fIev_ref()\fR before |
859 | stopping it. |
897 | stopping it. |
860 | .Sp |
898 | .Sp |
861 | As an example, libev itself uses this for its internal signal pipe: It is |
899 | As an example, libev itself uses this for its internal signal pipe: It |
862 | not visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting |
900 | is not visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from |
863 | if no event watchers registered by it are active. It is also an excellent |
901 | exiting if no event watchers registered by it are active. It is also an |
864 | way to do this for generic recurring timers or from within third-party |
902 | excellent way to do this for generic recurring timers or from within |
865 | libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR |
903 | third-party libraries. Just remember to \fIunref after start\fR and \fIref |
866 | (but only if the watcher wasn't active before, or was active before, |
904 | before stop\fR (but only if the watcher wasn't active before, or was active |
867 | respectively). |
905 | before, respectively. Note also that libev might stop watchers itself |
|
|
906 | (e.g. non-repeating timers) in which case you have to \f(CW\*(C`ev_ref\*(C'\fR |
|
|
907 | in the callback). |
868 | .Sp |
908 | .Sp |
869 | Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR |
909 | Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR |
870 | running when nothing else is active. |
910 | running when nothing else is active. |
871 | .Sp |
911 | .Sp |
872 | .Vb 4 |
912 | .Vb 4 |
… | |
… | |
1060 | \&\f(CW\*(C`ev_fork\*(C'\fR). |
1100 | \&\f(CW\*(C`ev_fork\*(C'\fR). |
1061 | .ie n .IP """EV_ASYNC""" 4 |
1101 | .ie n .IP """EV_ASYNC""" 4 |
1062 | .el .IP "\f(CWEV_ASYNC\fR" 4 |
1102 | .el .IP "\f(CWEV_ASYNC\fR" 4 |
1063 | .IX Item "EV_ASYNC" |
1103 | .IX Item "EV_ASYNC" |
1064 | The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR). |
1104 | The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR). |
|
|
1105 | .ie n .IP """EV_CUSTOM""" 4 |
|
|
1106 | .el .IP "\f(CWEV_CUSTOM\fR" 4 |
|
|
1107 | .IX Item "EV_CUSTOM" |
|
|
1108 | Not ever sent (or otherwise used) by libev itself, but can be freely used |
|
|
1109 | by libev users to signal watchers (e.g. via \f(CW\*(C`ev_feed_event\*(C'\fR). |
1065 | .ie n .IP """EV_ERROR""" 4 |
1110 | .ie n .IP """EV_ERROR""" 4 |
1066 | .el .IP "\f(CWEV_ERROR\fR" 4 |
1111 | .el .IP "\f(CWEV_ERROR\fR" 4 |
1067 | .IX Item "EV_ERROR" |
1112 | .IX Item "EV_ERROR" |
1068 | An unspecified error has occurred, the watcher has been stopped. This might |
1113 | An unspecified error has occurred, the watcher has been stopped. This might |
1069 | happen because the watcher could not be properly started because libev |
1114 | happen because the watcher could not be properly started because libev |
… | |
… | |
1184 | integer between \f(CW\*(C`EV_MAXPRI\*(C'\fR (default: \f(CW2\fR) and \f(CW\*(C`EV_MINPRI\*(C'\fR |
1229 | integer between \f(CW\*(C`EV_MAXPRI\*(C'\fR (default: \f(CW2\fR) and \f(CW\*(C`EV_MINPRI\*(C'\fR |
1185 | (default: \f(CW\*(C`\-2\*(C'\fR). Pending watchers with higher priority will be invoked |
1230 | (default: \f(CW\*(C`\-2\*(C'\fR). Pending watchers with higher priority will be invoked |
1186 | before watchers with lower priority, but priority will not keep watchers |
1231 | before watchers with lower priority, but priority will not keep watchers |
1187 | from being executed (except for \f(CW\*(C`ev_idle\*(C'\fR watchers). |
1232 | from being executed (except for \f(CW\*(C`ev_idle\*(C'\fR watchers). |
1188 | .Sp |
1233 | .Sp |
1189 | This means that priorities are \fIonly\fR used for ordering callback |
|
|
1190 | invocation after new events have been received. This is useful, for |
|
|
1191 | example, to reduce latency after idling, or more often, to bind two |
|
|
1192 | watchers on the same event and make sure one is called first. |
|
|
1193 | .Sp |
|
|
1194 | If you need to suppress invocation when higher priority events are pending |
1234 | If you need to suppress invocation when higher priority events are pending |
1195 | you need to look at \f(CW\*(C`ev_idle\*(C'\fR watchers, which provide this functionality. |
1235 | you need to look at \f(CW\*(C`ev_idle\*(C'\fR watchers, which provide this functionality. |
1196 | .Sp |
1236 | .Sp |
1197 | You \fImust not\fR change the priority of a watcher as long as it is active or |
1237 | You \fImust not\fR change the priority of a watcher as long as it is active or |
1198 | pending. |
1238 | pending. |
1199 | .Sp |
|
|
1200 | The default priority used by watchers when no priority has been set is |
|
|
1201 | always \f(CW0\fR, which is supposed to not be too high and not be too low :). |
|
|
1202 | .Sp |
1239 | .Sp |
1203 | Setting a priority outside the range of \f(CW\*(C`EV_MINPRI\*(C'\fR to \f(CW\*(C`EV_MAXPRI\*(C'\fR is |
1240 | Setting a priority outside the range of \f(CW\*(C`EV_MINPRI\*(C'\fR to \f(CW\*(C`EV_MAXPRI\*(C'\fR is |
1204 | fine, as long as you do not mind that the priority value you query might |
1241 | fine, as long as you do not mind that the priority value you query might |
1205 | or might not have been clamped to the valid range. |
1242 | or might not have been clamped to the valid range. |
|
|
1243 | .Sp |
|
|
1244 | The default priority used by watchers when no priority has been set is |
|
|
1245 | always \f(CW0\fR, which is supposed to not be too high and not be too low :). |
|
|
1246 | .Sp |
|
|
1247 | See \*(L"\s-1WATCHER\s0 \s-1PRIORITY\s0 \s-1MODELS\s0\*(R", below, for a more thorough treatment of |
|
|
1248 | priorities. |
1206 | .IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4 |
1249 | .IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4 |
1207 | .IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)" |
1250 | .IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)" |
1208 | Invoke the \f(CW\*(C`watcher\*(C'\fR with the given \f(CW\*(C`loop\*(C'\fR and \f(CW\*(C`revents\*(C'\fR. Neither |
1251 | Invoke the \f(CW\*(C`watcher\*(C'\fR with the given \f(CW\*(C`loop\*(C'\fR and \f(CW\*(C`revents\*(C'\fR. Neither |
1209 | \&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback |
1252 | \&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback |
1210 | can deal with that fact, as both are simply passed through to the |
1253 | can deal with that fact, as both are simply passed through to the |
… | |
… | |
1287 | \& { |
1330 | \& { |
1288 | \& struct my_biggy big = (struct my_biggy * |
1331 | \& struct my_biggy big = (struct my_biggy * |
1289 | \& (((char *)w) \- offsetof (struct my_biggy, t2)); |
1332 | \& (((char *)w) \- offsetof (struct my_biggy, t2)); |
1290 | \& } |
1333 | \& } |
1291 | .Ve |
1334 | .Ve |
|
|
1335 | .Sh "\s-1WATCHER\s0 \s-1PRIORITY\s0 \s-1MODELS\s0" |
|
|
1336 | .IX Subsection "WATCHER PRIORITY MODELS" |
|
|
1337 | Many event loops support \fIwatcher priorities\fR, which are usually small |
|
|
1338 | integers that influence the ordering of event callback invocation |
|
|
1339 | between watchers in some way, all else being equal. |
|
|
1340 | .PP |
|
|
1341 | In libev, Watcher priorities can be set using \f(CW\*(C`ev_set_priority\*(C'\fR. See its |
|
|
1342 | description for the more technical details such as the actual priority |
|
|
1343 | range. |
|
|
1344 | .PP |
|
|
1345 | There are two common ways how these these priorities are being interpreted |
|
|
1346 | by event loops: |
|
|
1347 | .PP |
|
|
1348 | In the more common lock-out model, higher priorities \*(L"lock out\*(R" invocation |
|
|
1349 | of lower priority watchers, which means as long as higher priority |
|
|
1350 | watchers receive events, lower priority watchers are not being invoked. |
|
|
1351 | .PP |
|
|
1352 | The less common only-for-ordering model uses priorities solely to order |
|
|
1353 | callback invocation within a single event loop iteration: Higher priority |
|
|
1354 | watchers are invoked before lower priority ones, but they all get invoked |
|
|
1355 | before polling for new events. |
|
|
1356 | .PP |
|
|
1357 | Libev uses the second (only-for-ordering) model for all its watchers |
|
|
1358 | except for idle watchers (which use the lock-out model). |
|
|
1359 | .PP |
|
|
1360 | The rationale behind this is that implementing the lock-out model for |
|
|
1361 | watchers is not well supported by most kernel interfaces, and most event |
|
|
1362 | libraries will just poll for the same events again and again as long as |
|
|
1363 | their callbacks have not been executed, which is very inefficient in the |
|
|
1364 | common case of one high-priority watcher locking out a mass of lower |
|
|
1365 | priority ones. |
|
|
1366 | .PP |
|
|
1367 | Static (ordering) priorities are most useful when you have two or more |
|
|
1368 | watchers handling the same resource: a typical usage example is having an |
|
|
1369 | \&\f(CW\*(C`ev_io\*(C'\fR watcher to receive data, and an associated \f(CW\*(C`ev_timer\*(C'\fR to handle |
|
|
1370 | timeouts. Under load, data might be received while the program handles |
|
|
1371 | other jobs, but since timers normally get invoked first, the timeout |
|
|
1372 | handler will be executed before checking for data. In that case, giving |
|
|
1373 | the timer a lower priority than the I/O watcher ensures that I/O will be |
|
|
1374 | handled first even under adverse conditions (which is usually, but not |
|
|
1375 | always, what you want). |
|
|
1376 | .PP |
|
|
1377 | Since idle watchers use the \*(L"lock-out\*(R" model, meaning that idle watchers |
|
|
1378 | will only be executed when no same or higher priority watchers have |
|
|
1379 | received events, they can be used to implement the \*(L"lock-out\*(R" model when |
|
|
1380 | required. |
|
|
1381 | .PP |
|
|
1382 | For example, to emulate how many other event libraries handle priorities, |
|
|
1383 | you can associate an \f(CW\*(C`ev_idle\*(C'\fR watcher to each such watcher, and in |
|
|
1384 | the normal watcher callback, you just start the idle watcher. The real |
|
|
1385 | processing is done in the idle watcher callback. This causes libev to |
|
|
1386 | continously poll and process kernel event data for the watcher, but when |
|
|
1387 | the lock-out case is known to be rare (which in turn is rare :), this is |
|
|
1388 | workable. |
|
|
1389 | .PP |
|
|
1390 | Usually, however, the lock-out model implemented that way will perform |
|
|
1391 | miserably under the type of load it was designed to handle. In that case, |
|
|
1392 | it might be preferable to stop the real watcher before starting the |
|
|
1393 | idle watcher, so the kernel will not have to process the event in case |
|
|
1394 | the actual processing will be delayed for considerable time. |
|
|
1395 | .PP |
|
|
1396 | Here is an example of an I/O watcher that should run at a strictly lower |
|
|
1397 | priority than the default, and which should only process data when no |
|
|
1398 | other events are pending: |
|
|
1399 | .PP |
|
|
1400 | .Vb 2 |
|
|
1401 | \& ev_idle idle; // actual processing watcher |
|
|
1402 | \& ev_io io; // actual event watcher |
|
|
1403 | \& |
|
|
1404 | \& static void |
|
|
1405 | \& io_cb (EV_P_ ev_io *w, int revents) |
|
|
1406 | \& { |
|
|
1407 | \& // stop the I/O watcher, we received the event, but |
|
|
1408 | \& // are not yet ready to handle it. |
|
|
1409 | \& ev_io_stop (EV_A_ w); |
|
|
1410 | \& |
|
|
1411 | \& // start the idle watcher to ahndle the actual event. |
|
|
1412 | \& // it will not be executed as long as other watchers |
|
|
1413 | \& // with the default priority are receiving events. |
|
|
1414 | \& ev_idle_start (EV_A_ &idle); |
|
|
1415 | \& } |
|
|
1416 | \& |
|
|
1417 | \& static void |
|
|
1418 | \& idle\-cb (EV_P_ ev_idle *w, int revents) |
|
|
1419 | \& { |
|
|
1420 | \& // actual processing |
|
|
1421 | \& read (STDIN_FILENO, ...); |
|
|
1422 | \& |
|
|
1423 | \& // have to start the I/O watcher again, as |
|
|
1424 | \& // we have handled the event |
|
|
1425 | \& ev_io_start (EV_P_ &io); |
|
|
1426 | \& } |
|
|
1427 | \& |
|
|
1428 | \& // initialisation |
|
|
1429 | \& ev_idle_init (&idle, idle_cb); |
|
|
1430 | \& ev_io_init (&io, io_cb, STDIN_FILENO, EV_READ); |
|
|
1431 | \& ev_io_start (EV_DEFAULT_ &io); |
|
|
1432 | .Ve |
|
|
1433 | .PP |
|
|
1434 | In the \*(L"real\*(R" world, it might also be beneficial to start a timer, so that |
|
|
1435 | low-priority connections can not be locked out forever under load. This |
|
|
1436 | enables your program to keep a lower latency for important connections |
|
|
1437 | during short periods of high load, while not completely locking out less |
|
|
1438 | important ones. |
1292 | .SH "WATCHER TYPES" |
1439 | .SH "WATCHER TYPES" |
1293 | .IX Header "WATCHER TYPES" |
1440 | .IX Header "WATCHER TYPES" |
1294 | This section describes each watcher in detail, but will not repeat |
1441 | This section describes each watcher in detail, but will not repeat |
1295 | information given in the last section. Any initialisation/set macros, |
1442 | information given in the last section. Any initialisation/set macros, |
1296 | functions and members specific to the watcher type are explained. |
1443 | functions and members specific to the watcher type are explained. |
… | |
… | |
1319 | descriptors to non-blocking mode is also usually a good idea (but not |
1466 | descriptors to non-blocking mode is also usually a good idea (but not |
1320 | required if you know what you are doing). |
1467 | required if you know what you are doing). |
1321 | .PP |
1468 | .PP |
1322 | If you cannot use non-blocking mode, then force the use of a |
1469 | If you cannot use non-blocking mode, then force the use of a |
1323 | known-to-be-good backend (at the time of this writing, this includes only |
1470 | known-to-be-good backend (at the time of this writing, this includes only |
1324 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR). |
1471 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR). The same applies to file |
|
|
1472 | descriptors for which non-blocking operation makes no sense (such as |
|
|
1473 | files) \- libev doesn't guarentee any specific behaviour in that case. |
1325 | .PP |
1474 | .PP |
1326 | Another thing you have to watch out for is that it is quite easy to |
1475 | Another thing you have to watch out for is that it is quite easy to |
1327 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1476 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1328 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1477 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1329 | because there is no data. Not only are some backends known to create a |
1478 | because there is no data. Not only are some backends known to create a |
… | |
… | |
1451 | year, it will still time out after (roughly) one hour. \*(L"Roughly\*(R" because |
1600 | year, it will still time out after (roughly) one hour. \*(L"Roughly\*(R" because |
1452 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1601 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1453 | monotonic clock option helps a lot here). |
1602 | monotonic clock option helps a lot here). |
1454 | .PP |
1603 | .PP |
1455 | The callback is guaranteed to be invoked only \fIafter\fR its timeout has |
1604 | The callback is guaranteed to be invoked only \fIafter\fR its timeout has |
1456 | passed, but if multiple timers become ready during the same loop iteration |
1605 | passed (not \fIat\fR, so on systems with very low-resolution clocks this |
1457 | then order of execution is undefined. |
1606 | might introduce a small delay). If multiple timers become ready during the |
|
|
1607 | same loop iteration then the ones with earlier time-out values are invoked |
|
|
1608 | before ones with later time-out values (but this is no longer true when a |
|
|
1609 | callback calls \f(CW\*(C`ev_loop\*(C'\fR recursively). |
1458 | .PP |
1610 | .PP |
1459 | \fIBe smart about timeouts\fR |
1611 | \fIBe smart about timeouts\fR |
1460 | .IX Subsection "Be smart about timeouts" |
1612 | .IX Subsection "Be smart about timeouts" |
1461 | .PP |
1613 | .PP |
1462 | Many real-world problems involve some kind of timeout, usually for error |
1614 | Many real-world problems involve some kind of timeout, usually for error |
… | |
… | |
1743 | .el .Sh "\f(CWev_periodic\fP \- to cron or not to cron?" |
1895 | .el .Sh "\f(CWev_periodic\fP \- to cron or not to cron?" |
1744 | .IX Subsection "ev_periodic - to cron or not to cron?" |
1896 | .IX Subsection "ev_periodic - to cron or not to cron?" |
1745 | Periodic watchers are also timers of a kind, but they are very versatile |
1897 | Periodic watchers are also timers of a kind, but they are very versatile |
1746 | (and unfortunately a bit complex). |
1898 | (and unfortunately a bit complex). |
1747 | .PP |
1899 | .PP |
1748 | Unlike \f(CW\*(C`ev_timer\*(C'\fR's, they are not based on real time (or relative time) |
1900 | Unlike \f(CW\*(C`ev_timer\*(C'\fR, periodic watchers are not based on real time (or |
1749 | but on wall clock time (absolute time). You can tell a periodic watcher |
1901 | relative time, the physical time that passes) but on wall clock time |
1750 | to trigger after some specific point in time. For example, if you tell a |
1902 | (absolute time, the thing you can read on your calender or clock). The |
1751 | periodic watcher to trigger in 10 seconds (by specifying e.g. \f(CW\*(C`ev_now () |
1903 | difference is that wall clock time can run faster or slower than real |
1752 | + 10.\*(C'\fR, that is, an absolute time not a delay) and then reset your system |
1904 | time, and time jumps are not uncommon (e.g. when you adjust your |
1753 | clock to January of the previous year, then it will take more than year |
1905 | wrist-watch). |
1754 | to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger |
|
|
1755 | roughly 10 seconds later as it uses a relative timeout). |
|
|
1756 | .PP |
1906 | .PP |
|
|
1907 | You can tell a periodic watcher to trigger after some specific point |
|
|
1908 | in time: for example, if you tell a periodic watcher to trigger \*(L"in 10 |
|
|
1909 | seconds\*(R" (by specifying e.g. \f(CW\*(C`ev_now () + 10.\*(C'\fR, that is, an absolute time |
|
|
1910 | not a delay) and then reset your system clock to January of the previous |
|
|
1911 | year, then it will take a year or more to trigger the event (unlike an |
|
|
1912 | \&\f(CW\*(C`ev_timer\*(C'\fR, which would still trigger roughly 10 seconds after starting |
|
|
1913 | it, as it uses a relative timeout). |
|
|
1914 | .PP |
1757 | \&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers, |
1915 | \&\f(CW\*(C`ev_periodic\*(C'\fR watchers can also be used to implement vastly more complex |
1758 | such as triggering an event on each \*(L"midnight, local time\*(R", or other |
1916 | timers, such as triggering an event on each \*(L"midnight, local time\*(R", or |
1759 | complicated rules. |
1917 | other complicated rules. This cannot be done with \f(CW\*(C`ev_timer\*(C'\fR watchers, as |
|
|
1918 | those cannot react to time jumps. |
1760 | .PP |
1919 | .PP |
1761 | As with timers, the callback is guaranteed to be invoked only when the |
1920 | As with timers, the callback is guaranteed to be invoked only when the |
1762 | time (\f(CW\*(C`at\*(C'\fR) has passed, but if multiple periodic timers become ready |
1921 | point in time where it is supposed to trigger has passed. If multiple |
1763 | during the same loop iteration, then order of execution is undefined. |
1922 | timers become ready during the same loop iteration then the ones with |
|
|
1923 | earlier time-out values are invoked before ones with later time-out values |
|
|
1924 | (but this is no longer true when a callback calls \f(CW\*(C`ev_loop\*(C'\fR recursively). |
1764 | .PP |
1925 | .PP |
1765 | \fIWatcher-Specific Functions and Data Members\fR |
1926 | \fIWatcher-Specific Functions and Data Members\fR |
1766 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1927 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1767 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 |
1928 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp offset, ev_tstamp interval, reschedule_cb)" 4 |
1768 | .IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" |
1929 | .IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp offset, ev_tstamp interval, reschedule_cb)" |
1769 | .PD 0 |
1930 | .PD 0 |
1770 | .IP "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" 4 |
1931 | .IP "ev_periodic_set (ev_periodic *, ev_tstamp offset, ev_tstamp interval, reschedule_cb)" 4 |
1771 | .IX Item "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" |
1932 | .IX Item "ev_periodic_set (ev_periodic *, ev_tstamp offset, ev_tstamp interval, reschedule_cb)" |
1772 | .PD |
1933 | .PD |
1773 | Lots of arguments, lets sort it out... There are basically three modes of |
1934 | Lots of arguments, let's sort it out... There are basically three modes of |
1774 | operation, and we will explain them from simplest to most complex: |
1935 | operation, and we will explain them from simplest to most complex: |
1775 | .RS 4 |
1936 | .RS 4 |
1776 | .IP "\(bu" 4 |
1937 | .IP "\(bu" 4 |
1777 | absolute timer (at = time, interval = reschedule_cb = 0) |
1938 | absolute timer (offset = absolute time, interval = 0, reschedule_cb = 0) |
1778 | .Sp |
1939 | .Sp |
1779 | In this configuration the watcher triggers an event after the wall clock |
1940 | In this configuration the watcher triggers an event after the wall clock |
1780 | time \f(CW\*(C`at\*(C'\fR has passed. It will not repeat and will not adjust when a time |
1941 | time \f(CW\*(C`offset\*(C'\fR has passed. It will not repeat and will not adjust when a |
1781 | jump occurs, that is, if it is to be run at January 1st 2011 then it will |
1942 | time jump occurs, that is, if it is to be run at January 1st 2011 then it |
1782 | only run when the system clock reaches or surpasses this time. |
1943 | will be stopped and invoked when the system clock reaches or surpasses |
|
|
1944 | this point in time. |
1783 | .IP "\(bu" 4 |
1945 | .IP "\(bu" 4 |
1784 | repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1946 | repeating interval timer (offset = offset within interval, interval > 0, reschedule_cb = 0) |
1785 | .Sp |
1947 | .Sp |
1786 | In this mode the watcher will always be scheduled to time out at the next |
1948 | In this mode the watcher will always be scheduled to time out at the next |
1787 | \&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) |
1949 | \&\f(CW\*(C`offset + N * interval\*(C'\fR time (for some integer N, which can also be |
1788 | and then repeat, regardless of any time jumps. |
1950 | negative) and then repeat, regardless of any time jumps. The \f(CW\*(C`offset\*(C'\fR |
|
|
1951 | argument is merely an offset into the \f(CW\*(C`interval\*(C'\fR periods. |
1789 | .Sp |
1952 | .Sp |
1790 | This can be used to create timers that do not drift with respect to the |
1953 | This can be used to create timers that do not drift with respect to the |
1791 | system clock, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each |
1954 | system clock, for example, here is an \f(CW\*(C`ev_periodic\*(C'\fR that triggers each |
1792 | hour, on the hour: |
1955 | hour, on the hour (with respect to \s-1UTC\s0): |
1793 | .Sp |
1956 | .Sp |
1794 | .Vb 1 |
1957 | .Vb 1 |
1795 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1958 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1796 | .Ve |
1959 | .Ve |
1797 | .Sp |
1960 | .Sp |
… | |
… | |
1800 | full hour (\s-1UTC\s0), or more correctly, when the system time is evenly divisible |
1963 | full hour (\s-1UTC\s0), or more correctly, when the system time is evenly divisible |
1801 | by 3600. |
1964 | by 3600. |
1802 | .Sp |
1965 | .Sp |
1803 | Another way to think about it (for the mathematically inclined) is that |
1966 | Another way to think about it (for the mathematically inclined) is that |
1804 | \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible |
1967 | \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible |
1805 | time where \f(CW\*(C`time = at (mod interval)\*(C'\fR, regardless of any time jumps. |
1968 | time where \f(CW\*(C`time = offset (mod interval)\*(C'\fR, regardless of any time jumps. |
1806 | .Sp |
1969 | .Sp |
1807 | For numerical stability it is preferable that the \f(CW\*(C`at\*(C'\fR value is near |
1970 | For numerical stability it is preferable that the \f(CW\*(C`offset\*(C'\fR value is near |
1808 | \&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for |
1971 | \&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for |
1809 | this value, and in fact is often specified as zero. |
1972 | this value, and in fact is often specified as zero. |
1810 | .Sp |
1973 | .Sp |
1811 | Note also that there is an upper limit to how often a timer can fire (\s-1CPU\s0 |
1974 | Note also that there is an upper limit to how often a timer can fire (\s-1CPU\s0 |
1812 | speed for example), so if \f(CW\*(C`interval\*(C'\fR is very small then timing stability |
1975 | speed for example), so if \f(CW\*(C`interval\*(C'\fR is very small then timing stability |
1813 | will of course deteriorate. Libev itself tries to be exact to be about one |
1976 | will of course deteriorate. Libev itself tries to be exact to be about one |
1814 | millisecond (if the \s-1OS\s0 supports it and the machine is fast enough). |
1977 | millisecond (if the \s-1OS\s0 supports it and the machine is fast enough). |
1815 | .IP "\(bu" 4 |
1978 | .IP "\(bu" 4 |
1816 | manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1979 | manual reschedule mode (offset ignored, interval ignored, reschedule_cb = callback) |
1817 | .Sp |
1980 | .Sp |
1818 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`at\*(C'\fR are both being |
1981 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`offset\*(C'\fR are both being |
1819 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1982 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1820 | reschedule callback will be called with the watcher as first, and the |
1983 | reschedule callback will be called with the watcher as first, and the |
1821 | current time as second argument. |
1984 | current time as second argument. |
1822 | .Sp |
1985 | .Sp |
1823 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, |
1986 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, ever, |
1824 | ever, or make \s-1ANY\s0 event loop modifications whatsoever\fR. |
1987 | or make \s-1ANY\s0 other event loop modifications whatsoever, unless explicitly |
|
|
1988 | allowed by documentation here\fR. |
1825 | .Sp |
1989 | .Sp |
1826 | If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop |
1990 | If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop |
1827 | it afterwards (e.g. by starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is the |
1991 | it afterwards (e.g. by starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is the |
1828 | only event loop modification you are allowed to do). |
1992 | only event loop modification you are allowed to do). |
1829 | .Sp |
1993 | .Sp |
… | |
… | |
1860 | when you changed some parameters or the reschedule callback would return |
2024 | when you changed some parameters or the reschedule callback would return |
1861 | a different time than the last time it was called (e.g. in a crond like |
2025 | a different time than the last time it was called (e.g. in a crond like |
1862 | program when the crontabs have changed). |
2026 | program when the crontabs have changed). |
1863 | .IP "ev_tstamp ev_periodic_at (ev_periodic *)" 4 |
2027 | .IP "ev_tstamp ev_periodic_at (ev_periodic *)" 4 |
1864 | .IX Item "ev_tstamp ev_periodic_at (ev_periodic *)" |
2028 | .IX Item "ev_tstamp ev_periodic_at (ev_periodic *)" |
1865 | When active, returns the absolute time that the watcher is supposed to |
2029 | When active, returns the absolute time that the watcher is supposed |
1866 | trigger next. |
2030 | to trigger next. This is not the same as the \f(CW\*(C`offset\*(C'\fR argument to |
|
|
2031 | \&\f(CW\*(C`ev_periodic_set\*(C'\fR, but indeed works even in interval and manual |
|
|
2032 | rescheduling modes. |
1867 | .IP "ev_tstamp offset [read\-write]" 4 |
2033 | .IP "ev_tstamp offset [read\-write]" 4 |
1868 | .IX Item "ev_tstamp offset [read-write]" |
2034 | .IX Item "ev_tstamp offset [read-write]" |
1869 | When repeating, this contains the offset value, otherwise this is the |
2035 | When repeating, this contains the offset value, otherwise this is the |
1870 | absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR). |
2036 | absolute point in time (the \f(CW\*(C`offset\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR, |
|
|
2037 | although libev might modify this value for better numerical stability). |
1871 | .Sp |
2038 | .Sp |
1872 | Can be modified any time, but changes only take effect when the periodic |
2039 | Can be modified any time, but changes only take effect when the periodic |
1873 | timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. |
2040 | timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. |
1874 | .IP "ev_tstamp interval [read\-write]" 4 |
2041 | .IP "ev_tstamp interval [read\-write]" 4 |
1875 | .IX Item "ev_tstamp interval [read-write]" |
2042 | .IX Item "ev_tstamp interval [read-write]" |
… | |
… | |
2327 | \&\*(L"pseudo-background processing\*(R", or delay processing stuff to after the |
2494 | \&\*(L"pseudo-background processing\*(R", or delay processing stuff to after the |
2328 | event loop has handled all outstanding events. |
2495 | event loop has handled all outstanding events. |
2329 | .PP |
2496 | .PP |
2330 | \fIWatcher-Specific Functions and Data Members\fR |
2497 | \fIWatcher-Specific Functions and Data Members\fR |
2331 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2498 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2332 | .IP "ev_idle_init (ev_signal *, callback)" 4 |
2499 | .IP "ev_idle_init (ev_idle *, callback)" 4 |
2333 | .IX Item "ev_idle_init (ev_signal *, callback)" |
2500 | .IX Item "ev_idle_init (ev_idle *, callback)" |
2334 | Initialises and configures the idle watcher \- it has no parameters of any |
2501 | Initialises and configures the idle watcher \- it has no parameters of any |
2335 | kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless, |
2502 | kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless, |
2336 | believe me. |
2503 | believe me. |
2337 | .PP |
2504 | .PP |
2338 | \fIExamples\fR |
2505 | \fIExamples\fR |
… | |
… | |
2698 | event loop blocks next and before \f(CW\*(C`ev_check\*(C'\fR watchers are being called, |
2865 | event loop blocks next and before \f(CW\*(C`ev_check\*(C'\fR watchers are being called, |
2699 | and only in the child after the fork. If whoever good citizen calling |
2866 | and only in the child after the fork. If whoever good citizen calling |
2700 | \&\f(CW\*(C`ev_default_fork\*(C'\fR cheats and calls it in the wrong process, the fork |
2867 | \&\f(CW\*(C`ev_default_fork\*(C'\fR cheats and calls it in the wrong process, the fork |
2701 | handlers will be invoked, too, of course. |
2868 | handlers will be invoked, too, of course. |
2702 | .PP |
2869 | .PP |
|
|
2870 | \fIThe special problem of life after fork \- how is it possible?\fR |
|
|
2871 | .IX Subsection "The special problem of life after fork - how is it possible?" |
|
|
2872 | .PP |
|
|
2873 | Most uses of \f(CW\*(C`fork()\*(C'\fR consist of forking, then some simple calls to ste |
|
|
2874 | up/change the process environment, followed by a call to \f(CW\*(C`exec()\*(C'\fR. This |
|
|
2875 | sequence should be handled by libev without any problems. |
|
|
2876 | .PP |
|
|
2877 | This changes when the application actually wants to do event handling |
|
|
2878 | in the child, or both parent in child, in effect \*(L"continuing\*(R" after the |
|
|
2879 | fork. |
|
|
2880 | .PP |
|
|
2881 | The default mode of operation (for libev, with application help to detect |
|
|
2882 | forks) is to duplicate all the state in the child, as would be expected |
|
|
2883 | when \fIeither\fR the parent \fIor\fR the child process continues. |
|
|
2884 | .PP |
|
|
2885 | When both processes want to continue using libev, then this is usually the |
|
|
2886 | wrong result. In that case, usually one process (typically the parent) is |
|
|
2887 | supposed to continue with all watchers in place as before, while the other |
|
|
2888 | process typically wants to start fresh, i.e. without any active watchers. |
|
|
2889 | .PP |
|
|
2890 | The cleanest and most efficient way to achieve that with libev is to |
|
|
2891 | simply create a new event loop, which of course will be \*(L"empty\*(R", and |
|
|
2892 | use that for new watchers. This has the advantage of not touching more |
|
|
2893 | memory than necessary, and thus avoiding the copy-on-write, and the |
|
|
2894 | disadvantage of having to use multiple event loops (which do not support |
|
|
2895 | signal watchers). |
|
|
2896 | .PP |
|
|
2897 | When this is not possible, or you want to use the default loop for |
|
|
2898 | other reasons, then in the process that wants to start \*(L"fresh\*(R", call |
|
|
2899 | \&\f(CW\*(C`ev_default_destroy ()\*(C'\fR followed by \f(CW\*(C`ev_default_loop (...)\*(C'\fR. Destroying |
|
|
2900 | the default loop will \*(L"orphan\*(R" (not stop) all registered watchers, so you |
|
|
2901 | have to be careful not to execute code that modifies those watchers. Note |
|
|
2902 | also that in that case, you have to re-register any signal watchers. |
|
|
2903 | .PP |
2703 | \fIWatcher-Specific Functions and Data Members\fR |
2904 | \fIWatcher-Specific Functions and Data Members\fR |
2704 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2905 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2705 | .IP "ev_fork_init (ev_signal *, callback)" 4 |
2906 | .IP "ev_fork_init (ev_signal *, callback)" 4 |
2706 | .IX Item "ev_fork_init (ev_signal *, callback)" |
2907 | .IX Item "ev_fork_init (ev_signal *, callback)" |
2707 | Initialises and configures the fork watcher \- it has no parameters of any |
2908 | Initialises and configures the fork watcher \- it has no parameters of any |
… | |
… | |
2825 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike |
3026 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike |
2826 | \&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, signal or |
3027 | \&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, signal or |
2827 | similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
3028 | similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
2828 | section below on what exactly this means). |
3029 | section below on what exactly this means). |
2829 | .Sp |
3030 | .Sp |
|
|
3031 | Note that, as with other watchers in libev, multiple events might get |
|
|
3032 | compressed into a single callback invocation (another way to look at this |
|
|
3033 | is that \f(CW\*(C`ev_async\*(C'\fR watchers are level-triggered, set on \f(CW\*(C`ev_async_send\*(C'\fR, |
|
|
3034 | reset when the event loop detects that). |
|
|
3035 | .Sp |
2830 | This call incurs the overhead of a system call only once per loop iteration, |
3036 | This call incurs the overhead of a system call only once per event loop |
2831 | so while the overhead might be noticeable, it doesn't apply to repeated |
3037 | iteration, so while the overhead might be noticeable, it doesn't apply to |
2832 | calls to \f(CW\*(C`ev_async_send\*(C'\fR. |
3038 | repeated calls to \f(CW\*(C`ev_async_send\*(C'\fR for the same event loop. |
2833 | .IP "bool = ev_async_pending (ev_async *)" 4 |
3039 | .IP "bool = ev_async_pending (ev_async *)" 4 |
2834 | .IX Item "bool = ev_async_pending (ev_async *)" |
3040 | .IX Item "bool = ev_async_pending (ev_async *)" |
2835 | Returns a non-zero value when \f(CW\*(C`ev_async_send\*(C'\fR has been called on the |
3041 | Returns a non-zero value when \f(CW\*(C`ev_async_send\*(C'\fR has been called on the |
2836 | watcher but the event has not yet been processed (or even noted) by the |
3042 | watcher but the event has not yet been processed (or even noted) by the |
2837 | event loop. |
3043 | event loop. |
… | |
… | |
2839 | \&\f(CW\*(C`ev_async_send\*(C'\fR sets a flag in the watcher and wakes up the loop. When |
3045 | \&\f(CW\*(C`ev_async_send\*(C'\fR sets a flag in the watcher and wakes up the loop. When |
2840 | the loop iterates next and checks for the watcher to have become active, |
3046 | the loop iterates next and checks for the watcher to have become active, |
2841 | it will reset the flag again. \f(CW\*(C`ev_async_pending\*(C'\fR can be used to very |
3047 | it will reset the flag again. \f(CW\*(C`ev_async_pending\*(C'\fR can be used to very |
2842 | quickly check whether invoking the loop might be a good idea. |
3048 | quickly check whether invoking the loop might be a good idea. |
2843 | .Sp |
3049 | .Sp |
2844 | Not that this does \fInot\fR check whether the watcher itself is pending, only |
3050 | Not that this does \fInot\fR check whether the watcher itself is pending, |
2845 | whether it has been requested to make this watcher pending. |
3051 | only whether it has been requested to make this watcher pending: there |
|
|
3052 | is a time window between the event loop checking and resetting the async |
|
|
3053 | notification, and the callback being invoked. |
2846 | .SH "OTHER FUNCTIONS" |
3054 | .SH "OTHER FUNCTIONS" |
2847 | .IX Header "OTHER FUNCTIONS" |
3055 | .IX Header "OTHER FUNCTIONS" |
2848 | There are some other functions of possible interest. Described. Here. Now. |
3056 | There are some other functions of possible interest. Described. Here. Now. |
2849 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
3057 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
2850 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
3058 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
… | |
… | |
3131 | It can be found and installed via \s-1CPAN\s0, its homepage is at |
3339 | It can be found and installed via \s-1CPAN\s0, its homepage is at |
3132 | <http://software.schmorp.de/pkg/EV>. |
3340 | <http://software.schmorp.de/pkg/EV>. |
3133 | .IP "Python" 4 |
3341 | .IP "Python" 4 |
3134 | .IX Item "Python" |
3342 | .IX Item "Python" |
3135 | Python bindings can be found at <http://code.google.com/p/pyev/>. It |
3343 | Python bindings can be found at <http://code.google.com/p/pyev/>. It |
3136 | seems to be quite complete and well-documented. Note, however, that the |
3344 | seems to be quite complete and well-documented. |
3137 | patch they require for libev is outright dangerous as it breaks the \s-1ABI\s0 |
|
|
3138 | for everybody else, and therefore, should never be applied in an installed |
|
|
3139 | libev (if python requires an incompatible \s-1ABI\s0 then it needs to embed |
|
|
3140 | libev). |
|
|
3141 | .IP "Ruby" 4 |
3345 | .IP "Ruby" 4 |
3142 | .IX Item "Ruby" |
3346 | .IX Item "Ruby" |
3143 | Tony Arcieri has written a ruby extension that offers access to a subset |
3347 | Tony Arcieri has written a ruby extension that offers access to a subset |
3144 | of the libev \s-1API\s0 and adds file handle abstractions, asynchronous \s-1DNS\s0 and |
3348 | of the libev \s-1API\s0 and adds file handle abstractions, asynchronous \s-1DNS\s0 and |
3145 | more on top of it. It can be found via gem servers. Its homepage is at |
3349 | more on top of it. It can be found via gem servers. Its homepage is at |
3146 | <http://rev.rubyforge.org/>. |
3350 | <http://rev.rubyforge.org/>. |
3147 | .Sp |
3351 | .Sp |
3148 | Roger Pack reports that using the link order \f(CW\*(C`\-lws2_32 \-lmsvcrt\-ruby\-190\*(C'\fR |
3352 | Roger Pack reports that using the link order \f(CW\*(C`\-lws2_32 \-lmsvcrt\-ruby\-190\*(C'\fR |
3149 | makes rev work even on mingw. |
3353 | makes rev work even on mingw. |
|
|
3354 | .IP "Haskell" 4 |
|
|
3355 | .IX Item "Haskell" |
|
|
3356 | A haskell binding to libev is available at |
|
|
3357 | <http://hackage.haskell.org/cgi\-bin/hackage\-scripts/package/hlibev>. |
3150 | .IP "D" 4 |
3358 | .IP "D" 4 |
3151 | .IX Item "D" |
3359 | .IX Item "D" |
3152 | Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to |
3360 | Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to |
3153 | be found at <http://proj.llucax.com.ar/wiki/evd>. |
3361 | be found at <http://proj.llucax.com.ar/wiki/evd>. |
3154 | .IP "Ocaml" 4 |
3362 | .IP "Ocaml" 4 |
… | |
… | |
3823 | way (note also that glib is the slowest event library known to man). |
4031 | way (note also that glib is the slowest event library known to man). |
3824 | .PP |
4032 | .PP |
3825 | There is no supported compilation method available on windows except |
4033 | There is no supported compilation method available on windows except |
3826 | embedding it into other applications. |
4034 | embedding it into other applications. |
3827 | .PP |
4035 | .PP |
|
|
4036 | Sensible signal handling is officially unsupported by Microsoft \- libev |
|
|
4037 | tries its best, but under most conditions, signals will simply not work. |
|
|
4038 | .PP |
3828 | Not a libev limitation but worth mentioning: windows apparently doesn't |
4039 | Not a libev limitation but worth mentioning: windows apparently doesn't |
3829 | accept large writes: instead of resulting in a partial write, windows will |
4040 | accept large writes: instead of resulting in a partial write, windows will |
3830 | either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large, |
4041 | either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large, |
3831 | so make sure you only write small amounts into your sockets (less than a |
4042 | so make sure you only write small amounts into your sockets (less than a |
3832 | megabyte seems safe, but this apparently depends on the amount of memory |
4043 | megabyte seems safe, but this apparently depends on the amount of memory |
… | |
… | |
3836 | the abysmal performance of winsockets, using a large number of sockets |
4047 | the abysmal performance of winsockets, using a large number of sockets |
3837 | is not recommended (and not reasonable). If your program needs to use |
4048 | is not recommended (and not reasonable). If your program needs to use |
3838 | more than a hundred or so sockets, then likely it needs to use a totally |
4049 | more than a hundred or so sockets, then likely it needs to use a totally |
3839 | different implementation for windows, as libev offers the \s-1POSIX\s0 readiness |
4050 | different implementation for windows, as libev offers the \s-1POSIX\s0 readiness |
3840 | notification model, which cannot be implemented efficiently on windows |
4051 | notification model, which cannot be implemented efficiently on windows |
3841 | (Microsoft monopoly games). |
4052 | (due to Microsoft monopoly games). |
3842 | .PP |
4053 | .PP |
3843 | A typical way to use libev under windows is to embed it (see the embedding |
4054 | A typical way to use libev under windows is to embed it (see the embedding |
3844 | section for details) and use the following \fIevwrap.h\fR header file instead |
4055 | section for details) and use the following \fIevwrap.h\fR header file instead |
3845 | of \fIev.h\fR: |
4056 | of \fIev.h\fR: |
3846 | .PP |
4057 | .PP |
… | |
… | |
3884 | .Sp |
4095 | .Sp |
3885 | Early versions of winsocket's select only supported waiting for a maximum |
4096 | Early versions of winsocket's select only supported waiting for a maximum |
3886 | of \f(CW64\fR handles (probably owning to the fact that all windows kernels |
4097 | of \f(CW64\fR handles (probably owning to the fact that all windows kernels |
3887 | can only wait for \f(CW64\fR things at the same time internally; Microsoft |
4098 | can only wait for \f(CW64\fR things at the same time internally; Microsoft |
3888 | recommends spawning a chain of threads and wait for 63 handles and the |
4099 | recommends spawning a chain of threads and wait for 63 handles and the |
3889 | previous thread in each. Great). |
4100 | previous thread in each. Sounds great!). |
3890 | .Sp |
4101 | .Sp |
3891 | Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR |
4102 | Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR |
3892 | to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select |
4103 | to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select |
3893 | call (which might be in libev or elsewhere, for example, perl does its own |
4104 | call (which might be in libev or elsewhere, for example, perl and many |
3894 | select emulation on windows). |
4105 | other interpreters do their own select emulation on windows). |
3895 | .Sp |
4106 | .Sp |
3896 | Another limit is the number of file descriptors in the Microsoft runtime |
4107 | Another limit is the number of file descriptors in the Microsoft runtime |
3897 | libraries, which by default is \f(CW64\fR (there must be a hidden \fI64\fR fetish |
4108 | libraries, which by default is \f(CW64\fR (there must be a hidden \fI64\fR |
3898 | or something like this inside Microsoft). You can increase this by calling |
4109 | fetish or something like this inside Microsoft). You can increase this |
3899 | \&\f(CW\*(C`_setmaxstdio\*(C'\fR, which can increase this limit to \f(CW2048\fR (another |
4110 | by calling \f(CW\*(C`_setmaxstdio\*(C'\fR, which can increase this limit to \f(CW2048\fR |
3900 | arbitrary limit), but is broken in many versions of the Microsoft runtime |
4111 | (another arbitrary limit), but is broken in many versions of the Microsoft |
3901 | libraries. |
|
|
3902 | .Sp |
|
|
3903 | This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on |
4112 | runtime libraries. This might get you to about \f(CW512\fR or \f(CW2048\fR sockets |
3904 | windows version and/or the phase of the moon). To get more, you need to |
4113 | (depending on windows version and/or the phase of the moon). To get more, |
3905 | wrap all I/O functions and provide your own fd management, but the cost of |
4114 | you need to wrap all I/O functions and provide your own fd management, but |
3906 | calling select (O(nA\*^X)) will likely make this unworkable. |
4115 | the cost of calling select (O(nA\*^X)) will likely make this unworkable. |
3907 | .Sh "\s-1PORTABILITY\s0 \s-1REQUIREMENTS\s0" |
4116 | .Sh "\s-1PORTABILITY\s0 \s-1REQUIREMENTS\s0" |
3908 | .IX Subsection "PORTABILITY REQUIREMENTS" |
4117 | .IX Subsection "PORTABILITY REQUIREMENTS" |
3909 | In addition to a working ISO-C implementation and of course the |
4118 | In addition to a working ISO-C implementation and of course the |
3910 | backend-specific APIs, libev relies on a few additional extensions: |
4119 | backend-specific APIs, libev relies on a few additional extensions: |
3911 | .ie n .IP """void (*)(ev_watcher_type *, int revents)""\fR must have compatible calling conventions regardless of \f(CW""ev_watcher_type *""." 4 |
4120 | .ie n .IP """void (*)(ev_watcher_type *, int revents)""\fR must have compatible calling conventions regardless of \f(CW""ev_watcher_type *""." 4 |
… | |
… | |
4014 | .IX Item "Processing signals: O(max_signal_number)" |
4223 | .IX Item "Processing signals: O(max_signal_number)" |
4015 | .PD |
4224 | .PD |
4016 | Sending involves a system call \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR |
4225 | Sending involves a system call \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR |
4017 | calls in the current loop iteration. Checking for async and signal events |
4226 | calls in the current loop iteration. Checking for async and signal events |
4018 | involves iterating over all running async watchers or all signal numbers. |
4227 | involves iterating over all running async watchers or all signal numbers. |
|
|
4228 | .SH "GLOSSARY" |
|
|
4229 | .IX Header "GLOSSARY" |
|
|
4230 | .IP "active" 4 |
|
|
4231 | .IX Item "active" |
|
|
4232 | A watcher is active as long as it has been started (has been attached to |
|
|
4233 | an event loop) but not yet stopped (disassociated from the event loop). |
|
|
4234 | .IP "application" 4 |
|
|
4235 | .IX Item "application" |
|
|
4236 | In this document, an application is whatever is using libev. |
|
|
4237 | .IP "callback" 4 |
|
|
4238 | .IX Item "callback" |
|
|
4239 | The address of a function that is called when some event has been |
|
|
4240 | detected. Callbacks are being passed the event loop, the watcher that |
|
|
4241 | received the event, and the actual event bitset. |
|
|
4242 | .IP "callback invocation" 4 |
|
|
4243 | .IX Item "callback invocation" |
|
|
4244 | The act of calling the callback associated with a watcher. |
|
|
4245 | .IP "event" 4 |
|
|
4246 | .IX Item "event" |
|
|
4247 | A change of state of some external event, such as data now being available |
|
|
4248 | for reading on a file descriptor, time having passed or simply not having |
|
|
4249 | any other events happening anymore. |
|
|
4250 | .Sp |
|
|
4251 | In libev, events are represented as single bits (such as \f(CW\*(C`EV_READ\*(C'\fR or |
|
|
4252 | \&\f(CW\*(C`EV_TIMEOUT\*(C'\fR). |
|
|
4253 | .IP "event library" 4 |
|
|
4254 | .IX Item "event library" |
|
|
4255 | A software package implementing an event model and loop. |
|
|
4256 | .IP "event loop" 4 |
|
|
4257 | .IX Item "event loop" |
|
|
4258 | An entity that handles and processes external events and converts them |
|
|
4259 | into callback invocations. |
|
|
4260 | .IP "event model" 4 |
|
|
4261 | .IX Item "event model" |
|
|
4262 | The model used to describe how an event loop handles and processes |
|
|
4263 | watchers and events. |
|
|
4264 | .IP "pending" 4 |
|
|
4265 | .IX Item "pending" |
|
|
4266 | A watcher is pending as soon as the corresponding event has been detected, |
|
|
4267 | and stops being pending as soon as the watcher will be invoked or its |
|
|
4268 | pending status is explicitly cleared by the application. |
|
|
4269 | .Sp |
|
|
4270 | A watcher can be pending, but not active. Stopping a watcher also clears |
|
|
4271 | its pending status. |
|
|
4272 | .IP "real time" 4 |
|
|
4273 | .IX Item "real time" |
|
|
4274 | The physical time that is observed. It is apparently strictly monotonic :) |
|
|
4275 | .IP "wall-clock time" 4 |
|
|
4276 | .IX Item "wall-clock time" |
|
|
4277 | The time and date as shown on clocks. Unlike real time, it can actually |
|
|
4278 | be wrong and jump forwards and backwards, e.g. when the you adjust your |
|
|
4279 | clock. |
|
|
4280 | .IP "watcher" 4 |
|
|
4281 | .IX Item "watcher" |
|
|
4282 | A data structure that describes interest in certain events. Watchers need |
|
|
4283 | to be started (attached to an event loop) before they can receive events. |
|
|
4284 | .IP "watcher invocation" 4 |
|
|
4285 | .IX Item "watcher invocation" |
|
|
4286 | The act of calling the callback associated with a watcher. |
4019 | .SH "AUTHOR" |
4287 | .SH "AUTHOR" |
4020 | .IX Header "AUTHOR" |
4288 | .IX Header "AUTHOR" |
4021 | Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson. |
4289 | Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson. |