1 | .\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.30) |
1 | .\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) |
2 | .\" |
2 | .\" |
3 | .\" Standard preamble: |
3 | .\" Standard preamble: |
4 | .\" ======================================================================== |
4 | .\" ======================================================================== |
5 | .de Sp \" Vertical space (when we can't use .PP) |
5 | .de Sp \" Vertical space (when we can't use .PP) |
6 | .if t .sp .5v |
6 | .if t .sp .5v |
… | |
… | |
44 | .\" |
44 | .\" |
45 | .\" Escape single quotes in literal strings from groff's Unicode transform. |
45 | .\" Escape single quotes in literal strings from groff's Unicode transform. |
46 | .ie \n(.g .ds Aq \(aq |
46 | .ie \n(.g .ds Aq \(aq |
47 | .el .ds Aq ' |
47 | .el .ds Aq ' |
48 | .\" |
48 | .\" |
49 | .\" If the F register is turned on, we'll generate index entries on stderr for |
49 | .\" If the F register is >0, we'll generate index entries on stderr for |
50 | .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
50 | .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
51 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
51 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
52 | .\" output yourself in some meaningful fashion. |
52 | .\" output yourself in some meaningful fashion. |
53 | .\" |
53 | .\" |
54 | .\" Avoid warning from groff about undefined register 'F'. |
54 | .\" Avoid warning from groff about undefined register 'F'. |
55 | .de IX |
55 | .de IX |
56 | .. |
56 | .. |
57 | .nr rF 0 |
57 | .nr rF 0 |
58 | .if \n(.g .if rF .nr rF 1 |
58 | .if \n(.g .if rF .nr rF 1 |
59 | .if (\n(rF:(\n(.g==0)) \{ |
59 | .if (\n(rF:(\n(.g==0)) \{\ |
60 | . if \nF \{ |
60 | . if \nF \{\ |
61 | . de IX |
61 | . de IX |
62 | . tm Index:\\$1\t\\n%\t"\\$2" |
62 | . tm Index:\\$1\t\\n%\t"\\$2" |
63 | .. |
63 | .. |
64 | . if !\nF==2 \{ |
64 | . if !\nF==2 \{\ |
65 | . nr % 0 |
65 | . nr % 0 |
66 | . nr F 2 |
66 | . nr F 2 |
67 | . \} |
67 | . \} |
68 | . \} |
68 | . \} |
69 | .\} |
69 | .\} |
… | |
… | |
131 | .\} |
131 | .\} |
132 | .rm #[ #] #H #V #F C |
132 | .rm #[ #] #H #V #F C |
133 | .\" ======================================================================== |
133 | .\" ======================================================================== |
134 | .\" |
134 | .\" |
135 | .IX Title "LIBEV 3" |
135 | .IX Title "LIBEV 3" |
136 | .TH LIBEV 3 "2016-02-18" "libev-4.22" "libev - high performance full featured event loop" |
136 | .TH LIBEV 3 "2019-06-20" "libev-4.25" "libev - high performance full featured event loop" |
137 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
137 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
138 | .\" way too many mistakes in technical documents. |
138 | .\" way too many mistakes in technical documents. |
139 | .if n .ad l |
139 | .if n .ad l |
140 | .nh |
140 | .nh |
141 | .SH "NAME" |
141 | .SH "NAME" |
… | |
… | |
392 | .Sp |
392 | .Sp |
393 | You could override this function in high-availability programs to, say, |
393 | You could override this function in high-availability programs to, say, |
394 | free some memory if it cannot allocate memory, to use a special allocator, |
394 | free some memory if it cannot allocate memory, to use a special allocator, |
395 | or even to sleep a while and retry until some memory is available. |
395 | or even to sleep a while and retry until some memory is available. |
396 | .Sp |
396 | .Sp |
|
|
397 | Example: The following is the \f(CW\*(C`realloc\*(C'\fR function that libev itself uses |
|
|
398 | which should work with \f(CW\*(C`realloc\*(C'\fR and \f(CW\*(C`free\*(C'\fR functions of all kinds and |
|
|
399 | is probably a good basis for your own implementation. |
|
|
400 | .Sp |
|
|
401 | .Vb 5 |
|
|
402 | \& static void * |
|
|
403 | \& ev_realloc_emul (void *ptr, long size) EV_NOEXCEPT |
|
|
404 | \& { |
|
|
405 | \& if (size) |
|
|
406 | \& return realloc (ptr, size); |
|
|
407 | \& |
|
|
408 | \& free (ptr); |
|
|
409 | \& return 0; |
|
|
410 | \& } |
|
|
411 | .Ve |
|
|
412 | .Sp |
397 | Example: Replace the libev allocator with one that waits a bit and then |
413 | Example: Replace the libev allocator with one that waits a bit and then |
398 | retries (example requires a standards-compliant \f(CW\*(C`realloc\*(C'\fR). |
414 | retries. |
399 | .Sp |
415 | .Sp |
400 | .Vb 6 |
416 | .Vb 8 |
401 | \& static void * |
417 | \& static void * |
402 | \& persistent_realloc (void *ptr, size_t size) |
418 | \& persistent_realloc (void *ptr, size_t size) |
403 | \& { |
419 | \& { |
|
|
420 | \& if (!size) |
|
|
421 | \& { |
|
|
422 | \& free (ptr); |
|
|
423 | \& return 0; |
|
|
424 | \& } |
|
|
425 | \& |
404 | \& for (;;) |
426 | \& for (;;) |
405 | \& { |
427 | \& { |
406 | \& void *newptr = realloc (ptr, size); |
428 | \& void *newptr = realloc (ptr, size); |
407 | \& |
429 | \& |
408 | \& if (newptr) |
430 | \& if (newptr) |
… | |
… | |
536 | make libev check for a fork in each iteration by enabling this flag. |
558 | make libev check for a fork in each iteration by enabling this flag. |
537 | .Sp |
559 | .Sp |
538 | This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, |
560 | This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, |
539 | and thus this might slow down your event loop if you do a lot of loop |
561 | and thus this might slow down your event loop if you do a lot of loop |
540 | iterations and little real work, but is usually not noticeable (on my |
562 | iterations and little real work, but is usually not noticeable (on my |
541 | GNU/Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence |
563 | GNU/Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn |
542 | without a system call and thus \fIvery\fR fast, but my GNU/Linux system also has |
564 | sequence without a system call and thus \fIvery\fR fast, but my GNU/Linux |
543 | \&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster). |
565 | system also has \f(CW\*(C`pthread_atfork\*(C'\fR which is even faster). (Update: glibc |
|
|
566 | versions 2.25 apparently removed the \f(CW\*(C`getpid\*(C'\fR optimisation again). |
544 | .Sp |
567 | .Sp |
545 | The big advantage of this flag is that you can forget about fork (and |
568 | The big advantage of this flag is that you can forget about fork (and |
546 | forget about forgetting to tell libev about forking, although you still |
569 | forget about forgetting to tell libev about forking, although you still |
547 | have to ignore \f(CW\*(C`SIGPIPE\*(C'\fR) when you use this flag. |
570 | have to ignore \f(CW\*(C`SIGPIPE\*(C'\fR) when you use this flag. |
548 | .Sp |
571 | .Sp |
… | |
… | |
584 | .Sp |
607 | .Sp |
585 | This flag's behaviour will become the default in future versions of libev. |
608 | This flag's behaviour will become the default in future versions of libev. |
586 | .ie n .IP """EVBACKEND_SELECT"" (value 1, portable select backend)" 4 |
609 | .ie n .IP """EVBACKEND_SELECT"" (value 1, portable select backend)" 4 |
587 | .el .IP "\f(CWEVBACKEND_SELECT\fR (value 1, portable select backend)" 4 |
610 | .el .IP "\f(CWEVBACKEND_SELECT\fR (value 1, portable select backend)" 4 |
588 | .IX Item "EVBACKEND_SELECT (value 1, portable select backend)" |
611 | .IX Item "EVBACKEND_SELECT (value 1, portable select backend)" |
589 | This is your standard \fIselect\fR\|(2) backend. Not \fIcompletely\fR standard, as |
612 | This is your standard \fBselect\fR\|(2) backend. Not \fIcompletely\fR standard, as |
590 | libev tries to roll its own fd_set with no limits on the number of fds, |
613 | libev tries to roll its own fd_set with no limits on the number of fds, |
591 | but if that fails, expect a fairly low limit on the number of fds when |
614 | but if that fails, expect a fairly low limit on the number of fds when |
592 | using this backend. It doesn't scale too well (O(highest_fd)), but its |
615 | using this backend. It doesn't scale too well (O(highest_fd)), but its |
593 | usually the fastest backend for a low number of (low-numbered :) fds. |
616 | usually the fastest backend for a low number of (low-numbered :) fds. |
594 | .Sp |
617 | .Sp |
… | |
… | |
603 | \&\f(CW\*(C`writefds\*(C'\fR set (and to work around Microsoft Windows bugs, also onto the |
626 | \&\f(CW\*(C`writefds\*(C'\fR set (and to work around Microsoft Windows bugs, also onto the |
604 | \&\f(CW\*(C`exceptfds\*(C'\fR set on that platform). |
627 | \&\f(CW\*(C`exceptfds\*(C'\fR set on that platform). |
605 | .ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 |
628 | .ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 |
606 | .el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 |
629 | .el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 |
607 | .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" |
630 | .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" |
608 | And this is your standard \fIpoll\fR\|(2) backend. It's more complicated |
631 | And this is your standard \fBpoll\fR\|(2) backend. It's more complicated |
609 | than select, but handles sparse fds better and has no artificial |
632 | than select, but handles sparse fds better and has no artificial |
610 | limit on the number of fds you can use (except it will slow down |
633 | limit on the number of fds you can use (except it will slow down |
611 | considerably with a lot of inactive fds). It scales similarly to select, |
634 | considerably with a lot of inactive fds). It scales similarly to select, |
612 | i.e. O(total_fds). See the entry for \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR, above, for |
635 | i.e. O(total_fds). See the entry for \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR, above, for |
613 | performance tips. |
636 | performance tips. |
… | |
… | |
615 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR to \f(CW\*(C`POLLIN | POLLERR | POLLHUP\*(C'\fR, and |
638 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR to \f(CW\*(C`POLLIN | POLLERR | POLLHUP\*(C'\fR, and |
616 | \&\f(CW\*(C`EV_WRITE\*(C'\fR to \f(CW\*(C`POLLOUT | POLLERR | POLLHUP\*(C'\fR. |
639 | \&\f(CW\*(C`EV_WRITE\*(C'\fR to \f(CW\*(C`POLLOUT | POLLERR | POLLHUP\*(C'\fR. |
617 | .ie n .IP """EVBACKEND_EPOLL"" (value 4, Linux)" 4 |
640 | .ie n .IP """EVBACKEND_EPOLL"" (value 4, Linux)" 4 |
618 | .el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4 |
641 | .el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4 |
619 | .IX Item "EVBACKEND_EPOLL (value 4, Linux)" |
642 | .IX Item "EVBACKEND_EPOLL (value 4, Linux)" |
620 | Use the linux-specific \fIepoll\fR\|(7) interface (for both pre\- and post\-2.6.9 |
643 | Use the linux-specific \fBepoll\fR\|(7) interface (for both pre\- and post\-2.6.9 |
621 | kernels). |
644 | kernels). |
622 | .Sp |
645 | .Sp |
623 | For few fds, this backend is a bit little slower than poll and select, but |
646 | For few fds, this backend is a bit little slower than poll and select, but |
624 | it scales phenomenally better. While poll and select usually scale like |
647 | it scales phenomenally better. While poll and select usually scale like |
625 | O(total_fds) where total_fds is the total number of fds (or the highest |
648 | O(total_fds) where total_fds is the total number of fds (or the highest |
… | |
… | |
1364 | bug in your program. |
1387 | bug in your program. |
1365 | .Sp |
1388 | .Sp |
1366 | Libev will usually signal a few \*(L"dummy\*(R" events together with an error, for |
1389 | Libev will usually signal a few \*(L"dummy\*(R" events together with an error, for |
1367 | example it might indicate that a fd is readable or writable, and if your |
1390 | example it might indicate that a fd is readable or writable, and if your |
1368 | callbacks is well-written it can just attempt the operation and cope with |
1391 | callbacks is well-written it can just attempt the operation and cope with |
1369 | the error from \fIread()\fR or \fIwrite()\fR. This will not work in multi-threaded |
1392 | the error from \fBread()\fR or \fBwrite()\fR. This will not work in multi-threaded |
1370 | programs, though, as the fd could already be closed and reused for another |
1393 | programs, though, as the fd could already be closed and reused for another |
1371 | thing, so beware. |
1394 | thing, so beware. |
1372 | .SS "\s-1GENERIC WATCHER FUNCTIONS\s0" |
1395 | .SS "\s-1GENERIC WATCHER FUNCTIONS\s0" |
1373 | .IX Subsection "GENERIC WATCHER FUNCTIONS" |
1396 | .IX Subsection "GENERIC WATCHER FUNCTIONS" |
1374 | .ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4 |
1397 | .ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4 |
… | |
… | |
1810 | when writing to a pipe whose other end has been closed, your program gets |
1833 | when writing to a pipe whose other end has been closed, your program gets |
1811 | sent a \s-1SIGPIPE,\s0 which, by default, aborts your program. For most programs |
1834 | sent a \s-1SIGPIPE,\s0 which, by default, aborts your program. For most programs |
1812 | this is sensible behaviour, for daemons, this is usually undesirable. |
1835 | this is sensible behaviour, for daemons, this is usually undesirable. |
1813 | .PP |
1836 | .PP |
1814 | So when you encounter spurious, unexplained daemon exits, make sure you |
1837 | So when you encounter spurious, unexplained daemon exits, make sure you |
1815 | ignore \s-1SIGPIPE \s0(and maybe make sure you log the exit status of your daemon |
1838 | ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon |
1816 | somewhere, as that would have given you a big clue). |
1839 | somewhere, as that would have given you a big clue). |
1817 | .PP |
1840 | .PP |
1818 | \fIThe special problem of \fIaccept()\fIing when you can't\fR |
1841 | \fIThe special problem of \f(BIaccept()\fIing when you can't\fR |
1819 | .IX Subsection "The special problem of accept()ing when you can't" |
1842 | .IX Subsection "The special problem of accept()ing when you can't" |
1820 | .PP |
1843 | .PP |
1821 | Many implementations of the \s-1POSIX \s0\f(CW\*(C`accept\*(C'\fR function (for example, |
1844 | Many implementations of the \s-1POSIX\s0 \f(CW\*(C`accept\*(C'\fR function (for example, |
1822 | found in post\-2004 Linux) have the peculiar behaviour of not removing a |
1845 | found in post\-2004 Linux) have the peculiar behaviour of not removing a |
1823 | connection from the pending queue in all error cases. |
1846 | connection from the pending queue in all error cases. |
1824 | .PP |
1847 | .PP |
1825 | For example, larger servers often run out of file descriptors (because |
1848 | For example, larger servers often run out of file descriptors (because |
1826 | of resource limits), causing \f(CW\*(C`accept\*(C'\fR to fail with \f(CW\*(C`ENFILE\*(C'\fR but not |
1849 | of resource limits), causing \f(CW\*(C`accept\*(C'\fR to fail with \f(CW\*(C`ENFILE\*(C'\fR but not |
… | |
… | |
2250 | .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" |
2273 | .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" |
2251 | .PD 0 |
2274 | .PD 0 |
2252 | .IP "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" 4 |
2275 | .IP "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" 4 |
2253 | .IX Item "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" |
2276 | .IX Item "ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat)" |
2254 | .PD |
2277 | .PD |
2255 | Configure the timer to trigger after \f(CW\*(C`after\*(C'\fR seconds. If \f(CW\*(C`repeat\*(C'\fR |
2278 | Configure the timer to trigger after \f(CW\*(C`after\*(C'\fR seconds (fractional and |
2256 | is \f(CW0.\fR, then it will automatically be stopped once the timeout is |
2279 | negative values are supported). If \f(CW\*(C`repeat\*(C'\fR is \f(CW0.\fR, then it will |
2257 | reached. If it is positive, then the timer will automatically be |
2280 | automatically be stopped once the timeout is reached. If it is positive, |
2258 | configured to trigger again \f(CW\*(C`repeat\*(C'\fR seconds later, again, and again, |
2281 | then the timer will automatically be configured to trigger again \f(CW\*(C`repeat\*(C'\fR |
2259 | until stopped manually. |
2282 | seconds later, again, and again, until stopped manually. |
2260 | .Sp |
2283 | .Sp |
2261 | The timer itself will do a best-effort at avoiding drift, that is, if |
2284 | The timer itself will do a best-effort at avoiding drift, that is, if |
2262 | you configure a timer to trigger every 10 seconds, then it will normally |
2285 | you configure a timer to trigger every 10 seconds, then it will normally |
2263 | trigger at exactly 10 second intervals. If, however, your program cannot |
2286 | trigger at exactly 10 second intervals. If, however, your program cannot |
2264 | keep up with the timer (because it takes longer than those 10 seconds to |
2287 | keep up with the timer (because it takes longer than those 10 seconds to |
… | |
… | |
2361 | \&\f(CW\*(C`ev_timer\*(C'\fR, which would still trigger roughly 10 seconds after starting |
2384 | \&\f(CW\*(C`ev_timer\*(C'\fR, which would still trigger roughly 10 seconds after starting |
2362 | it, as it uses a relative timeout). |
2385 | it, as it uses a relative timeout). |
2363 | .PP |
2386 | .PP |
2364 | \&\f(CW\*(C`ev_periodic\*(C'\fR watchers can also be used to implement vastly more complex |
2387 | \&\f(CW\*(C`ev_periodic\*(C'\fR watchers can also be used to implement vastly more complex |
2365 | timers, such as triggering an event on each \*(L"midnight, local time\*(R", or |
2388 | timers, such as triggering an event on each \*(L"midnight, local time\*(R", or |
2366 | other complicated rules. This cannot be done with \f(CW\*(C`ev_timer\*(C'\fR watchers, as |
2389 | other complicated rules. This cannot easily be done with \f(CW\*(C`ev_timer\*(C'\fR |
2367 | those cannot react to time jumps. |
2390 | watchers, as those cannot react to time jumps. |
2368 | .PP |
2391 | .PP |
2369 | As with timers, the callback is guaranteed to be invoked only when the |
2392 | As with timers, the callback is guaranteed to be invoked only when the |
2370 | point in time where it is supposed to trigger has passed. If multiple |
2393 | point in time where it is supposed to trigger has passed. If multiple |
2371 | timers become ready during the same loop iteration then the ones with |
2394 | timers become ready during the same loop iteration then the ones with |
2372 | earlier time-out values are invoked before ones with later time-out values |
2395 | earlier time-out values are invoked before ones with later time-out values |
… | |
… | |
2433 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`offset\*(C'\fR are both being |
2456 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`offset\*(C'\fR are both being |
2434 | ignored. Instead, each time the periodic watcher gets scheduled, the |
2457 | ignored. Instead, each time the periodic watcher gets scheduled, the |
2435 | reschedule callback will be called with the watcher as first, and the |
2458 | reschedule callback will be called with the watcher as first, and the |
2436 | current time as second argument. |
2459 | current time as second argument. |
2437 | .Sp |
2460 | .Sp |
2438 | \&\s-1NOTE: \s0\fIThis callback \s-1MUST NOT\s0 stop or destroy any periodic watcher, ever, |
2461 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST NOT\s0 stop or destroy any periodic watcher, ever, |
2439 | or make \s-1ANY\s0 other event loop modifications whatsoever, unless explicitly |
2462 | or make \s-1ANY\s0 other event loop modifications whatsoever, unless explicitly |
2440 | allowed by documentation here\fR. |
2463 | allowed by documentation here\fR. |
2441 | .Sp |
2464 | .Sp |
2442 | If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop |
2465 | If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop |
2443 | it afterwards (e.g. by starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is the |
2466 | it afterwards (e.g. by starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is the |
… | |
… | |
2457 | It must return the next time to trigger, based on the passed time value |
2480 | It must return the next time to trigger, based on the passed time value |
2458 | (that is, the lowest time value larger than to the second argument). It |
2481 | (that is, the lowest time value larger than to the second argument). It |
2459 | will usually be called just before the callback will be triggered, but |
2482 | will usually be called just before the callback will be triggered, but |
2460 | might be called at other times, too. |
2483 | might be called at other times, too. |
2461 | .Sp |
2484 | .Sp |
2462 | \&\s-1NOTE: \s0\fIThis callback must always return a time that is higher than or |
2485 | \&\s-1NOTE:\s0 \fIThis callback must always return a time that is higher than or |
2463 | equal to the passed \f(CI\*(C`now\*(C'\fI value\fR. |
2486 | equal to the passed \f(CI\*(C`now\*(C'\fI value\fR. |
2464 | .Sp |
2487 | .Sp |
2465 | This can be used to create very complex timers, such as a timer that |
2488 | This can be used to create very complex timers, such as a timer that |
2466 | triggers on \*(L"next midnight, local time\*(R". To do this, you would calculate the |
2489 | triggers on \*(L"next midnight, local time\*(R". To do this, you would calculate |
2467 | next midnight after \f(CW\*(C`now\*(C'\fR and return the timestamp value for this. How |
2490 | the next midnight after \f(CW\*(C`now\*(C'\fR and return the timestamp value for |
2468 | you do this is, again, up to you (but it is not trivial, which is the main |
2491 | this. Here is a (completely untested, no error checking) example on how to |
2469 | reason I omitted it as an example). |
2492 | do this: |
|
|
2493 | .Sp |
|
|
2494 | .Vb 1 |
|
|
2495 | \& #include <time.h> |
|
|
2496 | \& |
|
|
2497 | \& static ev_tstamp |
|
|
2498 | \& my_rescheduler (ev_periodic *w, ev_tstamp now) |
|
|
2499 | \& { |
|
|
2500 | \& time_t tnow = (time_t)now; |
|
|
2501 | \& struct tm tm; |
|
|
2502 | \& localtime_r (&tnow, &tm); |
|
|
2503 | \& |
|
|
2504 | \& tm.tm_sec = tm.tm_min = tm.tm_hour = 0; // midnight current day |
|
|
2505 | \& ++tm.tm_mday; // midnight next day |
|
|
2506 | \& |
|
|
2507 | \& return mktime (&tm); |
|
|
2508 | \& } |
|
|
2509 | .Ve |
|
|
2510 | .Sp |
|
|
2511 | Note: this code might run into trouble on days that have more then two |
|
|
2512 | midnights (beginning and end). |
2470 | .RE |
2513 | .RE |
2471 | .RS 4 |
2514 | .RS 4 |
2472 | .RE |
2515 | .RE |
2473 | .IP "ev_periodic_again (loop, ev_periodic *)" 4 |
2516 | .IP "ev_periodic_again (loop, ev_periodic *)" 4 |
2474 | .IX Item "ev_periodic_again (loop, ev_periodic *)" |
2517 | .IX Item "ev_periodic_again (loop, ev_periodic *)" |
… | |
… | |
2592 | The simplest way to ensure that the signal mask is reset in the child is |
2635 | The simplest way to ensure that the signal mask is reset in the child is |
2593 | to install a fork handler with \f(CW\*(C`pthread_atfork\*(C'\fR that resets it. That will |
2636 | to install a fork handler with \f(CW\*(C`pthread_atfork\*(C'\fR that resets it. That will |
2594 | catch fork calls done by libraries (such as the libc) as well. |
2637 | catch fork calls done by libraries (such as the libc) as well. |
2595 | .PP |
2638 | .PP |
2596 | In current versions of libev, the signal will not be blocked indefinitely |
2639 | In current versions of libev, the signal will not be blocked indefinitely |
2597 | unless you use the \f(CW\*(C`signalfd\*(C'\fR \s-1API \s0(\f(CW\*(C`EV_SIGNALFD\*(C'\fR). While this reduces |
2640 | unless you use the \f(CW\*(C`signalfd\*(C'\fR \s-1API\s0 (\f(CW\*(C`EV_SIGNALFD\*(C'\fR). While this reduces |
2598 | the window of opportunity for problems, it will not go away, as libev |
2641 | the window of opportunity for problems, it will not go away, as libev |
2599 | \&\fIhas\fR to modify the signal mask, at least temporarily. |
2642 | \&\fIhas\fR to modify the signal mask, at least temporarily. |
2600 | .PP |
2643 | .PP |
2601 | So I can't stress this enough: \fIIf you do not reset your signal mask when |
2644 | So I can't stress this enough: \fIIf you do not reset your signal mask when |
2602 | you expect it to be empty, you have a race condition in your code\fR. This |
2645 | you expect it to be empty, you have a race condition in your code\fR. This |
… | |
… | |
3644 | is a time window between the event loop checking and resetting the async |
3687 | is a time window between the event loop checking and resetting the async |
3645 | notification, and the callback being invoked. |
3688 | notification, and the callback being invoked. |
3646 | .SH "OTHER FUNCTIONS" |
3689 | .SH "OTHER FUNCTIONS" |
3647 | .IX Header "OTHER FUNCTIONS" |
3690 | .IX Header "OTHER FUNCTIONS" |
3648 | There are some other functions of possible interest. Described. Here. Now. |
3691 | There are some other functions of possible interest. Described. Here. Now. |
3649 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
3692 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback, arg)" 4 |
3650 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
3693 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback, arg)" |
3651 | This function combines a simple timer and an I/O watcher, calls your |
3694 | This function combines a simple timer and an I/O watcher, calls your |
3652 | callback on whichever event happens first and automatically stops both |
3695 | callback on whichever event happens first and automatically stops both |
3653 | watchers. This is useful if you want to wait for a single event on an fd |
3696 | watchers. This is useful if you want to wait for a single event on an fd |
3654 | or timeout without having to allocate/configure/start/stop/free one or |
3697 | or timeout without having to allocate/configure/start/stop/free one or |
3655 | more watchers yourself. |
3698 | more watchers yourself. |
… | |
… | |
4105 | The normal C \s-1API\s0 should work fine when used from \*(C+: both ev.h and the |
4148 | The normal C \s-1API\s0 should work fine when used from \*(C+: both ev.h and the |
4106 | libev sources can be compiled as \*(C+. Therefore, code that uses the C \s-1API\s0 |
4149 | libev sources can be compiled as \*(C+. Therefore, code that uses the C \s-1API\s0 |
4107 | will work fine. |
4150 | will work fine. |
4108 | .PP |
4151 | .PP |
4109 | Proper exception specifications might have to be added to callbacks passed |
4152 | Proper exception specifications might have to be added to callbacks passed |
4110 | to libev: exceptions may be thrown only from watcher callbacks, all |
4153 | to libev: exceptions may be thrown only from watcher callbacks, all other |
4111 | other callbacks (allocator, syserr, loop acquire/release and periodic |
4154 | callbacks (allocator, syserr, loop acquire/release and periodic reschedule |
4112 | reschedule callbacks) must not throw exceptions, and might need a \f(CW\*(C`throw |
4155 | callbacks) must not throw exceptions, and might need a \f(CW\*(C`noexcept\*(C'\fR |
4113 | ()\*(C'\fR specification. If you have code that needs to be compiled as both C |
4156 | specification. If you have code that needs to be compiled as both C and |
4114 | and \*(C+ you can use the \f(CW\*(C`EV_THROW\*(C'\fR macro for this: |
4157 | \&\*(C+ you can use the \f(CW\*(C`EV_NOEXCEPT\*(C'\fR macro for this: |
4115 | .PP |
4158 | .PP |
4116 | .Vb 6 |
4159 | .Vb 6 |
4117 | \& static void |
4160 | \& static void |
4118 | \& fatal_error (const char *msg) EV_THROW |
4161 | \& fatal_error (const char *msg) EV_NOEXCEPT |
4119 | \& { |
4162 | \& { |
4120 | \& perror (msg); |
4163 | \& perror (msg); |
4121 | \& abort (); |
4164 | \& abort (); |
4122 | \& } |
4165 | \& } |
4123 | \& |
4166 | \& |
… | |
… | |
4497 | \& #include "ev.c" |
4540 | \& #include "ev.c" |
4498 | .Ve |
4541 | .Ve |
4499 | .PP |
4542 | .PP |
4500 | This will automatically include \fIev.h\fR, too, and should be done in a |
4543 | This will automatically include \fIev.h\fR, too, and should be done in a |
4501 | single C source file only to provide the function implementations. To use |
4544 | single C source file only to provide the function implementations. To use |
4502 | it, do the same for \fIev.h\fR in all files wishing to use this \s-1API \s0(best |
4545 | it, do the same for \fIev.h\fR in all files wishing to use this \s-1API\s0 (best |
4503 | done by writing a wrapper around \fIev.h\fR that you can include instead and |
4546 | done by writing a wrapper around \fIev.h\fR that you can include instead and |
4504 | where you can put other configuration options): |
4547 | where you can put other configuration options): |
4505 | .PP |
4548 | .PP |
4506 | .Vb 2 |
4549 | .Vb 2 |
4507 | \& #define EV_STANDALONE 1 |
4550 | \& #define EV_STANDALONE 1 |
… | |
… | |
4521 | \& ev_vars.h |
4564 | \& ev_vars.h |
4522 | \& ev_wrap.h |
4565 | \& ev_wrap.h |
4523 | \& |
4566 | \& |
4524 | \& ev_win32.c required on win32 platforms only |
4567 | \& ev_win32.c required on win32 platforms only |
4525 | \& |
4568 | \& |
4526 | \& ev_select.c only when select backend is enabled (which is enabled by default) |
4569 | \& ev_select.c only when select backend is enabled |
4527 | \& ev_poll.c only when poll backend is enabled (disabled by default) |
4570 | \& ev_poll.c only when poll backend is enabled |
4528 | \& ev_epoll.c only when the epoll backend is enabled (disabled by default) |
4571 | \& ev_epoll.c only when the epoll backend is enabled |
4529 | \& ev_kqueue.c only when the kqueue backend is enabled (disabled by default) |
4572 | \& ev_kqueue.c only when the kqueue backend is enabled |
4530 | \& ev_port.c only when the solaris port backend is enabled (disabled by default) |
4573 | \& ev_port.c only when the solaris port backend is enabled |
4531 | .Ve |
4574 | .Ve |
4532 | .PP |
4575 | .PP |
4533 | \&\fIev.c\fR includes the backend files directly when enabled, so you only need |
4576 | \&\fIev.c\fR includes the backend files directly when enabled, so you only need |
4534 | to compile this single file. |
4577 | to compile this single file. |
4535 | .PP |
4578 | .PP |
… | |
… | |
4580 | values when compiling libev vs. including \fIev.h\fR, so it is permissible |
4623 | values when compiling libev vs. including \fIev.h\fR, so it is permissible |
4581 | to redefine them before including \fIev.h\fR without breaking compatibility |
4624 | to redefine them before including \fIev.h\fR without breaking compatibility |
4582 | to a compiled library. All other symbols change the \s-1ABI,\s0 which means all |
4625 | to a compiled library. All other symbols change the \s-1ABI,\s0 which means all |
4583 | users of libev and the libev code itself must be compiled with compatible |
4626 | users of libev and the libev code itself must be compiled with compatible |
4584 | settings. |
4627 | settings. |
4585 | .IP "\s-1EV_COMPAT3 \s0(h)" 4 |
4628 | .IP "\s-1EV_COMPAT3\s0 (h)" 4 |
4586 | .IX Item "EV_COMPAT3 (h)" |
4629 | .IX Item "EV_COMPAT3 (h)" |
4587 | Backwards compatibility is a major concern for libev. This is why this |
4630 | Backwards compatibility is a major concern for libev. This is why this |
4588 | release of libev comes with wrappers for the functions and symbols that |
4631 | release of libev comes with wrappers for the functions and symbols that |
4589 | have been renamed between libev version 3 and 4. |
4632 | have been renamed between libev version 3 and 4. |
4590 | .Sp |
4633 | .Sp |
… | |
… | |
4595 | typedef in that case. |
4638 | typedef in that case. |
4596 | .Sp |
4639 | .Sp |
4597 | In some future version, the default for \f(CW\*(C`EV_COMPAT3\*(C'\fR will become \f(CW0\fR, |
4640 | In some future version, the default for \f(CW\*(C`EV_COMPAT3\*(C'\fR will become \f(CW0\fR, |
4598 | and in some even more future version the compatibility code will be |
4641 | and in some even more future version the compatibility code will be |
4599 | removed completely. |
4642 | removed completely. |
4600 | .IP "\s-1EV_STANDALONE \s0(h)" 4 |
4643 | .IP "\s-1EV_STANDALONE\s0 (h)" 4 |
4601 | .IX Item "EV_STANDALONE (h)" |
4644 | .IX Item "EV_STANDALONE (h)" |
4602 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
4645 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
4603 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
4646 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
4604 | implementations for some libevent functions (such as logging, which is not |
4647 | implementations for some libevent functions (such as logging, which is not |
4605 | supported). It will also not define any of the structs usually found in |
4648 | supported). It will also not define any of the structs usually found in |
… | |
… | |
4763 | handler \*(L"locking\*(R" as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR |
4806 | handler \*(L"locking\*(R" as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR |
4764 | watchers. |
4807 | watchers. |
4765 | .Sp |
4808 | .Sp |
4766 | In the absence of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR |
4809 | In the absence of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR |
4767 | (from \fIsignal.h\fR), which is usually good enough on most platforms. |
4810 | (from \fIsignal.h\fR), which is usually good enough on most platforms. |
4768 | .IP "\s-1EV_H \s0(h)" 4 |
4811 | .IP "\s-1EV_H\s0 (h)" 4 |
4769 | .IX Item "EV_H (h)" |
4812 | .IX Item "EV_H (h)" |
4770 | The name of the \fIev.h\fR header file used to include it. The default if |
4813 | The name of the \fIev.h\fR header file used to include it. The default if |
4771 | undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be |
4814 | undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be |
4772 | used to virtually rename the \fIev.h\fR header file in case of conflicts. |
4815 | used to virtually rename the \fIev.h\fR header file in case of conflicts. |
4773 | .IP "\s-1EV_CONFIG_H \s0(h)" 4 |
4816 | .IP "\s-1EV_CONFIG_H\s0 (h)" 4 |
4774 | .IX Item "EV_CONFIG_H (h)" |
4817 | .IX Item "EV_CONFIG_H (h)" |
4775 | If \f(CW\*(C`EV_STANDALONE\*(C'\fR isn't \f(CW1\fR, this variable can be used to override |
4818 | If \f(CW\*(C`EV_STANDALONE\*(C'\fR isn't \f(CW1\fR, this variable can be used to override |
4776 | \&\fIev.c\fR's idea of where to find the \fIconfig.h\fR file, similarly to |
4819 | \&\fIev.c\fR's idea of where to find the \fIconfig.h\fR file, similarly to |
4777 | \&\f(CW\*(C`EV_H\*(C'\fR, above. |
4820 | \&\f(CW\*(C`EV_H\*(C'\fR, above. |
4778 | .IP "\s-1EV_EVENT_H \s0(h)" 4 |
4821 | .IP "\s-1EV_EVENT_H\s0 (h)" 4 |
4779 | .IX Item "EV_EVENT_H (h)" |
4822 | .IX Item "EV_EVENT_H (h)" |
4780 | Similarly to \f(CW\*(C`EV_H\*(C'\fR, this macro can be used to override \fIevent.c\fR's idea |
4823 | Similarly to \f(CW\*(C`EV_H\*(C'\fR, this macro can be used to override \fIevent.c\fR's idea |
4781 | of how the \fIevent.h\fR header can be found, the default is \f(CW"event.h"\fR. |
4824 | of how the \fIevent.h\fR header can be found, the default is \f(CW"event.h"\fR. |
4782 | .IP "\s-1EV_PROTOTYPES \s0(h)" 4 |
4825 | .IP "\s-1EV_PROTOTYPES\s0 (h)" 4 |
4783 | .IX Item "EV_PROTOTYPES (h)" |
4826 | .IX Item "EV_PROTOTYPES (h)" |
4784 | If defined to be \f(CW0\fR, then \fIev.h\fR will not define any function |
4827 | If defined to be \f(CW0\fR, then \fIev.h\fR will not define any function |
4785 | prototypes, but still define all the structs and other symbols. This is |
4828 | prototypes, but still define all the structs and other symbols. This is |
4786 | occasionally useful if you want to provide your own wrapper functions |
4829 | occasionally useful if you want to provide your own wrapper functions |
4787 | around libev functions. |
4830 | around libev functions. |
… | |
… | |
4996 | .Vb 3 |
5039 | .Vb 3 |
4997 | \& #define EV_COMMON \e |
5040 | \& #define EV_COMMON \e |
4998 | \& SV *self; /* contains this struct */ \e |
5041 | \& SV *self; /* contains this struct */ \e |
4999 | \& SV *cb_sv, *fh /* note no trailing ";" */ |
5042 | \& SV *cb_sv, *fh /* note no trailing ";" */ |
5000 | .Ve |
5043 | .Ve |
5001 | .IP "\s-1EV_CB_DECLARE \s0(type)" 4 |
5044 | .IP "\s-1EV_CB_DECLARE\s0 (type)" 4 |
5002 | .IX Item "EV_CB_DECLARE (type)" |
5045 | .IX Item "EV_CB_DECLARE (type)" |
5003 | .PD 0 |
5046 | .PD 0 |
5004 | .IP "\s-1EV_CB_INVOKE \s0(watcher, revents)" 4 |
5047 | .IP "\s-1EV_CB_INVOKE\s0 (watcher, revents)" 4 |
5005 | .IX Item "EV_CB_INVOKE (watcher, revents)" |
5048 | .IX Item "EV_CB_INVOKE (watcher, revents)" |
5006 | .IP "ev_set_cb (ev, cb)" 4 |
5049 | .IP "ev_set_cb (ev, cb)" 4 |
5007 | .IX Item "ev_set_cb (ev, cb)" |
5050 | .IX Item "ev_set_cb (ev, cb)" |
5008 | .PD |
5051 | .PD |
5009 | Can be used to change the callback member declaration in each watcher, |
5052 | Can be used to change the callback member declaration in each watcher, |
… | |
… | |
5012 | their default definitions. One possible use for overriding these is to |
5055 | their default definitions. One possible use for overriding these is to |
5013 | avoid the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument in all cases, or to use |
5056 | avoid the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument in all cases, or to use |
5014 | method calls instead of plain function calls in \*(C+. |
5057 | method calls instead of plain function calls in \*(C+. |
5015 | .SS "\s-1EXPORTED API SYMBOLS\s0" |
5058 | .SS "\s-1EXPORTED API SYMBOLS\s0" |
5016 | .IX Subsection "EXPORTED API SYMBOLS" |
5059 | .IX Subsection "EXPORTED API SYMBOLS" |
5017 | If you need to re-export the \s-1API \s0(e.g. via a \s-1DLL\s0) and you need a list of |
5060 | If you need to re-export the \s-1API\s0 (e.g. via a \s-1DLL\s0) and you need a list of |
5018 | exported symbols, you can use the provided \fISymbol.*\fR files which list |
5061 | exported symbols, you can use the provided \fISymbol.*\fR files which list |
5019 | all public symbols, one per line: |
5062 | all public symbols, one per line: |
5020 | .PP |
5063 | .PP |
5021 | .Vb 2 |
5064 | .Vb 2 |
5022 | \& Symbols.ev for libev proper |
5065 | \& Symbols.ev for libev proper |
… | |
… | |
5254 | .PP |
5297 | .PP |
5255 | \fI\f(CI\*(C`select\*(C'\fI is buggy\fR |
5298 | \fI\f(CI\*(C`select\*(C'\fI is buggy\fR |
5256 | .IX Subsection "select is buggy" |
5299 | .IX Subsection "select is buggy" |
5257 | .PP |
5300 | .PP |
5258 | All that's left is \f(CW\*(C`select\*(C'\fR, and of course Apple found a way to fuck this |
5301 | All that's left is \f(CW\*(C`select\*(C'\fR, and of course Apple found a way to fuck this |
5259 | one up as well: On \s-1OS/X, \s0\f(CW\*(C`select\*(C'\fR actively limits the number of file |
5302 | one up as well: On \s-1OS/X,\s0 \f(CW\*(C`select\*(C'\fR actively limits the number of file |
5260 | descriptors you can pass in to 1024 \- your program suddenly crashes when |
5303 | descriptors you can pass in to 1024 \- your program suddenly crashes when |
5261 | you use more. |
5304 | you use more. |
5262 | .PP |
5305 | .PP |
5263 | There is an undocumented \*(L"workaround\*(R" for this \- defining |
5306 | There is an undocumented \*(L"workaround\*(R" for this \- defining |
5264 | \&\f(CW\*(C`_DARWIN_UNLIMITED_SELECT\*(C'\fR, which libev tries to use, so select \fIshould\fR |
5307 | \&\f(CW\*(C`_DARWIN_UNLIMITED_SELECT\*(C'\fR, which libev tries to use, so select \fIshould\fR |
… | |
… | |
5412 | Libev assumes not only that all watcher pointers have the same internal |
5455 | Libev assumes not only that all watcher pointers have the same internal |
5413 | structure (guaranteed by \s-1POSIX\s0 but not by \s-1ISO C\s0 for example), but it also |
5456 | structure (guaranteed by \s-1POSIX\s0 but not by \s-1ISO C\s0 for example), but it also |
5414 | assumes that the same (machine) code can be used to call any watcher |
5457 | assumes that the same (machine) code can be used to call any watcher |
5415 | callback: The watcher callbacks have different type signatures, but libev |
5458 | callback: The watcher callbacks have different type signatures, but libev |
5416 | calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally. |
5459 | calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally. |
|
|
5460 | .IP "null pointers and integer zero are represented by 0 bytes" 4 |
|
|
5461 | .IX Item "null pointers and integer zero are represented by 0 bytes" |
|
|
5462 | Libev uses \f(CW\*(C`memset\*(C'\fR to initialise structs and arrays to \f(CW0\fR bytes, and |
|
|
5463 | relies on this setting pointers and integers to null. |
5417 | .IP "pointer accesses must be thread-atomic" 4 |
5464 | .IP "pointer accesses must be thread-atomic" 4 |
5418 | .IX Item "pointer accesses must be thread-atomic" |
5465 | .IX Item "pointer accesses must be thread-atomic" |
5419 | Accessing a pointer value must be atomic, it must both be readable and |
5466 | Accessing a pointer value must be atomic, it must both be readable and |
5420 | writable in one piece \- this is the case on all current architectures. |
5467 | writable in one piece \- this is the case on all current architectures. |
5421 | .ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4 |
5468 | .ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4 |