… | |
… | |
567 | |
567 | |
568 | It scales in the same way as the epoll backend, but the interface to the |
568 | It scales in the same way as the epoll backend, but the interface to the |
569 | kernel is more efficient (which says nothing about its actual speed, of |
569 | kernel is more efficient (which says nothing about its actual speed, of |
570 | course). While stopping, setting and starting an I/O watcher does never |
570 | course). While stopping, setting and starting an I/O watcher does never |
571 | cause an extra system call as with C<EVBACKEND_EPOLL>, it still adds up to |
571 | cause an extra system call as with C<EVBACKEND_EPOLL>, it still adds up to |
572 | two event changes per incident. Support for C<fork ()> is very bad (but |
572 | two event changes per incident. Support for C<fork ()> is very bad (you |
573 | sane, unlike epoll) and it drops fds silently in similarly hard-to-detect |
573 | might have to leak fd's on fork, but it's more sane than epoll) and it |
574 | cases |
574 | drops fds silently in similarly hard-to-detect cases |
575 | |
575 | |
576 | This backend usually performs well under most conditions. |
576 | This backend usually performs well under most conditions. |
577 | |
577 | |
578 | While nominally embeddable in other event loops, this doesn't work |
578 | While nominally embeddable in other event loops, this doesn't work |
579 | everywhere, so you might need to test for this. And since it is broken |
579 | everywhere, so you might need to test for this. And since it is broken |
… | |
… | |
792 | without a previous call to C<ev_suspend>. |
792 | without a previous call to C<ev_suspend>. |
793 | |
793 | |
794 | Calling C<ev_suspend>/C<ev_resume> has the side effect of updating the |
794 | Calling C<ev_suspend>/C<ev_resume> has the side effect of updating the |
795 | event loop time (see C<ev_now_update>). |
795 | event loop time (see C<ev_now_update>). |
796 | |
796 | |
797 | =item ev_run (loop, int flags) |
797 | =item bool ev_run (loop, int flags) |
798 | |
798 | |
799 | Finally, this is it, the event handler. This function usually is called |
799 | Finally, this is it, the event handler. This function usually is called |
800 | after you have initialised all your watchers and you want to start |
800 | after you have initialised all your watchers and you want to start |
801 | handling events. It will ask the operating system for any new events, call |
801 | handling events. It will ask the operating system for any new events, call |
802 | the watcher callbacks, an then repeat the whole process indefinitely: This |
802 | the watcher callbacks, and then repeat the whole process indefinitely: This |
803 | is why event loops are called I<loops>. |
803 | is why event loops are called I<loops>. |
804 | |
804 | |
805 | If the flags argument is specified as C<0>, it will keep handling events |
805 | If the flags argument is specified as C<0>, it will keep handling events |
806 | until either no event watchers are active anymore or C<ev_break> was |
806 | until either no event watchers are active anymore or C<ev_break> was |
807 | called. |
807 | called. |
|
|
808 | |
|
|
809 | The return value is false if there are no more active watchers (which |
|
|
810 | usually means "all jobs done" or "deadlock"), and true in all other cases |
|
|
811 | (which usually means " you should call C<ev_run> again"). |
808 | |
812 | |
809 | Please note that an explicit C<ev_break> is usually better than |
813 | Please note that an explicit C<ev_break> is usually better than |
810 | relying on all watchers to be stopped when deciding when a program has |
814 | relying on all watchers to be stopped when deciding when a program has |
811 | finished (especially in interactive programs), but having a program |
815 | finished (especially in interactive programs), but having a program |
812 | that automatically loops as long as it has to and no longer by virtue |
816 | that automatically loops as long as it has to and no longer by virtue |
813 | of relying on its watchers stopping correctly, that is truly a thing of |
817 | of relying on its watchers stopping correctly, that is truly a thing of |
814 | beauty. |
818 | beauty. |
815 | |
819 | |
816 | This function is also I<mostly> exception-safe - you can break out of |
820 | This function is I<mostly> exception-safe - you can break out of a |
817 | a C<ev_run> call by calling C<longjmp> in a callback, throwing a C++ |
821 | C<ev_run> call by calling C<longjmp> in a callback, throwing a C++ |
818 | exception and so on. This does not decrement the C<ev_depth> value, nor |
822 | exception and so on. This does not decrement the C<ev_depth> value, nor |
819 | will it clear any outstanding C<EVBREAK_ONE> breaks. |
823 | will it clear any outstanding C<EVBREAK_ONE> breaks. |
820 | |
824 | |
821 | A flags value of C<EVRUN_NOWAIT> will look for new events, will handle |
825 | A flags value of C<EVRUN_NOWAIT> will look for new events, will handle |
822 | those events and any already outstanding ones, but will not wait and |
826 | those events and any already outstanding ones, but will not wait and |
… | |
… | |
1878 | // timeout occurred, take action |
1882 | // timeout occurred, take action |
1879 | } |
1883 | } |
1880 | else |
1884 | else |
1881 | { |
1885 | { |
1882 | // callback was invoked, but there was some recent |
1886 | // callback was invoked, but there was some recent |
1883 | // activity. simply restart the timer to time out |
1887 | // activity. simply restart the timer to time out |
1884 | // after "after" seconds, which is the earliest time |
1888 | // after "after" seconds, which is the earliest time |
1885 | // the timeout can occur. |
1889 | // the timeout can occur. |
1886 | ev_timer_set (w, after, 0.); |
1890 | ev_timer_set (w, after, 0.); |
1887 | ev_timer_start (EV_A_ w); |
1891 | ev_timer_start (EV_A_ w); |
1888 | } |
1892 | } |
… | |
… | |
2108 | keep up with the timer (because it takes longer than those 10 seconds to |
2112 | keep up with the timer (because it takes longer than those 10 seconds to |
2109 | do stuff) the timer will not fire more than once per event loop iteration. |
2113 | do stuff) the timer will not fire more than once per event loop iteration. |
2110 | |
2114 | |
2111 | =item ev_timer_again (loop, ev_timer *) |
2115 | =item ev_timer_again (loop, ev_timer *) |
2112 | |
2116 | |
2113 | This will act as if the timer timed out and restarts it again if it is |
2117 | This will act as if the timer timed out, and restarts it again if it is |
2114 | repeating. The exact semantics are: |
2118 | repeating. It basically works like calling C<ev_timer_stop>, updating the |
|
|
2119 | timeout to the C<repeat> value and calling C<ev_timer_start>. |
2115 | |
2120 | |
|
|
2121 | The exact semantics are as in the following rules, all of which will be |
|
|
2122 | applied to the watcher: |
|
|
2123 | |
|
|
2124 | =over 4 |
|
|
2125 | |
2116 | If the timer is pending, its pending status is cleared. |
2126 | =item If the timer is pending, the pending status is always cleared. |
2117 | |
2127 | |
2118 | If the timer is started but non-repeating, stop it (as if it timed out). |
2128 | =item If the timer is started but non-repeating, stop it (as if it timed |
|
|
2129 | out, without invoking it). |
2119 | |
2130 | |
2120 | If the timer is repeating, either start it if necessary (with the |
2131 | =item If the timer is repeating, make the C<repeat> value the new timeout |
2121 | C<repeat> value), or reset the running timer to the C<repeat> value. |
2132 | and start the timer, if necessary. |
|
|
2133 | |
|
|
2134 | =back |
2122 | |
2135 | |
2123 | This sounds a bit complicated, see L<Be smart about timeouts>, above, for a |
2136 | This sounds a bit complicated, see L<Be smart about timeouts>, above, for a |
2124 | usage example. |
2137 | usage example. |
2125 | |
2138 | |
2126 | =item ev_tstamp ev_timer_remaining (loop, ev_timer *) |
2139 | =item ev_tstamp ev_timer_remaining (loop, ev_timer *) |
… | |
… | |
3908 | with C<operator ()> can be used as callbacks. Other types should be easy |
3921 | with C<operator ()> can be used as callbacks. Other types should be easy |
3909 | to add as long as they only need one additional pointer for context. If |
3922 | to add as long as they only need one additional pointer for context. If |
3910 | you need support for other types of functors please contact the author |
3923 | you need support for other types of functors please contact the author |
3911 | (preferably after implementing it). |
3924 | (preferably after implementing it). |
3912 | |
3925 | |
|
|
3926 | For all this to work, your C++ compiler either has to use the same calling |
|
|
3927 | conventions as your C compiler (for static member functions), or you have |
|
|
3928 | to embed libev and compile libev itself as C++. |
|
|
3929 | |
3913 | Here is a list of things available in the C<ev> namespace: |
3930 | Here is a list of things available in the C<ev> namespace: |
3914 | |
3931 | |
3915 | =over 4 |
3932 | =over 4 |
3916 | |
3933 | |
3917 | =item C<ev::READ>, C<ev::WRITE> etc. |
3934 | =item C<ev::READ>, C<ev::WRITE> etc. |
… | |
… | |
4495 | If defined to be C<1>, libev will compile in support for the Linux inotify |
4512 | If defined to be C<1>, libev will compile in support for the Linux inotify |
4496 | interface to speed up C<ev_stat> watchers. Its actual availability will |
4513 | interface to speed up C<ev_stat> watchers. Its actual availability will |
4497 | be detected at runtime. If undefined, it will be enabled if the headers |
4514 | be detected at runtime. If undefined, it will be enabled if the headers |
4498 | indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
4515 | indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
4499 | |
4516 | |
|
|
4517 | =item EV_NO_SMP |
|
|
4518 | |
|
|
4519 | If defined to be C<1>, libev will assume that memory is always coherent |
|
|
4520 | between threads, that is, threads can be used, but threads never run on |
|
|
4521 | different cpus (or different cpu cores). This reduces dependencies |
|
|
4522 | and makes libev faster. |
|
|
4523 | |
|
|
4524 | =item EV_NO_THREADS |
|
|
4525 | |
|
|
4526 | If defined to be C<1>, libev will assume that it will never be called |
|
|
4527 | from different threads, which is a stronger assumption than C<EV_NO_SMP>, |
|
|
4528 | above. This reduces dependencies and makes libev faster. |
|
|
4529 | |
4500 | =item EV_ATOMIC_T |
4530 | =item EV_ATOMIC_T |
4501 | |
4531 | |
4502 | Libev requires an integer type (suitable for storing C<0> or C<1>) whose |
4532 | Libev requires an integer type (suitable for storing C<0> or C<1>) whose |
4503 | access is atomic and serialised with respect to other threads or signal |
4533 | access is atomic and serialised with respect to other threads or signal |
4504 | contexts. No such type is easily found in the C language, so you can |
4534 | contexts. No such type is easily found in the C language, so you can |
… | |
… | |
4662 | when you embed libev, only want to use libev functions in a single file, |
4692 | when you embed libev, only want to use libev functions in a single file, |
4663 | and do not want its identifiers to be visible. |
4693 | and do not want its identifiers to be visible. |
4664 | |
4694 | |
4665 | To use this, define C<EV_API_STATIC> and include F<ev.c> in the file that |
4695 | To use this, define C<EV_API_STATIC> and include F<ev.c> in the file that |
4666 | wants to use libev. |
4696 | wants to use libev. |
|
|
4697 | |
|
|
4698 | This option only works when libev is compiled with a C compiler, as C++ |
|
|
4699 | doesn't support the required declaration syntax. |
4667 | |
4700 | |
4668 | =item EV_AVOID_STDIO |
4701 | =item EV_AVOID_STDIO |
4669 | |
4702 | |
4670 | If this is set to C<1> at compiletime, then libev will avoid using stdio |
4703 | If this is set to C<1> at compiletime, then libev will avoid using stdio |
4671 | functions (printf, scanf, perror etc.). This will increase the code size |
4704 | functions (printf, scanf, perror etc.). This will increase the code size |