… | |
… | |
633 | This function is rarely useful, but when some event callback runs for a |
633 | This function is rarely useful, but when some event callback runs for a |
634 | very long time without entering the event loop, updating libev's idea of |
634 | very long time without entering the event loop, updating libev's idea of |
635 | the current time is a good idea. |
635 | the current time is a good idea. |
636 | |
636 | |
637 | See also "The special problem of time updates" in the C<ev_timer> section. |
637 | See also "The special problem of time updates" in the C<ev_timer> section. |
|
|
638 | |
|
|
639 | =item ev_suspend (loop) |
|
|
640 | |
|
|
641 | =item ev_resume (loop) |
|
|
642 | |
|
|
643 | These two functions suspend and resume a loop, for use when the loop is |
|
|
644 | not used for a while and timeouts should not be processed. |
|
|
645 | |
|
|
646 | A typical use case would be an interactive program such as a game: When |
|
|
647 | the user presses C<^Z> to suspend the game and resumes it an hour later it |
|
|
648 | would be best to handle timeouts as if no time had actually passed while |
|
|
649 | the program was suspended. This can be achieved by calling C<ev_suspend> |
|
|
650 | in your C<SIGTSTP> handler, sending yourself a C<SIGSTOP> and calling |
|
|
651 | C<ev_resume> directly afterwards to resume timer processing. |
|
|
652 | |
|
|
653 | Effectively, all C<ev_timer> watchers will be delayed by the time spend |
|
|
654 | between C<ev_suspend> and C<ev_resume>, and all C<ev_periodic> watchers |
|
|
655 | will be rescheduled (that is, they will lose any events that would have |
|
|
656 | occured while suspended). |
|
|
657 | |
|
|
658 | After calling C<ev_suspend> you B<must not> call I<any> function on the |
|
|
659 | given loop other than C<ev_resume>, and you B<must not> call C<ev_resume> |
|
|
660 | without a previous call to C<ev_suspend>. |
|
|
661 | |
|
|
662 | Calling C<ev_suspend>/C<ev_resume> has the side effect of updating the |
|
|
663 | event loop time (see C<ev_now_update>). |
638 | |
664 | |
639 | =item ev_loop (loop, int flags) |
665 | =item ev_loop (loop, int flags) |
640 | |
666 | |
641 | Finally, this is it, the event handler. This function usually is called |
667 | Finally, this is it, the event handler. This function usually is called |
642 | after you initialised all your watchers and you want to start handling |
668 | after you initialised all your watchers and you want to start handling |
… | |
… | |
726 | |
752 | |
727 | If you have a watcher you never unregister that should not keep C<ev_loop> |
753 | If you have a watcher you never unregister that should not keep C<ev_loop> |
728 | from returning, call ev_unref() after starting, and ev_ref() before |
754 | from returning, call ev_unref() after starting, and ev_ref() before |
729 | stopping it. |
755 | stopping it. |
730 | |
756 | |
731 | As an example, libev itself uses this for its internal signal pipe: It is |
757 | As an example, libev itself uses this for its internal signal pipe: It |
732 | not visible to the libev user and should not keep C<ev_loop> from exiting |
758 | is not visible to the libev user and should not keep C<ev_loop> from |
733 | if no event watchers registered by it are active. It is also an excellent |
759 | exiting if no event watchers registered by it are active. It is also an |
734 | way to do this for generic recurring timers or from within third-party |
760 | excellent way to do this for generic recurring timers or from within |
735 | libraries. Just remember to I<unref after start> and I<ref before stop> |
761 | third-party libraries. Just remember to I<unref after start> and I<ref |
736 | (but only if the watcher wasn't active before, or was active before, |
762 | before stop> (but only if the watcher wasn't active before, or was active |
737 | respectively). |
763 | before, respectively. Note also that libev might stop watchers itself |
|
|
764 | (e.g. non-repeating timers) in which case you have to C<ev_ref> |
|
|
765 | in the callback). |
738 | |
766 | |
739 | Example: Create a signal watcher, but keep it from keeping C<ev_loop> |
767 | Example: Create a signal watcher, but keep it from keeping C<ev_loop> |
740 | running when nothing else is active. |
768 | running when nothing else is active. |
741 | |
769 | |
742 | ev_signal exitsig; |
770 | ev_signal exitsig; |
… | |
… | |
925 | C<ev_fork>). |
953 | C<ev_fork>). |
926 | |
954 | |
927 | =item C<EV_ASYNC> |
955 | =item C<EV_ASYNC> |
928 | |
956 | |
929 | The given async watcher has been asynchronously notified (see C<ev_async>). |
957 | The given async watcher has been asynchronously notified (see C<ev_async>). |
|
|
958 | |
|
|
959 | =item C<EV_CUSTOM> |
|
|
960 | |
|
|
961 | Not ever sent (or otherwise used) by libev itself, but can be freely used |
|
|
962 | by libev users to signal watchers (e.g. via C<ev_feed_event>). |
930 | |
963 | |
931 | =item C<EV_ERROR> |
964 | =item C<EV_ERROR> |
932 | |
965 | |
933 | An unspecified error has occurred, the watcher has been stopped. This might |
966 | An unspecified error has occurred, the watcher has been stopped. This might |
934 | happen because the watcher could not be properly started because libev |
967 | happen because the watcher could not be properly started because libev |
… | |
… | |
1317 | year, it will still time out after (roughly) one hour. "Roughly" because |
1350 | year, it will still time out after (roughly) one hour. "Roughly" because |
1318 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1351 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1319 | monotonic clock option helps a lot here). |
1352 | monotonic clock option helps a lot here). |
1320 | |
1353 | |
1321 | The callback is guaranteed to be invoked only I<after> its timeout has |
1354 | The callback is guaranteed to be invoked only I<after> its timeout has |
1322 | passed, but if multiple timers become ready during the same loop iteration |
1355 | passed. If multiple timers become ready during the same loop iteration |
1323 | then order of execution is undefined. |
1356 | then the ones with earlier time-out values are invoked before ones with |
|
|
1357 | later time-out values (but this is no longer true when a callback calls |
|
|
1358 | C<ev_loop> recursively). |
1324 | |
1359 | |
1325 | =head3 Be smart about timeouts |
1360 | =head3 Be smart about timeouts |
1326 | |
1361 | |
1327 | Many real-world problems involve some kind of timeout, usually for error |
1362 | Many real-world problems involve some kind of timeout, usually for error |
1328 | recovery. A typical example is an HTTP request - if the other side hangs, |
1363 | recovery. A typical example is an HTTP request - if the other side hangs, |
… | |
… | |
1617 | timers, such as triggering an event on each "midnight, local time", or |
1652 | timers, such as triggering an event on each "midnight, local time", or |
1618 | other complicated rules. This cannot be done with C<ev_timer> watchers, as |
1653 | other complicated rules. This cannot be done with C<ev_timer> watchers, as |
1619 | those cannot react to time jumps. |
1654 | those cannot react to time jumps. |
1620 | |
1655 | |
1621 | As with timers, the callback is guaranteed to be invoked only when the |
1656 | As with timers, the callback is guaranteed to be invoked only when the |
1622 | point in time where it is supposed to trigger has passed, but if multiple |
1657 | point in time where it is supposed to trigger has passed. If multiple |
1623 | periodic timers become ready during the same loop iteration, then order of |
1658 | timers become ready during the same loop iteration then the ones with |
1624 | execution is undefined. |
1659 | earlier time-out values are invoked before ones with later time-out values |
|
|
1660 | (but this is no longer true when a callback calls C<ev_loop> recursively). |
1625 | |
1661 | |
1626 | =head3 Watcher-Specific Functions and Data Members |
1662 | =head3 Watcher-Specific Functions and Data Members |
1627 | |
1663 | |
1628 | =over 4 |
1664 | =over 4 |
1629 | |
1665 | |
… | |
… | |
3033 | L<http://software.schmorp.de/pkg/EV>. |
3069 | L<http://software.schmorp.de/pkg/EV>. |
3034 | |
3070 | |
3035 | =item Python |
3071 | =item Python |
3036 | |
3072 | |
3037 | Python bindings can be found at L<http://code.google.com/p/pyev/>. It |
3073 | Python bindings can be found at L<http://code.google.com/p/pyev/>. It |
3038 | seems to be quite complete and well-documented. Note, however, that the |
3074 | seems to be quite complete and well-documented. |
3039 | patch they require for libev is outright dangerous as it breaks the ABI |
|
|
3040 | for everybody else, and therefore, should never be applied in an installed |
|
|
3041 | libev (if python requires an incompatible ABI then it needs to embed |
|
|
3042 | libev). |
|
|
3043 | |
3075 | |
3044 | =item Ruby |
3076 | =item Ruby |
3045 | |
3077 | |
3046 | Tony Arcieri has written a ruby extension that offers access to a subset |
3078 | Tony Arcieri has written a ruby extension that offers access to a subset |
3047 | of the libev API and adds file handle abstractions, asynchronous DNS and |
3079 | of the libev API and adds file handle abstractions, asynchronous DNS and |
3048 | more on top of it. It can be found via gem servers. Its homepage is at |
3080 | more on top of it. It can be found via gem servers. Its homepage is at |
3049 | L<http://rev.rubyforge.org/>. |
3081 | L<http://rev.rubyforge.org/>. |
3050 | |
3082 | |
3051 | Roger Pack reports that using the link order C<-lws2_32 -lmsvcrt-ruby-190> |
3083 | Roger Pack reports that using the link order C<-lws2_32 -lmsvcrt-ruby-190> |
3052 | makes rev work even on mingw. |
3084 | makes rev work even on mingw. |
|
|
3085 | |
|
|
3086 | =item Haskell |
|
|
3087 | |
|
|
3088 | A haskell binding to libev is available at |
|
|
3089 | L<http://hackage.haskell.org/cgi-bin/hackage-scripts/package/hlibev>. |
3053 | |
3090 | |
3054 | =item D |
3091 | =item D |
3055 | |
3092 | |
3056 | Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to |
3093 | Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to |
3057 | be found at L<http://proj.llucax.com.ar/wiki/evd>. |
3094 | be found at L<http://proj.llucax.com.ar/wiki/evd>. |