… | |
… | |
122 | .\} |
122 | .\} |
123 | .rm #[ #] #H #V #F C |
123 | .rm #[ #] #H #V #F C |
124 | .\" ======================================================================== |
124 | .\" ======================================================================== |
125 | .\" |
125 | .\" |
126 | .IX Title "LIBEV 3" |
126 | .IX Title "LIBEV 3" |
127 | .TH LIBEV 3 "2010-11-03" "libev-4.01" "libev - high performance full featured event loop" |
127 | .TH LIBEV 3 "2011-02-16" "libev-4.04" "libev - high performance full featured event loop" |
128 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
128 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
129 | .\" way too many mistakes in technical documents. |
129 | .\" way too many mistakes in technical documents. |
130 | .if n .ad l |
130 | .if n .ad l |
131 | .nh |
131 | .nh |
132 | .SH "NAME" |
132 | .SH "NAME" |
… | |
… | |
189 | \& ev_timer_start (loop, &timeout_watcher); |
189 | \& ev_timer_start (loop, &timeout_watcher); |
190 | \& |
190 | \& |
191 | \& // now wait for events to arrive |
191 | \& // now wait for events to arrive |
192 | \& ev_run (loop, 0); |
192 | \& ev_run (loop, 0); |
193 | \& |
193 | \& |
194 | \& // unloop was called, so exit |
194 | \& // break was called, so exit |
195 | \& return 0; |
195 | \& return 0; |
196 | \& } |
196 | \& } |
197 | .Ve |
197 | .Ve |
198 | .SH "ABOUT THIS DOCUMENT" |
198 | .SH "ABOUT THIS DOCUMENT" |
199 | .IX Header "ABOUT THIS DOCUMENT" |
199 | .IX Header "ABOUT THIS DOCUMENT" |
… | |
… | |
421 | \& } |
421 | \& } |
422 | \& |
422 | \& |
423 | \& ... |
423 | \& ... |
424 | \& ev_set_syserr_cb (fatal_error); |
424 | \& ev_set_syserr_cb (fatal_error); |
425 | .Ve |
425 | .Ve |
|
|
426 | .IP "ev_feed_signal (int signum)" 4 |
|
|
427 | .IX Item "ev_feed_signal (int signum)" |
|
|
428 | This function can be used to \*(L"simulate\*(R" a signal receive. It is completely |
|
|
429 | safe to call this function at any time, from any context, including signal |
|
|
430 | handlers or random threads. |
|
|
431 | .Sp |
|
|
432 | Its main use is to customise signal handling in your process, especially |
|
|
433 | in the presence of threads. For example, you could block signals |
|
|
434 | by default in all threads (and specifying \f(CW\*(C`EVFLAG_NOSIGMASK\*(C'\fR when |
|
|
435 | creating any loops), and in one thread, use \f(CW\*(C`sigwait\*(C'\fR or any other |
|
|
436 | mechanism to wait for signals, then \*(L"deliver\*(R" them to libev by calling |
|
|
437 | \&\f(CW\*(C`ev_feed_signal\*(C'\fR. |
426 | .SH "FUNCTIONS CONTROLLING EVENT LOOPS" |
438 | .SH "FUNCTIONS CONTROLLING EVENT LOOPS" |
427 | .IX Header "FUNCTIONS CONTROLLING EVENT LOOPS" |
439 | .IX Header "FUNCTIONS CONTROLLING EVENT LOOPS" |
428 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR (the \f(CW\*(C`struct\*(C'\fR is |
440 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR (the \f(CW\*(C`struct\*(C'\fR is |
429 | \&\fInot\fR optional in this case unless libev 3 compatibility is disabled, as |
441 | \&\fInot\fR optional in this case unless libev 3 compatibility is disabled, as |
430 | libev 3 had an \f(CW\*(C`ev_loop\*(C'\fR function colliding with the struct name). |
442 | libev 3 had an \f(CW\*(C`ev_loop\*(C'\fR function colliding with the struct name). |
… | |
… | |
475 | .IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4 |
487 | .IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4 |
476 | .IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" |
488 | .IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" |
477 | This will create and initialise a new event loop object. If the loop |
489 | This will create and initialise a new event loop object. If the loop |
478 | could not be initialised, returns false. |
490 | could not be initialised, returns false. |
479 | .Sp |
491 | .Sp |
480 | Note that this function \fIis\fR thread-safe, and one common way to use |
492 | This function is thread-safe, and one common way to use libev with |
481 | libev with threads is indeed to create one loop per thread, and using the |
493 | threads is indeed to create one loop per thread, and using the default |
482 | default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread. |
494 | loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread. |
483 | .Sp |
495 | .Sp |
484 | The flags argument can be used to specify special behaviour or specific |
496 | The flags argument can be used to specify special behaviour or specific |
485 | backends to use, and is usually specified as \f(CW0\fR (or \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). |
497 | backends to use, and is usually specified as \f(CW0\fR (or \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). |
486 | .Sp |
498 | .Sp |
487 | The following flags are supported: |
499 | The following flags are supported: |
… | |
… | |
537 | threads that are not interested in handling them. |
549 | threads that are not interested in handling them. |
538 | .Sp |
550 | .Sp |
539 | Signalfd will not be used by default as this changes your signal mask, and |
551 | Signalfd will not be used by default as this changes your signal mask, and |
540 | there are a lot of shoddy libraries and programs (glib's threadpool for |
552 | there are a lot of shoddy libraries and programs (glib's threadpool for |
541 | example) that can't properly initialise their signal masks. |
553 | example) that can't properly initialise their signal masks. |
|
|
554 | .ie n .IP """EVFLAG_NOSIGMASK""" 4 |
|
|
555 | .el .IP "\f(CWEVFLAG_NOSIGMASK\fR" 4 |
|
|
556 | .IX Item "EVFLAG_NOSIGMASK" |
|
|
557 | When this flag is specified, then libev will avoid to modify the signal |
|
|
558 | mask. Specifically, this means you ahve to make sure signals are unblocked |
|
|
559 | when you want to receive them. |
|
|
560 | .Sp |
|
|
561 | This behaviour is useful when you want to do your own signal handling, or |
|
|
562 | want to handle signals only in specific threads and want to avoid libev |
|
|
563 | unblocking the signals. |
|
|
564 | .Sp |
|
|
565 | It's also required by \s-1POSIX\s0 in a threaded program, as libev calls |
|
|
566 | \&\f(CW\*(C`sigprocmask\*(C'\fR, whose behaviour is officially unspecified. |
|
|
567 | .Sp |
|
|
568 | This flag's behaviour will become the default in future versions of libev. |
542 | .ie n .IP """EVBACKEND_SELECT"" (value 1, portable select backend)" 4 |
569 | .ie n .IP """EVBACKEND_SELECT"" (value 1, portable select backend)" 4 |
543 | .el .IP "\f(CWEVBACKEND_SELECT\fR (value 1, portable select backend)" 4 |
570 | .el .IP "\f(CWEVBACKEND_SELECT\fR (value 1, portable select backend)" 4 |
544 | .IX Item "EVBACKEND_SELECT (value 1, portable select backend)" |
571 | .IX Item "EVBACKEND_SELECT (value 1, portable select backend)" |
545 | This is your standard \fIselect\fR\|(2) backend. Not \fIcompletely\fR standard, as |
572 | This is your standard \fIselect\fR\|(2) backend. Not \fIcompletely\fR standard, as |
546 | libev tries to roll its own fd_set with no limits on the number of fds, |
573 | libev tries to roll its own fd_set with no limits on the number of fds, |
… | |
… | |
600 | employing an additional generation counter and comparing that against the |
627 | employing an additional generation counter and comparing that against the |
601 | events to filter out spurious ones, recreating the set when required. Last |
628 | events to filter out spurious ones, recreating the set when required. Last |
602 | not least, it also refuses to work with some file descriptors which work |
629 | not least, it also refuses to work with some file descriptors which work |
603 | perfectly fine with \f(CW\*(C`select\*(C'\fR (files, many character devices...). |
630 | perfectly fine with \f(CW\*(C`select\*(C'\fR (files, many character devices...). |
604 | .Sp |
631 | .Sp |
605 | Epoll is truly the train wreck analog among event poll mechanisms. |
632 | Epoll is truly the train wreck analog among event poll mechanisms, |
|
|
633 | a frankenpoll, cobbled together in a hurry, no thought to design or |
|
|
634 | interaction with others. |
606 | .Sp |
635 | .Sp |
607 | While stopping, setting and starting an I/O watcher in the same iteration |
636 | While stopping, setting and starting an I/O watcher in the same iteration |
608 | will result in some caching, there is still a system call per such |
637 | will result in some caching, there is still a system call per such |
609 | incident (because the same \fIfile descriptor\fR could point to a different |
638 | incident (because the same \fIfile descriptor\fR could point to a different |
610 | \&\fIfile description\fR now), so its best to avoid that. Also, \f(CW\*(C`dup ()\*(C'\fR'ed |
639 | \&\fIfile description\fR now), so its best to avoid that. Also, \f(CW\*(C`dup ()\*(C'\fR'ed |
… | |
… | |
676 | .el .IP "\f(CWEVBACKEND_PORT\fR (value 32, Solaris 10)" 4 |
705 | .el .IP "\f(CWEVBACKEND_PORT\fR (value 32, Solaris 10)" 4 |
677 | .IX Item "EVBACKEND_PORT (value 32, Solaris 10)" |
706 | .IX Item "EVBACKEND_PORT (value 32, Solaris 10)" |
678 | This uses the Solaris 10 event port mechanism. As with everything on Solaris, |
707 | This uses the Solaris 10 event port mechanism. As with everything on Solaris, |
679 | it's really slow, but it still scales very well (O(active_fds)). |
708 | it's really slow, but it still scales very well (O(active_fds)). |
680 | .Sp |
709 | .Sp |
681 | Please note that Solaris event ports can deliver a lot of spurious |
|
|
682 | notifications, so you need to use non-blocking I/O or other means to avoid |
|
|
683 | blocking when no data (or space) is available. |
|
|
684 | .Sp |
|
|
685 | While this backend scales well, it requires one system call per active |
710 | While this backend scales well, it requires one system call per active |
686 | file descriptor per loop iteration. For small and medium numbers of file |
711 | file descriptor per loop iteration. For small and medium numbers of file |
687 | descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend |
712 | descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend |
688 | might perform better. |
713 | might perform better. |
689 | .Sp |
714 | .Sp |
690 | On the positive side, with the exception of the spurious readiness |
715 | On the positive side, this backend actually performed fully to |
691 | notifications, this backend actually performed fully to specification |
|
|
692 | in all tests and is fully embeddable, which is a rare feat among the |
716 | specification in all tests and is fully embeddable, which is a rare feat |
693 | OS-specific backends (I vastly prefer correctness over speed hacks). |
717 | among the OS-specific backends (I vastly prefer correctness over speed |
|
|
718 | hacks). |
|
|
719 | .Sp |
|
|
720 | On the negative side, the interface is \fIbizarre\fR \- so bizarre that |
|
|
721 | even sun itself gets it wrong in their code examples: The event polling |
|
|
722 | function sometimes returning events to the caller even though an error |
|
|
723 | occurred, but with no indication whether it has done so or not (yes, it's |
|
|
724 | even documented that way) \- deadly for edge-triggered interfaces where |
|
|
725 | you absolutely have to know whether an event occurred or not because you |
|
|
726 | have to re-arm the watcher. |
|
|
727 | .Sp |
|
|
728 | Fortunately libev seems to be able to work around these idiocies. |
694 | .Sp |
729 | .Sp |
695 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as |
730 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as |
696 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
731 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
697 | .ie n .IP """EVBACKEND_ALL""" 4 |
732 | .ie n .IP """EVBACKEND_ALL""" 4 |
698 | .el .IP "\f(CWEVBACKEND_ALL\fR" 4 |
733 | .el .IP "\f(CWEVBACKEND_ALL\fR" 4 |
699 | .IX Item "EVBACKEND_ALL" |
734 | .IX Item "EVBACKEND_ALL" |
700 | Try all backends (even potentially broken ones that wouldn't be tried |
735 | Try all backends (even potentially broken ones that wouldn't be tried |
701 | with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as |
736 | with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as |
702 | \&\f(CW\*(C`EVBACKEND_ALL & ~EVBACKEND_KQUEUE\*(C'\fR. |
737 | \&\f(CW\*(C`EVBACKEND_ALL & ~EVBACKEND_KQUEUE\*(C'\fR. |
703 | .Sp |
738 | .Sp |
704 | It is definitely not recommended to use this flag. |
739 | It is definitely not recommended to use this flag, use whatever |
|
|
740 | \&\f(CW\*(C`ev_recommended_backends ()\*(C'\fR returns, or simply do not specify a backend |
|
|
741 | at all. |
|
|
742 | .ie n .IP """EVBACKEND_MASK""" 4 |
|
|
743 | .el .IP "\f(CWEVBACKEND_MASK\fR" 4 |
|
|
744 | .IX Item "EVBACKEND_MASK" |
|
|
745 | Not a backend at all, but a mask to select all backend bits from a |
|
|
746 | \&\f(CW\*(C`flags\*(C'\fR value, in case you want to mask out any backends from a flags |
|
|
747 | value (e.g. when modifying the \f(CW\*(C`LIBEV_FLAGS\*(C'\fR environment variable). |
705 | .RE |
748 | .RE |
706 | .RS 4 |
749 | .RS 4 |
707 | .Sp |
750 | .Sp |
708 | If one or more of the backend flags are or'ed into the flags value, |
751 | If one or more of the backend flags are or'ed into the flags value, |
709 | then only these backends will be tried (in the reverse order as listed |
752 | then only these backends will be tried (in the reverse order as listed |
… | |
… | |
798 | \&\f(CW\*(C`ev_prepare\*(C'\fR and \f(CW\*(C`ev_check\*(C'\fR calls \- and is incremented between the |
841 | \&\f(CW\*(C`ev_prepare\*(C'\fR and \f(CW\*(C`ev_check\*(C'\fR calls \- and is incremented between the |
799 | prepare and check phases. |
842 | prepare and check phases. |
800 | .IP "unsigned int ev_depth (loop)" 4 |
843 | .IP "unsigned int ev_depth (loop)" 4 |
801 | .IX Item "unsigned int ev_depth (loop)" |
844 | .IX Item "unsigned int ev_depth (loop)" |
802 | Returns the number of times \f(CW\*(C`ev_run\*(C'\fR was entered minus the number of |
845 | Returns the number of times \f(CW\*(C`ev_run\*(C'\fR was entered minus the number of |
803 | times \f(CW\*(C`ev_run\*(C'\fR was exited, in other words, the recursion depth. |
846 | times \f(CW\*(C`ev_run\*(C'\fR was exited normally, in other words, the recursion depth. |
804 | .Sp |
847 | .Sp |
805 | Outside \f(CW\*(C`ev_run\*(C'\fR, this number is zero. In a callback, this number is |
848 | Outside \f(CW\*(C`ev_run\*(C'\fR, this number is zero. In a callback, this number is |
806 | \&\f(CW1\fR, unless \f(CW\*(C`ev_run\*(C'\fR was invoked recursively (or from another thread), |
849 | \&\f(CW1\fR, unless \f(CW\*(C`ev_run\*(C'\fR was invoked recursively (or from another thread), |
807 | in which case it is higher. |
850 | in which case it is higher. |
808 | .Sp |
851 | .Sp |
809 | Leaving \f(CW\*(C`ev_run\*(C'\fR abnormally (setjmp/longjmp, cancelling the thread |
852 | Leaving \f(CW\*(C`ev_run\*(C'\fR abnormally (setjmp/longjmp, cancelling the thread, |
810 | etc.), doesn't count as \*(L"exit\*(R" \- consider this as a hint to avoid such |
853 | throwing an exception etc.), doesn't count as \*(L"exit\*(R" \- consider this |
811 | ungentleman-like behaviour unless it's really convenient. |
854 | as a hint to avoid such ungentleman-like behaviour unless it's really |
|
|
855 | convenient, in which case it is fully supported. |
812 | .IP "unsigned int ev_backend (loop)" 4 |
856 | .IP "unsigned int ev_backend (loop)" 4 |
813 | .IX Item "unsigned int ev_backend (loop)" |
857 | .IX Item "unsigned int ev_backend (loop)" |
814 | Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in |
858 | Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in |
815 | use. |
859 | use. |
816 | .IP "ev_tstamp ev_now (loop)" 4 |
860 | .IP "ev_tstamp ev_now (loop)" 4 |
… | |
… | |
874 | relying on all watchers to be stopped when deciding when a program has |
918 | relying on all watchers to be stopped when deciding when a program has |
875 | finished (especially in interactive programs), but having a program |
919 | finished (especially in interactive programs), but having a program |
876 | that automatically loops as long as it has to and no longer by virtue |
920 | that automatically loops as long as it has to and no longer by virtue |
877 | of relying on its watchers stopping correctly, that is truly a thing of |
921 | of relying on its watchers stopping correctly, that is truly a thing of |
878 | beauty. |
922 | beauty. |
|
|
923 | .Sp |
|
|
924 | This function is also \fImostly\fR exception-safe \- you can break out of |
|
|
925 | a \f(CW\*(C`ev_run\*(C'\fR call by calling \f(CW\*(C`longjmp\*(C'\fR in a callback, throwing a \*(C+ |
|
|
926 | exception and so on. This does not decrement the \f(CW\*(C`ev_depth\*(C'\fR value, nor |
|
|
927 | will it clear any outstanding \f(CW\*(C`EVBREAK_ONE\*(C'\fR breaks. |
879 | .Sp |
928 | .Sp |
880 | A flags value of \f(CW\*(C`EVRUN_NOWAIT\*(C'\fR will look for new events, will handle |
929 | A flags value of \f(CW\*(C`EVRUN_NOWAIT\*(C'\fR will look for new events, will handle |
881 | those events and any already outstanding ones, but will not wait and |
930 | those events and any already outstanding ones, but will not wait and |
882 | block your process in case there are no events and will return after one |
931 | block your process in case there are no events and will return after one |
883 | iteration of the loop. This is sometimes useful to poll and handle new |
932 | iteration of the loop. This is sometimes useful to poll and handle new |
… | |
… | |
939 | .Sp |
988 | .Sp |
940 | .Vb 4 |
989 | .Vb 4 |
941 | \& ... queue jobs here, make sure they register event watchers as long |
990 | \& ... queue jobs here, make sure they register event watchers as long |
942 | \& ... as they still have work to do (even an idle watcher will do..) |
991 | \& ... as they still have work to do (even an idle watcher will do..) |
943 | \& ev_run (my_loop, 0); |
992 | \& ev_run (my_loop, 0); |
944 | \& ... jobs done or somebody called unloop. yeah! |
993 | \& ... jobs done or somebody called break. yeah! |
945 | .Ve |
994 | .Ve |
946 | .IP "ev_break (loop, how)" 4 |
995 | .IP "ev_break (loop, how)" 4 |
947 | .IX Item "ev_break (loop, how)" |
996 | .IX Item "ev_break (loop, how)" |
948 | Can be used to make a call to \f(CW\*(C`ev_run\*(C'\fR return early (but only after it |
997 | Can be used to make a call to \f(CW\*(C`ev_run\*(C'\fR return early (but only after it |
949 | has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either |
998 | has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either |
950 | \&\f(CW\*(C`EVBREAK_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_run\*(C'\fR call return, or |
999 | \&\f(CW\*(C`EVBREAK_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_run\*(C'\fR call return, or |
951 | \&\f(CW\*(C`EVBREAK_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_run\*(C'\fR calls return. |
1000 | \&\f(CW\*(C`EVBREAK_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_run\*(C'\fR calls return. |
952 | .Sp |
1001 | .Sp |
953 | This \*(L"break state\*(R" will be cleared when entering \f(CW\*(C`ev_run\*(C'\fR again. |
1002 | This \*(L"break state\*(R" will be cleared on the next call to \f(CW\*(C`ev_run\*(C'\fR. |
954 | .Sp |
1003 | .Sp |
955 | It is safe to call \f(CW\*(C`ev_break\*(C'\fR from outside any \f(CW\*(C`ev_run\*(C'\fR calls, too. |
1004 | It is safe to call \f(CW\*(C`ev_break\*(C'\fR from outside any \f(CW\*(C`ev_run\*(C'\fR calls, too, in |
|
|
1005 | which case it will have no effect. |
956 | .IP "ev_ref (loop)" 4 |
1006 | .IP "ev_ref (loop)" 4 |
957 | .IX Item "ev_ref (loop)" |
1007 | .IX Item "ev_ref (loop)" |
958 | .PD 0 |
1008 | .PD 0 |
959 | .IP "ev_unref (loop)" 4 |
1009 | .IP "ev_unref (loop)" 4 |
960 | .IX Item "ev_unref (loop)" |
1010 | .IX Item "ev_unref (loop)" |
… | |
… | |
983 | .Sp |
1033 | .Sp |
984 | .Vb 4 |
1034 | .Vb 4 |
985 | \& ev_signal exitsig; |
1035 | \& ev_signal exitsig; |
986 | \& ev_signal_init (&exitsig, sig_cb, SIGINT); |
1036 | \& ev_signal_init (&exitsig, sig_cb, SIGINT); |
987 | \& ev_signal_start (loop, &exitsig); |
1037 | \& ev_signal_start (loop, &exitsig); |
988 | \& evf_unref (loop); |
1038 | \& ev_unref (loop); |
989 | .Ve |
1039 | .Ve |
990 | .Sp |
1040 | .Sp |
991 | Example: For some weird reason, unregister the above signal handler again. |
1041 | Example: For some weird reason, unregister the above signal handler again. |
992 | .Sp |
1042 | .Sp |
993 | .Vb 2 |
1043 | .Vb 2 |
… | |
… | |
1107 | See also the locking example in the \f(CW\*(C`THREADS\*(C'\fR section later in this |
1157 | See also the locking example in the \f(CW\*(C`THREADS\*(C'\fR section later in this |
1108 | document. |
1158 | document. |
1109 | .IP "ev_set_userdata (loop, void *data)" 4 |
1159 | .IP "ev_set_userdata (loop, void *data)" 4 |
1110 | .IX Item "ev_set_userdata (loop, void *data)" |
1160 | .IX Item "ev_set_userdata (loop, void *data)" |
1111 | .PD 0 |
1161 | .PD 0 |
1112 | .IP "ev_userdata (loop)" 4 |
1162 | .IP "void *ev_userdata (loop)" 4 |
1113 | .IX Item "ev_userdata (loop)" |
1163 | .IX Item "void *ev_userdata (loop)" |
1114 | .PD |
1164 | .PD |
1115 | Set and retrieve a single \f(CW\*(C`void *\*(C'\fR associated with a loop. When |
1165 | Set and retrieve a single \f(CW\*(C`void *\*(C'\fR associated with a loop. When |
1116 | \&\f(CW\*(C`ev_set_userdata\*(C'\fR has never been called, then \f(CW\*(C`ev_userdata\*(C'\fR returns |
1166 | \&\f(CW\*(C`ev_set_userdata\*(C'\fR has never been called, then \f(CW\*(C`ev_userdata\*(C'\fR returns |
1117 | \&\f(CW0.\fR |
1167 | \&\f(CW0\fR. |
1118 | .Sp |
1168 | .Sp |
1119 | These two functions can be used to associate arbitrary data with a loop, |
1169 | These two functions can be used to associate arbitrary data with a loop, |
1120 | and are intended solely for the \f(CW\*(C`invoke_pending_cb\*(C'\fR, \f(CW\*(C`release\*(C'\fR and |
1170 | and are intended solely for the \f(CW\*(C`invoke_pending_cb\*(C'\fR, \f(CW\*(C`release\*(C'\fR and |
1121 | \&\f(CW\*(C`acquire\*(C'\fR callbacks described above, but of course can be (ab\-)used for |
1171 | \&\f(CW\*(C`acquire\*(C'\fR callbacks described above, but of course can be (ab\-)used for |
1122 | any other purpose as well. |
1172 | any other purpose as well. |
… | |
… | |
1428 | \&\f(CW\*(C`ev_clear_pending\*(C'\fR will clear the pending event, even if the watcher was |
1478 | \&\f(CW\*(C`ev_clear_pending\*(C'\fR will clear the pending event, even if the watcher was |
1429 | not started in the first place. |
1479 | not started in the first place. |
1430 | .Sp |
1480 | .Sp |
1431 | See also \f(CW\*(C`ev_feed_fd_event\*(C'\fR and \f(CW\*(C`ev_feed_signal_event\*(C'\fR for related |
1481 | See also \f(CW\*(C`ev_feed_fd_event\*(C'\fR and \f(CW\*(C`ev_feed_signal_event\*(C'\fR for related |
1432 | functions that do not need a watcher. |
1482 | functions that do not need a watcher. |
1433 | .SS "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" |
|
|
1434 | .IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" |
|
|
1435 | Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change |
|
|
1436 | and read at any time: libev will completely ignore it. This can be used |
|
|
1437 | to associate arbitrary data with your watcher. If you need more data and |
|
|
1438 | don't want to allocate memory and store a pointer to it in that data |
|
|
1439 | member, you can also \*(L"subclass\*(R" the watcher type and provide your own |
|
|
1440 | data: |
|
|
1441 | .PP |
1483 | .PP |
1442 | .Vb 7 |
1484 | See also the \*(L"\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0\*(R" and \*(L"\s-1BUILDING\s0 \s-1YOUR\s0 |
1443 | \& struct my_io |
1485 | \&\s-1OWN\s0 \s-1COMPOSITE\s0 \s-1WATCHERS\s0\*(R" idioms. |
1444 | \& { |
|
|
1445 | \& ev_io io; |
|
|
1446 | \& int otherfd; |
|
|
1447 | \& void *somedata; |
|
|
1448 | \& struct whatever *mostinteresting; |
|
|
1449 | \& }; |
|
|
1450 | \& |
|
|
1451 | \& ... |
|
|
1452 | \& struct my_io w; |
|
|
1453 | \& ev_io_init (&w.io, my_cb, fd, EV_READ); |
|
|
1454 | .Ve |
|
|
1455 | .PP |
|
|
1456 | And since your callback will be called with a pointer to the watcher, you |
|
|
1457 | can cast it back to your own type: |
|
|
1458 | .PP |
|
|
1459 | .Vb 5 |
|
|
1460 | \& static void my_cb (struct ev_loop *loop, ev_io *w_, int revents) |
|
|
1461 | \& { |
|
|
1462 | \& struct my_io *w = (struct my_io *)w_; |
|
|
1463 | \& ... |
|
|
1464 | \& } |
|
|
1465 | .Ve |
|
|
1466 | .PP |
|
|
1467 | More interesting and less C\-conformant ways of casting your callback type |
|
|
1468 | instead have been omitted. |
|
|
1469 | .PP |
|
|
1470 | Another common scenario is to use some data structure with multiple |
|
|
1471 | embedded watchers: |
|
|
1472 | .PP |
|
|
1473 | .Vb 6 |
|
|
1474 | \& struct my_biggy |
|
|
1475 | \& { |
|
|
1476 | \& int some_data; |
|
|
1477 | \& ev_timer t1; |
|
|
1478 | \& ev_timer t2; |
|
|
1479 | \& } |
|
|
1480 | .Ve |
|
|
1481 | .PP |
|
|
1482 | In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more |
|
|
1483 | complicated: Either you store the address of your \f(CW\*(C`my_biggy\*(C'\fR struct |
|
|
1484 | in the \f(CW\*(C`data\*(C'\fR member of the watcher (for woozies), or you need to use |
|
|
1485 | some pointer arithmetic using \f(CW\*(C`offsetof\*(C'\fR inside your watchers (for real |
|
|
1486 | programmers): |
|
|
1487 | .PP |
|
|
1488 | .Vb 1 |
|
|
1489 | \& #include <stddef.h> |
|
|
1490 | \& |
|
|
1491 | \& static void |
|
|
1492 | \& t1_cb (EV_P_ ev_timer *w, int revents) |
|
|
1493 | \& { |
|
|
1494 | \& struct my_biggy big = (struct my_biggy *) |
|
|
1495 | \& (((char *)w) \- offsetof (struct my_biggy, t1)); |
|
|
1496 | \& } |
|
|
1497 | \& |
|
|
1498 | \& static void |
|
|
1499 | \& t2_cb (EV_P_ ev_timer *w, int revents) |
|
|
1500 | \& { |
|
|
1501 | \& struct my_biggy big = (struct my_biggy *) |
|
|
1502 | \& (((char *)w) \- offsetof (struct my_biggy, t2)); |
|
|
1503 | \& } |
|
|
1504 | .Ve |
|
|
1505 | .SS "\s-1WATCHER\s0 \s-1STATES\s0" |
1486 | .SS "\s-1WATCHER\s0 \s-1STATES\s0" |
1506 | .IX Subsection "WATCHER STATES" |
1487 | .IX Subsection "WATCHER STATES" |
1507 | There are various watcher states mentioned throughout this manual \- |
1488 | There are various watcher states mentioned throughout this manual \- |
1508 | active, pending and so on. In this section these states and the rules to |
1489 | active, pending and so on. In this section these states and the rules to |
1509 | transition between them will be described in more detail \- and while these |
1490 | transition between them will be described in more detail \- and while these |
… | |
… | |
1512 | .IX Item "initialiased" |
1493 | .IX Item "initialiased" |
1513 | Before a watcher can be registered with the event looop it has to be |
1494 | Before a watcher can be registered with the event looop it has to be |
1514 | initialised. This can be done with a call to \f(CW\*(C`ev_TYPE_init\*(C'\fR, or calls to |
1495 | initialised. This can be done with a call to \f(CW\*(C`ev_TYPE_init\*(C'\fR, or calls to |
1515 | \&\f(CW\*(C`ev_init\*(C'\fR followed by the watcher-specific \f(CW\*(C`ev_TYPE_set\*(C'\fR function. |
1496 | \&\f(CW\*(C`ev_init\*(C'\fR followed by the watcher-specific \f(CW\*(C`ev_TYPE_set\*(C'\fR function. |
1516 | .Sp |
1497 | .Sp |
1517 | In this state it is simply some block of memory that is suitable for use |
1498 | In this state it is simply some block of memory that is suitable for |
1518 | in an event loop. It can be moved around, freed, reused etc. at will. |
1499 | use in an event loop. It can be moved around, freed, reused etc. at |
|
|
1500 | will \- as long as you either keep the memory contents intact, or call |
|
|
1501 | \&\f(CW\*(C`ev_TYPE_init\*(C'\fR again. |
1519 | .IP "started/running/active" 4 |
1502 | .IP "started/running/active" 4 |
1520 | .IX Item "started/running/active" |
1503 | .IX Item "started/running/active" |
1521 | Once a watcher has been started with a call to \f(CW\*(C`ev_TYPE_start\*(C'\fR it becomes |
1504 | Once a watcher has been started with a call to \f(CW\*(C`ev_TYPE_start\*(C'\fR it becomes |
1522 | property of the event loop, and is actively waiting for events. While in |
1505 | property of the event loop, and is actively waiting for events. While in |
1523 | this state it cannot be accessed (except in a few documented ways), moved, |
1506 | this state it cannot be accessed (except in a few documented ways), moved, |
… | |
… | |
1548 | latter will clear any pending state the watcher might be in, regardless |
1531 | latter will clear any pending state the watcher might be in, regardless |
1549 | of whether it was active or not, so stopping a watcher explicitly before |
1532 | of whether it was active or not, so stopping a watcher explicitly before |
1550 | freeing it is often a good idea. |
1533 | freeing it is often a good idea. |
1551 | .Sp |
1534 | .Sp |
1552 | While stopped (and not pending) the watcher is essentially in the |
1535 | While stopped (and not pending) the watcher is essentially in the |
1553 | initialised state, that is it can be reused, moved, modified in any way |
1536 | initialised state, that is, it can be reused, moved, modified in any way |
1554 | you wish. |
1537 | you wish (but when you trash the memory block, you need to \f(CW\*(C`ev_TYPE_init\*(C'\fR |
|
|
1538 | it again). |
1555 | .SS "\s-1WATCHER\s0 \s-1PRIORITY\s0 \s-1MODELS\s0" |
1539 | .SS "\s-1WATCHER\s0 \s-1PRIORITY\s0 \s-1MODELS\s0" |
1556 | .IX Subsection "WATCHER PRIORITY MODELS" |
1540 | .IX Subsection "WATCHER PRIORITY MODELS" |
1557 | Many event loops support \fIwatcher priorities\fR, which are usually small |
1541 | Many event loops support \fIwatcher priorities\fR, which are usually small |
1558 | integers that influence the ordering of event callback invocation |
1542 | integers that influence the ordering of event callback invocation |
1559 | between watchers in some way, all else being equal. |
1543 | between watchers in some way, all else being equal. |
… | |
… | |
1684 | In general you can register as many read and/or write event watchers per |
1668 | In general you can register as many read and/or write event watchers per |
1685 | fd as you want (as long as you don't confuse yourself). Setting all file |
1669 | fd as you want (as long as you don't confuse yourself). Setting all file |
1686 | descriptors to non-blocking mode is also usually a good idea (but not |
1670 | descriptors to non-blocking mode is also usually a good idea (but not |
1687 | required if you know what you are doing). |
1671 | required if you know what you are doing). |
1688 | .PP |
1672 | .PP |
1689 | If you cannot use non-blocking mode, then force the use of a |
|
|
1690 | known-to-be-good backend (at the time of this writing, this includes only |
|
|
1691 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR). The same applies to file |
|
|
1692 | descriptors for which non-blocking operation makes no sense (such as |
|
|
1693 | files) \- libev doesn't guarantee any specific behaviour in that case. |
|
|
1694 | .PP |
|
|
1695 | Another thing you have to watch out for is that it is quite easy to |
1673 | Another thing you have to watch out for is that it is quite easy to |
1696 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1674 | receive \*(L"spurious\*(R" readiness notifications, that is, your callback might |
1697 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1675 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1698 | because there is no data. Not only are some backends known to create a |
1676 | because there is no data. It is very easy to get into this situation even |
1699 | lot of those (for example Solaris ports), it is very easy to get into |
1677 | with a relatively standard program structure. Thus it is best to always |
1700 | this situation even with a relatively standard program structure. Thus |
1678 | use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning \f(CW\*(C`EAGAIN\*(C'\fR is far |
1701 | it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning |
|
|
1702 | \&\f(CW\*(C`EAGAIN\*(C'\fR is far preferable to a program hanging until some data arrives. |
1679 | preferable to a program hanging until some data arrives. |
1703 | .PP |
1680 | .PP |
1704 | If you cannot run the fd in non-blocking mode (for example you should |
1681 | If you cannot run the fd in non-blocking mode (for example you should |
1705 | not play around with an Xlib connection), then you have to separately |
1682 | not play around with an Xlib connection), then you have to separately |
1706 | re-test whether a file descriptor is really ready with a known-to-be good |
1683 | re-test whether a file descriptor is really ready with a known-to-be good |
1707 | interface such as poll (fortunately in our Xlib example, Xlib already |
1684 | interface such as poll (fortunately in the case of Xlib, it already does |
1708 | does this on its own, so its quite safe to use). Some people additionally |
1685 | this on its own, so its quite safe to use). Some people additionally |
1709 | use \f(CW\*(C`SIGALRM\*(C'\fR and an interval timer, just to be sure you won't block |
1686 | use \f(CW\*(C`SIGALRM\*(C'\fR and an interval timer, just to be sure you won't block |
1710 | indefinitely. |
1687 | indefinitely. |
1711 | .PP |
1688 | .PP |
1712 | But really, best use non-blocking mode. |
1689 | But really, best use non-blocking mode. |
1713 | .PP |
1690 | .PP |
… | |
… | |
1743 | .PP |
1720 | .PP |
1744 | There is no workaround possible except not registering events |
1721 | There is no workaround possible except not registering events |
1745 | for potentially \f(CW\*(C`dup ()\*(C'\fR'ed file descriptors, or to resort to |
1722 | for potentially \f(CW\*(C`dup ()\*(C'\fR'ed file descriptors, or to resort to |
1746 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
1723 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
1747 | .PP |
1724 | .PP |
|
|
1725 | \fIThe special problem of files\fR |
|
|
1726 | .IX Subsection "The special problem of files" |
|
|
1727 | .PP |
|
|
1728 | Many people try to use \f(CW\*(C`select\*(C'\fR (or libev) on file descriptors |
|
|
1729 | representing files, and expect it to become ready when their program |
|
|
1730 | doesn't block on disk accesses (which can take a long time on their own). |
|
|
1731 | .PP |
|
|
1732 | However, this cannot ever work in the \*(L"expected\*(R" way \- you get a readiness |
|
|
1733 | notification as soon as the kernel knows whether and how much data is |
|
|
1734 | there, and in the case of open files, that's always the case, so you |
|
|
1735 | always get a readiness notification instantly, and your read (or possibly |
|
|
1736 | write) will still block on the disk I/O. |
|
|
1737 | .PP |
|
|
1738 | Another way to view it is that in the case of sockets, pipes, character |
|
|
1739 | devices and so on, there is another party (the sender) that delivers data |
|
|
1740 | on its own, but in the case of files, there is no such thing: the disk |
|
|
1741 | will not send data on its own, simply because it doesn't know what you |
|
|
1742 | wish to read \- you would first have to request some data. |
|
|
1743 | .PP |
|
|
1744 | Since files are typically not-so-well supported by advanced notification |
|
|
1745 | mechanism, libev tries hard to emulate \s-1POSIX\s0 behaviour with respect |
|
|
1746 | to files, even though you should not use it. The reason for this is |
|
|
1747 | convenience: sometimes you want to watch \s-1STDIN\s0 or \s-1STDOUT\s0, which is |
|
|
1748 | usually a tty, often a pipe, but also sometimes files or special devices |
|
|
1749 | (for example, \f(CW\*(C`epoll\*(C'\fR on Linux works with \fI/dev/random\fR but not with |
|
|
1750 | \&\fI/dev/urandom\fR), and even though the file might better be served with |
|
|
1751 | asynchronous I/O instead of with non-blocking I/O, it is still useful when |
|
|
1752 | it \*(L"just works\*(R" instead of freezing. |
|
|
1753 | .PP |
|
|
1754 | So avoid file descriptors pointing to files when you know it (e.g. use |
|
|
1755 | libeio), but use them when it is convenient, e.g. for \s-1STDIN/STDOUT\s0, or |
|
|
1756 | when you rarely read from a file instead of from a socket, and want to |
|
|
1757 | reuse the same code path. |
|
|
1758 | .PP |
1748 | \fIThe special problem of fork\fR |
1759 | \fIThe special problem of fork\fR |
1749 | .IX Subsection "The special problem of fork" |
1760 | .IX Subsection "The special problem of fork" |
1750 | .PP |
1761 | .PP |
1751 | Some backends (epoll, kqueue) do not support \f(CW\*(C`fork ()\*(C'\fR at all or exhibit |
1762 | Some backends (epoll, kqueue) do not support \f(CW\*(C`fork ()\*(C'\fR at all or exhibit |
1752 | useless behaviour. Libev fully supports fork, but needs to be told about |
1763 | useless behaviour. Libev fully supports fork, but needs to be told about |
1753 | it in the child. |
1764 | it in the child if you want to continue to use it in the child. |
1754 | .PP |
1765 | .PP |
1755 | To support fork in your programs, you either have to call |
1766 | To support fork in your child processes, you have to call \f(CW\*(C`ev_loop_fork |
1756 | \&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child, |
1767 | ()\*(C'\fR after a fork in the child, enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to |
1757 | enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or |
1768 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
1758 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
|
|
1759 | .PP |
1769 | .PP |
1760 | \fIThe special problem of \s-1SIGPIPE\s0\fR |
1770 | \fIThe special problem of \s-1SIGPIPE\s0\fR |
1761 | .IX Subsection "The special problem of SIGPIPE" |
1771 | .IX Subsection "The special problem of SIGPIPE" |
1762 | .PP |
1772 | .PP |
1763 | While not really specific to libev, it is easy to forget about \f(CW\*(C`SIGPIPE\*(C'\fR: |
1773 | While not really specific to libev, it is easy to forget about \f(CW\*(C`SIGPIPE\*(C'\fR: |
… | |
… | |
2425 | .IX Subsection "The special problem of inheritance over fork/execve/pthread_create" |
2435 | .IX Subsection "The special problem of inheritance over fork/execve/pthread_create" |
2426 | .PP |
2436 | .PP |
2427 | Both the signal mask (\f(CW\*(C`sigprocmask\*(C'\fR) and the signal disposition |
2437 | Both the signal mask (\f(CW\*(C`sigprocmask\*(C'\fR) and the signal disposition |
2428 | (\f(CW\*(C`sigaction\*(C'\fR) are unspecified after starting a signal watcher (and after |
2438 | (\f(CW\*(C`sigaction\*(C'\fR) are unspecified after starting a signal watcher (and after |
2429 | stopping it again), that is, libev might or might not block the signal, |
2439 | stopping it again), that is, libev might or might not block the signal, |
2430 | and might or might not set or restore the installed signal handler. |
2440 | and might or might not set or restore the installed signal handler (but |
|
|
2441 | see \f(CW\*(C`EVFLAG_NOSIGMASK\*(C'\fR). |
2431 | .PP |
2442 | .PP |
2432 | While this does not matter for the signal disposition (libev never |
2443 | While this does not matter for the signal disposition (libev never |
2433 | sets signals to \f(CW\*(C`SIG_IGN\*(C'\fR, so handlers will be reset to \f(CW\*(C`SIG_DFL\*(C'\fR on |
2444 | sets signals to \f(CW\*(C`SIG_IGN\*(C'\fR, so handlers will be reset to \f(CW\*(C`SIG_DFL\*(C'\fR on |
2434 | \&\f(CW\*(C`execve\*(C'\fR), this matters for the signal mask: many programs do not expect |
2445 | \&\f(CW\*(C`execve\*(C'\fR), this matters for the signal mask: many programs do not expect |
2435 | certain signals to be blocked. |
2446 | certain signals to be blocked. |
… | |
… | |
2448 | \&\fIhas\fR to modify the signal mask, at least temporarily. |
2459 | \&\fIhas\fR to modify the signal mask, at least temporarily. |
2449 | .PP |
2460 | .PP |
2450 | So I can't stress this enough: \fIIf you do not reset your signal mask when |
2461 | So I can't stress this enough: \fIIf you do not reset your signal mask when |
2451 | you expect it to be empty, you have a race condition in your code\fR. This |
2462 | you expect it to be empty, you have a race condition in your code\fR. This |
2452 | is not a libev-specific thing, this is true for most event libraries. |
2463 | is not a libev-specific thing, this is true for most event libraries. |
|
|
2464 | .PP |
|
|
2465 | \fIThe special problem of threads signal handling\fR |
|
|
2466 | .IX Subsection "The special problem of threads signal handling" |
|
|
2467 | .PP |
|
|
2468 | \&\s-1POSIX\s0 threads has problematic signal handling semantics, specifically, |
|
|
2469 | a lot of functionality (sigfd, sigwait etc.) only really works if all |
|
|
2470 | threads in a process block signals, which is hard to achieve. |
|
|
2471 | .PP |
|
|
2472 | When you want to use sigwait (or mix libev signal handling with your own |
|
|
2473 | for the same signals), you can tackle this problem by globally blocking |
|
|
2474 | all signals before creating any threads (or creating them with a fully set |
|
|
2475 | sigprocmask) and also specifying the \f(CW\*(C`EVFLAG_NOSIGMASK\*(C'\fR when creating |
|
|
2476 | loops. Then designate one thread as \*(L"signal receiver thread\*(R" which handles |
|
|
2477 | these signals. You can pass on any signals that libev might be interested |
|
|
2478 | in by calling \f(CW\*(C`ev_feed_signal\*(C'\fR. |
2453 | .PP |
2479 | .PP |
2454 | \fIWatcher-Specific Functions and Data Members\fR |
2480 | \fIWatcher-Specific Functions and Data Members\fR |
2455 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2481 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2456 | .IP "ev_signal_init (ev_signal *, callback, int signum)" 4 |
2482 | .IP "ev_signal_init (ev_signal *, callback, int signum)" 4 |
2457 | .IX Item "ev_signal_init (ev_signal *, callback, int signum)" |
2483 | .IX Item "ev_signal_init (ev_signal *, callback, int signum)" |
… | |
… | |
3288 | \& atexit (program_exits); |
3314 | \& atexit (program_exits); |
3289 | .Ve |
3315 | .Ve |
3290 | .ie n .SS """ev_async"" \- how to wake up an event loop" |
3316 | .ie n .SS """ev_async"" \- how to wake up an event loop" |
3291 | .el .SS "\f(CWev_async\fP \- how to wake up an event loop" |
3317 | .el .SS "\f(CWev_async\fP \- how to wake up an event loop" |
3292 | .IX Subsection "ev_async - how to wake up an event loop" |
3318 | .IX Subsection "ev_async - how to wake up an event loop" |
3293 | In general, you cannot use an \f(CW\*(C`ev_run\*(C'\fR from multiple threads or other |
3319 | In general, you cannot use an \f(CW\*(C`ev_loop\*(C'\fR from multiple threads or other |
3294 | asynchronous sources such as signal handlers (as opposed to multiple event |
3320 | asynchronous sources such as signal handlers (as opposed to multiple event |
3295 | loops \- those are of course safe to use in different threads). |
3321 | loops \- those are of course safe to use in different threads). |
3296 | .PP |
3322 | .PP |
3297 | Sometimes, however, you need to wake up an event loop you do not control, |
3323 | Sometimes, however, you need to wake up an event loop you do not control, |
3298 | for example because it belongs to another thread. This is what \f(CW\*(C`ev_async\*(C'\fR |
3324 | for example because it belongs to another thread. This is what \f(CW\*(C`ev_async\*(C'\fR |
… | |
… | |
3300 | it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal safe. |
3326 | it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal safe. |
3301 | .PP |
3327 | .PP |
3302 | This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals, |
3328 | This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals, |
3303 | too, are asynchronous in nature, and signals, too, will be compressed |
3329 | too, are asynchronous in nature, and signals, too, will be compressed |
3304 | (i.e. the number of callback invocations may be less than the number of |
3330 | (i.e. the number of callback invocations may be less than the number of |
3305 | \&\f(CW\*(C`ev_async_sent\*(C'\fR calls). |
3331 | \&\f(CW\*(C`ev_async_sent\*(C'\fR calls). In fact, you could use signal watchers as a kind |
|
|
3332 | of \*(L"global async watchers\*(R" by using a watcher on an otherwise unused |
|
|
3333 | signal, and \f(CW\*(C`ev_feed_signal\*(C'\fR to signal this watcher from another thread, |
|
|
3334 | even without knowing which loop owns the signal. |
3306 | .PP |
3335 | .PP |
3307 | Unlike \f(CW\*(C`ev_signal\*(C'\fR watchers, \f(CW\*(C`ev_async\*(C'\fR works with any event loop, not |
3336 | Unlike \f(CW\*(C`ev_signal\*(C'\fR watchers, \f(CW\*(C`ev_async\*(C'\fR works with any event loop, not |
3308 | just the default loop. |
3337 | just the default loop. |
3309 | .PP |
3338 | .PP |
3310 | \fIQueueing\fR |
3339 | \fIQueueing\fR |
… | |
… | |
3400 | kind. There is a \f(CW\*(C`ev_async_set\*(C'\fR macro, but using it is utterly pointless, |
3429 | kind. There is a \f(CW\*(C`ev_async_set\*(C'\fR macro, but using it is utterly pointless, |
3401 | trust me. |
3430 | trust me. |
3402 | .IP "ev_async_send (loop, ev_async *)" 4 |
3431 | .IP "ev_async_send (loop, ev_async *)" 4 |
3403 | .IX Item "ev_async_send (loop, ev_async *)" |
3432 | .IX Item "ev_async_send (loop, ev_async *)" |
3404 | Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds |
3433 | Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds |
3405 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike |
3434 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop, and instantly |
|
|
3435 | returns. |
|
|
3436 | .Sp |
3406 | \&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, signal or |
3437 | Unlike \f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, |
3407 | similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
3438 | signal or similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the |
3408 | section below on what exactly this means). |
3439 | embedding section below on what exactly this means). |
3409 | .Sp |
3440 | .Sp |
3410 | Note that, as with other watchers in libev, multiple events might get |
3441 | Note that, as with other watchers in libev, multiple events might get |
3411 | compressed into a single callback invocation (another way to look at this |
3442 | compressed into a single callback invocation (another way to look at this |
3412 | is that \f(CW\*(C`ev_async\*(C'\fR watchers are level-triggered, set on \f(CW\*(C`ev_async_send\*(C'\fR, |
3443 | is that \f(CW\*(C`ev_async\*(C'\fR watchers are level-triggered, set on \f(CW\*(C`ev_async_send\*(C'\fR, |
3413 | reset when the event loop detects that). |
3444 | reset when the event loop detects that). |
… | |
… | |
3473 | .IX Item "ev_feed_fd_event (loop, int fd, int revents)" |
3504 | .IX Item "ev_feed_fd_event (loop, int fd, int revents)" |
3474 | Feed an event on the given fd, as if a file descriptor backend detected |
3505 | Feed an event on the given fd, as if a file descriptor backend detected |
3475 | the given events it. |
3506 | the given events it. |
3476 | .IP "ev_feed_signal_event (loop, int signum)" 4 |
3507 | .IP "ev_feed_signal_event (loop, int signum)" 4 |
3477 | .IX Item "ev_feed_signal_event (loop, int signum)" |
3508 | .IX Item "ev_feed_signal_event (loop, int signum)" |
3478 | Feed an event as if the given signal occurred (\f(CW\*(C`loop\*(C'\fR must be the default |
3509 | Feed an event as if the given signal occurred. See also \f(CW\*(C`ev_feed_signal\*(C'\fR, |
3479 | loop!). |
3510 | which is async-safe. |
|
|
3511 | .SH "COMMON OR USEFUL IDIOMS (OR BOTH)" |
|
|
3512 | .IX Header "COMMON OR USEFUL IDIOMS (OR BOTH)" |
|
|
3513 | This section explains some common idioms that are not immediately |
|
|
3514 | obvious. Note that examples are sprinkled over the whole manual, and this |
|
|
3515 | section only contains stuff that wouldn't fit anywhere else. |
|
|
3516 | .SS "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" |
|
|
3517 | .IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" |
|
|
3518 | Each watcher has, by default, a \f(CW\*(C`void *data\*(C'\fR member that you can read |
|
|
3519 | or modify at any time: libev will completely ignore it. This can be used |
|
|
3520 | to associate arbitrary data with your watcher. If you need more data and |
|
|
3521 | don't want to allocate memory separately and store a pointer to it in that |
|
|
3522 | data member, you can also \*(L"subclass\*(R" the watcher type and provide your own |
|
|
3523 | data: |
|
|
3524 | .PP |
|
|
3525 | .Vb 7 |
|
|
3526 | \& struct my_io |
|
|
3527 | \& { |
|
|
3528 | \& ev_io io; |
|
|
3529 | \& int otherfd; |
|
|
3530 | \& void *somedata; |
|
|
3531 | \& struct whatever *mostinteresting; |
|
|
3532 | \& }; |
|
|
3533 | \& |
|
|
3534 | \& ... |
|
|
3535 | \& struct my_io w; |
|
|
3536 | \& ev_io_init (&w.io, my_cb, fd, EV_READ); |
|
|
3537 | .Ve |
|
|
3538 | .PP |
|
|
3539 | And since your callback will be called with a pointer to the watcher, you |
|
|
3540 | can cast it back to your own type: |
|
|
3541 | .PP |
|
|
3542 | .Vb 5 |
|
|
3543 | \& static void my_cb (struct ev_loop *loop, ev_io *w_, int revents) |
|
|
3544 | \& { |
|
|
3545 | \& struct my_io *w = (struct my_io *)w_; |
|
|
3546 | \& ... |
|
|
3547 | \& } |
|
|
3548 | .Ve |
|
|
3549 | .PP |
|
|
3550 | More interesting and less C\-conformant ways of casting your callback |
|
|
3551 | function type instead have been omitted. |
|
|
3552 | .SS "\s-1BUILDING\s0 \s-1YOUR\s0 \s-1OWN\s0 \s-1COMPOSITE\s0 \s-1WATCHERS\s0" |
|
|
3553 | .IX Subsection "BUILDING YOUR OWN COMPOSITE WATCHERS" |
|
|
3554 | Another common scenario is to use some data structure with multiple |
|
|
3555 | embedded watchers, in effect creating your own watcher that combines |
|
|
3556 | multiple libev event sources into one \*(L"super-watcher\*(R": |
|
|
3557 | .PP |
|
|
3558 | .Vb 6 |
|
|
3559 | \& struct my_biggy |
|
|
3560 | \& { |
|
|
3561 | \& int some_data; |
|
|
3562 | \& ev_timer t1; |
|
|
3563 | \& ev_timer t2; |
|
|
3564 | \& } |
|
|
3565 | .Ve |
|
|
3566 | .PP |
|
|
3567 | In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more |
|
|
3568 | complicated: Either you store the address of your \f(CW\*(C`my_biggy\*(C'\fR struct in |
|
|
3569 | the \f(CW\*(C`data\*(C'\fR member of the watcher (for woozies or \*(C+ coders), or you need |
|
|
3570 | to use some pointer arithmetic using \f(CW\*(C`offsetof\*(C'\fR inside your watchers (for |
|
|
3571 | real programmers): |
|
|
3572 | .PP |
|
|
3573 | .Vb 1 |
|
|
3574 | \& #include <stddef.h> |
|
|
3575 | \& |
|
|
3576 | \& static void |
|
|
3577 | \& t1_cb (EV_P_ ev_timer *w, int revents) |
|
|
3578 | \& { |
|
|
3579 | \& struct my_biggy big = (struct my_biggy *) |
|
|
3580 | \& (((char *)w) \- offsetof (struct my_biggy, t1)); |
|
|
3581 | \& } |
|
|
3582 | \& |
|
|
3583 | \& static void |
|
|
3584 | \& t2_cb (EV_P_ ev_timer *w, int revents) |
|
|
3585 | \& { |
|
|
3586 | \& struct my_biggy big = (struct my_biggy *) |
|
|
3587 | \& (((char *)w) \- offsetof (struct my_biggy, t2)); |
|
|
3588 | \& } |
|
|
3589 | .Ve |
|
|
3590 | .SS "\s-1MODEL/NESTED\s0 \s-1EVENT\s0 \s-1LOOP\s0 \s-1INVOCATIONS\s0 \s-1AND\s0 \s-1EXIT\s0 \s-1CONDITIONS\s0" |
|
|
3591 | .IX Subsection "MODEL/NESTED EVENT LOOP INVOCATIONS AND EXIT CONDITIONS" |
|
|
3592 | Often (especially in \s-1GUI\s0 toolkits) there are places where you have |
|
|
3593 | \&\fImodal\fR interaction, which is most easily implemented by recursively |
|
|
3594 | invoking \f(CW\*(C`ev_run\*(C'\fR. |
|
|
3595 | .PP |
|
|
3596 | This brings the problem of exiting \- a callback might want to finish the |
|
|
3597 | main \f(CW\*(C`ev_run\*(C'\fR call, but not the nested one (e.g. user clicked \*(L"Quit\*(R", but |
|
|
3598 | a modal \*(L"Are you sure?\*(R" dialog is still waiting), or just the nested one |
|
|
3599 | and not the main one (e.g. user clocked \*(L"Ok\*(R" in a modal dialog), or some |
|
|
3600 | other combination: In these cases, \f(CW\*(C`ev_break\*(C'\fR will not work alone. |
|
|
3601 | .PP |
|
|
3602 | The solution is to maintain \*(L"break this loop\*(R" variable for each \f(CW\*(C`ev_run\*(C'\fR |
|
|
3603 | invocation, and use a loop around \f(CW\*(C`ev_run\*(C'\fR until the condition is |
|
|
3604 | triggered, using \f(CW\*(C`EVRUN_ONCE\*(C'\fR: |
|
|
3605 | .PP |
|
|
3606 | .Vb 2 |
|
|
3607 | \& // main loop |
|
|
3608 | \& int exit_main_loop = 0; |
|
|
3609 | \& |
|
|
3610 | \& while (!exit_main_loop) |
|
|
3611 | \& ev_run (EV_DEFAULT_ EVRUN_ONCE); |
|
|
3612 | \& |
|
|
3613 | \& // in a model watcher |
|
|
3614 | \& int exit_nested_loop = 0; |
|
|
3615 | \& |
|
|
3616 | \& while (!exit_nested_loop) |
|
|
3617 | \& ev_run (EV_A_ EVRUN_ONCE); |
|
|
3618 | .Ve |
|
|
3619 | .PP |
|
|
3620 | To exit from any of these loops, just set the corresponding exit variable: |
|
|
3621 | .PP |
|
|
3622 | .Vb 2 |
|
|
3623 | \& // exit modal loop |
|
|
3624 | \& exit_nested_loop = 1; |
|
|
3625 | \& |
|
|
3626 | \& // exit main program, after modal loop is finished |
|
|
3627 | \& exit_main_loop = 1; |
|
|
3628 | \& |
|
|
3629 | \& // exit both |
|
|
3630 | \& exit_main_loop = exit_nested_loop = 1; |
|
|
3631 | .Ve |
|
|
3632 | .SS "\s-1THREAD\s0 \s-1LOCKING\s0 \s-1EXAMPLE\s0" |
|
|
3633 | .IX Subsection "THREAD LOCKING EXAMPLE" |
|
|
3634 | Here is a fictitious example of how to run an event loop in a different |
|
|
3635 | thread from where callbacks are being invoked and watchers are |
|
|
3636 | created/added/removed. |
|
|
3637 | .PP |
|
|
3638 | For a real-world example, see the \f(CW\*(C`EV::Loop::Async\*(C'\fR perl module, |
|
|
3639 | which uses exactly this technique (which is suited for many high-level |
|
|
3640 | languages). |
|
|
3641 | .PP |
|
|
3642 | The example uses a pthread mutex to protect the loop data, a condition |
|
|
3643 | variable to wait for callback invocations, an async watcher to notify the |
|
|
3644 | event loop thread and an unspecified mechanism to wake up the main thread. |
|
|
3645 | .PP |
|
|
3646 | First, you need to associate some data with the event loop: |
|
|
3647 | .PP |
|
|
3648 | .Vb 6 |
|
|
3649 | \& typedef struct { |
|
|
3650 | \& mutex_t lock; /* global loop lock */ |
|
|
3651 | \& ev_async async_w; |
|
|
3652 | \& thread_t tid; |
|
|
3653 | \& cond_t invoke_cv; |
|
|
3654 | \& } userdata; |
|
|
3655 | \& |
|
|
3656 | \& void prepare_loop (EV_P) |
|
|
3657 | \& { |
|
|
3658 | \& // for simplicity, we use a static userdata struct. |
|
|
3659 | \& static userdata u; |
|
|
3660 | \& |
|
|
3661 | \& ev_async_init (&u\->async_w, async_cb); |
|
|
3662 | \& ev_async_start (EV_A_ &u\->async_w); |
|
|
3663 | \& |
|
|
3664 | \& pthread_mutex_init (&u\->lock, 0); |
|
|
3665 | \& pthread_cond_init (&u\->invoke_cv, 0); |
|
|
3666 | \& |
|
|
3667 | \& // now associate this with the loop |
|
|
3668 | \& ev_set_userdata (EV_A_ u); |
|
|
3669 | \& ev_set_invoke_pending_cb (EV_A_ l_invoke); |
|
|
3670 | \& ev_set_loop_release_cb (EV_A_ l_release, l_acquire); |
|
|
3671 | \& |
|
|
3672 | \& // then create the thread running ev_run |
|
|
3673 | \& pthread_create (&u\->tid, 0, l_run, EV_A); |
|
|
3674 | \& } |
|
|
3675 | .Ve |
|
|
3676 | .PP |
|
|
3677 | The callback for the \f(CW\*(C`ev_async\*(C'\fR watcher does nothing: the watcher is used |
|
|
3678 | solely to wake up the event loop so it takes notice of any new watchers |
|
|
3679 | that might have been added: |
|
|
3680 | .PP |
|
|
3681 | .Vb 5 |
|
|
3682 | \& static void |
|
|
3683 | \& async_cb (EV_P_ ev_async *w, int revents) |
|
|
3684 | \& { |
|
|
3685 | \& // just used for the side effects |
|
|
3686 | \& } |
|
|
3687 | .Ve |
|
|
3688 | .PP |
|
|
3689 | The \f(CW\*(C`l_release\*(C'\fR and \f(CW\*(C`l_acquire\*(C'\fR callbacks simply unlock/lock the mutex |
|
|
3690 | protecting the loop data, respectively. |
|
|
3691 | .PP |
|
|
3692 | .Vb 6 |
|
|
3693 | \& static void |
|
|
3694 | \& l_release (EV_P) |
|
|
3695 | \& { |
|
|
3696 | \& userdata *u = ev_userdata (EV_A); |
|
|
3697 | \& pthread_mutex_unlock (&u\->lock); |
|
|
3698 | \& } |
|
|
3699 | \& |
|
|
3700 | \& static void |
|
|
3701 | \& l_acquire (EV_P) |
|
|
3702 | \& { |
|
|
3703 | \& userdata *u = ev_userdata (EV_A); |
|
|
3704 | \& pthread_mutex_lock (&u\->lock); |
|
|
3705 | \& } |
|
|
3706 | .Ve |
|
|
3707 | .PP |
|
|
3708 | The event loop thread first acquires the mutex, and then jumps straight |
|
|
3709 | into \f(CW\*(C`ev_run\*(C'\fR: |
|
|
3710 | .PP |
|
|
3711 | .Vb 4 |
|
|
3712 | \& void * |
|
|
3713 | \& l_run (void *thr_arg) |
|
|
3714 | \& { |
|
|
3715 | \& struct ev_loop *loop = (struct ev_loop *)thr_arg; |
|
|
3716 | \& |
|
|
3717 | \& l_acquire (EV_A); |
|
|
3718 | \& pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); |
|
|
3719 | \& ev_run (EV_A_ 0); |
|
|
3720 | \& l_release (EV_A); |
|
|
3721 | \& |
|
|
3722 | \& return 0; |
|
|
3723 | \& } |
|
|
3724 | .Ve |
|
|
3725 | .PP |
|
|
3726 | Instead of invoking all pending watchers, the \f(CW\*(C`l_invoke\*(C'\fR callback will |
|
|
3727 | signal the main thread via some unspecified mechanism (signals? pipe |
|
|
3728 | writes? \f(CW\*(C`Async::Interrupt\*(C'\fR?) and then waits until all pending watchers |
|
|
3729 | have been called (in a while loop because a) spurious wakeups are possible |
|
|
3730 | and b) skipping inter-thread-communication when there are no pending |
|
|
3731 | watchers is very beneficial): |
|
|
3732 | .PP |
|
|
3733 | .Vb 4 |
|
|
3734 | \& static void |
|
|
3735 | \& l_invoke (EV_P) |
|
|
3736 | \& { |
|
|
3737 | \& userdata *u = ev_userdata (EV_A); |
|
|
3738 | \& |
|
|
3739 | \& while (ev_pending_count (EV_A)) |
|
|
3740 | \& { |
|
|
3741 | \& wake_up_other_thread_in_some_magic_or_not_so_magic_way (); |
|
|
3742 | \& pthread_cond_wait (&u\->invoke_cv, &u\->lock); |
|
|
3743 | \& } |
|
|
3744 | \& } |
|
|
3745 | .Ve |
|
|
3746 | .PP |
|
|
3747 | Now, whenever the main thread gets told to invoke pending watchers, it |
|
|
3748 | will grab the lock, call \f(CW\*(C`ev_invoke_pending\*(C'\fR and then signal the loop |
|
|
3749 | thread to continue: |
|
|
3750 | .PP |
|
|
3751 | .Vb 4 |
|
|
3752 | \& static void |
|
|
3753 | \& real_invoke_pending (EV_P) |
|
|
3754 | \& { |
|
|
3755 | \& userdata *u = ev_userdata (EV_A); |
|
|
3756 | \& |
|
|
3757 | \& pthread_mutex_lock (&u\->lock); |
|
|
3758 | \& ev_invoke_pending (EV_A); |
|
|
3759 | \& pthread_cond_signal (&u\->invoke_cv); |
|
|
3760 | \& pthread_mutex_unlock (&u\->lock); |
|
|
3761 | \& } |
|
|
3762 | .Ve |
|
|
3763 | .PP |
|
|
3764 | Whenever you want to start/stop a watcher or do other modifications to an |
|
|
3765 | event loop, you will now have to lock: |
|
|
3766 | .PP |
|
|
3767 | .Vb 2 |
|
|
3768 | \& ev_timer timeout_watcher; |
|
|
3769 | \& userdata *u = ev_userdata (EV_A); |
|
|
3770 | \& |
|
|
3771 | \& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
3772 | \& |
|
|
3773 | \& pthread_mutex_lock (&u\->lock); |
|
|
3774 | \& ev_timer_start (EV_A_ &timeout_watcher); |
|
|
3775 | \& ev_async_send (EV_A_ &u\->async_w); |
|
|
3776 | \& pthread_mutex_unlock (&u\->lock); |
|
|
3777 | .Ve |
|
|
3778 | .PP |
|
|
3779 | Note that sending the \f(CW\*(C`ev_async\*(C'\fR watcher is required because otherwise |
|
|
3780 | an event loop currently blocking in the kernel will have no knowledge |
|
|
3781 | about the newly added timer. By waking up the loop it will pick up any new |
|
|
3782 | watchers in the next event loop iteration. |
|
|
3783 | .SS "\s-1THREADS\s0, \s-1COROUTINES\s0, \s-1CONTINUATIONS\s0, \s-1QUEUES\s0... \s-1INSTEAD\s0 \s-1OF\s0 \s-1CALLBACKS\s0" |
|
|
3784 | .IX Subsection "THREADS, COROUTINES, CONTINUATIONS, QUEUES... INSTEAD OF CALLBACKS" |
|
|
3785 | While the overhead of a callback that e.g. schedules a thread is small, it |
|
|
3786 | is still an overhead. If you embed libev, and your main usage is with some |
|
|
3787 | kind of threads or coroutines, you might want to customise libev so that |
|
|
3788 | doesn't need callbacks anymore. |
|
|
3789 | .PP |
|
|
3790 | Imagine you have coroutines that you can switch to using a function |
|
|
3791 | \&\f(CW\*(C`switch_to (coro)\*(C'\fR, that libev runs in a coroutine called \f(CW\*(C`libev_coro\*(C'\fR |
|
|
3792 | and that due to some magic, the currently active coroutine is stored in a |
|
|
3793 | global called \f(CW\*(C`current_coro\*(C'\fR. Then you can build your own \*(L"wait for libev |
|
|
3794 | event\*(R" primitive by changing \f(CW\*(C`EV_CB_DECLARE\*(C'\fR and \f(CW\*(C`EV_CB_INVOKE\*(C'\fR (note |
|
|
3795 | the differing \f(CW\*(C`;\*(C'\fR conventions): |
|
|
3796 | .PP |
|
|
3797 | .Vb 2 |
|
|
3798 | \& #define EV_CB_DECLARE(type) struct my_coro *cb; |
|
|
3799 | \& #define EV_CB_INVOKE(watcher) switch_to ((watcher)\->cb) |
|
|
3800 | .Ve |
|
|
3801 | .PP |
|
|
3802 | That means instead of having a C callback function, you store the |
|
|
3803 | coroutine to switch to in each watcher, and instead of having libev call |
|
|
3804 | your callback, you instead have it switch to that coroutine. |
|
|
3805 | .PP |
|
|
3806 | A coroutine might now wait for an event with a function called |
|
|
3807 | \&\f(CW\*(C`wait_for_event\*(C'\fR. (the watcher needs to be started, as always, but it doesn't |
|
|
3808 | matter when, or whether the watcher is active or not when this function is |
|
|
3809 | called): |
|
|
3810 | .PP |
|
|
3811 | .Vb 6 |
|
|
3812 | \& void |
|
|
3813 | \& wait_for_event (ev_watcher *w) |
|
|
3814 | \& { |
|
|
3815 | \& ev_cb_set (w) = current_coro; |
|
|
3816 | \& switch_to (libev_coro); |
|
|
3817 | \& } |
|
|
3818 | .Ve |
|
|
3819 | .PP |
|
|
3820 | That basically suspends the coroutine inside \f(CW\*(C`wait_for_event\*(C'\fR and |
|
|
3821 | continues the libev coroutine, which, when appropriate, switches back to |
|
|
3822 | this or any other coroutine. I am sure if you sue this your own :) |
|
|
3823 | .PP |
|
|
3824 | You can do similar tricks if you have, say, threads with an event queue \- |
|
|
3825 | instead of storing a coroutine, you store the queue object and instead of |
|
|
3826 | switching to a coroutine, you push the watcher onto the queue and notify |
|
|
3827 | any waiters. |
|
|
3828 | .PP |
|
|
3829 | To embed libev, see \s-1EMBEDDING\s0, but in short, it's easiest to create two |
|
|
3830 | files, \fImy_ev.h\fR and \fImy_ev.c\fR that include the respective libev files: |
|
|
3831 | .PP |
|
|
3832 | .Vb 4 |
|
|
3833 | \& // my_ev.h |
|
|
3834 | \& #define EV_CB_DECLARE(type) struct my_coro *cb; |
|
|
3835 | \& #define EV_CB_INVOKE(watcher) switch_to ((watcher)\->cb); |
|
|
3836 | \& #include "../libev/ev.h" |
|
|
3837 | \& |
|
|
3838 | \& // my_ev.c |
|
|
3839 | \& #define EV_H "my_ev.h" |
|
|
3840 | \& #include "../libev/ev.c" |
|
|
3841 | .Ve |
|
|
3842 | .PP |
|
|
3843 | And then use \fImy_ev.h\fR when you would normally use \fIev.h\fR, and compile |
|
|
3844 | \&\fImy_ev.c\fR into your project. When properly specifying include paths, you |
|
|
3845 | can even use \fIev.h\fR as header file name directly. |
3480 | .SH "LIBEVENT EMULATION" |
3846 | .SH "LIBEVENT EMULATION" |
3481 | .IX Header "LIBEVENT EMULATION" |
3847 | .IX Header "LIBEVENT EMULATION" |
3482 | Libev offers a compatibility emulation layer for libevent. It cannot |
3848 | Libev offers a compatibility emulation layer for libevent. It cannot |
3483 | emulate the internals of libevent, so here are some usage hints: |
3849 | emulate the internals of libevent, so here are some usage hints: |
|
|
3850 | .IP "\(bu" 4 |
|
|
3851 | Only the libevent\-1.4.1\-beta \s-1API\s0 is being emulated. |
|
|
3852 | .Sp |
|
|
3853 | This was the newest libevent version available when libev was implemented, |
|
|
3854 | and is still mostly unchanged in 2010. |
3484 | .IP "\(bu" 4 |
3855 | .IP "\(bu" 4 |
3485 | Use it by including <event.h>, as usual. |
3856 | Use it by including <event.h>, as usual. |
3486 | .IP "\(bu" 4 |
3857 | .IP "\(bu" 4 |
3487 | The following members are fully supported: ev_base, ev_callback, |
3858 | The following members are fully supported: ev_base, ev_callback, |
3488 | ev_arg, ev_fd, ev_res, ev_events. |
3859 | ev_arg, ev_fd, ev_res, ev_events. |
… | |
… | |
3494 | Priorities are not currently supported. Initialising priorities |
3865 | Priorities are not currently supported. Initialising priorities |
3495 | will fail and all watchers will have the same priority, even though there |
3866 | will fail and all watchers will have the same priority, even though there |
3496 | is an ev_pri field. |
3867 | is an ev_pri field. |
3497 | .IP "\(bu" 4 |
3868 | .IP "\(bu" 4 |
3498 | In libevent, the last base created gets the signals, in libev, the |
3869 | In libevent, the last base created gets the signals, in libev, the |
3499 | first base created (== the default loop) gets the signals. |
3870 | base that registered the signal gets the signals. |
3500 | .IP "\(bu" 4 |
3871 | .IP "\(bu" 4 |
3501 | Other members are not supported. |
3872 | Other members are not supported. |
3502 | .IP "\(bu" 4 |
3873 | .IP "\(bu" 4 |
3503 | The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need |
3874 | The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need |
3504 | to use the libev header file and library. |
3875 | to use the libev header file and library. |
… | |
… | |
3522 | Care has been taken to keep the overhead low. The only data member the \*(C+ |
3893 | Care has been taken to keep the overhead low. The only data member the \*(C+ |
3523 | classes add (compared to plain C\-style watchers) is the event loop pointer |
3894 | classes add (compared to plain C\-style watchers) is the event loop pointer |
3524 | that the watcher is associated with (or no additional members at all if |
3895 | that the watcher is associated with (or no additional members at all if |
3525 | you disable \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR when embedding libev). |
3896 | you disable \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR when embedding libev). |
3526 | .PP |
3897 | .PP |
3527 | Currently, functions, and static and non-static member functions can be |
3898 | Currently, functions, static and non-static member functions and classes |
3528 | used as callbacks. Other types should be easy to add as long as they only |
3899 | with \f(CW\*(C`operator ()\*(C'\fR can be used as callbacks. Other types should be easy |
3529 | need one additional pointer for context. If you need support for other |
3900 | to add as long as they only need one additional pointer for context. If |
3530 | types of functors please contact the author (preferably after implementing |
3901 | you need support for other types of functors please contact the author |
3531 | it). |
3902 | (preferably after implementing it). |
3532 | .PP |
3903 | .PP |
3533 | Here is a list of things available in the \f(CW\*(C`ev\*(C'\fR namespace: |
3904 | Here is a list of things available in the \f(CW\*(C`ev\*(C'\fR namespace: |
3534 | .ie n .IP """ev::READ"", ""ev::WRITE"" etc." 4 |
3905 | .ie n .IP """ev::READ"", ""ev::WRITE"" etc." 4 |
3535 | .el .IP "\f(CWev::READ\fR, \f(CWev::WRITE\fR etc." 4 |
3906 | .el .IP "\f(CWev::READ\fR, \f(CWev::WRITE\fR etc." 4 |
3536 | .IX Item "ev::READ, ev::WRITE etc." |
3907 | .IX Item "ev::READ, ev::WRITE etc." |
… | |
… | |
4374 | .PP |
4745 | .PP |
4375 | .Vb 2 |
4746 | .Vb 2 |
4376 | \& #include "ev_cpp.h" |
4747 | \& #include "ev_cpp.h" |
4377 | \& #include "ev.c" |
4748 | \& #include "ev.c" |
4378 | .Ve |
4749 | .Ve |
4379 | .SH "INTERACTION WITH OTHER PROGRAMS OR LIBRARIES" |
4750 | .SH "INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT" |
4380 | .IX Header "INTERACTION WITH OTHER PROGRAMS OR LIBRARIES" |
4751 | .IX Header "INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT" |
4381 | .SS "\s-1THREADS\s0 \s-1AND\s0 \s-1COROUTINES\s0" |
4752 | .SS "\s-1THREADS\s0 \s-1AND\s0 \s-1COROUTINES\s0" |
4382 | .IX Subsection "THREADS AND COROUTINES" |
4753 | .IX Subsection "THREADS AND COROUTINES" |
4383 | \fI\s-1THREADS\s0\fR |
4754 | \fI\s-1THREADS\s0\fR |
4384 | .IX Subsection "THREADS" |
4755 | .IX Subsection "THREADS" |
4385 | .PP |
4756 | .PP |
… | |
… | |
4432 | An example use would be to communicate signals or other events that only |
4803 | An example use would be to communicate signals or other events that only |
4433 | work in the default loop by registering the signal watcher with the |
4804 | work in the default loop by registering the signal watcher with the |
4434 | default loop and triggering an \f(CW\*(C`ev_async\*(C'\fR watcher from the default loop |
4805 | default loop and triggering an \f(CW\*(C`ev_async\*(C'\fR watcher from the default loop |
4435 | watcher callback into the event loop interested in the signal. |
4806 | watcher callback into the event loop interested in the signal. |
4436 | .PP |
4807 | .PP |
4437 | \s-1THREAD\s0 \s-1LOCKING\s0 \s-1EXAMPLE\s0 |
4808 | See also \*(L"\s-1THREAD\s0 \s-1LOCKING\s0 \s-1EXAMPLE\s0\*(R". |
4438 | .IX Subsection "THREAD LOCKING EXAMPLE" |
|
|
4439 | .PP |
|
|
4440 | Here is a fictitious example of how to run an event loop in a different |
|
|
4441 | thread than where callbacks are being invoked and watchers are |
|
|
4442 | created/added/removed. |
|
|
4443 | .PP |
|
|
4444 | For a real-world example, see the \f(CW\*(C`EV::Loop::Async\*(C'\fR perl module, |
|
|
4445 | which uses exactly this technique (which is suited for many high-level |
|
|
4446 | languages). |
|
|
4447 | .PP |
|
|
4448 | The example uses a pthread mutex to protect the loop data, a condition |
|
|
4449 | variable to wait for callback invocations, an async watcher to notify the |
|
|
4450 | event loop thread and an unspecified mechanism to wake up the main thread. |
|
|
4451 | .PP |
|
|
4452 | First, you need to associate some data with the event loop: |
|
|
4453 | .PP |
|
|
4454 | .Vb 6 |
|
|
4455 | \& typedef struct { |
|
|
4456 | \& mutex_t lock; /* global loop lock */ |
|
|
4457 | \& ev_async async_w; |
|
|
4458 | \& thread_t tid; |
|
|
4459 | \& cond_t invoke_cv; |
|
|
4460 | \& } userdata; |
|
|
4461 | \& |
|
|
4462 | \& void prepare_loop (EV_P) |
|
|
4463 | \& { |
|
|
4464 | \& // for simplicity, we use a static userdata struct. |
|
|
4465 | \& static userdata u; |
|
|
4466 | \& |
|
|
4467 | \& ev_async_init (&u\->async_w, async_cb); |
|
|
4468 | \& ev_async_start (EV_A_ &u\->async_w); |
|
|
4469 | \& |
|
|
4470 | \& pthread_mutex_init (&u\->lock, 0); |
|
|
4471 | \& pthread_cond_init (&u\->invoke_cv, 0); |
|
|
4472 | \& |
|
|
4473 | \& // now associate this with the loop |
|
|
4474 | \& ev_set_userdata (EV_A_ u); |
|
|
4475 | \& ev_set_invoke_pending_cb (EV_A_ l_invoke); |
|
|
4476 | \& ev_set_loop_release_cb (EV_A_ l_release, l_acquire); |
|
|
4477 | \& |
|
|
4478 | \& // then create the thread running ev_loop |
|
|
4479 | \& pthread_create (&u\->tid, 0, l_run, EV_A); |
|
|
4480 | \& } |
|
|
4481 | .Ve |
|
|
4482 | .PP |
|
|
4483 | The callback for the \f(CW\*(C`ev_async\*(C'\fR watcher does nothing: the watcher is used |
|
|
4484 | solely to wake up the event loop so it takes notice of any new watchers |
|
|
4485 | that might have been added: |
|
|
4486 | .PP |
|
|
4487 | .Vb 5 |
|
|
4488 | \& static void |
|
|
4489 | \& async_cb (EV_P_ ev_async *w, int revents) |
|
|
4490 | \& { |
|
|
4491 | \& // just used for the side effects |
|
|
4492 | \& } |
|
|
4493 | .Ve |
|
|
4494 | .PP |
|
|
4495 | The \f(CW\*(C`l_release\*(C'\fR and \f(CW\*(C`l_acquire\*(C'\fR callbacks simply unlock/lock the mutex |
|
|
4496 | protecting the loop data, respectively. |
|
|
4497 | .PP |
|
|
4498 | .Vb 6 |
|
|
4499 | \& static void |
|
|
4500 | \& l_release (EV_P) |
|
|
4501 | \& { |
|
|
4502 | \& userdata *u = ev_userdata (EV_A); |
|
|
4503 | \& pthread_mutex_unlock (&u\->lock); |
|
|
4504 | \& } |
|
|
4505 | \& |
|
|
4506 | \& static void |
|
|
4507 | \& l_acquire (EV_P) |
|
|
4508 | \& { |
|
|
4509 | \& userdata *u = ev_userdata (EV_A); |
|
|
4510 | \& pthread_mutex_lock (&u\->lock); |
|
|
4511 | \& } |
|
|
4512 | .Ve |
|
|
4513 | .PP |
|
|
4514 | The event loop thread first acquires the mutex, and then jumps straight |
|
|
4515 | into \f(CW\*(C`ev_run\*(C'\fR: |
|
|
4516 | .PP |
|
|
4517 | .Vb 4 |
|
|
4518 | \& void * |
|
|
4519 | \& l_run (void *thr_arg) |
|
|
4520 | \& { |
|
|
4521 | \& struct ev_loop *loop = (struct ev_loop *)thr_arg; |
|
|
4522 | \& |
|
|
4523 | \& l_acquire (EV_A); |
|
|
4524 | \& pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); |
|
|
4525 | \& ev_run (EV_A_ 0); |
|
|
4526 | \& l_release (EV_A); |
|
|
4527 | \& |
|
|
4528 | \& return 0; |
|
|
4529 | \& } |
|
|
4530 | .Ve |
|
|
4531 | .PP |
|
|
4532 | Instead of invoking all pending watchers, the \f(CW\*(C`l_invoke\*(C'\fR callback will |
|
|
4533 | signal the main thread via some unspecified mechanism (signals? pipe |
|
|
4534 | writes? \f(CW\*(C`Async::Interrupt\*(C'\fR?) and then waits until all pending watchers |
|
|
4535 | have been called (in a while loop because a) spurious wakeups are possible |
|
|
4536 | and b) skipping inter-thread-communication when there are no pending |
|
|
4537 | watchers is very beneficial): |
|
|
4538 | .PP |
|
|
4539 | .Vb 4 |
|
|
4540 | \& static void |
|
|
4541 | \& l_invoke (EV_P) |
|
|
4542 | \& { |
|
|
4543 | \& userdata *u = ev_userdata (EV_A); |
|
|
4544 | \& |
|
|
4545 | \& while (ev_pending_count (EV_A)) |
|
|
4546 | \& { |
|
|
4547 | \& wake_up_other_thread_in_some_magic_or_not_so_magic_way (); |
|
|
4548 | \& pthread_cond_wait (&u\->invoke_cv, &u\->lock); |
|
|
4549 | \& } |
|
|
4550 | \& } |
|
|
4551 | .Ve |
|
|
4552 | .PP |
|
|
4553 | Now, whenever the main thread gets told to invoke pending watchers, it |
|
|
4554 | will grab the lock, call \f(CW\*(C`ev_invoke_pending\*(C'\fR and then signal the loop |
|
|
4555 | thread to continue: |
|
|
4556 | .PP |
|
|
4557 | .Vb 4 |
|
|
4558 | \& static void |
|
|
4559 | \& real_invoke_pending (EV_P) |
|
|
4560 | \& { |
|
|
4561 | \& userdata *u = ev_userdata (EV_A); |
|
|
4562 | \& |
|
|
4563 | \& pthread_mutex_lock (&u\->lock); |
|
|
4564 | \& ev_invoke_pending (EV_A); |
|
|
4565 | \& pthread_cond_signal (&u\->invoke_cv); |
|
|
4566 | \& pthread_mutex_unlock (&u\->lock); |
|
|
4567 | \& } |
|
|
4568 | .Ve |
|
|
4569 | .PP |
|
|
4570 | Whenever you want to start/stop a watcher or do other modifications to an |
|
|
4571 | event loop, you will now have to lock: |
|
|
4572 | .PP |
|
|
4573 | .Vb 2 |
|
|
4574 | \& ev_timer timeout_watcher; |
|
|
4575 | \& userdata *u = ev_userdata (EV_A); |
|
|
4576 | \& |
|
|
4577 | \& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
4578 | \& |
|
|
4579 | \& pthread_mutex_lock (&u\->lock); |
|
|
4580 | \& ev_timer_start (EV_A_ &timeout_watcher); |
|
|
4581 | \& ev_async_send (EV_A_ &u\->async_w); |
|
|
4582 | \& pthread_mutex_unlock (&u\->lock); |
|
|
4583 | .Ve |
|
|
4584 | .PP |
|
|
4585 | Note that sending the \f(CW\*(C`ev_async\*(C'\fR watcher is required because otherwise |
|
|
4586 | an event loop currently blocking in the kernel will have no knowledge |
|
|
4587 | about the newly added timer. By waking up the loop it will pick up any new |
|
|
4588 | watchers in the next event loop iteration. |
|
|
4589 | .PP |
4809 | .PP |
4590 | \fI\s-1COROUTINES\s0\fR |
4810 | \fI\s-1COROUTINES\s0\fR |
4591 | .IX Subsection "COROUTINES" |
4811 | .IX Subsection "COROUTINES" |
4592 | .PP |
4812 | .PP |
4593 | Libev is very accommodating to coroutines (\*(L"cooperative threads\*(R"): |
4813 | Libev is very accommodating to coroutines (\*(L"cooperative threads\*(R"): |
… | |
… | |
5077 | .IX Item "real time" |
5297 | .IX Item "real time" |
5078 | The physical time that is observed. It is apparently strictly monotonic :) |
5298 | The physical time that is observed. It is apparently strictly monotonic :) |
5079 | .IP "wall-clock time" 4 |
5299 | .IP "wall-clock time" 4 |
5080 | .IX Item "wall-clock time" |
5300 | .IX Item "wall-clock time" |
5081 | The time and date as shown on clocks. Unlike real time, it can actually |
5301 | The time and date as shown on clocks. Unlike real time, it can actually |
5082 | be wrong and jump forwards and backwards, e.g. when the you adjust your |
5302 | be wrong and jump forwards and backwards, e.g. when you adjust your |
5083 | clock. |
5303 | clock. |
5084 | .IP "watcher" 4 |
5304 | .IP "watcher" 4 |
5085 | .IX Item "watcher" |
5305 | .IX Item "watcher" |
5086 | A data structure that describes interest in certain events. Watchers need |
5306 | A data structure that describes interest in certain events. Watchers need |
5087 | to be started (attached to an event loop) before they can receive events. |
5307 | to be started (attached to an event loop) before they can receive events. |
5088 | .SH "AUTHOR" |
5308 | .SH "AUTHOR" |
5089 | .IX Header "AUTHOR" |
5309 | .IX Header "AUTHOR" |
5090 | Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael |
5310 | Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael |
5091 | Magnusson and Emanuele Giaquinta. |
5311 | Magnusson and Emanuele Giaquinta, and minor corrections by many others. |