… | |
… | |
620 | happily wraps around with enough iterations. |
620 | happily wraps around with enough iterations. |
621 | |
621 | |
622 | This value can sometimes be useful as a generation counter of sorts (it |
622 | This value can sometimes be useful as a generation counter of sorts (it |
623 | "ticks" the number of loop iterations), as it roughly corresponds with |
623 | "ticks" the number of loop iterations), as it roughly corresponds with |
624 | C<ev_prepare> and C<ev_check> calls. |
624 | C<ev_prepare> and C<ev_check> calls. |
|
|
625 | |
|
|
626 | =item unsigned int ev_loop_depth (loop) |
|
|
627 | |
|
|
628 | Returns the number of times C<ev_loop> was entered minus the number of |
|
|
629 | times C<ev_loop> was exited, in other words, the recursion depth. |
|
|
630 | |
|
|
631 | Outside C<ev_loop>, this number is zero. In a callback, this number is |
|
|
632 | C<1>, unless C<ev_loop> was invoked recursively (or from another thread), |
|
|
633 | in which case it is higher. |
|
|
634 | |
|
|
635 | Leaving C<ev_loop> abnormally (setjmp/longjmp, cancelling the thread |
|
|
636 | etc.), doesn't count as exit. |
625 | |
637 | |
626 | =item unsigned int ev_backend (loop) |
638 | =item unsigned int ev_backend (loop) |
627 | |
639 | |
628 | Returns one of the C<EVBACKEND_*> flags indicating the event backend in |
640 | Returns one of the C<EVBACKEND_*> flags indicating the event backend in |
629 | use. |
641 | use. |
… | |
… | |
811 | |
823 | |
812 | By setting a higher I<io collect interval> you allow libev to spend more |
824 | By setting a higher I<io collect interval> you allow libev to spend more |
813 | time collecting I/O events, so you can handle more events per iteration, |
825 | time collecting I/O events, so you can handle more events per iteration, |
814 | at the cost of increasing latency. Timeouts (both C<ev_periodic> and |
826 | at the cost of increasing latency. Timeouts (both C<ev_periodic> and |
815 | C<ev_timer>) will be not affected. Setting this to a non-null value will |
827 | C<ev_timer>) will be not affected. Setting this to a non-null value will |
816 | introduce an additional C<ev_sleep ()> call into most loop iterations. |
828 | introduce an additional C<ev_sleep ()> call into most loop iterations. The |
|
|
829 | sleep time ensures that libev will not poll for I/O events more often then |
|
|
830 | once per this interval, on average. |
817 | |
831 | |
818 | Likewise, by setting a higher I<timeout collect interval> you allow libev |
832 | Likewise, by setting a higher I<timeout collect interval> you allow libev |
819 | to spend more time collecting timeouts, at the expense of increased |
833 | to spend more time collecting timeouts, at the expense of increased |
820 | latency/jitter/inexactness (the watcher callback will be called |
834 | latency/jitter/inexactness (the watcher callback will be called |
821 | later). C<ev_io> watchers will not be affected. Setting this to a non-null |
835 | later). C<ev_io> watchers will not be affected. Setting this to a non-null |
… | |
… | |
823 | |
837 | |
824 | Many (busy) programs can usually benefit by setting the I/O collect |
838 | Many (busy) programs can usually benefit by setting the I/O collect |
825 | interval to a value near C<0.1> or so, which is often enough for |
839 | interval to a value near C<0.1> or so, which is often enough for |
826 | interactive servers (of course not for games), likewise for timeouts. It |
840 | interactive servers (of course not for games), likewise for timeouts. It |
827 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
841 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
828 | as this approaches the timing granularity of most systems. |
842 | as this approaches the timing granularity of most systems. Note that if |
|
|
843 | you do transactions with the outside world and you can't increase the |
|
|
844 | parallelity, then this setting will limit your transaction rate (if you |
|
|
845 | need to poll once per transaction and the I/O collect interval is 0.01, |
|
|
846 | then you can't do more than 100 transations per second). |
829 | |
847 | |
830 | Setting the I<timeout collect interval> can improve the opportunity for |
848 | Setting the I<timeout collect interval> can improve the opportunity for |
831 | saving power, as the program will "bundle" timer callback invocations that |
849 | saving power, as the program will "bundle" timer callback invocations that |
832 | are "near" in time together, by delaying some, thus reducing the number of |
850 | are "near" in time together, by delaying some, thus reducing the number of |
833 | times the process sleeps and wakes up again. Another useful technique to |
851 | times the process sleeps and wakes up again. Another useful technique to |
834 | reduce iterations/wake-ups is to use C<ev_periodic> watchers and make sure |
852 | reduce iterations/wake-ups is to use C<ev_periodic> watchers and make sure |
835 | they fire on, say, one-second boundaries only. |
853 | they fire on, say, one-second boundaries only. |
|
|
854 | |
|
|
855 | Example: we only need 0.1s timeout granularity, and we wish not to poll |
|
|
856 | more often than 100 times per second: |
|
|
857 | |
|
|
858 | ev_set_timeout_collect_interval (EV_DEFAULT_UC_ 0.1); |
|
|
859 | ev_set_io_collect_interval (EV_DEFAULT_UC_ 0.01); |
|
|
860 | |
|
|
861 | =item ev_invoke_pending (loop) |
|
|
862 | |
|
|
863 | This call will simply invoke all pending watchers while resetting their |
|
|
864 | pending state. Normally, C<ev_loop> does this automatically when required, |
|
|
865 | but when overriding the invoke callback this call comes handy. |
|
|
866 | |
|
|
867 | =item ev_set_invoke_pending_cb (loop, void (*invoke_pending_cb)(EV_P)) |
|
|
868 | |
|
|
869 | This overrides the invoke pending functionality of the loop: Instead of |
|
|
870 | invoking all pending watchers when there are any, C<ev_loop> will call |
|
|
871 | this callback instead. This is useful, for example, when you want to |
|
|
872 | invoke the actual watchers inside another context (another thread etc.). |
|
|
873 | |
|
|
874 | If you want to reset the callback, use C<ev_invoke_pending> as new |
|
|
875 | callback. |
|
|
876 | |
|
|
877 | =item ev_set_loop_release_cb (loop, void (*release)(EV_P), void (*acquire)(EV_P)) |
|
|
878 | |
|
|
879 | Sometimes you want to share the same loop between multiple threads. This |
|
|
880 | can be done relatively simply by putting mutex_lock/unlock calls around |
|
|
881 | each call to a libev function. |
|
|
882 | |
|
|
883 | However, C<ev_loop> can run an indefinite time, so it is not feasible to |
|
|
884 | wait for it to return. One way around this is to wake up the loop via |
|
|
885 | C<ev_unloop> and C<av_async_send>, another way is to set these I<release> |
|
|
886 | and I<acquire> callbacks on the loop. |
|
|
887 | |
|
|
888 | When set, then C<release> will be called just before the thread is |
|
|
889 | suspended waiting for new events, and C<acquire> is called just |
|
|
890 | afterwards. |
|
|
891 | |
|
|
892 | Ideally, C<release> will just call your mutex_unlock function, and |
|
|
893 | C<acquire> will just call the mutex_lock function again. |
|
|
894 | |
|
|
895 | =item ev_set_userdata (loop, void *data) |
|
|
896 | |
|
|
897 | =item ev_userdata (loop) |
|
|
898 | |
|
|
899 | Set and retrieve a single C<void *> associated with a loop. When |
|
|
900 | C<ev_set_userdata> has never been called, then C<ev_userdata> returns |
|
|
901 | C<0.> |
|
|
902 | |
|
|
903 | These two functions can be used to associate arbitrary data with a loop, |
|
|
904 | and are intended solely for the C<invoke_pending_cb>, C<release> and |
|
|
905 | C<acquire> callbacks described above, but of course can be (ab-)used for |
|
|
906 | any other purpose as well. |
836 | |
907 | |
837 | =item ev_loop_verify (loop) |
908 | =item ev_loop_verify (loop) |
838 | |
909 | |
839 | This function only does something when C<EV_VERIFY> support has been |
910 | This function only does something when C<EV_VERIFY> support has been |
840 | compiled in, which is the default for non-minimal builds. It tries to go |
911 | compiled in, which is the default for non-minimal builds. It tries to go |
… | |
… | |
1468 | |
1539 | |
1469 | The callback is guaranteed to be invoked only I<after> its timeout has |
1540 | The callback is guaranteed to be invoked only I<after> its timeout has |
1470 | passed (not I<at>, so on systems with very low-resolution clocks this |
1541 | passed (not I<at>, so on systems with very low-resolution clocks this |
1471 | might introduce a small delay). If multiple timers become ready during the |
1542 | might introduce a small delay). If multiple timers become ready during the |
1472 | same loop iteration then the ones with earlier time-out values are invoked |
1543 | same loop iteration then the ones with earlier time-out values are invoked |
1473 | before ones with later time-out values (but this is no longer true when a |
1544 | before ones of the same priority with later time-out values (but this is |
1474 | callback calls C<ev_loop> recursively). |
1545 | no longer true when a callback calls C<ev_loop> recursively). |
1475 | |
1546 | |
1476 | =head3 Be smart about timeouts |
1547 | =head3 Be smart about timeouts |
1477 | |
1548 | |
1478 | Many real-world problems involve some kind of timeout, usually for error |
1549 | Many real-world problems involve some kind of timeout, usually for error |
1479 | recovery. A typical example is an HTTP request - if the other side hangs, |
1550 | recovery. A typical example is an HTTP request - if the other side hangs, |
… | |
… | |
2007 | but forking and registering a watcher a few event loop iterations later or |
2078 | but forking and registering a watcher a few event loop iterations later or |
2008 | in the next callback invocation is not. |
2079 | in the next callback invocation is not. |
2009 | |
2080 | |
2010 | Only the default event loop is capable of handling signals, and therefore |
2081 | Only the default event loop is capable of handling signals, and therefore |
2011 | you can only register child watchers in the default event loop. |
2082 | you can only register child watchers in the default event loop. |
|
|
2083 | |
|
|
2084 | Due to some design glitches inside libev, child watchers will always be |
|
|
2085 | handled at maximum priority (their priority is set to C<EV_MAXPRI> by |
|
|
2086 | libev) |
2012 | |
2087 | |
2013 | =head3 Process Interaction |
2088 | =head3 Process Interaction |
2014 | |
2089 | |
2015 | Libev grabs C<SIGCHLD> as soon as the default event loop is |
2090 | Libev grabs C<SIGCHLD> as soon as the default event loop is |
2016 | initialised. This is necessary to guarantee proper behaviour even if |
2091 | initialised. This is necessary to guarantee proper behaviour even if |
… | |
… | |
3643 | defined to be C<0>, then they are not. |
3718 | defined to be C<0>, then they are not. |
3644 | |
3719 | |
3645 | =item EV_MINIMAL |
3720 | =item EV_MINIMAL |
3646 | |
3721 | |
3647 | If you need to shave off some kilobytes of code at the expense of some |
3722 | If you need to shave off some kilobytes of code at the expense of some |
3648 | speed, define this symbol to C<1>. Currently this is used to override some |
3723 | speed (but with the full API), define this symbol to C<1>. Currently this |
3649 | inlining decisions, saves roughly 30% code size on amd64. It also selects a |
3724 | is used to override some inlining decisions, saves roughly 30% code size |
3650 | much smaller 2-heap for timer management over the default 4-heap. |
3725 | on amd64. It also selects a much smaller 2-heap for timer management over |
|
|
3726 | the default 4-heap. |
|
|
3727 | |
|
|
3728 | You can save even more by disabling watcher types you do not need |
|
|
3729 | and setting C<EV_MAXPRI> == C<EV_MINPRI>. Also, disabling C<assert> |
|
|
3730 | (C<-DNDEBUG>) will usually reduce code size a lot. |
|
|
3731 | |
|
|
3732 | Defining C<EV_MINIMAL> to C<2> will additionally reduce the core API to |
|
|
3733 | provide a bare-bones event library. See C<ev.h> for details on what parts |
|
|
3734 | of the API are still available, and do not complain if this subset changes |
|
|
3735 | over time. |
3651 | |
3736 | |
3652 | =item EV_PID_HASHSIZE |
3737 | =item EV_PID_HASHSIZE |
3653 | |
3738 | |
3654 | C<ev_child> watchers use a small hash table to distribute workload by |
3739 | C<ev_child> watchers use a small hash table to distribute workload by |
3655 | pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more |
3740 | pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more |
… | |
… | |
3841 | default loop and triggering an C<ev_async> watcher from the default loop |
3926 | default loop and triggering an C<ev_async> watcher from the default loop |
3842 | watcher callback into the event loop interested in the signal. |
3927 | watcher callback into the event loop interested in the signal. |
3843 | |
3928 | |
3844 | =back |
3929 | =back |
3845 | |
3930 | |
|
|
3931 | =head4 THREAD LOCKING EXAMPLE |
|
|
3932 | |
3846 | =head3 COROUTINES |
3933 | =head3 COROUTINES |
3847 | |
3934 | |
3848 | Libev is very accommodating to coroutines ("cooperative threads"): |
3935 | Libev is very accommodating to coroutines ("cooperative threads"): |
3849 | libev fully supports nesting calls to its functions from different |
3936 | libev fully supports nesting calls to its functions from different |
3850 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3937 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
… | |
… | |
4065 | =item C<double> must hold a time value in seconds with enough accuracy |
4152 | =item C<double> must hold a time value in seconds with enough accuracy |
4066 | |
4153 | |
4067 | The type C<double> is used to represent timestamps. It is required to |
4154 | The type C<double> is used to represent timestamps. It is required to |
4068 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
4155 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
4069 | enough for at least into the year 4000. This requirement is fulfilled by |
4156 | enough for at least into the year 4000. This requirement is fulfilled by |
4070 | implementations implementing IEEE 754 (basically all existing ones). |
4157 | implementations implementing IEEE 754, which is basically all existing |
|
|
4158 | ones. With IEEE 754 doubles, you get microsecond accuracy until at least |
|
|
4159 | 2200. |
4071 | |
4160 | |
4072 | =back |
4161 | =back |
4073 | |
4162 | |
4074 | If you know of other additional requirements drop me a note. |
4163 | If you know of other additional requirements drop me a note. |
4075 | |
4164 | |