… | |
… | |
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 |
… | |
… | |
1878 | // timeout occurred, take action |
1878 | // timeout occurred, take action |
1879 | } |
1879 | } |
1880 | else |
1880 | else |
1881 | { |
1881 | { |
1882 | // callback was invoked, but there was some recent |
1882 | // callback was invoked, but there was some recent |
1883 | // activity. simply restart the timer to time out |
1883 | // activity. simply restart the timer to time out |
1884 | // after "after" seconds, which is the earliest time |
1884 | // after "after" seconds, which is the earliest time |
1885 | // the timeout can occur. |
1885 | // the timeout can occur. |
1886 | ev_timer_set (w, after, 0.); |
1886 | ev_timer_set (w, after, 0.); |
1887 | ev_timer_start (EV_A_ w); |
1887 | ev_timer_start (EV_A_ w); |
1888 | } |
1888 | } |
… | |
… | |
2108 | keep up with the timer (because it takes longer than those 10 seconds to |
2108 | 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. |
2109 | do stuff) the timer will not fire more than once per event loop iteration. |
2110 | |
2110 | |
2111 | =item ev_timer_again (loop, ev_timer *) |
2111 | =item ev_timer_again (loop, ev_timer *) |
2112 | |
2112 | |
2113 | This will act as if the timer timed out and restarts it again if it is |
2113 | This will act as if the timer timed out, and restarts it again if it is |
2114 | repeating. The exact semantics are: |
2114 | repeating. It basically works like calling C<ev_timer_stop>, updating the |
|
|
2115 | timeout to the C<repeat> value and calling C<ev_timer_start>. |
2115 | |
2116 | |
|
|
2117 | The exact semantics are as in the following rules, all of which will be |
|
|
2118 | applied to the watcher: |
|
|
2119 | |
|
|
2120 | =over 4 |
|
|
2121 | |
2116 | If the timer is pending, its pending status is cleared. |
2122 | =item If the timer is pending, the pending status is always cleared. |
2117 | |
2123 | |
2118 | If the timer is started but non-repeating, stop it (as if it timed out). |
2124 | =item If the timer is started but non-repeating, stop it (as if it timed |
|
|
2125 | out, without invoking it). |
2119 | |
2126 | |
2120 | If the timer is repeating, either start it if necessary (with the |
2127 | =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. |
2128 | and start the timer, if necessary. |
|
|
2129 | |
|
|
2130 | =back |
2122 | |
2131 | |
2123 | This sounds a bit complicated, see L<Be smart about timeouts>, above, for a |
2132 | This sounds a bit complicated, see L<Be smart about timeouts>, above, for a |
2124 | usage example. |
2133 | usage example. |
2125 | |
2134 | |
2126 | =item ev_tstamp ev_timer_remaining (loop, ev_timer *) |
2135 | =item ev_tstamp ev_timer_remaining (loop, ev_timer *) |
… | |
… | |
3633 | int exit_main_loop = 0; |
3642 | int exit_main_loop = 0; |
3634 | |
3643 | |
3635 | while (!exit_main_loop) |
3644 | while (!exit_main_loop) |
3636 | ev_run (EV_DEFAULT_ EVRUN_ONCE); |
3645 | ev_run (EV_DEFAULT_ EVRUN_ONCE); |
3637 | |
3646 | |
3638 | // in a model watcher |
3647 | // in a modal watcher |
3639 | int exit_nested_loop = 0; |
3648 | int exit_nested_loop = 0; |
3640 | |
3649 | |
3641 | while (!exit_nested_loop) |
3650 | while (!exit_nested_loop) |
3642 | ev_run (EV_A_ EVRUN_ONCE); |
3651 | ev_run (EV_A_ EVRUN_ONCE); |
3643 | |
3652 | |
… | |
… | |
3823 | switch_to (libev_coro); |
3832 | switch_to (libev_coro); |
3824 | } |
3833 | } |
3825 | |
3834 | |
3826 | That basically suspends the coroutine inside C<wait_for_event> and |
3835 | That basically suspends the coroutine inside C<wait_for_event> and |
3827 | continues the libev coroutine, which, when appropriate, switches back to |
3836 | continues the libev coroutine, which, when appropriate, switches back to |
3828 | this or any other coroutine. I am sure if you sue this your own :) |
3837 | this or any other coroutine. |
3829 | |
3838 | |
3830 | You can do similar tricks if you have, say, threads with an event queue - |
3839 | You can do similar tricks if you have, say, threads with an event queue - |
3831 | instead of storing a coroutine, you store the queue object and instead of |
3840 | instead of storing a coroutine, you store the queue object and instead of |
3832 | switching to a coroutine, you push the watcher onto the queue and notify |
3841 | switching to a coroutine, you push the watcher onto the queue and notify |
3833 | any waiters. |
3842 | any waiters. |
… | |
… | |
3908 | with C<operator ()> can be used as callbacks. Other types should be easy |
3917 | 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 |
3918 | 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 |
3919 | you need support for other types of functors please contact the author |
3911 | (preferably after implementing it). |
3920 | (preferably after implementing it). |
3912 | |
3921 | |
|
|
3922 | For all this to work, your C++ compiler either has to use the same calling |
|
|
3923 | conventions as your C compiler (for static member functions), or you have |
|
|
3924 | to embed libev and compile libev itself as C++. |
|
|
3925 | |
3913 | Here is a list of things available in the C<ev> namespace: |
3926 | Here is a list of things available in the C<ev> namespace: |
3914 | |
3927 | |
3915 | =over 4 |
3928 | =over 4 |
3916 | |
3929 | |
3917 | =item C<ev::READ>, C<ev::WRITE> etc. |
3930 | =item C<ev::READ>, C<ev::WRITE> etc. |
… | |
… | |
3926 | =item C<ev::io>, C<ev::timer>, C<ev::periodic>, C<ev::idle>, C<ev::sig> etc. |
3939 | =item C<ev::io>, C<ev::timer>, C<ev::periodic>, C<ev::idle>, C<ev::sig> etc. |
3927 | |
3940 | |
3928 | For each C<ev_TYPE> watcher in F<ev.h> there is a corresponding class of |
3941 | For each C<ev_TYPE> watcher in F<ev.h> there is a corresponding class of |
3929 | the same name in the C<ev> namespace, with the exception of C<ev_signal> |
3942 | the same name in the C<ev> namespace, with the exception of C<ev_signal> |
3930 | which is called C<ev::sig> to avoid clashes with the C<signal> macro |
3943 | which is called C<ev::sig> to avoid clashes with the C<signal> macro |
3931 | defines by many implementations. |
3944 | defined by many implementations. |
3932 | |
3945 | |
3933 | All of those classes have these methods: |
3946 | All of those classes have these methods: |
3934 | |
3947 | |
3935 | =over 4 |
3948 | =over 4 |
3936 | |
3949 | |
… | |
… | |
4495 | If defined to be C<1>, libev will compile in support for the Linux inotify |
4508 | 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 |
4509 | 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 |
4510 | be detected at runtime. If undefined, it will be enabled if the headers |
4498 | indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
4511 | indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
4499 | |
4512 | |
|
|
4513 | =item EV_NO_SMP |
|
|
4514 | |
|
|
4515 | If defined to be C<1>, libev will assume that memory is always coherent |
|
|
4516 | between threads, that is, threads can be used, but threads never run on |
|
|
4517 | different cpus (or different cpu cores). This reduces dependencies |
|
|
4518 | and makes libev faster. |
|
|
4519 | |
|
|
4520 | =item EV_NO_THREADS |
|
|
4521 | |
|
|
4522 | If defined to be C<1>, libev will assume that it will never be called |
|
|
4523 | from different threads, which is a stronger assumption than C<EV_NO_SMP>, |
|
|
4524 | above. This reduces dependencies and makes libev faster. |
|
|
4525 | |
4500 | =item EV_ATOMIC_T |
4526 | =item EV_ATOMIC_T |
4501 | |
4527 | |
4502 | Libev requires an integer type (suitable for storing C<0> or C<1>) whose |
4528 | 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 |
4529 | 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 |
4530 | 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, |
4688 | when you embed libev, only want to use libev functions in a single file, |
4663 | and do not want its identifiers to be visible. |
4689 | and do not want its identifiers to be visible. |
4664 | |
4690 | |
4665 | To use this, define C<EV_API_STATIC> and include F<ev.c> in the file that |
4691 | To use this, define C<EV_API_STATIC> and include F<ev.c> in the file that |
4666 | wants to use libev. |
4692 | wants to use libev. |
|
|
4693 | |
|
|
4694 | This option only works when libev is compiled with a C compiler, as C++ |
|
|
4695 | doesn't support the required declaration syntax. |
4667 | |
4696 | |
4668 | =item EV_AVOID_STDIO |
4697 | =item EV_AVOID_STDIO |
4669 | |
4698 | |
4670 | If this is set to C<1> at compiletime, then libev will avoid using stdio |
4699 | 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 |
4700 | functions (printf, scanf, perror etc.). This will increase the code size |