… | |
… | |
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-07-05" "libev-3.43" "libev - high performance full featured event loop" |
135 | .TH LIBEV 3 "2008-09-29" "libev-3.44" "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" |
… | |
… | |
344 | might be supported on the current system, you would need to look at |
344 | might be supported on the current system, you would need to look at |
345 | \&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for |
345 | \&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for |
346 | recommended ones. |
346 | recommended ones. |
347 | .Sp |
347 | .Sp |
348 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
348 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
349 | .IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 |
349 | .IP "ev_set_allocator (void *(*cb)(void *ptr, long size)) [\s-1NOT\s0 \s-1REENTRANT\s0]" 4 |
350 | .IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" |
350 | .IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size)) [NOT REENTRANT]" |
351 | Sets the allocation function to use (the prototype is similar \- the |
351 | Sets the allocation function to use (the prototype is similar \- the |
352 | semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is |
352 | semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is |
353 | used to allocate and free memory (no surprises here). If it returns zero |
353 | used to allocate and free memory (no surprises here). If it returns zero |
354 | when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort |
354 | when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort |
355 | or take some potentially destructive action. |
355 | or take some potentially destructive action. |
… | |
… | |
381 | \& } |
381 | \& } |
382 | \& |
382 | \& |
383 | \& ... |
383 | \& ... |
384 | \& ev_set_allocator (persistent_realloc); |
384 | \& ev_set_allocator (persistent_realloc); |
385 | .Ve |
385 | .Ve |
386 | .IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4 |
386 | .IP "ev_set_syserr_cb (void (*cb)(const char *msg)); [\s-1NOT\s0 \s-1REENTRANT\s0]" 4 |
387 | .IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));" |
387 | .IX Item "ev_set_syserr_cb (void (*cb)(const char *msg)); [NOT REENTRANT]" |
388 | Set the callback function to call on a retryable system call error (such |
388 | Set the callback function to call on a retryable system call error (such |
389 | as failed select, poll, epoll_wait). The message is a printable string |
389 | as failed select, poll, epoll_wait). The message is a printable string |
390 | indicating the system call or subsystem causing the problem. If this |
390 | indicating the system call or subsystem causing the problem. If this |
391 | callback is set, then libev will expect it to remedy the situation, no |
391 | callback is set, then libev will expect it to remedy the situation, no |
392 | matter what, when it returns. That is, libev will generally retry the |
392 | matter what, when it returns. That is, libev will generally retry the |
… | |
… | |
484 | parallelism (most of the file descriptors should be busy). If you are |
484 | parallelism (most of the file descriptors should be busy). If you are |
485 | 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 |
486 | 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 |
487 | 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 |
488 | readiness notifications you get per iteration. |
488 | readiness notifications you get per iteration. |
|
|
489 | .Sp |
|
|
490 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR to the \f(CW\*(C`readfds\*(C'\fR set and \f(CW\*(C`EV_WRITE\*(C'\fR to the |
|
|
491 | \&\f(CW\*(C`writefds\*(C'\fR set (and to work around Microsoft Windows bugs, also onto the |
|
|
492 | \&\f(CW\*(C`exceptfds\*(C'\fR set on that platform). |
489 | .ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 |
493 | .ie n .IP """EVBACKEND_POLL"" (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 |
494 | .el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 |
491 | .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" |
495 | .IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" |
492 | And this is your standard \fIpoll\fR\|(2) backend. It's more complicated |
496 | And this is your standard \fIpoll\fR\|(2) backend. It's more complicated |
493 | than select, but handles sparse fds better and has no artificial |
497 | than select, but handles sparse fds better and has no artificial |
494 | limit on the number of fds you can use (except it will slow down |
498 | limit on the number of fds you can use (except it will slow down |
495 | considerably with a lot of inactive fds). It scales similarly to select, |
499 | considerably with a lot of inactive fds). It scales similarly to select, |
496 | i.e. O(total_fds). See the entry for \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR, above, for |
500 | i.e. O(total_fds). See the entry for \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR, above, for |
497 | performance tips. |
501 | performance tips. |
|
|
502 | .Sp |
|
|
503 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR to \f(CW\*(C`POLLIN | POLLERR | POLLHUP\*(C'\fR, and |
|
|
504 | \&\f(CW\*(C`EV_WRITE\*(C'\fR to \f(CW\*(C`POLLOUT | POLLERR | POLLHUP\*(C'\fR. |
498 | .ie n .IP """EVBACKEND_EPOLL"" (value 4, Linux)" 4 |
505 | .ie n .IP """EVBACKEND_EPOLL"" (value 4, Linux)" 4 |
499 | .el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4 |
506 | .el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4 |
500 | .IX Item "EVBACKEND_EPOLL (value 4, Linux)" |
507 | .IX Item "EVBACKEND_EPOLL (value 4, Linux)" |
501 | For few fds, this backend is a bit little slower than poll and select, |
508 | For few fds, this backend is a bit little slower than poll and select, |
502 | but it scales phenomenally better. While poll and select usually scale |
509 | but it scales phenomenally better. While poll and select usually scale |
… | |
… | |
515 | Please note that epoll sometimes generates spurious notifications, so you |
522 | Please note that epoll sometimes generates spurious notifications, so you |
516 | need to use non-blocking I/O or other means to avoid blocking when no data |
523 | need to use non-blocking I/O or other means to avoid blocking when no data |
517 | (or space) is available. |
524 | (or space) is available. |
518 | .Sp |
525 | .Sp |
519 | Best performance from this backend is achieved by not unregistering all |
526 | Best performance from this backend is achieved by not unregistering all |
520 | watchers for a file descriptor until it has been closed, if possible, i.e. |
527 | watchers for a file descriptor until it has been closed, if possible, |
521 | keep at least one watcher active per fd at all times. |
528 | i.e. keep at least one watcher active per fd at all times. Stopping and |
|
|
529 | starting a watcher (without re-setting it) also usually doesn't cause |
|
|
530 | extra overhead. |
522 | .Sp |
531 | .Sp |
523 | While nominally embeddable in other event loops, this feature is broken in |
532 | While nominally embeddable in other event loops, this feature is broken in |
524 | all kernel versions tested so far. |
533 | all kernel versions tested so far. |
|
|
534 | .Sp |
|
|
535 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as |
|
|
536 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
525 | .ie n .IP """EVBACKEND_KQUEUE"" (value 8, most \s-1BSD\s0 clones)" 4 |
537 | .ie n .IP """EVBACKEND_KQUEUE"" (value 8, most \s-1BSD\s0 clones)" 4 |
526 | .el .IP "\f(CWEVBACKEND_KQUEUE\fR (value 8, most \s-1BSD\s0 clones)" 4 |
538 | .el .IP "\f(CWEVBACKEND_KQUEUE\fR (value 8, most \s-1BSD\s0 clones)" 4 |
527 | .IX Item "EVBACKEND_KQUEUE (value 8, most BSD clones)" |
539 | .IX Item "EVBACKEND_KQUEUE (value 8, most BSD clones)" |
528 | Kqueue deserves special mention, as at the time of this writing, it |
540 | Kqueue deserves special mention, as at the time of this writing, it was |
529 | was broken on all BSDs except NetBSD (usually it doesn't work reliably |
541 | broken on all BSDs except NetBSD (usually it doesn't work reliably with |
530 | with anything but sockets and pipes, except on Darwin, where of course |
542 | anything but sockets and pipes, except on Darwin, where of course it's |
531 | it's completely useless). For this reason it's not being \*(L"auto-detected\*(R" |
543 | completely useless). For this reason it's not being \*(L"auto-detected\*(R" unless |
532 | unless you explicitly specify it explicitly in the flags (i.e. using |
544 | you explicitly specify it in the flags (i.e. using \f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR) or |
533 | \&\f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR) or libev was compiled on a known-to-be-good (\-enough) |
545 | libev was compiled on a known-to-be-good (\-enough) system like NetBSD. |
534 | system like NetBSD. |
|
|
535 | .Sp |
546 | .Sp |
536 | You still can embed kqueue into a normal poll or select backend and use it |
547 | You still can embed kqueue into a normal poll or select backend and use it |
537 | only for sockets (after having made sure that sockets work with kqueue on |
548 | only for sockets (after having made sure that sockets work with kqueue on |
538 | the target platform). See \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
549 | the target platform). See \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
539 | .Sp |
550 | .Sp |
540 | It scales in the same way as the epoll backend, but the interface to the |
551 | It scales in the same way as the epoll backend, but the interface to the |
541 | kernel is more efficient (which says nothing about its actual speed, of |
552 | kernel is more efficient (which says nothing about its actual speed, of |
542 | course). While stopping, setting and starting an I/O watcher does never |
553 | course). While stopping, setting and starting an I/O watcher does never |
543 | cause an extra system call as with \f(CW\*(C`EVBACKEND_EPOLL\*(C'\fR, it still adds up to |
554 | cause an extra system call as with \f(CW\*(C`EVBACKEND_EPOLL\*(C'\fR, it still adds up to |
544 | two event changes per incident, support for \f(CW\*(C`fork ()\*(C'\fR is very bad and it |
555 | two event changes per incident. Support for \f(CW\*(C`fork ()\*(C'\fR is very bad and it |
545 | drops fds silently in similarly hard-to-detect cases. |
556 | drops fds silently in similarly hard-to-detect cases. |
546 | .Sp |
557 | .Sp |
547 | This backend usually performs well under most conditions. |
558 | This backend usually performs well under most conditions. |
548 | .Sp |
559 | .Sp |
549 | While nominally embeddable in other event loops, this doesn't work |
560 | While nominally embeddable in other event loops, this doesn't work |
550 | everywhere, so you might need to test for this. And since it is broken |
561 | everywhere, so you might need to test for this. And since it is broken |
551 | almost everywhere, you should only use it when you have a lot of sockets |
562 | almost everywhere, you should only use it when you have a lot of sockets |
552 | (for which it usually works), by embedding it into another event loop |
563 | (for which it usually works), by embedding it into another event loop |
553 | (e.g. \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR) and using it only for |
564 | (e.g. \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR) and, did I mention it, |
554 | sockets. |
565 | using it only for sockets. |
|
|
566 | .Sp |
|
|
567 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR into an \f(CW\*(C`EVFILT_READ\*(C'\fR kevent with |
|
|
568 | \&\f(CW\*(C`NOTE_EOF\*(C'\fR, and \f(CW\*(C`EV_WRITE\*(C'\fR into an \f(CW\*(C`EVFILT_WRITE\*(C'\fR kevent with |
|
|
569 | \&\f(CW\*(C`NOTE_EOF\*(C'\fR. |
555 | .ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4 |
570 | .ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4 |
556 | .el .IP "\f(CWEVBACKEND_DEVPOLL\fR (value 16, Solaris 8)" 4 |
571 | .el .IP "\f(CWEVBACKEND_DEVPOLL\fR (value 16, Solaris 8)" 4 |
557 | .IX Item "EVBACKEND_DEVPOLL (value 16, Solaris 8)" |
572 | .IX Item "EVBACKEND_DEVPOLL (value 16, Solaris 8)" |
558 | This is not implemented yet (and might never be, unless you send me an |
573 | This is not implemented yet (and might never be, unless you send me an |
559 | implementation). According to reports, \f(CW\*(C`/dev/poll\*(C'\fR only supports sockets |
574 | implementation). According to reports, \f(CW\*(C`/dev/poll\*(C'\fR only supports sockets |
… | |
… | |
572 | While this backend scales well, it requires one system call per active |
587 | While this backend scales well, it requires one system call per active |
573 | file descriptor per loop iteration. For small and medium numbers of file |
588 | file descriptor per loop iteration. For small and medium numbers of file |
574 | descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend |
589 | descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend |
575 | might perform better. |
590 | might perform better. |
576 | .Sp |
591 | .Sp |
577 | On the positive side, ignoring the spurious readiness notifications, this |
592 | On the positive side, with the exception of the spurious readiness |
578 | backend actually performed to specification in all tests and is fully |
593 | notifications, this backend actually performed fully to specification |
579 | embeddable, which is a rare feat among the OS-specific backends. |
594 | in all tests and is fully embeddable, which is a rare feat among the |
|
|
595 | OS-specific backends. |
|
|
596 | .Sp |
|
|
597 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as |
|
|
598 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
580 | .ie n .IP """EVBACKEND_ALL""" 4 |
599 | .ie n .IP """EVBACKEND_ALL""" 4 |
581 | .el .IP "\f(CWEVBACKEND_ALL\fR" 4 |
600 | .el .IP "\f(CWEVBACKEND_ALL\fR" 4 |
582 | .IX Item "EVBACKEND_ALL" |
601 | .IX Item "EVBACKEND_ALL" |
583 | Try all backends (even potentially broken ones that wouldn't be tried |
602 | Try all backends (even potentially broken ones that wouldn't be tried |
584 | with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as |
603 | with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as |
… | |
… | |
590 | .Sp |
609 | .Sp |
591 | If one or more of these are or'ed into the flags value, then only these |
610 | If one or more of these are or'ed into the flags value, then only these |
592 | backends will be tried (in the reverse order as listed here). If none are |
611 | backends will be tried (in the reverse order as listed here). If none are |
593 | specified, all backends in \f(CW\*(C`ev_recommended_backends ()\*(C'\fR will be tried. |
612 | specified, all backends in \f(CW\*(C`ev_recommended_backends ()\*(C'\fR will be tried. |
594 | .Sp |
613 | .Sp |
595 | The most typical usage is like this: |
614 | Example: This is the most typical usage. |
596 | .Sp |
615 | .Sp |
597 | .Vb 2 |
616 | .Vb 2 |
598 | \& if (!ev_default_loop (0)) |
617 | \& if (!ev_default_loop (0)) |
599 | \& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); |
618 | \& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); |
600 | .Ve |
619 | .Ve |
601 | .Sp |
620 | .Sp |
602 | Restrict libev to the select and poll backends, and do not allow |
621 | Example: Restrict libev to the select and poll backends, and do not allow |
603 | environment settings to be taken into account: |
622 | environment settings to be taken into account: |
604 | .Sp |
623 | .Sp |
605 | .Vb 1 |
624 | .Vb 1 |
606 | \& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); |
625 | \& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); |
607 | .Ve |
626 | .Ve |
608 | .Sp |
627 | .Sp |
609 | Use whatever libev has to offer, but make sure that kqueue is used if |
628 | Example: Use whatever libev has to offer, but make sure that kqueue is |
610 | available (warning, breaks stuff, best use only with your own private |
629 | used if available (warning, breaks stuff, best use only with your own |
611 | event loop and only if you know the \s-1OS\s0 supports your types of fds): |
630 | private event loop and only if you know the \s-1OS\s0 supports your types of |
|
|
631 | fds): |
612 | .Sp |
632 | .Sp |
613 | .Vb 1 |
633 | .Vb 1 |
614 | \& ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); |
634 | \& ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); |
615 | .Ve |
635 | .Ve |
616 | .RE |
636 | .RE |
… | |
… | |
676 | .Ve |
696 | .Ve |
677 | .IP "ev_loop_fork (loop)" 4 |
697 | .IP "ev_loop_fork (loop)" 4 |
678 | .IX Item "ev_loop_fork (loop)" |
698 | .IX Item "ev_loop_fork (loop)" |
679 | Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by |
699 | Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by |
680 | \&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop |
700 | \&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop |
681 | after fork, and how you do this is entirely your own problem. |
701 | after fork that you want to re-use in the child, and how you do this is |
|
|
702 | entirely your own problem. |
682 | .IP "int ev_is_default_loop (loop)" 4 |
703 | .IP "int ev_is_default_loop (loop)" 4 |
683 | .IX Item "int ev_is_default_loop (loop)" |
704 | .IX Item "int ev_is_default_loop (loop)" |
684 | Returns true when the given loop actually is the default loop, false otherwise. |
705 | Returns true when the given loop is, in fact, the default loop, and false |
|
|
706 | otherwise. |
685 | .IP "unsigned int ev_loop_count (loop)" 4 |
707 | .IP "unsigned int ev_loop_count (loop)" 4 |
686 | .IX Item "unsigned int ev_loop_count (loop)" |
708 | .IX Item "unsigned int ev_loop_count (loop)" |
687 | Returns the count of loop iterations for the loop, which is identical to |
709 | Returns the count of loop iterations for the loop, which is identical to |
688 | the number of times libev did poll for new events. It starts at \f(CW0\fR and |
710 | the number of times libev did poll for new events. It starts at \f(CW0\fR and |
689 | happily wraps around with enough iterations. |
711 | happily wraps around with enough iterations. |
… | |
… | |
700 | Returns the current \*(L"event loop time\*(R", which is the time the event loop |
722 | Returns the current \*(L"event loop time\*(R", which is the time the event loop |
701 | received events and started processing them. This timestamp does not |
723 | received events and started processing them. This timestamp does not |
702 | change as long as callbacks are being processed, and this is also the base |
724 | change as long as callbacks are being processed, and this is also the base |
703 | time used for relative timers. You can treat it as the timestamp of the |
725 | time used for relative timers. You can treat it as the timestamp of the |
704 | event occurring (or more correctly, libev finding out about it). |
726 | event occurring (or more correctly, libev finding out about it). |
|
|
727 | .IP "ev_now_update (loop)" 4 |
|
|
728 | .IX Item "ev_now_update (loop)" |
|
|
729 | Establishes the current time by querying the kernel, updating the time |
|
|
730 | returned by \f(CW\*(C`ev_now ()\*(C'\fR in the progress. This is a costly operation and |
|
|
731 | is usually done automatically within \f(CW\*(C`ev_loop ()\*(C'\fR. |
|
|
732 | .Sp |
|
|
733 | This function is rarely useful, but when some event callback runs for a |
|
|
734 | very long time without entering the event loop, updating libev's idea of |
|
|
735 | the current time is a good idea. |
|
|
736 | .Sp |
|
|
737 | See also \*(L"The special problem of time updates\*(R" in the \f(CW\*(C`ev_timer\*(C'\fR section. |
705 | .IP "ev_loop (loop, int flags)" 4 |
738 | .IP "ev_loop (loop, int flags)" 4 |
706 | .IX Item "ev_loop (loop, int flags)" |
739 | .IX Item "ev_loop (loop, int flags)" |
707 | Finally, this is it, the event handler. This function usually is called |
740 | Finally, this is it, the event handler. This function usually is called |
708 | after you initialised all your watchers and you want to start handling |
741 | after you initialised all your watchers and you want to start handling |
709 | events. |
742 | events. |
… | |
… | |
711 | If the flags argument is specified as \f(CW0\fR, it will not return until |
744 | If the flags argument is specified as \f(CW0\fR, it will not return until |
712 | either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called. |
745 | either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called. |
713 | .Sp |
746 | .Sp |
714 | Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than |
747 | Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than |
715 | relying on all watchers to be stopped when deciding when a program has |
748 | relying on all watchers to be stopped when deciding when a program has |
716 | finished (especially in interactive programs), but having a program that |
749 | finished (especially in interactive programs), but having a program |
717 | automatically loops as long as it has to and no longer by virtue of |
750 | that automatically loops as long as it has to and no longer by virtue |
718 | relying on its watchers stopping correctly is a thing of beauty. |
751 | of relying on its watchers stopping correctly, that is truly a thing of |
|
|
752 | beauty. |
719 | .Sp |
753 | .Sp |
720 | A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle |
754 | A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle |
721 | those events and any outstanding ones, but will not block your process in |
755 | those events and any already outstanding ones, but will not block your |
722 | case there are no events and will return after one iteration of the loop. |
756 | process in case there are no events and will return after one iteration of |
|
|
757 | the loop. |
723 | .Sp |
758 | .Sp |
724 | A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if |
759 | A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if |
725 | necessary) and will handle those and any outstanding ones. It will block |
760 | necessary) and will handle those and any already outstanding ones. It |
726 | your process until at least one new event arrives, and will return after |
761 | will block your process until at least one new event arrives (which could |
727 | one iteration of the loop. This is useful if you are waiting for some |
762 | be an event internal to libev itself, so there is no guarentee that a |
728 | external event in conjunction with something not expressible using other |
763 | user-registered callback will be called), and will return after one |
|
|
764 | iteration of the loop. |
|
|
765 | .Sp |
|
|
766 | This is useful if you are waiting for some external event in conjunction |
|
|
767 | with something not expressible using other libev watchers (i.e. "roll your |
729 | libev watchers. However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is |
768 | own \f(CW\*(C`ev_loop\*(C'\fR"). However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is |
730 | usually a better approach for this kind of thing. |
769 | usually a better approach for this kind of thing. |
731 | .Sp |
770 | .Sp |
732 | Here are the gory details of what \f(CW\*(C`ev_loop\*(C'\fR does: |
771 | Here are the gory details of what \f(CW\*(C`ev_loop\*(C'\fR does: |
733 | .Sp |
772 | .Sp |
734 | .Vb 10 |
773 | .Vb 10 |
735 | \& \- Before the first iteration, call any pending watchers. |
774 | \& \- Before the first iteration, call any pending watchers. |
736 | \& * If EVFLAG_FORKCHECK was used, check for a fork. |
775 | \& * If EVFLAG_FORKCHECK was used, check for a fork. |
737 | \& \- If a fork was detected, queue and call all fork watchers. |
776 | \& \- If a fork was detected (by any means), queue and call all fork watchers. |
738 | \& \- Queue and call all prepare watchers. |
777 | \& \- Queue and call all prepare watchers. |
739 | \& \- If we have been forked, recreate the kernel state. |
778 | \& \- If we have been forked, detach and recreate the kernel state |
|
|
779 | \& as to not disturb the other process. |
740 | \& \- Update the kernel state with all outstanding changes. |
780 | \& \- Update the kernel state with all outstanding changes. |
741 | \& \- Update the "event loop time". |
781 | \& \- Update the "event loop time" (ev_now ()). |
742 | \& \- Calculate for how long to sleep or block, if at all |
782 | \& \- Calculate for how long to sleep or block, if at all |
743 | \& (active idle watchers, EVLOOP_NONBLOCK or not having |
783 | \& (active idle watchers, EVLOOP_NONBLOCK or not having |
744 | \& any active watchers at all will result in not sleeping). |
784 | \& any active watchers at all will result in not sleeping). |
745 | \& \- Sleep if the I/O and timer collect interval say so. |
785 | \& \- Sleep if the I/O and timer collect interval say so. |
746 | \& \- Block the process, waiting for any events. |
786 | \& \- Block the process, waiting for any events. |
747 | \& \- Queue all outstanding I/O (fd) events. |
787 | \& \- Queue all outstanding I/O (fd) events. |
748 | \& \- Update the "event loop time" and do time jump handling. |
788 | \& \- Update the "event loop time" (ev_now ()), and do time jump adjustments. |
749 | \& \- Queue all outstanding timers. |
789 | \& \- Queue all expired timers. |
750 | \& \- Queue all outstanding periodics. |
790 | \& \- Queue all expired periodics. |
751 | \& \- If no events are pending now, queue all idle watchers. |
791 | \& \- Unless any events are pending now, queue all idle watchers. |
752 | \& \- Queue all check watchers. |
792 | \& \- Queue all check watchers. |
753 | \& \- Call all queued watchers in reverse order (i.e. check watchers first). |
793 | \& \- Call all queued watchers in reverse order (i.e. check watchers first). |
754 | \& Signals and child watchers are implemented as I/O watchers, and will |
794 | \& Signals and child watchers are implemented as I/O watchers, and will |
755 | \& be handled here by queueing them when their watcher gets executed. |
795 | \& be handled here by queueing them when their watcher gets executed. |
756 | \& \- If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
796 | \& \- If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
… | |
… | |
763 | .Sp |
803 | .Sp |
764 | .Vb 4 |
804 | .Vb 4 |
765 | \& ... queue jobs here, make sure they register event watchers as long |
805 | \& ... queue jobs here, make sure they register event watchers as long |
766 | \& ... as they still have work to do (even an idle watcher will do..) |
806 | \& ... as they still have work to do (even an idle watcher will do..) |
767 | \& ev_loop (my_loop, 0); |
807 | \& ev_loop (my_loop, 0); |
768 | \& ... jobs done. yeah! |
808 | \& ... jobs done or somebody called unloop. yeah! |
769 | .Ve |
809 | .Ve |
770 | .IP "ev_unloop (loop, how)" 4 |
810 | .IP "ev_unloop (loop, how)" 4 |
771 | .IX Item "ev_unloop (loop, how)" |
811 | .IX Item "ev_unloop (loop, how)" |
772 | Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it |
812 | Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it |
773 | has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either |
813 | has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either |
… | |
… | |
781 | .IP "ev_unref (loop)" 4 |
821 | .IP "ev_unref (loop)" 4 |
782 | .IX Item "ev_unref (loop)" |
822 | .IX Item "ev_unref (loop)" |
783 | .PD |
823 | .PD |
784 | Ref/unref can be used to add or remove a reference count on the event |
824 | Ref/unref can be used to add or remove a reference count on the event |
785 | loop: Every watcher keeps one reference, and as long as the reference |
825 | loop: Every watcher keeps one reference, and as long as the reference |
786 | count is nonzero, \f(CW\*(C`ev_loop\*(C'\fR will not return on its own. If you have |
826 | count is nonzero, \f(CW\*(C`ev_loop\*(C'\fR will not return on its own. |
|
|
827 | .Sp |
787 | a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR from |
828 | If you have a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR |
788 | returning, \fIev_unref()\fR after starting, and \fIev_ref()\fR before stopping it. For |
829 | from returning, call \fIev_unref()\fR after starting, and \fIev_ref()\fR before |
|
|
830 | stopping it. |
|
|
831 | .Sp |
789 | example, libev itself uses this for its internal signal pipe: It is not |
832 | As an example, libev itself uses this for its internal signal pipe: It is |
790 | visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if |
833 | not visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting |
791 | no event watchers registered by it are active. It is also an excellent |
834 | if no event watchers registered by it are active. It is also an excellent |
792 | way to do this for generic recurring timers or from within third-party |
835 | way to do this for generic recurring timers or from within third-party |
793 | libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR |
836 | libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR |
794 | (but only if the watcher wasn't active before, or was active before, |
837 | (but only if the watcher wasn't active before, or was active before, |
795 | respectively). |
838 | respectively). |
796 | .Sp |
839 | .Sp |
… | |
… | |
815 | .PD 0 |
858 | .PD 0 |
816 | .IP "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 4 |
859 | .IP "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 4 |
817 | .IX Item "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" |
860 | .IX Item "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" |
818 | .PD |
861 | .PD |
819 | These advanced functions influence the time that libev will spend waiting |
862 | These advanced functions influence the time that libev will spend waiting |
820 | for events. Both are by default \f(CW0\fR, meaning that libev will try to |
863 | for events. Both time intervals are by default \f(CW0\fR, meaning that libev |
821 | invoke timer/periodic callbacks and I/O callbacks with minimum latency. |
864 | will try to invoke timer/periodic callbacks and I/O callbacks with minimum |
|
|
865 | latency. |
822 | .Sp |
866 | .Sp |
823 | Setting these to a higher value (the \f(CW\*(C`interval\*(C'\fR \fImust\fR be >= \f(CW0\fR) |
867 | Setting these to a higher value (the \f(CW\*(C`interval\*(C'\fR \fImust\fR be >= \f(CW0\fR) |
824 | allows libev to delay invocation of I/O and timer/periodic callbacks to |
868 | allows libev to delay invocation of I/O and timer/periodic callbacks |
825 | increase efficiency of loop iterations. |
869 | to increase efficiency of loop iterations (or to increase power-saving |
|
|
870 | opportunities). |
826 | .Sp |
871 | .Sp |
827 | The background is that sometimes your program runs just fast enough to |
872 | The idea is that sometimes your program runs just fast enough to handle |
828 | handle one (or very few) event(s) per loop iteration. While this makes |
873 | one (or very few) event(s) per loop iteration. While this makes the |
829 | the program responsive, it also wastes a lot of \s-1CPU\s0 time to poll for new |
874 | program responsive, it also wastes a lot of \s-1CPU\s0 time to poll for new |
830 | events, especially with backends like \f(CW\*(C`select ()\*(C'\fR which have a high |
875 | events, especially with backends like \f(CW\*(C`select ()\*(C'\fR which have a high |
831 | overhead for the actual polling but can deliver many events at once. |
876 | overhead for the actual polling but can deliver many events at once. |
832 | .Sp |
877 | .Sp |
833 | By setting a higher \fIio collect interval\fR you allow libev to spend more |
878 | By setting a higher \fIio collect interval\fR you allow libev to spend more |
834 | time collecting I/O events, so you can handle more events per iteration, |
879 | time collecting I/O events, so you can handle more events per iteration, |
… | |
… | |
836 | \&\f(CW\*(C`ev_timer\*(C'\fR) will be not affected. Setting this to a non-null value will |
881 | \&\f(CW\*(C`ev_timer\*(C'\fR) will be not affected. Setting this to a non-null value will |
837 | introduce an additional \f(CW\*(C`ev_sleep ()\*(C'\fR call into most loop iterations. |
882 | introduce an additional \f(CW\*(C`ev_sleep ()\*(C'\fR call into most loop iterations. |
838 | .Sp |
883 | .Sp |
839 | Likewise, by setting a higher \fItimeout collect interval\fR you allow libev |
884 | Likewise, by setting a higher \fItimeout collect interval\fR you allow libev |
840 | to spend more time collecting timeouts, at the expense of increased |
885 | to spend more time collecting timeouts, at the expense of increased |
841 | latency (the watcher callback will be called later). \f(CW\*(C`ev_io\*(C'\fR watchers |
886 | latency/jitter/inexactness (the watcher callback will be called |
842 | will not be affected. Setting this to a non-null value will not introduce |
887 | later). \f(CW\*(C`ev_io\*(C'\fR watchers will not be affected. Setting this to a non-null |
843 | any overhead in libev. |
888 | value will not introduce any overhead in libev. |
844 | .Sp |
889 | .Sp |
845 | Many (busy) programs can usually benefit by setting the I/O collect |
890 | Many (busy) programs can usually benefit by setting the I/O collect |
846 | interval to a value near \f(CW0.1\fR or so, which is often enough for |
891 | interval to a value near \f(CW0.1\fR or so, which is often enough for |
847 | interactive servers (of course not for games), likewise for timeouts. It |
892 | interactive servers (of course not for games), likewise for timeouts. It |
848 | usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR, |
893 | usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR, |
849 | as this approaches the timing granularity of most systems. |
894 | as this approaches the timing granularity of most systems. |
|
|
895 | .Sp |
|
|
896 | Setting the \fItimeout collect interval\fR can improve the opportunity for |
|
|
897 | saving power, as the program will \*(L"bundle\*(R" timer callback invocations that |
|
|
898 | are \*(L"near\*(R" in time together, by delaying some, thus reducing the number of |
|
|
899 | times the process sleeps and wakes up again. Another useful technique to |
|
|
900 | reduce iterations/wake\-ups is to use \f(CW\*(C`ev_periodic\*(C'\fR watchers and make sure |
|
|
901 | they fire on, say, one-second boundaries only. |
850 | .IP "ev_loop_verify (loop)" 4 |
902 | .IP "ev_loop_verify (loop)" 4 |
851 | .IX Item "ev_loop_verify (loop)" |
903 | .IX Item "ev_loop_verify (loop)" |
852 | This function only does something when \f(CW\*(C`EV_VERIFY\*(C'\fR support has been |
904 | 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 |
905 | compiled in. which is the default for non-minimal builds. It tries to go |
854 | them for validity. If anything is found to be inconsistent, it will print |
906 | through all internal structures and checks them for validity. If anything |
855 | an error message to standard error and call \f(CW\*(C`abort ()\*(C'\fR. |
907 | is found to be inconsistent, it will print an error message to standard |
|
|
908 | error and call \f(CW\*(C`abort ()\*(C'\fR. |
856 | .Sp |
909 | .Sp |
857 | This can be used to catch bugs inside libev itself: under normal |
910 | 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 |
911 | circumstances, this function will never abort as of course libev keeps its |
859 | data structures consistent. |
912 | data structures consistent. |
860 | .SH "ANATOMY OF A WATCHER" |
913 | .SH "ANATOMY OF A WATCHER" |
… | |
… | |
978 | happen because the watcher could not be properly started because libev |
1031 | happen because the watcher could not be properly started because libev |
979 | ran out of memory, a file descriptor was found to be closed or any other |
1032 | ran out of memory, a file descriptor was found to be closed or any other |
980 | problem. You best act on it by reporting the problem and somehow coping |
1033 | problem. You best act on it by reporting the problem and somehow coping |
981 | with the watcher being stopped. |
1034 | with the watcher being stopped. |
982 | .Sp |
1035 | .Sp |
983 | Libev will usually signal a few \*(L"dummy\*(R" events together with an error, |
1036 | Libev will usually signal a few \*(L"dummy\*(R" events together with an error, for |
984 | for example it might indicate that a fd is readable or writable, and if |
1037 | example it might indicate that a fd is readable or writable, and if your |
985 | your callbacks is well-written it can just attempt the operation and cope |
1038 | callbacks is well-written it can just attempt the operation and cope with |
986 | with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multi-threaded |
1039 | the error from \fIread()\fR or \fIwrite()\fR. This will not work in multi-threaded |
987 | programs, though, so beware. |
1040 | programs, though, as the fd could already be closed and reused for another |
|
|
1041 | thing, so beware. |
988 | .Sh "\s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0" |
1042 | .Sh "\s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0" |
989 | .IX Subsection "GENERIC WATCHER FUNCTIONS" |
1043 | .IX Subsection "GENERIC WATCHER FUNCTIONS" |
990 | In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type, |
1044 | In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type, |
991 | e.g. \f(CW\*(C`timer\*(C'\fR for \f(CW\*(C`ev_timer\*(C'\fR watchers and \f(CW\*(C`io\*(C'\fR for \f(CW\*(C`ev_io\*(C'\fR watchers. |
1045 | e.g. \f(CW\*(C`timer\*(C'\fR for \f(CW\*(C`ev_timer\*(C'\fR watchers and \f(CW\*(C`io\*(C'\fR for \f(CW\*(C`ev_io\*(C'\fR watchers. |
992 | .ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4 |
1046 | .ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4 |
… | |
… | |
1002 | You can reinitialise a watcher at any time as long as it has been stopped |
1056 | You can reinitialise a watcher at any time as long as it has been stopped |
1003 | (or never started) and there are no pending events outstanding. |
1057 | (or never started) and there are no pending events outstanding. |
1004 | .Sp |
1058 | .Sp |
1005 | The callback is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher, |
1059 | The callback is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher, |
1006 | int revents)\*(C'\fR. |
1060 | int revents)\*(C'\fR. |
|
|
1061 | .Sp |
|
|
1062 | Example: Initialise an \f(CW\*(C`ev_io\*(C'\fR watcher in two steps. |
|
|
1063 | .Sp |
|
|
1064 | .Vb 3 |
|
|
1065 | \& ev_io w; |
|
|
1066 | \& ev_init (&w, my_cb); |
|
|
1067 | \& ev_io_set (&w, STDIN_FILENO, EV_READ); |
|
|
1068 | .Ve |
1007 | .ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4 |
1069 | .ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4 |
1008 | .el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4 |
1070 | .el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4 |
1009 | .IX Item "ev_TYPE_set (ev_TYPE *, [args])" |
1071 | .IX Item "ev_TYPE_set (ev_TYPE *, [args])" |
1010 | This macro initialises the type-specific parts of a watcher. You need to |
1072 | This macro initialises the type-specific parts of a watcher. You need to |
1011 | call \f(CW\*(C`ev_init\*(C'\fR at least once before you call this macro, but you can |
1073 | call \f(CW\*(C`ev_init\*(C'\fR at least once before you call this macro, but you can |
… | |
… | |
1013 | macro on a watcher that is active (it can be pending, however, which is a |
1075 | macro on a watcher that is active (it can be pending, however, which is a |
1014 | difference to the \f(CW\*(C`ev_init\*(C'\fR macro). |
1076 | difference to the \f(CW\*(C`ev_init\*(C'\fR macro). |
1015 | .Sp |
1077 | .Sp |
1016 | Although some watcher types do not have type-specific arguments |
1078 | Although some watcher types do not have type-specific arguments |
1017 | (e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro. |
1079 | (e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro. |
|
|
1080 | .Sp |
|
|
1081 | See \f(CW\*(C`ev_init\*(C'\fR, above, for an example. |
1018 | .ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4 |
1082 | .ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4 |
1019 | .el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4 |
1083 | .el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4 |
1020 | .IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])" |
1084 | .IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])" |
1021 | This convenience macro rolls both \f(CW\*(C`ev_init\*(C'\fR and \f(CW\*(C`ev_TYPE_set\*(C'\fR macro |
1085 | This convenience macro rolls both \f(CW\*(C`ev_init\*(C'\fR and \f(CW\*(C`ev_TYPE_set\*(C'\fR macro |
1022 | calls into a single call. This is the most convenient method to initialise |
1086 | calls into a single call. This is the most convenient method to initialise |
1023 | a watcher. The same limitations apply, of course. |
1087 | a watcher. The same limitations apply, of course. |
|
|
1088 | .Sp |
|
|
1089 | Example: Initialise and set an \f(CW\*(C`ev_io\*(C'\fR watcher in one step. |
|
|
1090 | .Sp |
|
|
1091 | .Vb 1 |
|
|
1092 | \& ev_io_init (&w, my_cb, STDIN_FILENO, EV_READ); |
|
|
1093 | .Ve |
1024 | .ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4 |
1094 | .ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4 |
1025 | .el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4 |
1095 | .el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4 |
1026 | .IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)" |
1096 | .IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)" |
1027 | Starts (activates) the given watcher. Only active watchers will receive |
1097 | Starts (activates) the given watcher. Only active watchers will receive |
1028 | events. If the watcher is already active nothing will happen. |
1098 | events. If the watcher is already active nothing will happen. |
|
|
1099 | .Sp |
|
|
1100 | Example: Start the \f(CW\*(C`ev_io\*(C'\fR watcher that is being abused as example in this |
|
|
1101 | whole section. |
|
|
1102 | .Sp |
|
|
1103 | .Vb 1 |
|
|
1104 | \& ev_io_start (EV_DEFAULT_UC, &w); |
|
|
1105 | .Ve |
1029 | .ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4 |
1106 | .ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4 |
1030 | .el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4 |
1107 | .el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4 |
1031 | .IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)" |
1108 | .IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)" |
1032 | Stops the given watcher again (if active) and clears the pending |
1109 | Stops the given watcher again (if active) and clears the pending |
1033 | status. It is possible that stopped watchers are pending (for example, |
1110 | status. It is possible that stopped watchers are pending (for example, |
… | |
… | |
1086 | or might not have been adjusted to be within valid range. |
1163 | or might not have been adjusted to be within valid range. |
1087 | .IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4 |
1164 | .IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4 |
1088 | .IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)" |
1165 | .IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)" |
1089 | 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 |
1166 | 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 |
1090 | \&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback |
1167 | \&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback |
1091 | can deal with that fact. |
1168 | can deal with that fact, as both are simply passed through to the |
|
|
1169 | callback. |
1092 | .IP "int ev_clear_pending (loop, ev_TYPE *watcher)" 4 |
1170 | .IP "int ev_clear_pending (loop, ev_TYPE *watcher)" 4 |
1093 | .IX Item "int ev_clear_pending (loop, ev_TYPE *watcher)" |
1171 | .IX Item "int ev_clear_pending (loop, ev_TYPE *watcher)" |
1094 | If the watcher is pending, this function returns clears its pending status |
1172 | If the watcher is pending, this function clears its pending status and |
1095 | and returns its \f(CW\*(C`revents\*(C'\fR bitset (as if its callback was invoked). If the |
1173 | returns its \f(CW\*(C`revents\*(C'\fR bitset (as if its callback was invoked). If the |
1096 | watcher isn't pending it does nothing and returns \f(CW0\fR. |
1174 | watcher isn't pending it does nothing and returns \f(CW0\fR. |
|
|
1175 | .Sp |
|
|
1176 | Sometimes it can be useful to \*(L"poll\*(R" a watcher instead of waiting for its |
|
|
1177 | callback to be invoked, which can be accomplished with this function. |
1097 | .Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" |
1178 | .Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" |
1098 | .IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" |
1179 | .IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" |
1099 | Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change |
1180 | Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change |
1100 | and read at any time, libev will completely ignore it. This can be used |
1181 | and read at any time: libev will completely ignore it. This can be used |
1101 | to associate arbitrary data with your watcher. If you need more data and |
1182 | to associate arbitrary data with your watcher. If you need more data and |
1102 | don't want to allocate memory and store a pointer to it in that data |
1183 | don't want to allocate memory and store a pointer to it in that data |
1103 | member, you can also \*(L"subclass\*(R" the watcher type and provide your own |
1184 | member, you can also \*(L"subclass\*(R" the watcher type and provide your own |
1104 | data: |
1185 | data: |
1105 | .PP |
1186 | .PP |
… | |
… | |
1108 | \& { |
1189 | \& { |
1109 | \& struct ev_io io; |
1190 | \& struct ev_io io; |
1110 | \& int otherfd; |
1191 | \& int otherfd; |
1111 | \& void *somedata; |
1192 | \& void *somedata; |
1112 | \& struct whatever *mostinteresting; |
1193 | \& struct whatever *mostinteresting; |
1113 | \& } |
1194 | \& }; |
|
|
1195 | \& |
|
|
1196 | \& ... |
|
|
1197 | \& struct my_io w; |
|
|
1198 | \& ev_io_init (&w.io, my_cb, fd, EV_READ); |
1114 | .Ve |
1199 | .Ve |
1115 | .PP |
1200 | .PP |
1116 | And since your callback will be called with a pointer to the watcher, you |
1201 | And since your callback will be called with a pointer to the watcher, you |
1117 | can cast it back to your own type: |
1202 | can cast it back to your own type: |
1118 | .PP |
1203 | .PP |
… | |
… | |
1125 | .Ve |
1210 | .Ve |
1126 | .PP |
1211 | .PP |
1127 | More interesting and less C\-conformant ways of casting your callback type |
1212 | More interesting and less C\-conformant ways of casting your callback type |
1128 | instead have been omitted. |
1213 | instead have been omitted. |
1129 | .PP |
1214 | .PP |
1130 | Another common scenario is having some data structure with multiple |
1215 | Another common scenario is to use some data structure with multiple |
1131 | watchers: |
1216 | embedded watchers: |
1132 | .PP |
1217 | .PP |
1133 | .Vb 6 |
1218 | .Vb 6 |
1134 | \& struct my_biggy |
1219 | \& struct my_biggy |
1135 | \& { |
1220 | \& { |
1136 | \& int some_data; |
1221 | \& int some_data; |
1137 | \& ev_timer t1; |
1222 | \& ev_timer t1; |
1138 | \& ev_timer t2; |
1223 | \& ev_timer t2; |
1139 | \& } |
1224 | \& } |
1140 | .Ve |
1225 | .Ve |
1141 | .PP |
1226 | .PP |
1142 | In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more complicated, |
1227 | In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more |
1143 | you need to use \f(CW\*(C`offsetof\*(C'\fR: |
1228 | complicated: Either you store the address of your \f(CW\*(C`my_biggy\*(C'\fR struct |
|
|
1229 | in the \f(CW\*(C`data\*(C'\fR member of the watcher (for woozies), or you need to use |
|
|
1230 | some pointer arithmetic using \f(CW\*(C`offsetof\*(C'\fR inside your watchers (for real |
|
|
1231 | programmers): |
1144 | .PP |
1232 | .PP |
1145 | .Vb 1 |
1233 | .Vb 1 |
1146 | \& #include <stddef.h> |
1234 | \& #include <stddef.h> |
1147 | \& |
1235 | \& |
1148 | \& static void |
1236 | \& static void |
… | |
… | |
1187 | In general you can register as many read and/or write event watchers per |
1275 | In general you can register as many read and/or write event watchers per |
1188 | fd as you want (as long as you don't confuse yourself). Setting all file |
1276 | fd as you want (as long as you don't confuse yourself). Setting all file |
1189 | descriptors to non-blocking mode is also usually a good idea (but not |
1277 | descriptors to non-blocking mode is also usually a good idea (but not |
1190 | required if you know what you are doing). |
1278 | required if you know what you are doing). |
1191 | .PP |
1279 | .PP |
1192 | If you must do this, then force the use of a known-to-be-good backend |
1280 | If you cannot use non-blocking mode, then force the use of a |
1193 | (at the time of this writing, this includes only \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and |
1281 | known-to-be-good backend (at the time of this writing, this includes only |
1194 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR). |
1282 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR). |
1195 | .PP |
1283 | .PP |
1196 | Another thing you have to watch out for is that it is quite easy to |
1284 | Another thing you have to watch out for is that it is quite easy to |
1197 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1285 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1198 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1286 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1199 | because there is no data. Not only are some backends known to create a |
1287 | because there is no data. Not only are some backends known to create a |
1200 | lot of those (for example Solaris ports), it is very easy to get into |
1288 | lot of those (for example Solaris ports), it is very easy to get into |
1201 | this situation even with a relatively standard program structure. Thus |
1289 | this situation even with a relatively standard program structure. Thus |
1202 | it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning |
1290 | it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning |
1203 | \&\f(CW\*(C`EAGAIN\*(C'\fR is far preferable to a program hanging until some data arrives. |
1291 | \&\f(CW\*(C`EAGAIN\*(C'\fR is far preferable to a program hanging until some data arrives. |
1204 | .PP |
1292 | .PP |
1205 | If you cannot run the fd in non-blocking mode (for example you should not |
1293 | If you cannot run the fd in non-blocking mode (for example you should |
1206 | play around with an Xlib connection), then you have to separately re-test |
1294 | not play around with an Xlib connection), then you have to separately |
1207 | whether a file descriptor is really ready with a known-to-be good interface |
1295 | re-test whether a file descriptor is really ready with a known-to-be good |
1208 | such as poll (fortunately in our Xlib example, Xlib already does this on |
1296 | interface such as poll (fortunately in our Xlib example, Xlib already |
1209 | its own, so its quite safe to use). |
1297 | does this on its own, so its quite safe to use). Some people additionally |
|
|
1298 | use \f(CW\*(C`SIGALRM\*(C'\fR and an interval timer, just to be sure you won't block |
|
|
1299 | indefinitely. |
|
|
1300 | .PP |
|
|
1301 | But really, best use non-blocking mode. |
1210 | .PP |
1302 | .PP |
1211 | \fIThe special problem of disappearing file descriptors\fR |
1303 | \fIThe special problem of disappearing file descriptors\fR |
1212 | .IX Subsection "The special problem of disappearing file descriptors" |
1304 | .IX Subsection "The special problem of disappearing file descriptors" |
1213 | .PP |
1305 | .PP |
1214 | Some backends (e.g. kqueue, epoll) need to be told about closing a file |
1306 | Some backends (e.g. kqueue, epoll) need to be told about closing a file |
1215 | descriptor (either by calling \f(CW\*(C`close\*(C'\fR explicitly or by any other means, |
1307 | descriptor (either due to calling \f(CW\*(C`close\*(C'\fR explicitly or any other means, |
1216 | such as \f(CW\*(C`dup\*(C'\fR). The reason is that you register interest in some file |
1308 | such as \f(CW\*(C`dup2\*(C'\fR). The reason is that you register interest in some file |
1217 | descriptor, but when it goes away, the operating system will silently drop |
1309 | descriptor, but when it goes away, the operating system will silently drop |
1218 | this interest. If another file descriptor with the same number then is |
1310 | this interest. If another file descriptor with the same number then is |
1219 | registered with libev, there is no efficient way to see that this is, in |
1311 | registered with libev, there is no efficient way to see that this is, in |
1220 | fact, a different file descriptor. |
1312 | fact, a different file descriptor. |
1221 | .PP |
1313 | .PP |
… | |
… | |
1255 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
1347 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
1256 | .PP |
1348 | .PP |
1257 | \fIThe special problem of \s-1SIGPIPE\s0\fR |
1349 | \fIThe special problem of \s-1SIGPIPE\s0\fR |
1258 | .IX Subsection "The special problem of SIGPIPE" |
1350 | .IX Subsection "The special problem of SIGPIPE" |
1259 | .PP |
1351 | .PP |
1260 | While not really specific to libev, it is easy to forget about \s-1SIGPIPE:\s0 |
1352 | While not really specific to libev, it is easy to forget about \f(CW\*(C`SIGPIPE\*(C'\fR: |
1261 | when reading from a pipe whose other end has been closed, your program |
1353 | when writing to a pipe whose other end has been closed, your program gets |
1262 | gets send a \s-1SIGPIPE\s0, which, by default, aborts your program. For most |
1354 | sent a \s-1SIGPIPE\s0, which, by default, aborts your program. For most programs |
1263 | programs this is sensible behaviour, for daemons, this is usually |
1355 | this is sensible behaviour, for daemons, this is usually undesirable. |
1264 | undesirable. |
|
|
1265 | .PP |
1356 | .PP |
1266 | So when you encounter spurious, unexplained daemon exits, make sure you |
1357 | So when you encounter spurious, unexplained daemon exits, make sure you |
1267 | ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon |
1358 | ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon |
1268 | somewhere, as that would have given you a big clue). |
1359 | somewhere, as that would have given you a big clue). |
1269 | .PP |
1360 | .PP |
… | |
… | |
1274 | .PD 0 |
1365 | .PD 0 |
1275 | .IP "ev_io_set (ev_io *, int fd, int events)" 4 |
1366 | .IP "ev_io_set (ev_io *, int fd, int events)" 4 |
1276 | .IX Item "ev_io_set (ev_io *, int fd, int events)" |
1367 | .IX Item "ev_io_set (ev_io *, int fd, int events)" |
1277 | .PD |
1368 | .PD |
1278 | Configures an \f(CW\*(C`ev_io\*(C'\fR watcher. The \f(CW\*(C`fd\*(C'\fR is the file descriptor to |
1369 | Configures an \f(CW\*(C`ev_io\*(C'\fR watcher. The \f(CW\*(C`fd\*(C'\fR is the file descriptor to |
1279 | receive events for and events is either \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or |
1370 | receive events for and \f(CW\*(C`events\*(C'\fR is either \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or |
1280 | \&\f(CW\*(C`EV_READ | EV_WRITE\*(C'\fR to receive the given events. |
1371 | \&\f(CW\*(C`EV_READ | EV_WRITE\*(C'\fR, to express the desire to receive the given events. |
1281 | .IP "int fd [read\-only]" 4 |
1372 | .IP "int fd [read\-only]" 4 |
1282 | .IX Item "int fd [read-only]" |
1373 | .IX Item "int fd [read-only]" |
1283 | The file descriptor being watched. |
1374 | The file descriptor being watched. |
1284 | .IP "int events [read\-only]" 4 |
1375 | .IP "int events [read\-only]" 4 |
1285 | .IX Item "int events [read-only]" |
1376 | .IX Item "int events [read-only]" |
… | |
… | |
1295 | .Vb 6 |
1386 | .Vb 6 |
1296 | \& static void |
1387 | \& static void |
1297 | \& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
1388 | \& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
1298 | \& { |
1389 | \& { |
1299 | \& ev_io_stop (loop, w); |
1390 | \& ev_io_stop (loop, w); |
1300 | \& .. read from stdin here (or from w\->fd) and haqndle any I/O errors |
1391 | \& .. read from stdin here (or from w\->fd) and handle any I/O errors |
1301 | \& } |
1392 | \& } |
1302 | \& |
1393 | \& |
1303 | \& ... |
1394 | \& ... |
1304 | \& struct ev_loop *loop = ev_default_init (0); |
1395 | \& struct ev_loop *loop = ev_default_init (0); |
1305 | \& struct ev_io stdin_readable; |
1396 | \& struct ev_io stdin_readable; |
… | |
… | |
1313 | Timer watchers are simple relative timers that generate an event after a |
1404 | Timer watchers are simple relative timers that generate an event after a |
1314 | given time, and optionally repeating in regular intervals after that. |
1405 | given time, and optionally repeating in regular intervals after that. |
1315 | .PP |
1406 | .PP |
1316 | The timers are based on real time, that is, if you register an event that |
1407 | The timers are based on real time, that is, if you register an event that |
1317 | times out after an hour and you reset your system clock to January last |
1408 | times out after an hour and you reset your system clock to January last |
1318 | year, it will still time out after (roughly) and hour. \*(L"Roughly\*(R" because |
1409 | year, it will still time out after (roughly) one hour. \*(L"Roughly\*(R" because |
1319 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1410 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1320 | monotonic clock option helps a lot here). |
1411 | monotonic clock option helps a lot here). |
|
|
1412 | .PP |
|
|
1413 | The callback is guaranteed to be invoked only \fIafter\fR its timeout has |
|
|
1414 | passed, but if multiple timers become ready during the same loop iteration |
|
|
1415 | then order of execution is undefined. |
|
|
1416 | .PP |
|
|
1417 | \fIThe special problem of time updates\fR |
|
|
1418 | .IX Subsection "The special problem of time updates" |
|
|
1419 | .PP |
|
|
1420 | Establishing the current time is a costly operation (it usually takes at |
|
|
1421 | least two system calls): \s-1EV\s0 therefore updates its idea of the current |
|
|
1422 | time only before and after \f(CW\*(C`ev_loop\*(C'\fR collects new events, which causes a |
|
|
1423 | growing difference between \f(CW\*(C`ev_now ()\*(C'\fR and \f(CW\*(C`ev_time ()\*(C'\fR when handling |
|
|
1424 | lots of events in one iteration. |
1321 | .PP |
1425 | .PP |
1322 | The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR |
1426 | The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR |
1323 | time. This is usually the right thing as this timestamp refers to the time |
1427 | time. This is usually the right thing as this timestamp refers to the time |
1324 | of the event triggering whatever timeout you are modifying/starting. If |
1428 | of the event triggering whatever timeout you are modifying/starting. If |
1325 | you suspect event processing to be delayed and you \fIneed\fR to base the timeout |
1429 | you suspect event processing to be delayed and you \fIneed\fR to base the |
1326 | on the current time, use something like this to adjust for this: |
1430 | timeout on the current time, use something like this to adjust for this: |
1327 | .PP |
1431 | .PP |
1328 | .Vb 1 |
1432 | .Vb 1 |
1329 | \& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.); |
1433 | \& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.); |
1330 | .Ve |
1434 | .Ve |
1331 | .PP |
1435 | .PP |
1332 | The callback is guaranteed to be invoked only after its timeout has passed, |
1436 | If the event loop is suspended for a long time, you can also force an |
1333 | but if multiple timers become ready during the same loop iteration then |
1437 | update of the time returned by \f(CW\*(C`ev_now ()\*(C'\fR by calling \f(CW\*(C`ev_now_update |
1334 | order of execution is undefined. |
1438 | ()\*(C'\fR. |
1335 | .PP |
1439 | .PP |
1336 | \fIWatcher-Specific Functions and Data Members\fR |
1440 | \fIWatcher-Specific Functions and Data Members\fR |
1337 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1441 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1338 | .IP "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" 4 |
1442 | .IP "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" 4 |
1339 | .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" |
1443 | .IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" |
… | |
… | |
1388 | \& ev_timer_again (loop, timer); |
1492 | \& ev_timer_again (loop, timer); |
1389 | .Ve |
1493 | .Ve |
1390 | .Sp |
1494 | .Sp |
1391 | This is more slightly efficient then stopping/starting the timer each time |
1495 | This is more slightly efficient then stopping/starting the timer each time |
1392 | you want to modify its timeout value. |
1496 | you want to modify its timeout value. |
|
|
1497 | .Sp |
|
|
1498 | Note, however, that it is often even more efficient to remember the |
|
|
1499 | time of the last activity and let the timer time-out naturally. In the |
|
|
1500 | callback, you then check whether the time-out is real, or, if there was |
|
|
1501 | some activity, you reschedule the watcher to time-out in \*(L"last_activity + |
|
|
1502 | timeout \- ev_now ()\*(R" seconds. |
1393 | .IP "ev_tstamp repeat [read\-write]" 4 |
1503 | .IP "ev_tstamp repeat [read\-write]" 4 |
1394 | .IX Item "ev_tstamp repeat [read-write]" |
1504 | .IX Item "ev_tstamp repeat [read-write]" |
1395 | The current \f(CW\*(C`repeat\*(C'\fR value. Will be used each time the watcher times out |
1505 | The current \f(CW\*(C`repeat\*(C'\fR value. Will be used each time the watcher times out |
1396 | or \f(CW\*(C`ev_timer_again\*(C'\fR is called and determines the next timeout (if any), |
1506 | or \f(CW\*(C`ev_timer_again\*(C'\fR is called, and determines the next timeout (if any), |
1397 | which is also when any modifications are taken into account. |
1507 | which is also when any modifications are taken into account. |
1398 | .PP |
1508 | .PP |
1399 | \fIExamples\fR |
1509 | \fIExamples\fR |
1400 | .IX Subsection "Examples" |
1510 | .IX Subsection "Examples" |
1401 | .PP |
1511 | .PP |
… | |
… | |
1447 | to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger |
1557 | to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger |
1448 | roughly 10 seconds later as it uses a relative timeout). |
1558 | roughly 10 seconds later as it uses a relative timeout). |
1449 | .PP |
1559 | .PP |
1450 | \&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers, |
1560 | \&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers, |
1451 | such as triggering an event on each \*(L"midnight, local time\*(R", or other |
1561 | such as triggering an event on each \*(L"midnight, local time\*(R", or other |
1452 | complicated, rules. |
1562 | complicated rules. |
1453 | .PP |
1563 | .PP |
1454 | As with timers, the callback is guaranteed to be invoked only when the |
1564 | As with timers, the callback is guaranteed to be invoked only when the |
1455 | time (\f(CW\*(C`at\*(C'\fR) has passed, but if multiple periodic timers become ready |
1565 | time (\f(CW\*(C`at\*(C'\fR) has passed, but if multiple periodic timers become ready |
1456 | during the same loop iteration then order of execution is undefined. |
1566 | during the same loop iteration, then order of execution is undefined. |
1457 | .PP |
1567 | .PP |
1458 | \fIWatcher-Specific Functions and Data Members\fR |
1568 | \fIWatcher-Specific Functions and Data Members\fR |
1459 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1569 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1460 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 |
1570 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 |
1461 | .IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" |
1571 | .IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" |
1462 | .PD 0 |
1572 | .PD 0 |
1463 | .IP "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" 4 |
1573 | .IP "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" 4 |
1464 | .IX Item "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" |
1574 | .IX Item "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" |
1465 | .PD |
1575 | .PD |
1466 | Lots of arguments, lets sort it out... There are basically three modes of |
1576 | Lots of arguments, lets sort it out... There are basically three modes of |
1467 | operation, and we will explain them from simplest to complex: |
1577 | operation, and we will explain them from simplest to most complex: |
1468 | .RS 4 |
1578 | .RS 4 |
1469 | .IP "\(bu" 4 |
1579 | .IP "\(bu" 4 |
1470 | absolute timer (at = time, interval = reschedule_cb = 0) |
1580 | absolute timer (at = time, interval = reschedule_cb = 0) |
1471 | .Sp |
1581 | .Sp |
1472 | In this configuration the watcher triggers an event after the wall clock |
1582 | In this configuration the watcher triggers an event after the wall clock |
1473 | time \f(CW\*(C`at\*(C'\fR has passed and doesn't repeat. It will not adjust when a time |
1583 | time \f(CW\*(C`at\*(C'\fR has passed. It will not repeat and will not adjust when a time |
1474 | jump occurs, that is, if it is to be run at January 1st 2011 then it will |
1584 | jump occurs, that is, if it is to be run at January 1st 2011 then it will |
1475 | run when the system time reaches or surpasses this time. |
1585 | only run when the system clock reaches or surpasses this time. |
1476 | .IP "\(bu" 4 |
1586 | .IP "\(bu" 4 |
1477 | repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1587 | repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1478 | .Sp |
1588 | .Sp |
1479 | In this mode the watcher will always be scheduled to time out at the next |
1589 | In this mode the watcher will always be scheduled to time out at the next |
1480 | \&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) |
1590 | \&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) |
1481 | and then repeat, regardless of any time jumps. |
1591 | and then repeat, regardless of any time jumps. |
1482 | .Sp |
1592 | .Sp |
1483 | This can be used to create timers that do not drift with respect to system |
1593 | This can be used to create timers that do not drift with respect to the |
1484 | time, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each hour, on |
1594 | system clock, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each |
1485 | the hour: |
1595 | hour, on the hour: |
1486 | .Sp |
1596 | .Sp |
1487 | .Vb 1 |
1597 | .Vb 1 |
1488 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1598 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1489 | .Ve |
1599 | .Ve |
1490 | .Sp |
1600 | .Sp |
… | |
… | |
1576 | .PP |
1686 | .PP |
1577 | \fIExamples\fR |
1687 | \fIExamples\fR |
1578 | .IX Subsection "Examples" |
1688 | .IX Subsection "Examples" |
1579 | .PP |
1689 | .PP |
1580 | Example: Call a callback every hour, or, more precisely, whenever the |
1690 | Example: Call a callback every hour, or, more precisely, whenever the |
1581 | system clock is divisible by 3600. The callback invocation times have |
1691 | system time is divisible by 3600. The callback invocation times have |
1582 | potentially a lot of jitter, but good long-term stability. |
1692 | potentially a lot of jitter, but good long-term stability. |
1583 | .PP |
1693 | .PP |
1584 | .Vb 5 |
1694 | .Vb 5 |
1585 | \& static void |
1695 | \& static void |
1586 | \& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
1696 | \& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
… | |
… | |
1599 | \& #include <math.h> |
1709 | \& #include <math.h> |
1600 | \& |
1710 | \& |
1601 | \& static ev_tstamp |
1711 | \& static ev_tstamp |
1602 | \& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) |
1712 | \& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) |
1603 | \& { |
1713 | \& { |
1604 | \& return fmod (now, 3600.) + 3600.; |
1714 | \& return now + (3600. \- fmod (now, 3600.)); |
1605 | \& } |
1715 | \& } |
1606 | \& |
1716 | \& |
1607 | \& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); |
1717 | \& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); |
1608 | .Ve |
1718 | .Ve |
1609 | .PP |
1719 | .PP |
… | |
… | |
1621 | Signal watchers will trigger an event when the process receives a specific |
1731 | Signal watchers will trigger an event when the process receives a specific |
1622 | signal one or more times. Even though signals are very asynchronous, libev |
1732 | signal one or more times. Even though signals are very asynchronous, libev |
1623 | will try it's best to deliver signals synchronously, i.e. as part of the |
1733 | will try it's best to deliver signals synchronously, i.e. as part of the |
1624 | normal event processing, like any other event. |
1734 | normal event processing, like any other event. |
1625 | .PP |
1735 | .PP |
|
|
1736 | If you want signals asynchronously, just use \f(CW\*(C`sigaction\*(C'\fR as you would |
|
|
1737 | do without libev and forget about sharing the signal. You can even use |
|
|
1738 | \&\f(CW\*(C`ev_async\*(C'\fR from a signal handler to synchronously wake up an event loop. |
|
|
1739 | .PP |
1626 | You can configure as many watchers as you like per signal. Only when the |
1740 | You can configure as many watchers as you like per signal. Only when the |
1627 | first watcher gets started will libev actually register a signal watcher |
1741 | first watcher gets started will libev actually register a signal handler |
1628 | with the kernel (thus it coexists with your own signal handlers as long |
1742 | with the kernel (thus it coexists with your own signal handlers as long as |
1629 | as you don't register any with libev). Similarly, when the last signal |
1743 | you don't register any with libev for the same signal). Similarly, when |
1630 | watcher for a signal is stopped libev will reset the signal handler to |
1744 | the last signal watcher for a signal is stopped, libev will reset the |
1631 | \&\s-1SIG_DFL\s0 (regardless of what it was set to before). |
1745 | signal handler to \s-1SIG_DFL\s0 (regardless of what it was set to before). |
1632 | .PP |
1746 | .PP |
1633 | If possible and supported, libev will install its handlers with |
1747 | If possible and supported, libev will install its handlers with |
1634 | \&\f(CW\*(C`SA_RESTART\*(C'\fR behaviour enabled, so system calls should not be unduly |
1748 | \&\f(CW\*(C`SA_RESTART\*(C'\fR behaviour enabled, so system calls should not be unduly |
1635 | interrupted. If you have a problem with system calls getting interrupted by |
1749 | interrupted. If you have a problem with system calls getting interrupted by |
1636 | signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock |
1750 | signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock |
… | |
… | |
1668 | .Ve |
1782 | .Ve |
1669 | .ie n .Sh """ev_child"" \- watch out for process status changes" |
1783 | .ie n .Sh """ev_child"" \- watch out for process status changes" |
1670 | .el .Sh "\f(CWev_child\fP \- watch out for process status changes" |
1784 | .el .Sh "\f(CWev_child\fP \- watch out for process status changes" |
1671 | .IX Subsection "ev_child - watch out for process status changes" |
1785 | .IX Subsection "ev_child - watch out for process status changes" |
1672 | Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to |
1786 | Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to |
1673 | some child status changes (most typically when a child of yours dies). It |
1787 | some child status changes (most typically when a child of yours dies or |
1674 | is permissible to install a child watcher \fIafter\fR the child has been |
1788 | exits). It is permissible to install a child watcher \fIafter\fR the child |
1675 | forked (which implies it might have already exited), as long as the event |
1789 | has been forked (which implies it might have already exited), as long |
1676 | loop isn't entered (or is continued from a watcher). |
1790 | as the event loop isn't entered (or is continued from a watcher), i.e., |
|
|
1791 | forking and then immediately registering a watcher for the child is fine, |
|
|
1792 | but forking and registering a watcher a few event loop iterations later is |
|
|
1793 | not. |
1677 | .PP |
1794 | .PP |
1678 | Only the default event loop is capable of handling signals, and therefore |
1795 | Only the default event loop is capable of handling signals, and therefore |
1679 | you can only register child watchers in the default event loop. |
1796 | you can only register child watchers in the default event loop. |
1680 | .PP |
1797 | .PP |
1681 | \fIProcess Interaction\fR |
1798 | \fIProcess Interaction\fR |
… | |
… | |
1696 | handler, you can override it easily by installing your own handler for |
1813 | handler, you can override it easily by installing your own handler for |
1697 | \&\f(CW\*(C`SIGCHLD\*(C'\fR after initialising the default loop, and making sure the |
1814 | \&\f(CW\*(C`SIGCHLD\*(C'\fR after initialising the default loop, and making sure the |
1698 | default loop never gets destroyed. You are encouraged, however, to use an |
1815 | default loop never gets destroyed. You are encouraged, however, to use an |
1699 | event-based approach to child reaping and thus use libev's support for |
1816 | event-based approach to child reaping and thus use libev's support for |
1700 | that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely. |
1817 | that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely. |
|
|
1818 | .PP |
|
|
1819 | \fIStopping the Child Watcher\fR |
|
|
1820 | .IX Subsection "Stopping the Child Watcher" |
|
|
1821 | .PP |
|
|
1822 | Currently, the child watcher never gets stopped, even when the |
|
|
1823 | child terminates, so normally one needs to stop the watcher in the |
|
|
1824 | callback. Future versions of libev might stop the watcher automatically |
|
|
1825 | when a child exit is detected. |
1701 | .PP |
1826 | .PP |
1702 | \fIWatcher-Specific Functions and Data Members\fR |
1827 | \fIWatcher-Specific Functions and Data Members\fR |
1703 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1828 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1704 | .IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 |
1829 | .IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 |
1705 | .IX Item "ev_child_init (ev_child *, callback, int pid, int trace)" |
1830 | .IX Item "ev_child_init (ev_child *, callback, int pid, int trace)" |
… | |
… | |
1771 | the stat buffer having unspecified contents. |
1896 | the stat buffer having unspecified contents. |
1772 | .PP |
1897 | .PP |
1773 | The path \fIshould\fR be absolute and \fImust not\fR end in a slash. If it is |
1898 | The path \fIshould\fR be absolute and \fImust not\fR end in a slash. If it is |
1774 | relative and your working directory changes, the behaviour is undefined. |
1899 | relative and your working directory changes, the behaviour is undefined. |
1775 | .PP |
1900 | .PP |
1776 | Since there is no standard to do this, the portable implementation simply |
1901 | Since there is no standard kernel interface to do this, the portable |
1777 | calls \f(CW\*(C`stat (2)\*(C'\fR regularly on the path to see if it changed somehow. You |
1902 | implementation simply calls \f(CW\*(C`stat (2)\*(C'\fR regularly on the path to see if |
1778 | can specify a recommended polling interval for this case. If you specify |
1903 | it changed somehow. You can specify a recommended polling interval for |
1779 | a polling interval of \f(CW0\fR (highly recommended!) then a \fIsuitable, |
1904 | this case. If you specify a polling interval of \f(CW0\fR (highly recommended!) |
1780 | unspecified default\fR value will be used (which you can expect to be around |
1905 | then a \fIsuitable, unspecified default\fR value will be used (which |
1781 | five seconds, although this might change dynamically). Libev will also |
1906 | you can expect to be around five seconds, although this might change |
1782 | impose a minimum interval which is currently around \f(CW0.1\fR, but thats |
1907 | dynamically). Libev will also impose a minimum interval which is currently |
1783 | usually overkill. |
1908 | around \f(CW0.1\fR, but thats usually overkill. |
1784 | .PP |
1909 | .PP |
1785 | This watcher type is not meant for massive numbers of stat watchers, |
1910 | This watcher type is not meant for massive numbers of stat watchers, |
1786 | as even with OS-supported change notifications, this can be |
1911 | as even with OS-supported change notifications, this can be |
1787 | resource-intensive. |
1912 | resource-intensive. |
1788 | .PP |
1913 | .PP |
1789 | At the time of this writing, only the Linux inotify interface is |
1914 | At the time of this writing, the only OS-specific interface implemented |
1790 | implemented (implementing kqueue support is left as an exercise for the |
1915 | is the Linux inotify interface (implementing kqueue support is left as |
1791 | reader, note, however, that the author sees no way of implementing ev_stat |
1916 | an exercise for the reader. Note, however, that the author sees no way |
1792 | semantics with kqueue). Inotify will be used to give hints only and should |
1917 | of implementing \f(CW\*(C`ev_stat\*(C'\fR semantics with kqueue). |
1793 | not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev |
|
|
1794 | sometimes needs to fall back to regular polling again even with inotify, |
|
|
1795 | but changes are usually detected immediately, and if the file exists there |
|
|
1796 | will be no polling. |
|
|
1797 | .PP |
1918 | .PP |
1798 | \fI\s-1ABI\s0 Issues (Largefile Support)\fR |
1919 | \fI\s-1ABI\s0 Issues (Largefile Support)\fR |
1799 | .IX Subsection "ABI Issues (Largefile Support)" |
1920 | .IX Subsection "ABI Issues (Largefile Support)" |
1800 | .PP |
1921 | .PP |
1801 | Libev by default (unless the user overrides this) uses the default |
1922 | Libev by default (unless the user overrides this) uses the default |
… | |
… | |
1811 | file interfaces available by default (as e.g. FreeBSD does) and not |
1932 | file interfaces available by default (as e.g. FreeBSD does) and not |
1812 | optional. Libev cannot simply switch on large file support because it has |
1933 | optional. Libev cannot simply switch on large file support because it has |
1813 | to exchange stat structures with application programs compiled using the |
1934 | to exchange stat structures with application programs compiled using the |
1814 | default compilation environment. |
1935 | default compilation environment. |
1815 | .PP |
1936 | .PP |
1816 | \fIInotify\fR |
1937 | \fIInotify and Kqueue\fR |
1817 | .IX Subsection "Inotify" |
1938 | .IX Subsection "Inotify and Kqueue" |
1818 | .PP |
1939 | .PP |
1819 | When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only |
1940 | When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only |
1820 | available on Linux) and present at runtime, it will be used to speed up |
1941 | available with Linux) and present at runtime, it will be used to speed up |
1821 | change detection where possible. The inotify descriptor will be created lazily |
1942 | change detection where possible. The inotify descriptor will be created lazily |
1822 | when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. |
1943 | when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. |
1823 | .PP |
1944 | .PP |
1824 | Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers |
1945 | Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers |
1825 | except that changes might be detected earlier, and in some cases, to avoid |
1946 | except that changes might be detected earlier, and in some cases, to avoid |
1826 | making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support |
1947 | making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support |
1827 | there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling. |
1948 | there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling, |
|
|
1949 | but as long as the path exists, libev usually gets away without polling. |
1828 | .PP |
1950 | .PP |
1829 | (There is no support for kqueue, as apparently it cannot be used to |
1951 | There is no support for kqueue, as apparently it cannot be used to |
1830 | implement this functionality, due to the requirement of having a file |
1952 | implement this functionality, due to the requirement of having a file |
1831 | descriptor open on the object at all times). |
1953 | descriptor open on the object at all times, and detecting renames, unlinks |
|
|
1954 | etc. is difficult. |
1832 | .PP |
1955 | .PP |
1833 | \fIThe special problem of stat time resolution\fR |
1956 | \fIThe special problem of stat time resolution\fR |
1834 | .IX Subsection "The special problem of stat time resolution" |
1957 | .IX Subsection "The special problem of stat time resolution" |
1835 | .PP |
1958 | .PP |
1836 | The \f(CW\*(C`stat ()\*(C'\fR system call only supports full-second resolution portably, and |
1959 | The \f(CW\*(C`stat ()\*(C'\fR system call only supports full-second resolution portably, and |
1837 | even on systems where the resolution is higher, many file systems still |
1960 | even on systems where the resolution is higher, most file systems still |
1838 | only support whole seconds. |
1961 | only support whole seconds. |
1839 | .PP |
1962 | .PP |
1840 | That means that, if the time is the only thing that changes, you can |
1963 | That means that, if the time is the only thing that changes, you can |
1841 | easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and |
1964 | easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and |
1842 | calls your callback, which does something. When there is another update |
1965 | calls your callback, which does something. When there is another update |
1843 | within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it as the stat |
1966 | within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect unless the |
1844 | data does not change. |
1967 | stat data does change in other ways (e.g. file size). |
1845 | .PP |
1968 | .PP |
1846 | The solution to this is to delay acting on a change for slightly more |
1969 | The solution to this is to delay acting on a change for slightly more |
1847 | than a second (or till slightly after the next full second boundary), using |
1970 | than a second (or till slightly after the next full second boundary), using |
1848 | a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02); |
1971 | a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02); |
1849 | ev_timer_again (loop, w)\*(C'\fR). |
1972 | ev_timer_again (loop, w)\*(C'\fR). |
… | |
… | |
1869 | \&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to |
1992 | \&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to |
1870 | be detected and should normally be specified as \f(CW0\fR to let libev choose |
1993 | be detected and should normally be specified as \f(CW0\fR to let libev choose |
1871 | a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same |
1994 | a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same |
1872 | path for as long as the watcher is active. |
1995 | path for as long as the watcher is active. |
1873 | .Sp |
1996 | .Sp |
1874 | The callback will receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, relative |
1997 | The callback will receive an \f(CW\*(C`EV_STAT\*(C'\fR event when a change was detected, |
1875 | to the attributes at the time the watcher was started (or the last change |
1998 | relative to the attributes at the time the watcher was started (or the |
1876 | was detected). |
1999 | last change was detected). |
1877 | .IP "ev_stat_stat (loop, ev_stat *)" 4 |
2000 | .IP "ev_stat_stat (loop, ev_stat *)" 4 |
1878 | .IX Item "ev_stat_stat (loop, ev_stat *)" |
2001 | .IX Item "ev_stat_stat (loop, ev_stat *)" |
1879 | Updates the stat buffer immediately with new values. If you change the |
2002 | Updates the stat buffer immediately with new values. If you change the |
1880 | watched path in your callback, you could call this function to avoid |
2003 | watched path in your callback, you could call this function to avoid |
1881 | detecting this change (while introducing a race condition if you are not |
2004 | detecting this change (while introducing a race condition if you are not |
… | |
… | |
1961 | .Ve |
2084 | .Ve |
1962 | .ie n .Sh """ev_idle"" \- when you've got nothing better to do..." |
2085 | .ie n .Sh """ev_idle"" \- when you've got nothing better to do..." |
1963 | .el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." |
2086 | .el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." |
1964 | .IX Subsection "ev_idle - when you've got nothing better to do..." |
2087 | .IX Subsection "ev_idle - when you've got nothing better to do..." |
1965 | Idle watchers trigger events when no other events of the same or higher |
2088 | Idle watchers trigger events when no other events of the same or higher |
1966 | priority are pending (prepare, check and other idle watchers do not |
2089 | priority are pending (prepare, check and other idle watchers do not count |
1967 | count). |
2090 | as receiving \*(L"events\*(R"). |
1968 | .PP |
2091 | .PP |
1969 | That is, as long as your process is busy handling sockets or timeouts |
2092 | That is, as long as your process is busy handling sockets or timeouts |
1970 | (or even signals, imagine) of the same or higher priority it will not be |
2093 | (or even signals, imagine) of the same or higher priority it will not be |
1971 | triggered. But when your process is idle (or only lower-priority watchers |
2094 | triggered. But when your process is idle (or only lower-priority watchers |
1972 | are pending), the idle watchers are being called once per event loop |
2095 | are pending), the idle watchers are being called once per event loop |
… | |
… | |
2009 | \& ev_idle_start (loop, idle_cb); |
2132 | \& ev_idle_start (loop, idle_cb); |
2010 | .Ve |
2133 | .Ve |
2011 | .ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop!" |
2134 | .ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop!" |
2012 | .el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop!" |
2135 | .el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop!" |
2013 | .IX Subsection "ev_prepare and ev_check - customise your event loop!" |
2136 | .IX Subsection "ev_prepare and ev_check - customise your event loop!" |
2014 | Prepare and check watchers are usually (but not always) used in tandem: |
2137 | Prepare and check watchers are usually (but not always) used in pairs: |
2015 | prepare watchers get invoked before the process blocks and check watchers |
2138 | prepare watchers get invoked before the process blocks and check watchers |
2016 | afterwards. |
2139 | afterwards. |
2017 | .PP |
2140 | .PP |
2018 | You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter |
2141 | You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter |
2019 | the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR |
2142 | the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR |
… | |
… | |
2022 | those watchers, i.e. the sequence will always be \f(CW\*(C`ev_prepare\*(C'\fR, blocking, |
2145 | those watchers, i.e. the sequence will always be \f(CW\*(C`ev_prepare\*(C'\fR, blocking, |
2023 | \&\f(CW\*(C`ev_check\*(C'\fR so if you have one watcher of each kind they will always be |
2146 | \&\f(CW\*(C`ev_check\*(C'\fR so if you have one watcher of each kind they will always be |
2024 | called in pairs bracketing the blocking call. |
2147 | called in pairs bracketing the blocking call. |
2025 | .PP |
2148 | .PP |
2026 | Their main purpose is to integrate other event mechanisms into libev and |
2149 | Their main purpose is to integrate other event mechanisms into libev and |
2027 | their use is somewhat advanced. This could be used, for example, to track |
2150 | their use is somewhat advanced. They could be used, for example, to track |
2028 | variable changes, implement your own watchers, integrate net-snmp or a |
2151 | variable changes, implement your own watchers, integrate net-snmp or a |
2029 | coroutine library and lots more. They are also occasionally useful if |
2152 | coroutine library and lots more. They are also occasionally useful if |
2030 | you cache some data and want to flush it before blocking (for example, |
2153 | you cache some data and want to flush it before blocking (for example, |
2031 | in X programs you might want to do an \f(CW\*(C`XFlush ()\*(C'\fR in an \f(CW\*(C`ev_prepare\*(C'\fR |
2154 | in X programs you might want to do an \f(CW\*(C`XFlush ()\*(C'\fR in an \f(CW\*(C`ev_prepare\*(C'\fR |
2032 | watcher). |
2155 | watcher). |
2033 | .PP |
2156 | .PP |
2034 | This is done by examining in each prepare call which file descriptors need |
2157 | This is done by examining in each prepare call which file descriptors |
2035 | to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for |
2158 | need to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers |
2036 | them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries |
2159 | for them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many |
2037 | provide just this functionality). Then, in the check watcher you check for |
2160 | libraries provide exactly this functionality). Then, in the check watcher, |
2038 | any events that occurred (by checking the pending status of all watchers |
2161 | you check for any events that occurred (by checking the pending status |
2039 | and stopping them) and call back into the library. The I/O and timer |
2162 | of all watchers and stopping them) and call back into the library. The |
2040 | callbacks will never actually be called (but must be valid nevertheless, |
2163 | I/O and timer callbacks will never actually be called (but must be valid |
2041 | because you never know, you know?). |
2164 | nevertheless, because you never know, you know?). |
2042 | .PP |
2165 | .PP |
2043 | As another example, the Perl Coro module uses these hooks to integrate |
2166 | As another example, the Perl Coro module uses these hooks to integrate |
2044 | coroutines into libev programs, by yielding to other active coroutines |
2167 | coroutines into libev programs, by yielding to other active coroutines |
2045 | during each prepare and only letting the process block if no coroutines |
2168 | during each prepare and only letting the process block if no coroutines |
2046 | are ready to run (it's actually more complicated: it only runs coroutines |
2169 | are ready to run (it's actually more complicated: it only runs coroutines |
… | |
… | |
2049 | loop from blocking if lower-priority coroutines are active, thus mapping |
2172 | loop from blocking if lower-priority coroutines are active, thus mapping |
2050 | low-priority coroutines to idle/background tasks). |
2173 | low-priority coroutines to idle/background tasks). |
2051 | .PP |
2174 | .PP |
2052 | It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) |
2175 | It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) |
2053 | priority, to ensure that they are being run before any other watchers |
2176 | priority, to ensure that they are being run before any other watchers |
|
|
2177 | after the poll (this doesn't matter for \f(CW\*(C`ev_prepare\*(C'\fR watchers). |
|
|
2178 | .PP |
2054 | after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, |
2179 | Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, too) should not |
2055 | too) should not activate (\*(L"feed\*(R") events into libev. While libev fully |
2180 | activate (\*(L"feed\*(R") events into libev. While libev fully supports this, they |
2056 | supports this, they might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers |
2181 | might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers did their job. As |
2057 | did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other |
2182 | \&\f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other (non-libev) event |
2058 | (non-libev) event loops those other event loops might be in an unusable |
2183 | loops those other event loops might be in an unusable state until their |
2059 | state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to |
2184 | \&\f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to coexist peacefully with |
2060 | coexist peacefully with others). |
2185 | others). |
2061 | .PP |
2186 | .PP |
2062 | \fIWatcher-Specific Functions and Data Members\fR |
2187 | \fIWatcher-Specific Functions and Data Members\fR |
2063 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2188 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2064 | .IP "ev_prepare_init (ev_prepare *, callback)" 4 |
2189 | .IP "ev_prepare_init (ev_prepare *, callback)" 4 |
2065 | .IX Item "ev_prepare_init (ev_prepare *, callback)" |
2190 | .IX Item "ev_prepare_init (ev_prepare *, callback)" |
… | |
… | |
2067 | .IP "ev_check_init (ev_check *, callback)" 4 |
2192 | .IP "ev_check_init (ev_check *, callback)" 4 |
2068 | .IX Item "ev_check_init (ev_check *, callback)" |
2193 | .IX Item "ev_check_init (ev_check *, callback)" |
2069 | .PD |
2194 | .PD |
2070 | Initialises and configures the prepare or check watcher \- they have no |
2195 | Initialises and configures the prepare or check watcher \- they have no |
2071 | parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR |
2196 | parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR |
2072 | macros, but using them is utterly, utterly and completely pointless. |
2197 | macros, but using them is utterly, utterly, utterly and completely |
|
|
2198 | pointless. |
2073 | .PP |
2199 | .PP |
2074 | \fIExamples\fR |
2200 | \fIExamples\fR |
2075 | .IX Subsection "Examples" |
2201 | .IX Subsection "Examples" |
2076 | .PP |
2202 | .PP |
2077 | There are a number of principal ways to embed other event loops or modules |
2203 | There are a number of principal ways to embed other event loops or modules |
… | |
… | |
2173 | \& |
2299 | \& |
2174 | \& // do not ever call adns_afterpoll |
2300 | \& // do not ever call adns_afterpoll |
2175 | .Ve |
2301 | .Ve |
2176 | .PP |
2302 | .PP |
2177 | Method 4: Do not use a prepare or check watcher because the module you |
2303 | Method 4: Do not use a prepare or check watcher because the module you |
2178 | want to embed is too inflexible to support it. Instead, you can override |
2304 | want to embed is not flexible enough to support it. Instead, you can |
2179 | their poll function. The drawback with this solution is that the main |
2305 | override their poll function. The drawback with this solution is that the |
2180 | loop is now no longer controllable by \s-1EV\s0. The \f(CW\*(C`Glib::EV\*(C'\fR module does |
2306 | main loop is now no longer controllable by \s-1EV\s0. The \f(CW\*(C`Glib::EV\*(C'\fR module uses |
2181 | this. |
2307 | this approach, effectively embedding \s-1EV\s0 as a client into the horrible |
|
|
2308 | libglib event loop. |
2182 | .PP |
2309 | .PP |
2183 | .Vb 4 |
2310 | .Vb 4 |
2184 | \& static gint |
2311 | \& static gint |
2185 | \& event_poll_func (GPollFD *fds, guint nfds, gint timeout) |
2312 | \& event_poll_func (GPollFD *fds, guint nfds, gint timeout) |
2186 | \& { |
2313 | \& { |
… | |
… | |
2218 | prioritise I/O. |
2345 | prioritise I/O. |
2219 | .PP |
2346 | .PP |
2220 | As an example for a bug workaround, the kqueue backend might only support |
2347 | As an example for a bug workaround, the kqueue backend might only support |
2221 | sockets on some platform, so it is unusable as generic backend, but you |
2348 | sockets on some platform, so it is unusable as generic backend, but you |
2222 | still want to make use of it because you have many sockets and it scales |
2349 | still want to make use of it because you have many sockets and it scales |
2223 | so nicely. In this case, you would create a kqueue-based loop and embed it |
2350 | so nicely. In this case, you would create a kqueue-based loop and embed |
2224 | into your default loop (which might use e.g. poll). Overall operation will |
2351 | it into your default loop (which might use e.g. poll). Overall operation |
2225 | be a bit slower because first libev has to poll and then call kevent, but |
2352 | will be a bit slower because first libev has to call \f(CW\*(C`poll\*(C'\fR and then |
2226 | at least you can use both at what they are best. |
2353 | \&\f(CW\*(C`kevent\*(C'\fR, but at least you can use both mechanisms for what they are |
|
|
2354 | best: \f(CW\*(C`kqueue\*(C'\fR for scalable sockets and \f(CW\*(C`poll\*(C'\fR if you want it to work :) |
2227 | .PP |
2355 | .PP |
2228 | As for prioritising I/O: rarely you have the case where some fds have |
2356 | As for prioritising I/O: under rare circumstances you have the case where |
2229 | to be watched and handled very quickly (with low latency), and even |
2357 | some fds have to be watched and handled very quickly (with low latency), |
2230 | priorities and idle watchers might have too much overhead. In this case |
2358 | and even priorities and idle watchers might have too much overhead. In |
2231 | you would put all the high priority stuff in one loop and all the rest in |
2359 | this case you would put all the high priority stuff in one loop and all |
2232 | a second one, and embed the second one in the first. |
2360 | the rest in a second one, and embed the second one in the first. |
2233 | .PP |
2361 | .PP |
2234 | As long as the watcher is active, the callback will be invoked every time |
2362 | As long as the watcher is active, the callback will be invoked every time |
2235 | there might be events pending in the embedded loop. The callback must then |
2363 | there might be events pending in the embedded loop. The callback must then |
2236 | call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke |
2364 | call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke |
2237 | their callbacks (you could also start an idle watcher to give the embedded |
2365 | their callbacks (you could also start an idle watcher to give the embedded |
… | |
… | |
2245 | interested in that. |
2373 | interested in that. |
2246 | .PP |
2374 | .PP |
2247 | Also, there have not currently been made special provisions for forking: |
2375 | Also, there have not currently been made special provisions for forking: |
2248 | when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops, |
2376 | when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops, |
2249 | but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers |
2377 | but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers |
2250 | yourself. |
2378 | yourself \- but you can use a fork watcher to handle this automatically, |
|
|
2379 | and future versions of libev might do just that. |
2251 | .PP |
2380 | .PP |
2252 | Unfortunately, not all backends are embeddable, only the ones returned by |
2381 | Unfortunately, not all backends are embeddable: only the ones returned by |
2253 | \&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any |
2382 | \&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any |
2254 | portable one. |
2383 | portable one. |
2255 | .PP |
2384 | .PP |
2256 | So when you want to use this feature you will always have to be prepared |
2385 | So when you want to use this feature you will always have to be prepared |
2257 | that you cannot get an embeddable loop. The recommended way to get around |
2386 | that you cannot get an embeddable loop. The recommended way to get around |
2258 | this is to have a separate variables for your embeddable loop, try to |
2387 | this is to have a separate variables for your embeddable loop, try to |
2259 | create it, and if that fails, use the normal loop for everything. |
2388 | create it, and if that fails, use the normal loop for everything. |
|
|
2389 | .PP |
|
|
2390 | \fI\f(CI\*(C`ev_embed\*(C'\fI and fork\fR |
|
|
2391 | .IX Subsection "ev_embed and fork" |
|
|
2392 | .PP |
|
|
2393 | While the \f(CW\*(C`ev_embed\*(C'\fR watcher is running, forks in the embedding loop will |
|
|
2394 | automatically be applied to the embedded loop as well, so no special |
|
|
2395 | fork handling is required in that case. When the watcher is not running, |
|
|
2396 | however, it is still the task of the libev user to call \f(CW\*(C`ev_loop_fork ()\*(C'\fR |
|
|
2397 | as applicable. |
2260 | .PP |
2398 | .PP |
2261 | \fIWatcher-Specific Functions and Data Members\fR |
2399 | \fIWatcher-Specific Functions and Data Members\fR |
2262 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2400 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2263 | .IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4 |
2401 | .IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4 |
2264 | .IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" |
2402 | .IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" |
… | |
… | |
2378 | is that the author does not know of a simple (or any) algorithm for a |
2516 | is that the author does not know of a simple (or any) algorithm for a |
2379 | multiple-writer-single-reader queue that works in all cases and doesn't |
2517 | multiple-writer-single-reader queue that works in all cases and doesn't |
2380 | need elaborate support such as pthreads. |
2518 | need elaborate support such as pthreads. |
2381 | .PP |
2519 | .PP |
2382 | That means that if you want to queue data, you have to provide your own |
2520 | That means that if you want to queue data, you have to provide your own |
2383 | queue. But at least I can tell you would implement locking around your |
2521 | queue. But at least I can tell you how to implement locking around your |
2384 | queue: |
2522 | queue: |
2385 | .IP "queueing from a signal handler context" 4 |
2523 | .IP "queueing from a signal handler context" 4 |
2386 | .IX Item "queueing from a signal handler context" |
2524 | .IX Item "queueing from a signal handler context" |
2387 | To implement race-free queueing, you simply add to the queue in the signal |
2525 | To implement race-free queueing, you simply add to the queue in the signal |
2388 | handler but you block the signal handler in the watcher callback. Here is an example that does that for |
2526 | handler but you block the signal handler in the watcher callback. Here is an example that does that for |
… | |
… | |
2459 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2597 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2460 | .IP "ev_async_init (ev_async *, callback)" 4 |
2598 | .IP "ev_async_init (ev_async *, callback)" 4 |
2461 | .IX Item "ev_async_init (ev_async *, callback)" |
2599 | .IX Item "ev_async_init (ev_async *, callback)" |
2462 | Initialises and configures the async watcher \- it has no parameters of any |
2600 | Initialises and configures the async watcher \- it has no parameters of any |
2463 | kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless, |
2601 | kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless, |
2464 | believe me. |
2602 | trust me. |
2465 | .IP "ev_async_send (loop, ev_async *)" 4 |
2603 | .IP "ev_async_send (loop, ev_async *)" 4 |
2466 | .IX Item "ev_async_send (loop, ev_async *)" |
2604 | .IX Item "ev_async_send (loop, ev_async *)" |
2467 | Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds |
2605 | Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds |
2468 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike |
2606 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike |
2469 | \&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do in other threads, signal or |
2607 | \&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, signal or |
2470 | similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
2608 | similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
2471 | section below on what exactly this means). |
2609 | section below on what exactly this means). |
2472 | .Sp |
2610 | .Sp |
2473 | This call incurs the overhead of a system call only once per loop iteration, |
2611 | This call incurs the overhead of a system call only once per loop iteration, |
2474 | so while the overhead might be noticeable, it doesn't apply to repeated |
2612 | so while the overhead might be noticeable, it doesn't apply to repeated |
… | |
… | |
2662 | .Sp |
2800 | .Sp |
2663 | The prototype of the \f(CW\*(C`function\*(C'\fR must be \f(CW\*(C`void (*)(ev::TYPE &w, int)\*(C'\fR. |
2801 | The prototype of the \f(CW\*(C`function\*(C'\fR must be \f(CW\*(C`void (*)(ev::TYPE &w, int)\*(C'\fR. |
2664 | .Sp |
2802 | .Sp |
2665 | See the method\-\f(CW\*(C`set\*(C'\fR above for more details. |
2803 | See the method\-\f(CW\*(C`set\*(C'\fR above for more details. |
2666 | .Sp |
2804 | .Sp |
2667 | Example: |
2805 | Example: Use a plain function as callback. |
2668 | .Sp |
2806 | .Sp |
2669 | .Vb 2 |
2807 | .Vb 2 |
2670 | \& static void io_cb (ev::io &w, int revents) { } |
2808 | \& static void io_cb (ev::io &w, int revents) { } |
2671 | \& iow.set <io_cb> (); |
2809 | \& iow.set <io_cb> (); |
2672 | .Ve |
2810 | .Ve |
… | |
… | |
2708 | the constructor. |
2846 | the constructor. |
2709 | .PP |
2847 | .PP |
2710 | .Vb 4 |
2848 | .Vb 4 |
2711 | \& class myclass |
2849 | \& class myclass |
2712 | \& { |
2850 | \& { |
2713 | \& ev::io io; void io_cb (ev::io &w, int revents); |
2851 | \& ev::io io ; void io_cb (ev::io &w, int revents); |
2714 | \& ev:idle idle void idle_cb (ev::idle &w, int revents); |
2852 | \& ev::idle idle; void idle_cb (ev::idle &w, int revents); |
2715 | \& |
2853 | \& |
2716 | \& myclass (int fd) |
2854 | \& myclass (int fd) |
2717 | \& { |
2855 | \& { |
2718 | \& io .set <myclass, &myclass::io_cb > (this); |
2856 | \& io .set <myclass, &myclass::io_cb > (this); |
2719 | \& idle.set <myclass, &myclass::idle_cb> (this); |
2857 | \& idle.set <myclass, &myclass::idle_cb> (this); |
… | |
… | |
2731 | .IP "Perl" 4 |
2869 | .IP "Perl" 4 |
2732 | .IX Item "Perl" |
2870 | .IX Item "Perl" |
2733 | The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test |
2871 | The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test |
2734 | libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module, |
2872 | libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module, |
2735 | there are additional modules that implement libev-compatible interfaces |
2873 | there are additional modules that implement libev-compatible interfaces |
2736 | to \f(CW\*(C`libadns\*(C'\fR (\f(CW\*(C`EV::ADNS\*(C'\fR), \f(CW\*(C`Net::SNMP\*(C'\fR (\f(CW\*(C`Net::SNMP::EV\*(C'\fR) and the |
2874 | to \f(CW\*(C`libadns\*(C'\fR (\f(CW\*(C`EV::ADNS\*(C'\fR, but \f(CW\*(C`AnyEvent::DNS\*(C'\fR is preferred nowadays), |
2737 | \&\f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR and \f(CW\*(C`EV::Glib\*(C'\fR). |
2875 | \&\f(CW\*(C`Net::SNMP\*(C'\fR (\f(CW\*(C`Net::SNMP::EV\*(C'\fR) and the \f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR |
|
|
2876 | and \f(CW\*(C`EV::Glib\*(C'\fR). |
2738 | .Sp |
2877 | .Sp |
2739 | It can be found and installed via \s-1CPAN\s0, its homepage is at |
2878 | It can be found and installed via \s-1CPAN\s0, its homepage is at |
2740 | <http://software.schmorp.de/pkg/EV>. |
2879 | <http://software.schmorp.de/pkg/EV>. |
2741 | .IP "Python" 4 |
2880 | .IP "Python" 4 |
2742 | .IX Item "Python" |
2881 | .IX Item "Python" |
… | |
… | |
2753 | more on top of it. It can be found via gem servers. Its homepage is at |
2892 | more on top of it. It can be found via gem servers. Its homepage is at |
2754 | <http://rev.rubyforge.org/>. |
2893 | <http://rev.rubyforge.org/>. |
2755 | .IP "D" 4 |
2894 | .IP "D" 4 |
2756 | .IX Item "D" |
2895 | .IX Item "D" |
2757 | Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to |
2896 | Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to |
2758 | be found at <http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. |
2897 | be found at <http://proj.llucax.com.ar/wiki/evd>. |
2759 | .SH "MACRO MAGIC" |
2898 | .SH "MACRO MAGIC" |
2760 | .IX Header "MACRO MAGIC" |
2899 | .IX Header "MACRO MAGIC" |
2761 | Libev can be compiled with a variety of options, the most fundamental |
2900 | Libev can be compiled with a variety of options, the most fundamental |
2762 | of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) |
2901 | of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) |
2763 | functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. |
2902 | functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. |
… | |
… | |
2930 | .Ve |
3069 | .Ve |
2931 | .Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" |
3070 | .Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" |
2932 | .IX Subsection "PREPROCESSOR SYMBOLS/MACROS" |
3071 | .IX Subsection "PREPROCESSOR SYMBOLS/MACROS" |
2933 | Libev can be configured via a variety of preprocessor symbols you have to |
3072 | Libev can be configured via a variety of preprocessor symbols you have to |
2934 | define before including any of its files. The default in the absence of |
3073 | define before including any of its files. The default in the absence of |
2935 | autoconf is noted for every option. |
3074 | autoconf is documented for every option. |
2936 | .IP "\s-1EV_STANDALONE\s0" 4 |
3075 | .IP "\s-1EV_STANDALONE\s0" 4 |
2937 | .IX Item "EV_STANDALONE" |
3076 | .IX Item "EV_STANDALONE" |
2938 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
3077 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
2939 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
3078 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
2940 | implementations for some libevent functions (such as logging, which is not |
3079 | implementations for some libevent functions (such as logging, which is not |
… | |
… | |
3088 | When doing priority-based operations, libev usually has to linearly search |
3227 | When doing priority-based operations, libev usually has to linearly search |
3089 | all the priorities, so having many of them (hundreds) uses a lot of space |
3228 | all the priorities, so having many of them (hundreds) uses a lot of space |
3090 | and time, so using the defaults of five priorities (\-2 .. +2) is usually |
3229 | and time, so using the defaults of five priorities (\-2 .. +2) is usually |
3091 | fine. |
3230 | fine. |
3092 | .Sp |
3231 | .Sp |
3093 | If your embedding application does not need any priorities, defining these both to |
3232 | If your embedding application does not need any priorities, defining these |
3094 | \&\f(CW0\fR will save some memory and \s-1CPU\s0. |
3233 | both to \f(CW0\fR will save some memory and \s-1CPU\s0. |
3095 | .IP "\s-1EV_PERIODIC_ENABLE\s0" 4 |
3234 | .IP "\s-1EV_PERIODIC_ENABLE\s0" 4 |
3096 | .IX Item "EV_PERIODIC_ENABLE" |
3235 | .IX Item "EV_PERIODIC_ENABLE" |
3097 | If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If |
3236 | If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If |
3098 | defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of |
3237 | defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of |
3099 | code. |
3238 | code. |
… | |
… | |
3103 | defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of |
3242 | defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of |
3104 | code. |
3243 | code. |
3105 | .IP "\s-1EV_EMBED_ENABLE\s0" 4 |
3244 | .IP "\s-1EV_EMBED_ENABLE\s0" 4 |
3106 | .IX Item "EV_EMBED_ENABLE" |
3245 | .IX Item "EV_EMBED_ENABLE" |
3107 | If undefined or defined to be \f(CW1\fR, then embed watchers are supported. If |
3246 | If undefined or defined to be \f(CW1\fR, then embed watchers are supported. If |
3108 | defined to be \f(CW0\fR, then they are not. |
3247 | defined to be \f(CW0\fR, then they are not. Embed watchers rely on most other |
|
|
3248 | watcher types, which therefore must not be disabled. |
3109 | .IP "\s-1EV_STAT_ENABLE\s0" 4 |
3249 | .IP "\s-1EV_STAT_ENABLE\s0" 4 |
3110 | .IX Item "EV_STAT_ENABLE" |
3250 | .IX Item "EV_STAT_ENABLE" |
3111 | If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If |
3251 | If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If |
3112 | defined to be \f(CW0\fR, then they are not. |
3252 | defined to be \f(CW0\fR, then they are not. |
3113 | .IP "\s-1EV_FORK_ENABLE\s0" 4 |
3253 | .IP "\s-1EV_FORK_ENABLE\s0" 4 |
… | |
… | |
3138 | watchers you might want to increase this value (\fImust\fR be a power of |
3278 | watchers you might want to increase this value (\fImust\fR be a power of |
3139 | two). |
3279 | two). |
3140 | .IP "\s-1EV_USE_4HEAP\s0" 4 |
3280 | .IP "\s-1EV_USE_4HEAP\s0" 4 |
3141 | .IX Item "EV_USE_4HEAP" |
3281 | .IX Item "EV_USE_4HEAP" |
3142 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
3282 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
3143 | timer and periodics heap, libev uses a 4\-heap when this symbol is defined |
3283 | timer and periodics heaps, libev uses a 4\-heap when this symbol is defined |
3144 | to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has |
3284 | to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has noticeably |
3145 | noticeably faster performance with many (thousands) of watchers. |
3285 | faster performance with many (thousands) of watchers. |
3146 | .Sp |
3286 | .Sp |
3147 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
3287 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
3148 | (disabled). |
3288 | (disabled). |
3149 | .IP "\s-1EV_HEAP_CACHE_AT\s0" 4 |
3289 | .IP "\s-1EV_HEAP_CACHE_AT\s0" 4 |
3150 | .IX Item "EV_HEAP_CACHE_AT" |
3290 | .IX Item "EV_HEAP_CACHE_AT" |
3151 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
3291 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
3152 | timer and periodics heap, libev can cache the timestamp (\fIat\fR) within |
3292 | timer and periodics heaps, libev can cache the timestamp (\fIat\fR) within |
3153 | the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR), |
3293 | the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR), |
3154 | which uses 8\-12 bytes more per watcher and a few hundred bytes more code, |
3294 | which uses 8\-12 bytes more per watcher and a few hundred bytes more code, |
3155 | but avoids random read accesses on heap changes. This improves performance |
3295 | but avoids random read accesses on heap changes. This improves performance |
3156 | noticeably with with many (hundreds) of watchers. |
3296 | noticeably with many (hundreds) of watchers. |
3157 | .Sp |
3297 | .Sp |
3158 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
3298 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
3159 | (disabled). |
3299 | (disabled). |
3160 | .IP "\s-1EV_VERIFY\s0" 4 |
3300 | .IP "\s-1EV_VERIFY\s0" 4 |
3161 | .IX Item "EV_VERIFY" |
3301 | .IX Item "EV_VERIFY" |
… | |
… | |
3166 | called once per loop, which can slow down libev. If set to \f(CW3\fR, then the |
3306 | called once per loop, which can slow down libev. If set to \f(CW3\fR, then the |
3167 | verification code will be called very frequently, which will slow down |
3307 | verification code will be called very frequently, which will slow down |
3168 | libev considerably. |
3308 | libev considerably. |
3169 | .Sp |
3309 | .Sp |
3170 | The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be |
3310 | The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be |
3171 | \&\f(CW0.\fR |
3311 | \&\f(CW0\fR. |
3172 | .IP "\s-1EV_COMMON\s0" 4 |
3312 | .IP "\s-1EV_COMMON\s0" 4 |
3173 | .IX Item "EV_COMMON" |
3313 | .IX Item "EV_COMMON" |
3174 | By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining |
3314 | By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining |
3175 | this macro to a something else you can include more and other types of |
3315 | this macro to a something else you can include more and other types of |
3176 | members. You have to define it each time you include one of the files, |
3316 | members. You have to define it each time you include one of the files, |
… | |
… | |
3262 | .Ve |
3402 | .Ve |
3263 | .SH "THREADS AND COROUTINES" |
3403 | .SH "THREADS AND COROUTINES" |
3264 | .IX Header "THREADS AND COROUTINES" |
3404 | .IX Header "THREADS AND COROUTINES" |
3265 | .Sh "\s-1THREADS\s0" |
3405 | .Sh "\s-1THREADS\s0" |
3266 | .IX Subsection "THREADS" |
3406 | .IX Subsection "THREADS" |
3267 | Libev itself is completely thread-safe, but it uses no locking. This |
3407 | All libev functions are reentrant and thread-safe unless explicitly |
|
|
3408 | documented otherwise, but it uses no locking itself. This means that you |
3268 | means that you can use as many loops as you want in parallel, as long as |
3409 | can use as many loops as you want in parallel, as long as there are no |
3269 | only one thread ever calls into one libev function with the same loop |
3410 | concurrent calls into any libev function with the same loop parameter |
3270 | parameter. |
3411 | (\f(CW\*(C`ev_default_*\*(C'\fR calls have an implicit default loop parameter, of |
|
|
3412 | course): libev guarantees that different event loops share no data |
|
|
3413 | structures that need any locking. |
3271 | .PP |
3414 | .PP |
3272 | Or put differently: calls with different loop parameters can be done in |
3415 | Or to put it differently: calls with different loop parameters can be done |
3273 | parallel from multiple threads, calls with the same loop parameter must be |
3416 | concurrently from multiple threads, calls with the same loop parameter |
3274 | done serially (but can be done from different threads, as long as only one |
3417 | must be done serially (but can be done from different threads, as long as |
3275 | thread ever is inside a call at any point in time, e.g. by using a mutex |
3418 | only one thread ever is inside a call at any point in time, e.g. by using |
3276 | per loop). |
3419 | a mutex per loop). |
|
|
3420 | .PP |
|
|
3421 | Specifically to support threads (and signal handlers), libev implements |
|
|
3422 | so-called \f(CW\*(C`ev_async\*(C'\fR watchers, which allow some limited form of |
|
|
3423 | concurrency on the same event loop, namely waking it up \*(L"from the |
|
|
3424 | outside\*(R". |
3277 | .PP |
3425 | .PP |
3278 | If you want to know which design (one loop, locking, or multiple loops |
3426 | If you want to know which design (one loop, locking, or multiple loops |
3279 | without or something else still) is best for your problem, then I cannot |
3427 | without or something else still) is best for your problem, then I cannot |
3280 | help you. I can give some generic advice however: |
3428 | help you, but here is some generic advice: |
3281 | .IP "\(bu" 4 |
3429 | .IP "\(bu" 4 |
3282 | most applications have a main thread: use the default libev loop |
3430 | most applications have a main thread: use the default libev loop |
3283 | in that thread, or create a separate thread running only the default loop. |
3431 | in that thread, or create a separate thread running only the default loop. |
3284 | .Sp |
3432 | .Sp |
3285 | This helps integrating other libraries or software modules that use libev |
3433 | This helps integrating other libraries or software modules that use libev |
… | |
… | |
3295 | .Sp |
3443 | .Sp |
3296 | Choosing a model is hard \- look around, learn, know that usually you can do |
3444 | Choosing a model is hard \- look around, learn, know that usually you can do |
3297 | better than you currently do :\-) |
3445 | better than you currently do :\-) |
3298 | .IP "\(bu" 4 |
3446 | .IP "\(bu" 4 |
3299 | often you need to talk to some other thread which blocks in the |
3447 | often you need to talk to some other thread which blocks in the |
|
|
3448 | event loop. |
|
|
3449 | .Sp |
3300 | event loop \- \f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other |
3450 | \&\f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other threads safely |
3301 | threads safely (or from signal contexts...). |
3451 | (or from signal contexts...). |
|
|
3452 | .Sp |
|
|
3453 | An example use would be to communicate signals or other events that only |
|
|
3454 | work in the default loop by registering the signal watcher with the |
|
|
3455 | default loop and triggering an \f(CW\*(C`ev_async\*(C'\fR watcher from the default loop |
|
|
3456 | watcher callback into the event loop interested in the signal. |
3302 | .Sh "\s-1COROUTINES\s0" |
3457 | .Sh "\s-1COROUTINES\s0" |
3303 | .IX Subsection "COROUTINES" |
3458 | .IX Subsection "COROUTINES" |
3304 | Libev is much more accommodating to coroutines (\*(L"cooperative threads\*(R"): |
3459 | Libev is much more accommodating to coroutines (\*(L"cooperative threads\*(R"): |
3305 | libev fully supports nesting calls to it's functions from different |
3460 | libev fully supports nesting calls to it's functions from different |
3306 | coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two |
3461 | coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two |
3307 | different coroutines and switch freely between both coroutines running the |
3462 | different coroutines and switch freely between both coroutines running the |
3308 | loop, as long as you don't confuse yourself). The only exception is that |
3463 | loop, as long as you don't confuse yourself). The only exception is that |
3309 | you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks. |
3464 | you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks. |
3310 | .PP |
3465 | .PP |
3311 | Care has been invested into making sure that libev does not keep local |
3466 | Care has been taken to ensure that libev does not keep local state inside |
3312 | state inside \f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine |
3467 | \&\f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine switches. |
3313 | switches. |
|
|
3314 | .SH "COMPLEXITIES" |
3468 | .SH "COMPLEXITIES" |
3315 | .IX Header "COMPLEXITIES" |
3469 | .IX Header "COMPLEXITIES" |
3316 | In this section the complexities of (many of) the algorithms used inside |
3470 | In this section the complexities of (many of) the algorithms used inside |
3317 | libev will be explained. For complexity discussions about backends see the |
3471 | libev will be explained. For complexity discussions about backends see the |
3318 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
3472 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
… | |
… | |
3359 | .IX Item "Priority handling: O(number_of_priorities)" |
3513 | .IX Item "Priority handling: O(number_of_priorities)" |
3360 | .PD |
3514 | .PD |
3361 | Priorities are implemented by allocating some space for each |
3515 | Priorities are implemented by allocating some space for each |
3362 | priority. When doing priority-based operations, libev usually has to |
3516 | priority. When doing priority-based operations, libev usually has to |
3363 | linearly search all the priorities, but starting/stopping and activating |
3517 | linearly search all the priorities, but starting/stopping and activating |
3364 | watchers becomes O(1) w.r.t. priority handling. |
3518 | watchers becomes O(1) with respect to priority handling. |
3365 | .IP "Sending an ev_async: O(1)" 4 |
3519 | .IP "Sending an ev_async: O(1)" 4 |
3366 | .IX Item "Sending an ev_async: O(1)" |
3520 | .IX Item "Sending an ev_async: O(1)" |
3367 | .PD 0 |
3521 | .PD 0 |
3368 | .IP "Processing ev_async_send: O(number_of_async_watchers)" 4 |
3522 | .IP "Processing ev_async_send: O(number_of_async_watchers)" 4 |
3369 | .IX Item "Processing ev_async_send: O(number_of_async_watchers)" |
3523 | .IX Item "Processing ev_async_send: O(number_of_async_watchers)" |
… | |
… | |
3392 | .PP |
3546 | .PP |
3393 | Not a libev limitation but worth mentioning: windows apparently doesn't |
3547 | Not a libev limitation but worth mentioning: windows apparently doesn't |
3394 | accept large writes: instead of resulting in a partial write, windows will |
3548 | accept large writes: instead of resulting in a partial write, windows will |
3395 | either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large, |
3549 | either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large, |
3396 | so make sure you only write small amounts into your sockets (less than a |
3550 | so make sure you only write small amounts into your sockets (less than a |
3397 | megabyte seems safe, but thsi apparently depends on the amount of memory |
3551 | megabyte seems safe, but this apparently depends on the amount of memory |
3398 | available). |
3552 | available). |
3399 | .PP |
3553 | .PP |
3400 | Due to the many, low, and arbitrary limits on the win32 platform and |
3554 | Due to the many, low, and arbitrary limits on the win32 platform and |
3401 | the abysmal performance of winsockets, using a large number of sockets |
3555 | the abysmal performance of winsockets, using a large number of sockets |
3402 | is not recommended (and not reasonable). If your program needs to use |
3556 | is not recommended (and not reasonable). If your program needs to use |
… | |
… | |
3415 | \& |
3569 | \& |
3416 | \& #include "ev.h" |
3570 | \& #include "ev.h" |
3417 | .Ve |
3571 | .Ve |
3418 | .PP |
3572 | .PP |
3419 | And compile the following \fIevwrap.c\fR file into your project (make sure |
3573 | And compile the following \fIevwrap.c\fR file into your project (make sure |
3420 | you do \fInot\fR compile the \fIev.c\fR or any other embedded soruce files!): |
3574 | you do \fInot\fR compile the \fIev.c\fR or any other embedded source files!): |
3421 | .PP |
3575 | .PP |
3422 | .Vb 2 |
3576 | .Vb 2 |
3423 | \& #include "evwrap.h" |
3577 | \& #include "evwrap.h" |
3424 | \& #include "ev.c" |
3578 | \& #include "ev.c" |
3425 | .Ve |
3579 | .Ve |
… | |
… | |
3483 | calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally. |
3637 | calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally. |
3484 | .ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4 |
3638 | .ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4 |
3485 | .el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4 |
3639 | .el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4 |
3486 | .IX Item "sig_atomic_t volatile must be thread-atomic as well" |
3640 | .IX Item "sig_atomic_t volatile must be thread-atomic as well" |
3487 | The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as |
3641 | The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as |
3488 | \&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic w.r.t. accesses from different |
3642 | \&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic with respect to accesses from different |
3489 | threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is |
3643 | threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is |
3490 | believed to be sufficiently portable. |
3644 | believed to be sufficiently portable. |
3491 | .ie n .IP """sigprocmask"" must work in a threaded environment" 4 |
3645 | .ie n .IP """sigprocmask"" must work in a threaded environment" 4 |
3492 | .el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4 |
3646 | .el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4 |
3493 | .IX Item "sigprocmask must work in a threaded environment" |
3647 | .IX Item "sigprocmask must work in a threaded environment" |
… | |
… | |
3569 | If you need, for some reason, empty reports from valgrind for your project |
3723 | If you need, for some reason, empty reports from valgrind for your project |
3570 | I suggest using suppression lists. |
3724 | I suggest using suppression lists. |
3571 | .SH "AUTHOR" |
3725 | .SH "AUTHOR" |
3572 | .IX Header "AUTHOR" |
3726 | .IX Header "AUTHOR" |
3573 | Marc Lehmann <libev@schmorp.de>. |
3727 | Marc Lehmann <libev@schmorp.de>. |
3574 | .SH "POD ERRORS" |
|
|
3575 | .IX Header "POD ERRORS" |
|
|
3576 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
|
|
3577 | .IP "Around line 3122:" 4 |
|
|
3578 | .IX Item "Around line 3122:" |
|
|
3579 | You forgot a '=back' before '=head2' |
|
|