… | |
… | |
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 "2008-05-11" "libev-3.33" "libev - high perfromance full featured event loop" |
135 | .TH LIBEV 3 "2008-05-22" "libev-3.41" "libev - high perfromance 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" |
… | |
… | |
252 | called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases |
252 | called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases |
253 | to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on |
253 | to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on |
254 | it, you should treat it as some floatingpoint value. Unlike the name |
254 | it, you should treat it as some floatingpoint value. Unlike the name |
255 | component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences |
255 | component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences |
256 | throughout libev. |
256 | throughout libev. |
|
|
257 | .SH "ERROR HANDLING" |
|
|
258 | .IX Header "ERROR HANDLING" |
|
|
259 | Libev knows three classes of errors: operating system errors, usage errors |
|
|
260 | and internal errors (bugs). |
|
|
261 | .PP |
|
|
262 | When libev catches an operating system error it cannot handle (for example |
|
|
263 | a syscall indicating a condition libev cannot fix), it calls the callback |
|
|
264 | set via \f(CW\*(C`ev_set_syserr_cb\*(C'\fR, which is supposed to fix the problem or |
|
|
265 | abort. The default is to print a diagnostic message and to call \f(CW\*(C`abort |
|
|
266 | ()\*(C'\fR. |
|
|
267 | .PP |
|
|
268 | When libev detects a usage error such as a negative timer interval, then |
|
|
269 | it will print a diagnostic message and abort (via the \f(CW\*(C`assert\*(C'\fR mechanism, |
|
|
270 | so \f(CW\*(C`NDEBUG\*(C'\fR will disable this checking): these are programming errors in |
|
|
271 | the libev caller and need to be fixed there. |
|
|
272 | .PP |
|
|
273 | Libev also has a few internal error-checking \f(CW\*(C`assert\*(C'\fRions, and also has |
|
|
274 | extensive consistency checking code. These do not trigger under normal |
|
|
275 | circumstances, as they indicate either a bug in libev or worse. |
257 | .SH "GLOBAL FUNCTIONS" |
276 | .SH "GLOBAL FUNCTIONS" |
258 | .IX Header "GLOBAL FUNCTIONS" |
277 | .IX Header "GLOBAL FUNCTIONS" |
259 | These functions can be called anytime, even before initialising the |
278 | These functions can be called anytime, even before initialising the |
260 | library in any way. |
279 | library in any way. |
261 | .IP "ev_tstamp ev_time ()" 4 |
280 | .IP "ev_tstamp ev_time ()" 4 |
… | |
… | |
464 | To get good performance out of this backend you need a high amount of |
483 | To get good performance out of this backend you need a high amount of |
465 | parallelity (most of the file descriptors should be busy). If you are |
484 | parallelity (most of the file descriptors should be busy). If you are |
466 | writing a server, you should \f(CW\*(C`accept ()\*(C'\fR in a loop to accept as many |
485 | writing a server, you should \f(CW\*(C`accept ()\*(C'\fR in a loop to accept as many |
467 | connections as possible during one iteration. You might also want to have |
486 | connections as possible during one iteration. You might also want to have |
468 | a look at \f(CW\*(C`ev_set_io_collect_interval ()\*(C'\fR to increase the amount of |
487 | a look at \f(CW\*(C`ev_set_io_collect_interval ()\*(C'\fR to increase the amount of |
469 | readyness notifications you get per iteration. |
488 | readiness notifications you get per iteration. |
470 | .ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 |
489 | .ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 |
471 | .el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 |
490 | .el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 |
472 | .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" |
491 | .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" |
473 | And this is your standard \fIpoll\fR\|(2) backend. It's more complicated |
492 | And this is your standard \fIpoll\fR\|(2) backend. It's more complicated |
474 | than select, but handles sparse fds better and has no artificial |
493 | than select, but handles sparse fds better and has no artificial |
… | |
… | |
553 | While this backend scales well, it requires one system call per active |
572 | While this backend scales well, it requires one system call per active |
554 | file descriptor per loop iteration. For small and medium numbers of file |
573 | file descriptor per loop iteration. For small and medium numbers of file |
555 | descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend |
574 | descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend |
556 | might perform better. |
575 | might perform better. |
557 | .Sp |
576 | .Sp |
558 | On the positive side, ignoring the spurious readyness notifications, this |
577 | On the positive side, ignoring the spurious readiness notifications, this |
559 | backend actually performed to specification in all tests and is fully |
578 | backend actually performed to specification in all tests and is fully |
560 | embeddable, which is a rare feat among the OS-specific backends. |
579 | embeddable, which is a rare feat among the OS-specific backends. |
561 | .ie n .IP """EVBACKEND_ALL""" 4 |
580 | .ie n .IP """EVBACKEND_ALL""" 4 |
562 | .el .IP "\f(CWEVBACKEND_ALL\fR" 4 |
581 | .el .IP "\f(CWEVBACKEND_ALL\fR" 4 |
563 | .IX Item "EVBACKEND_ALL" |
582 | .IX Item "EVBACKEND_ALL" |
… | |
… | |
826 | Many (busy) programs can usually benefit by setting the io collect |
845 | Many (busy) programs can usually benefit by setting the io collect |
827 | interval to a value near \f(CW0.1\fR or so, which is often enough for |
846 | interval to a value near \f(CW0.1\fR or so, which is often enough for |
828 | interactive servers (of course not for games), likewise for timeouts. It |
847 | interactive servers (of course not for games), likewise for timeouts. It |
829 | usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR, |
848 | usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR, |
830 | as this approsaches the timing granularity of most systems. |
849 | as this approsaches the timing granularity of most systems. |
|
|
850 | .IP "ev_loop_verify (loop)" 4 |
|
|
851 | .IX Item "ev_loop_verify (loop)" |
|
|
852 | This function only does something when \f(CW\*(C`EV_VERIFY\*(C'\fR support has been |
|
|
853 | compiled in. It tries to go through all internal structures and checks |
|
|
854 | them for validity. If anything is found to be inconsistent, it will print |
|
|
855 | an error message to standard error and call \f(CW\*(C`abort ()\*(C'\fR. |
|
|
856 | .Sp |
|
|
857 | This can be used to catch bugs inside libev itself: under normal |
|
|
858 | circumstances, this function will never abort as of course libev keeps its |
|
|
859 | data structures consistent. |
831 | .SH "ANATOMY OF A WATCHER" |
860 | .SH "ANATOMY OF A WATCHER" |
832 | .IX Header "ANATOMY OF A WATCHER" |
861 | .IX Header "ANATOMY OF A WATCHER" |
833 | A watcher is a structure that you create and register to record your |
862 | A watcher is a structure that you create and register to record your |
834 | interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to |
863 | interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to |
835 | become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: |
864 | become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: |
… | |
… | |
1163 | If you must do this, then force the use of a known-to-be-good backend |
1192 | If you must do this, then force the use of a known-to-be-good backend |
1164 | (at the time of this writing, this includes only \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and |
1193 | (at the time of this writing, this includes only \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and |
1165 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR). |
1194 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR). |
1166 | .PP |
1195 | .PP |
1167 | Another thing you have to watch out for is that it is quite easy to |
1196 | Another thing you have to watch out for is that it is quite easy to |
1168 | receive \*(L"spurious\*(R" readyness notifications, that is your callback might |
1197 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1169 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1198 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1170 | because there is no data. Not only are some backends known to create a |
1199 | because there is no data. Not only are some backends known to create a |
1171 | lot of those (for example solaris ports), it is very easy to get into |
1200 | lot of those (for example solaris ports), it is very easy to get into |
1172 | this situation even with a relatively standard program structure. Thus |
1201 | this situation even with a relatively standard program structure. Thus |
1173 | it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning |
1202 | it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning |
… | |
… | |
1283 | .IX Subsection "ev_timer - relative and optionally repeating timeouts" |
1312 | .IX Subsection "ev_timer - relative and optionally repeating timeouts" |
1284 | Timer watchers are simple relative timers that generate an event after a |
1313 | Timer watchers are simple relative timers that generate an event after a |
1285 | given time, and optionally repeating in regular intervals after that. |
1314 | given time, and optionally repeating in regular intervals after that. |
1286 | .PP |
1315 | .PP |
1287 | The timers are based on real time, that is, if you register an event that |
1316 | The timers are based on real time, that is, if you register an event that |
1288 | times out after an hour and you reset your system clock to last years |
1317 | times out after an hour and you reset your system clock to january last |
1289 | time, it will still time out after (roughly) and hour. \*(L"Roughly\*(R" because |
1318 | year, it will still time out after (roughly) and hour. \*(L"Roughly\*(R" because |
1290 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1319 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1291 | monotonic clock option helps a lot here). |
1320 | monotonic clock option helps a lot here). |
1292 | .PP |
1321 | .PP |
1293 | The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR |
1322 | The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR |
1294 | time. This is usually the right thing as this timestamp refers to the time |
1323 | time. This is usually the right thing as this timestamp refers to the time |
… | |
… | |
1298 | .PP |
1327 | .PP |
1299 | .Vb 1 |
1328 | .Vb 1 |
1300 | \& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.); |
1329 | \& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.); |
1301 | .Ve |
1330 | .Ve |
1302 | .PP |
1331 | .PP |
1303 | The callback is guarenteed to be invoked only when its timeout has passed, |
1332 | The callback is guarenteed to be invoked only after its timeout has passed, |
1304 | but if multiple timers become ready during the same loop iteration then |
1333 | but if multiple timers become ready during the same loop iteration then |
1305 | order of execution is undefined. |
1334 | order of execution is undefined. |
1306 | .PP |
1335 | .PP |
1307 | \fIWatcher-Specific Functions and Data Members\fR |
1336 | \fIWatcher-Specific Functions and Data Members\fR |
1308 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1337 | .IX Subsection "Watcher-Specific Functions and Data Members" |
… | |
… | |
1310 | .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" |
1339 | .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" |
1311 | .PD 0 |
1340 | .PD 0 |
1312 | .IP "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" 4 |
1341 | .IP "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" 4 |
1313 | .IX Item "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" |
1342 | .IX Item "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" |
1314 | .PD |
1343 | .PD |
1315 | Configure the timer to trigger after \f(CW\*(C`after\*(C'\fR seconds. If \f(CW\*(C`repeat\*(C'\fR is |
1344 | Configure the timer to trigger after \f(CW\*(C`after\*(C'\fR seconds. If \f(CW\*(C`repeat\*(C'\fR |
1316 | \&\f(CW0.\fR, then it will automatically be stopped. If it is positive, then the |
1345 | is \f(CW0.\fR, then it will automatically be stopped once the timeout is |
1317 | timer will automatically be configured to trigger again \f(CW\*(C`repeat\*(C'\fR seconds |
1346 | reached. If it is positive, then the timer will automatically be |
1318 | later, again, and again, until stopped manually. |
1347 | configured to trigger again \f(CW\*(C`repeat\*(C'\fR seconds later, again, and again, |
|
|
1348 | until stopped manually. |
1319 | .Sp |
1349 | .Sp |
1320 | The timer itself will do a best-effort at avoiding drift, that is, if you |
1350 | The timer itself will do a best-effort at avoiding drift, that is, if |
1321 | configure a timer to trigger every 10 seconds, then it will trigger at |
1351 | you configure a timer to trigger every 10 seconds, then it will normally |
1322 | exactly 10 second intervals. If, however, your program cannot keep up with |
1352 | trigger at exactly 10 second intervals. If, however, your program cannot |
1323 | the timer (because it takes longer than those 10 seconds to do stuff) the |
1353 | keep up with the timer (because it takes longer than those 10 seconds to |
1324 | timer will not fire more than once per event loop iteration. |
1354 | do stuff) the timer will not fire more than once per event loop iteration. |
1325 | .IP "ev_timer_again (loop, ev_timer *)" 4 |
1355 | .IP "ev_timer_again (loop, ev_timer *)" 4 |
1326 | .IX Item "ev_timer_again (loop, ev_timer *)" |
1356 | .IX Item "ev_timer_again (loop, ev_timer *)" |
1327 | This will act as if the timer timed out and restart it again if it is |
1357 | This will act as if the timer timed out and restart it again if it is |
1328 | repeating. The exact semantics are: |
1358 | repeating. The exact semantics are: |
1329 | .Sp |
1359 | .Sp |
… | |
… | |
1408 | Periodic watchers are also timers of a kind, but they are very versatile |
1438 | Periodic watchers are also timers of a kind, but they are very versatile |
1409 | (and unfortunately a bit complex). |
1439 | (and unfortunately a bit complex). |
1410 | .PP |
1440 | .PP |
1411 | Unlike \f(CW\*(C`ev_timer\*(C'\fR's, they are not based on real time (or relative time) |
1441 | Unlike \f(CW\*(C`ev_timer\*(C'\fR's, they are not based on real time (or relative time) |
1412 | but on wallclock time (absolute time). You can tell a periodic watcher |
1442 | but on wallclock time (absolute time). You can tell a periodic watcher |
1413 | to trigger \*(L"at\*(R" some specific point in time. For example, if you tell a |
1443 | to trigger after some specific point in time. For example, if you tell a |
1414 | periodic watcher to trigger in 10 seconds (by specifiying e.g. \f(CW\*(C`ev_now () |
1444 | periodic watcher to trigger in 10 seconds (by specifiying e.g. \f(CW\*(C`ev_now () |
1415 | + 10.\*(C'\fR) and then reset your system clock to the last year, then it will |
1445 | + 10.\*(C'\fR, that is, an absolute time not a delay) and then reset your system |
|
|
1446 | clock to january of the previous year, then it will take more than year |
1416 | take a year to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would trigger |
1447 | to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger |
1417 | roughly 10 seconds later). |
1448 | roughly 10 seconds later as it uses a relative timeout). |
1418 | .PP |
1449 | .PP |
1419 | They can also be used to implement vastly more complex timers, such as |
1450 | \&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers, |
1420 | triggering an event on each midnight, local time or other, complicated, |
1451 | such as triggering an event on each \*(L"midnight, local time\*(R", or other |
1421 | rules. |
1452 | complicated, rules. |
1422 | .PP |
1453 | .PP |
1423 | As with timers, the callback is guarenteed to be invoked only when the |
1454 | As with timers, the callback is guarenteed to be invoked only when the |
1424 | time (\f(CW\*(C`at\*(C'\fR) has been passed, but if multiple periodic timers become ready |
1455 | time (\f(CW\*(C`at\*(C'\fR) has passed, but if multiple periodic timers become ready |
1425 | during the same loop iteration then order of execution is undefined. |
1456 | during the same loop iteration then order of execution is undefined. |
1426 | .PP |
1457 | .PP |
1427 | \fIWatcher-Specific Functions and Data Members\fR |
1458 | \fIWatcher-Specific Functions and Data Members\fR |
1428 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1459 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1429 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 |
1460 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 |
… | |
… | |
1436 | operation, and we will explain them from simplest to complex: |
1467 | operation, and we will explain them from simplest to complex: |
1437 | .RS 4 |
1468 | .RS 4 |
1438 | .IP "\(bu" 4 |
1469 | .IP "\(bu" 4 |
1439 | absolute timer (at = time, interval = reschedule_cb = 0) |
1470 | absolute timer (at = time, interval = reschedule_cb = 0) |
1440 | .Sp |
1471 | .Sp |
1441 | In this configuration the watcher triggers an event at the wallclock time |
1472 | In this configuration the watcher triggers an event after the wallclock |
1442 | \&\f(CW\*(C`at\*(C'\fR and doesn't repeat. It will not adjust when a time jump occurs, |
1473 | time \f(CW\*(C`at\*(C'\fR has passed and doesn't repeat. It will not adjust when a time |
1443 | that is, if it is to be run at January 1st 2011 then it will run when the |
1474 | jump occurs, that is, if it is to be run at January 1st 2011 then it will |
1444 | system time reaches or surpasses this time. |
1475 | run when the system time reaches or surpasses this time. |
1445 | .IP "\(bu" 4 |
1476 | .IP "\(bu" 4 |
1446 | repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1477 | repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1447 | .Sp |
1478 | .Sp |
1448 | In this mode the watcher will always be scheduled to time out at the next |
1479 | In this mode the watcher will always be scheduled to time out at the next |
1449 | \&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) |
1480 | \&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) |
1450 | and then repeat, regardless of any time jumps. |
1481 | and then repeat, regardless of any time jumps. |
1451 | .Sp |
1482 | .Sp |
1452 | This can be used to create timers that do not drift with respect to system |
1483 | This can be used to create timers that do not drift with respect to system |
1453 | time: |
1484 | time, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each hour, on |
|
|
1485 | the hour: |
1454 | .Sp |
1486 | .Sp |
1455 | .Vb 1 |
1487 | .Vb 1 |
1456 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1488 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1457 | .Ve |
1489 | .Ve |
1458 | .Sp |
1490 | .Sp |
… | |
… | |
1465 | \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible |
1497 | \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible |
1466 | time where \f(CW\*(C`time = at (mod interval)\*(C'\fR, regardless of any time jumps. |
1498 | time where \f(CW\*(C`time = at (mod interval)\*(C'\fR, regardless of any time jumps. |
1467 | .Sp |
1499 | .Sp |
1468 | For numerical stability it is preferable that the \f(CW\*(C`at\*(C'\fR value is near |
1500 | For numerical stability it is preferable that the \f(CW\*(C`at\*(C'\fR value is near |
1469 | \&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for |
1501 | \&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for |
1470 | this value. |
1502 | this value, and in fact is often specified as zero. |
|
|
1503 | .Sp |
|
|
1504 | Note also that there is an upper limit to how often a timer can fire (cpu |
|
|
1505 | speed for example), so if \f(CW\*(C`interval\*(C'\fR is very small then timing stability |
|
|
1506 | will of course detoriate. Libev itself tries to be exact to be about one |
|
|
1507 | millisecond (if the \s-1OS\s0 supports it and the machine is fast enough). |
1471 | .IP "\(bu" 4 |
1508 | .IP "\(bu" 4 |
1472 | manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1509 | manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1473 | .Sp |
1510 | .Sp |
1474 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`at\*(C'\fR are both being |
1511 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`at\*(C'\fR are both being |
1475 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1512 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1476 | reschedule callback will be called with the watcher as first, and the |
1513 | reschedule callback will be called with the watcher as first, and the |
1477 | current time as second argument. |
1514 | current time as second argument. |
1478 | .Sp |
1515 | .Sp |
1479 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, |
1516 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, |
1480 | ever, or make any event loop modifications\fR. If you need to stop it, |
1517 | ever, or make \s-1ANY\s0 event loop modifications whatsoever\fR. |
1481 | return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop it afterwards (e.g. by |
1518 | .Sp |
|
|
1519 | If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop |
1482 | starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is legal). |
1520 | it afterwards (e.g. by starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is the |
|
|
1521 | only event loop modification you are allowed to do). |
1483 | .Sp |
1522 | .Sp |
1484 | Its prototype is \f(CW\*(C`ev_tstamp (*reschedule_cb)(struct ev_periodic *w, |
1523 | The callback prototype is \f(CW\*(C`ev_tstamp (*reschedule_cb)(struct ev_periodic |
1485 | ev_tstamp now)\*(C'\fR, e.g.: |
1524 | *w, ev_tstamp now)\*(C'\fR, e.g.: |
1486 | .Sp |
1525 | .Sp |
1487 | .Vb 4 |
1526 | .Vb 4 |
1488 | \& static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
1527 | \& static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
1489 | \& { |
1528 | \& { |
1490 | \& return now + 60.; |
1529 | \& return now + 60.; |
… | |
… | |
1494 | It must return the next time to trigger, based on the passed time value |
1533 | It must return the next time to trigger, based on the passed time value |
1495 | (that is, the lowest time value larger than to the second argument). It |
1534 | (that is, the lowest time value larger than to the second argument). It |
1496 | will usually be called just before the callback will be triggered, but |
1535 | will usually be called just before the callback will be triggered, but |
1497 | might be called at other times, too. |
1536 | might be called at other times, too. |
1498 | .Sp |
1537 | .Sp |
1499 | \&\s-1NOTE:\s0 \fIThis callback must always return a time that is later than the |
1538 | \&\s-1NOTE:\s0 \fIThis callback must always return a time that is higher than or |
1500 | passed \f(CI\*(C`now\*(C'\fI value\fR. Not even \f(CW\*(C`now\*(C'\fR itself will do, it \fImust\fR be larger. |
1539 | equal to the passed \f(CI\*(C`now\*(C'\fI value\fR. |
1501 | .Sp |
1540 | .Sp |
1502 | This can be used to create very complex timers, such as a timer that |
1541 | This can be used to create very complex timers, such as a timer that |
1503 | triggers on each midnight, local time. To do this, you would calculate the |
1542 | triggers on \*(L"next midnight, local time\*(R". To do this, you would calculate the |
1504 | next midnight after \f(CW\*(C`now\*(C'\fR and return the timestamp value for this. How |
1543 | next midnight after \f(CW\*(C`now\*(C'\fR and return the timestamp value for this. How |
1505 | you do this is, again, up to you (but it is not trivial, which is the main |
1544 | you do this is, again, up to you (but it is not trivial, which is the main |
1506 | reason I omitted it as an example). |
1545 | reason I omitted it as an example). |
1507 | .RE |
1546 | .RE |
1508 | .RS 4 |
1547 | .RS 4 |
… | |
… | |
1797 | calls your callback, which does something. When there is another update |
1836 | calls your callback, which does something. When there is another update |
1798 | within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it as the stat |
1837 | within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it as the stat |
1799 | data does not change. |
1838 | data does not change. |
1800 | .PP |
1839 | .PP |
1801 | The solution to this is to delay acting on a change for slightly more |
1840 | The solution to this is to delay acting on a change for slightly more |
1802 | than second (or till slightly after the next full second boundary), using |
1841 | than a second (or till slightly after the next full second boundary), using |
1803 | a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02); |
1842 | a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02); |
1804 | ev_timer_again (loop, w)\*(C'\fR). |
1843 | ev_timer_again (loop, w)\*(C'\fR). |
1805 | .PP |
1844 | .PP |
1806 | The \f(CW.02\fR offset is added to work around small timing inconsistencies |
1845 | The \f(CW.02\fR offset is added to work around small timing inconsistencies |
1807 | of some operating systems (where the second counter of the current time |
1846 | of some operating systems (where the second counter of the current time |
… | |
… | |
3086 | two). |
3125 | two). |
3087 | .IP "\s-1EV_USE_4HEAP\s0" 4 |
3126 | .IP "\s-1EV_USE_4HEAP\s0" 4 |
3088 | .IX Item "EV_USE_4HEAP" |
3127 | .IX Item "EV_USE_4HEAP" |
3089 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
3128 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
3090 | timer and periodics heap, libev uses a 4\-heap when this symbol is defined |
3129 | timer and periodics heap, libev uses a 4\-heap when this symbol is defined |
3091 | to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has a |
3130 | to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has |
3092 | noticable after performance with many (thousands) of watchers. |
3131 | noticably faster performance with many (thousands) of watchers. |
3093 | .Sp |
3132 | .Sp |
3094 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
3133 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
3095 | (disabled). |
3134 | (disabled). |
3096 | .IP "\s-1EV_HEAP_CACHE_AT\s0" 4 |
3135 | .IP "\s-1EV_HEAP_CACHE_AT\s0" 4 |
3097 | .IX Item "EV_HEAP_CACHE_AT" |
3136 | .IX Item "EV_HEAP_CACHE_AT" |
3098 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
3137 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
3099 | timer and periodics heap, libev can cache the timestamp (\fIat\fR) within |
3138 | timer and periodics heap, libev can cache the timestamp (\fIat\fR) within |
3100 | the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR), |
3139 | the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR), |
3101 | which uses 8\-12 bytes more per watcher and a few hundred bytes more code, |
3140 | which uses 8\-12 bytes more per watcher and a few hundred bytes more code, |
3102 | but avoids random read accesses on heap changes. This noticably improves |
3141 | but avoids random read accesses on heap changes. This improves performance |
3103 | performance noticably with with many (hundreds) of watchers. |
3142 | noticably with with many (hundreds) of watchers. |
3104 | .Sp |
3143 | .Sp |
3105 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
3144 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
3106 | (disabled). |
3145 | (disabled). |
|
|
3146 | .IP "\s-1EV_VERIFY\s0" 4 |
|
|
3147 | .IX Item "EV_VERIFY" |
|
|
3148 | Controls how much internal verification (see \f(CW\*(C`ev_loop_verify ()\*(C'\fR) will |
|
|
3149 | be done: If set to \f(CW0\fR, no internal verification code will be compiled |
|
|
3150 | in. If set to \f(CW1\fR, then verification code will be compiled in, but not |
|
|
3151 | called. If set to \f(CW2\fR, then the internal verification code will be |
|
|
3152 | called once per loop, which can slow down libev. If set to \f(CW3\fR, then the |
|
|
3153 | verification code will be called very frequently, which will slow down |
|
|
3154 | libev considerably. |
|
|
3155 | .Sp |
|
|
3156 | The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be |
|
|
3157 | \&\f(CW0.\fR |
3107 | .IP "\s-1EV_COMMON\s0" 4 |
3158 | .IP "\s-1EV_COMMON\s0" 4 |
3108 | .IX Item "EV_COMMON" |
3159 | .IX Item "EV_COMMON" |
3109 | By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining |
3160 | By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining |
3110 | this macro to a something else you can include more and other types of |
3161 | this macro to a something else you can include more and other types of |
3111 | members. You have to define it each time you include one of the files, |
3162 | members. You have to define it each time you include one of the files, |
… | |
… | |
3326 | .PP |
3377 | .PP |
3327 | Due to the many, low, and arbitrary limits on the win32 platform and |
3378 | Due to the many, low, and arbitrary limits on the win32 platform and |
3328 | the abysmal performance of winsockets, using a large number of sockets |
3379 | the abysmal performance of winsockets, using a large number of sockets |
3329 | is not recommended (and not reasonable). If your program needs to use |
3380 | is not recommended (and not reasonable). If your program needs to use |
3330 | more than a hundred or so sockets, then likely it needs to use a totally |
3381 | more than a hundred or so sockets, then likely it needs to use a totally |
3331 | different implementation for windows, as libev offers the \s-1POSIX\s0 readyness |
3382 | different implementation for windows, as libev offers the \s-1POSIX\s0 readiness |
3332 | notification model, which cannot be implemented efficiently on windows |
3383 | notification model, which cannot be implemented efficiently on windows |
3333 | (microsoft monopoly games). |
3384 | (microsoft monopoly games). |
3334 | .IP "The winsocket select function" 4 |
3385 | .IP "The winsocket select function" 4 |
3335 | .IX Item "The winsocket select function" |
3386 | .IX Item "The winsocket select function" |
3336 | The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it requires |
3387 | The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it |
3337 | socket \fIhandles\fR and not socket \fIfile descriptors\fR. This makes select |
3388 | requires socket \fIhandles\fR and not socket \fIfile descriptors\fR (it is |
3338 | very inefficient, and also requires a mapping from file descriptors |
3389 | also extremely buggy). This makes select very inefficient, and also |
3339 | to socket handles. See the discussion of the \f(CW\*(C`EV_SELECT_USE_FD_SET\*(C'\fR, |
3390 | requires a mapping from file descriptors to socket handles. See the |
3340 | \&\f(CW\*(C`EV_SELECT_IS_WINSOCKET\*(C'\fR and \f(CW\*(C`EV_FD_TO_WIN32_HANDLE\*(C'\fR preprocessor |
3391 | discussion of the \f(CW\*(C`EV_SELECT_USE_FD_SET\*(C'\fR, \f(CW\*(C`EV_SELECT_IS_WINSOCKET\*(C'\fR and |
3341 | symbols for more info. |
3392 | \&\f(CW\*(C`EV_FD_TO_WIN32_HANDLE\*(C'\fR preprocessor symbols for more info. |
3342 | .Sp |
3393 | .Sp |
3343 | The configuration for a \*(L"naked\*(R" win32 using the microsoft runtime |
3394 | The configuration for a \*(L"naked\*(R" win32 using the microsoft runtime |
3344 | libraries and raw winsocket select is: |
3395 | libraries and raw winsocket select is: |
3345 | .Sp |
3396 | .Sp |
3346 | .Vb 2 |
3397 | .Vb 2 |
… | |
… | |
3415 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
3466 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
3416 | enough for at least into the year 4000. This requirement is fulfilled by |
3467 | enough for at least into the year 4000. This requirement is fulfilled by |
3417 | implementations implementing \s-1IEEE\s0 754 (basically all existing ones). |
3468 | implementations implementing \s-1IEEE\s0 754 (basically all existing ones). |
3418 | .PP |
3469 | .PP |
3419 | If you know of other additional requirements drop me a note. |
3470 | If you know of other additional requirements drop me a note. |
|
|
3471 | .SH "COMPILER WARNINGS" |
|
|
3472 | .IX Header "COMPILER WARNINGS" |
|
|
3473 | Depending on your compiler and compiler settings, you might get no or a |
|
|
3474 | lot of warnings when compiling libev code. Some people are apparently |
|
|
3475 | scared by this. |
|
|
3476 | .PP |
|
|
3477 | However, these are unavoidable for many reasons. For one, each compiler |
|
|
3478 | has different warnings, and each user has different tastes regarding |
|
|
3479 | warning options. \*(L"Warn-free\*(R" code therefore cannot be a goal except when |
|
|
3480 | targetting a specific compiler and compiler-version. |
|
|
3481 | .PP |
|
|
3482 | Another reason is that some compiler warnings require elaborate |
|
|
3483 | workarounds, or other changes to the code that make it less clear and less |
|
|
3484 | maintainable. |
|
|
3485 | .PP |
|
|
3486 | And of course, some compiler warnings are just plain stupid, or simply |
|
|
3487 | wrong (because they don't actually warn about the cindition their message |
|
|
3488 | seems to warn about). |
|
|
3489 | .PP |
|
|
3490 | While libev is written to generate as few warnings as possible, |
|
|
3491 | \&\*(L"warn-free\*(R" code is not a goal, and it is recommended not to build libev |
|
|
3492 | with any compiler warnings enabled unless you are prepared to cope with |
|
|
3493 | them (e.g. by ignoring them). Remember that warnings are just that: |
|
|
3494 | warnings, not errors, or proof of bugs. |
|
|
3495 | .SH "VALGRIND" |
|
|
3496 | .IX Header "VALGRIND" |
|
|
3497 | Valgrind has a special section here because it is a popular tool that is |
|
|
3498 | highly useful, but valgrind reports are very hard to interpret. |
|
|
3499 | .PP |
|
|
3500 | If you think you found a bug (memory leak, uninitialised data access etc.) |
|
|
3501 | in libev, then check twice: If valgrind reports something like: |
|
|
3502 | .PP |
|
|
3503 | .Vb 3 |
|
|
3504 | \& ==2274== definitely lost: 0 bytes in 0 blocks. |
|
|
3505 | \& ==2274== possibly lost: 0 bytes in 0 blocks. |
|
|
3506 | \& ==2274== still reachable: 256 bytes in 1 blocks. |
|
|
3507 | .Ve |
|
|
3508 | .PP |
|
|
3509 | then there is no memory leak. Similarly, under some circumstances, |
|
|
3510 | valgrind might report kernel bugs as if it were a bug in libev, or it |
|
|
3511 | might be confused (it is a very good tool, but only a tool). |
|
|
3512 | .PP |
|
|
3513 | If you are unsure about something, feel free to contact the mailing list |
|
|
3514 | with the full valgrind report and an explanation on why you think this is |
|
|
3515 | a bug in libev. However, don't be annoyed when you get a brisk \*(L"this is |
|
|
3516 | no bug\*(R" answer and take the chance of learning how to interpret valgrind |
|
|
3517 | properly. |
|
|
3518 | .PP |
|
|
3519 | If you need, for some reason, empty reports from valgrind for your project |
|
|
3520 | I suggest using suppression lists. |
3420 | .SH "AUTHOR" |
3521 | .SH "AUTHOR" |
3421 | .IX Header "AUTHOR" |
3522 | .IX Header "AUTHOR" |
3422 | Marc Lehmann <libev@schmorp.de>. |
3523 | Marc Lehmann <libev@schmorp.de>. |
3423 | .SH "POD ERRORS" |
3524 | .SH "POD ERRORS" |
3424 | .IX Header "POD ERRORS" |
3525 | .IX Header "POD ERRORS" |
3425 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
3526 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
3426 | .IP "Around line 3052:" 4 |
3527 | .IP "Around line 3107:" 4 |
3427 | .IX Item "Around line 3052:" |
3528 | .IX Item "Around line 3107:" |
3428 | You forgot a '=back' before '=head2' |
3529 | You forgot a '=back' before '=head2' |