… | |
… | |
63 | details of the event, and then hand it over to libev by I<starting> the |
63 | details of the event, and then hand it over to libev by I<starting> the |
64 | watcher. |
64 | watcher. |
65 | |
65 | |
66 | =head1 FEATURES |
66 | =head1 FEATURES |
67 | |
67 | |
68 | Libev supports C<select>, C<poll>, the linux-specific C<epoll>, the |
68 | Libev supports C<select>, C<poll>, the Linux-specific C<epoll>, the |
69 | bsd-specific C<kqueue> and the solaris-specific event port mechanisms |
69 | BSD-specific C<kqueue> and the Solaris-specific event port mechanisms |
70 | for file descriptor events (C<ev_io>), relative timers (C<ev_timer>), |
70 | for file descriptor events (C<ev_io>), the Linux C<inotify> interface |
|
|
71 | (for C<ev_stat>), relative timers (C<ev_timer>), absolute timers |
71 | absolute timers with customised rescheduling (C<ev_periodic>), synchronous |
72 | with customised rescheduling (C<ev_periodic>), synchronous signals |
72 | signals (C<ev_signal>), process status change events (C<ev_child>), and |
73 | (C<ev_signal>), process status change events (C<ev_child>), and event |
73 | event watchers dealing with the event loop mechanism itself (C<ev_idle>, |
74 | watchers dealing with the event loop mechanism itself (C<ev_idle>, |
74 | C<ev_embed>, C<ev_prepare> and C<ev_check> watchers) as well as |
75 | C<ev_embed>, C<ev_prepare> and C<ev_check> watchers) as well as |
75 | file watchers (C<ev_stat>) and even limited support for fork events |
76 | file watchers (C<ev_stat>) and even limited support for fork events |
76 | (C<ev_fork>). |
77 | (C<ev_fork>). |
77 | |
78 | |
78 | It also is quite fast (see this |
79 | It also is quite fast (see this |
… | |
… | |
162 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
163 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
163 | recommended ones. |
164 | recommended ones. |
164 | |
165 | |
165 | See the description of C<ev_embed> watchers for more info. |
166 | See the description of C<ev_embed> watchers for more info. |
166 | |
167 | |
167 | =item ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
168 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
168 | |
169 | |
169 | Sets the allocation function to use (the prototype and semantics are |
170 | Sets the allocation function to use (the prototype is similar - the |
170 | identical to the realloc C function). It is used to allocate and free |
171 | semantics is identical - to the realloc C function). It is used to |
171 | memory (no surprises here). If it returns zero when memory needs to be |
172 | allocate and free memory (no surprises here). If it returns zero when |
172 | allocated, the library might abort or take some potentially destructive |
173 | memory needs to be allocated, the library might abort or take some |
173 | action. The default is your system realloc function. |
174 | potentially destructive action. The default is your system realloc |
|
|
175 | function. |
174 | |
176 | |
175 | You could override this function in high-availability programs to, say, |
177 | You could override this function in high-availability programs to, say, |
176 | free some memory if it cannot allocate memory, to use a special allocator, |
178 | free some memory if it cannot allocate memory, to use a special allocator, |
177 | or even to sleep a while and retry until some memory is available. |
179 | or even to sleep a while and retry until some memory is available. |
178 | |
180 | |
… | |
… | |
263 | or setgid) then libev will I<not> look at the environment variable |
265 | or setgid) then libev will I<not> look at the environment variable |
264 | C<LIBEV_FLAGS>. Otherwise (the default), this environment variable will |
266 | C<LIBEV_FLAGS>. Otherwise (the default), this environment variable will |
265 | override the flags completely if it is found in the environment. This is |
267 | override the flags completely if it is found in the environment. This is |
266 | useful to try out specific backends to test their performance, or to work |
268 | useful to try out specific backends to test their performance, or to work |
267 | around bugs. |
269 | around bugs. |
|
|
270 | |
|
|
271 | =item C<EVFLAG_FORKCHECK> |
|
|
272 | |
|
|
273 | Instead of calling C<ev_default_fork> or C<ev_loop_fork> manually after |
|
|
274 | a fork, you can also make libev check for a fork in each iteration by |
|
|
275 | enabling this flag. |
|
|
276 | |
|
|
277 | This works by calling C<getpid ()> on every iteration of the loop, |
|
|
278 | and thus this might slow down your event loop if you do a lot of loop |
|
|
279 | iterations and little real work, but is usually not noticable (on my |
|
|
280 | Linux system for example, C<getpid> is actually a simple 5-insn sequence |
|
|
281 | without a syscall and thus I<very> fast, but my Linux system also has |
|
|
282 | C<pthread_atfork> which is even faster). |
|
|
283 | |
|
|
284 | The big advantage of this flag is that you can forget about fork (and |
|
|
285 | forget about forgetting to tell libev about forking) when you use this |
|
|
286 | flag. |
|
|
287 | |
|
|
288 | This flag setting cannot be overriden or specified in the C<LIBEV_FLAGS> |
|
|
289 | environment variable. |
268 | |
290 | |
269 | =item C<EVBACKEND_SELECT> (value 1, portable select backend) |
291 | =item C<EVBACKEND_SELECT> (value 1, portable select backend) |
270 | |
292 | |
271 | This is your standard select(2) backend. Not I<completely> standard, as |
293 | This is your standard select(2) backend. Not I<completely> standard, as |
272 | libev tries to roll its own fd_set with no limits on the number of fds, |
294 | libev tries to roll its own fd_set with no limits on the number of fds, |
… | |
… | |
703 | events but its callback has not yet been invoked). As long as a watcher |
725 | events but its callback has not yet been invoked). As long as a watcher |
704 | is pending (but not active) you must not call an init function on it (but |
726 | is pending (but not active) you must not call an init function on it (but |
705 | C<ev_TYPE_set> is safe) and you must make sure the watcher is available to |
727 | C<ev_TYPE_set> is safe) and you must make sure the watcher is available to |
706 | libev (e.g. you cnanot C<free ()> it). |
728 | libev (e.g. you cnanot C<free ()> it). |
707 | |
729 | |
708 | =item callback = ev_cb (ev_TYPE *watcher) |
730 | =item callback ev_cb (ev_TYPE *watcher) |
709 | |
731 | |
710 | Returns the callback currently set on the watcher. |
732 | Returns the callback currently set on the watcher. |
711 | |
733 | |
712 | =item ev_cb_set (ev_TYPE *watcher, callback) |
734 | =item ev_cb_set (ev_TYPE *watcher, callback) |
713 | |
735 | |
… | |
… | |
741 | { |
763 | { |
742 | struct my_io *w = (struct my_io *)w_; |
764 | struct my_io *w = (struct my_io *)w_; |
743 | ... |
765 | ... |
744 | } |
766 | } |
745 | |
767 | |
746 | More interesting and less C-conformant ways of catsing your callback type |
768 | More interesting and less C-conformant ways of casting your callback type |
747 | have been omitted.... |
769 | instead have been omitted. |
|
|
770 | |
|
|
771 | Another common scenario is having some data structure with multiple |
|
|
772 | watchers: |
|
|
773 | |
|
|
774 | struct my_biggy |
|
|
775 | { |
|
|
776 | int some_data; |
|
|
777 | ev_timer t1; |
|
|
778 | ev_timer t2; |
|
|
779 | } |
|
|
780 | |
|
|
781 | In this case getting the pointer to C<my_biggy> is a bit more complicated, |
|
|
782 | you need to use C<offsetof>: |
|
|
783 | |
|
|
784 | #include <stddef.h> |
|
|
785 | |
|
|
786 | static void |
|
|
787 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
|
|
788 | { |
|
|
789 | struct my_biggy big = (struct my_biggy * |
|
|
790 | (((char *)w) - offsetof (struct my_biggy, t1)); |
|
|
791 | } |
|
|
792 | |
|
|
793 | static void |
|
|
794 | t2_cb (EV_P_ struct ev_timer *w, int revents) |
|
|
795 | { |
|
|
796 | struct my_biggy big = (struct my_biggy * |
|
|
797 | (((char *)w) - offsetof (struct my_biggy, t2)); |
|
|
798 | } |
748 | |
799 | |
749 | |
800 | |
750 | =head1 WATCHER TYPES |
801 | =head1 WATCHER TYPES |
751 | |
802 | |
752 | This section describes each watcher in detail, but will not repeat |
803 | This section describes each watcher in detail, but will not repeat |
… | |
… | |
885 | =item ev_timer_again (loop) |
936 | =item ev_timer_again (loop) |
886 | |
937 | |
887 | This will act as if the timer timed out and restart it again if it is |
938 | This will act as if the timer timed out and restart it again if it is |
888 | repeating. The exact semantics are: |
939 | repeating. The exact semantics are: |
889 | |
940 | |
|
|
941 | If the timer is pending, its pending status is cleared. |
|
|
942 | |
890 | If the timer is started but nonrepeating, stop it. |
943 | If the timer is started but nonrepeating, stop it (as if it timed out). |
891 | |
944 | |
892 | If the timer is repeating, either start it if necessary (with the repeat |
945 | If the timer is repeating, either start it if necessary (with the |
893 | value), or reset the running timer to the repeat value. |
946 | C<repeat> value), or reset the running timer to the C<repeat> value. |
894 | |
947 | |
895 | This sounds a bit complicated, but here is a useful and typical |
948 | This sounds a bit complicated, but here is a useful and typical |
896 | example: Imagine you have a tcp connection and you want a so-called |
949 | example: Imagine you have a tcp connection and you want a so-called idle |
897 | idle timeout, that is, you want to be called when there have been, |
950 | timeout, that is, you want to be called when there have been, say, 60 |
898 | say, 60 seconds of inactivity on the socket. The easiest way to do |
951 | seconds of inactivity on the socket. The easiest way to do this is to |
899 | this is to configure an C<ev_timer> with C<after>=C<repeat>=C<60> and calling |
952 | configure an C<ev_timer> with a C<repeat> value of C<60> and then call |
900 | C<ev_timer_again> each time you successfully read or write some data. If |
953 | C<ev_timer_again> each time you successfully read or write some data. If |
901 | you go into an idle state where you do not expect data to travel on the |
954 | you go into an idle state where you do not expect data to travel on the |
902 | socket, you can stop the timer, and again will automatically restart it if |
955 | socket, you can C<ev_timer_stop> the timer, and C<ev_timer_again> will |
903 | need be. |
956 | automatically restart it if need be. |
904 | |
957 | |
905 | You can also ignore the C<after> value and C<ev_timer_start> altogether |
958 | That means you can ignore the C<after> value and C<ev_timer_start> |
906 | and only ever use the C<repeat> value: |
959 | altogether and only ever use the C<repeat> value and C<ev_timer_again>: |
907 | |
960 | |
908 | ev_timer_init (timer, callback, 0., 5.); |
961 | ev_timer_init (timer, callback, 0., 5.); |
909 | ev_timer_again (loop, timer); |
962 | ev_timer_again (loop, timer); |
910 | ... |
963 | ... |
911 | timer->again = 17.; |
964 | timer->again = 17.; |
912 | ev_timer_again (loop, timer); |
965 | ev_timer_again (loop, timer); |
913 | ... |
966 | ... |
914 | timer->again = 10.; |
967 | timer->again = 10.; |
915 | ev_timer_again (loop, timer); |
968 | ev_timer_again (loop, timer); |
916 | |
969 | |
917 | This is more efficient then stopping/starting the timer eahc time you want |
970 | This is more slightly efficient then stopping/starting the timer each time |
918 | to modify its timeout value. |
971 | you want to modify its timeout value. |
919 | |
972 | |
920 | =item ev_tstamp repeat [read-write] |
973 | =item ev_tstamp repeat [read-write] |
921 | |
974 | |
922 | The current C<repeat> value. Will be used each time the watcher times out |
975 | The current C<repeat> value. Will be used each time the watcher times out |
923 | or C<ev_timer_again> is called and determines the next timeout (if any), |
976 | or C<ev_timer_again> is called and determines the next timeout (if any), |
… | |
… | |
1192 | not exist" is a status change like any other. The condition "path does |
1245 | not exist" is a status change like any other. The condition "path does |
1193 | not exist" is signified by the C<st_nlink> field being zero (which is |
1246 | not exist" is signified by the C<st_nlink> field being zero (which is |
1194 | otherwise always forced to be at least one) and all the other fields of |
1247 | otherwise always forced to be at least one) and all the other fields of |
1195 | the stat buffer having unspecified contents. |
1248 | the stat buffer having unspecified contents. |
1196 | |
1249 | |
|
|
1250 | The path I<should> be absolute and I<must not> end in a slash. If it is |
|
|
1251 | relative and your working directory changes, the behaviour is undefined. |
|
|
1252 | |
1197 | Since there is no standard to do this, the portable implementation simply |
1253 | Since there is no standard to do this, the portable implementation simply |
1198 | calls C<stat (2)> regulalry on the path to see if it changed somehow. You |
1254 | calls C<stat (2)> regularly on the path to see if it changed somehow. You |
1199 | can specify a recommended polling interval for this case. If you specify |
1255 | can specify a recommended polling interval for this case. If you specify |
1200 | a polling interval of C<0> (highly recommended!) then a I<suitable, |
1256 | a polling interval of C<0> (highly recommended!) then a I<suitable, |
1201 | unspecified default> value will be used (which you can expect to be around |
1257 | unspecified default> value will be used (which you can expect to be around |
1202 | five seconds, although this might change dynamically). Libev will also |
1258 | five seconds, although this might change dynamically). Libev will also |
1203 | impose a minimum interval which is currently around C<0.1>, but thats |
1259 | impose a minimum interval which is currently around C<0.1>, but thats |
… | |
… | |
1205 | |
1261 | |
1206 | This watcher type is not meant for massive numbers of stat watchers, |
1262 | This watcher type is not meant for massive numbers of stat watchers, |
1207 | as even with OS-supported change notifications, this can be |
1263 | as even with OS-supported change notifications, this can be |
1208 | resource-intensive. |
1264 | resource-intensive. |
1209 | |
1265 | |
1210 | At the time of this writing, no specific OS backends are implemented, but |
1266 | At the time of this writing, only the Linux inotify interface is |
1211 | if demand increases, at least a kqueue and inotify backend will be added. |
1267 | implemented (implementing kqueue support is left as an exercise for the |
|
|
1268 | reader). Inotify will be used to give hints only and should not change the |
|
|
1269 | semantics of C<ev_stat> watchers, which means that libev sometimes needs |
|
|
1270 | to fall back to regular polling again even with inotify, but changes are |
|
|
1271 | usually detected immediately, and if the file exists there will be no |
|
|
1272 | polling. |
1212 | |
1273 | |
1213 | =over 4 |
1274 | =over 4 |
1214 | |
1275 | |
1215 | =item ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval) |
1276 | =item ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval) |
1216 | |
1277 | |
… | |
… | |
1787 | Similar to the other two macros, this gives you the value of the default |
1848 | Similar to the other two macros, this gives you the value of the default |
1788 | loop, if multiple loops are supported ("ev loop default"). |
1849 | loop, if multiple loops are supported ("ev loop default"). |
1789 | |
1850 | |
1790 | =back |
1851 | =back |
1791 | |
1852 | |
1792 | Example: Declare and initialise a check watcher, working regardless of |
1853 | Example: Declare and initialise a check watcher, utilising the above |
1793 | wether multiple loops are supported or not. |
1854 | macros so it will work regardless of wether multiple loops are supported |
|
|
1855 | or not. |
1794 | |
1856 | |
1795 | static void |
1857 | static void |
1796 | check_cb (EV_P_ ev_timer *w, int revents) |
1858 | check_cb (EV_P_ ev_timer *w, int revents) |
1797 | { |
1859 | { |
1798 | ev_check_stop (EV_A_ w); |
1860 | ev_check_stop (EV_A_ w); |
… | |
… | |
1800 | |
1862 | |
1801 | ev_check check; |
1863 | ev_check check; |
1802 | ev_check_init (&check, check_cb); |
1864 | ev_check_init (&check, check_cb); |
1803 | ev_check_start (EV_DEFAULT_ &check); |
1865 | ev_check_start (EV_DEFAULT_ &check); |
1804 | ev_loop (EV_DEFAULT_ 0); |
1866 | ev_loop (EV_DEFAULT_ 0); |
1805 | |
|
|
1806 | |
1867 | |
1807 | =head1 EMBEDDING |
1868 | =head1 EMBEDDING |
1808 | |
1869 | |
1809 | Libev can (and often is) directly embedded into host |
1870 | Libev can (and often is) directly embedded into host |
1810 | applications. Examples of applications that embed it include the Deliantra |
1871 | applications. Examples of applications that embed it include the Deliantra |
… | |
… | |
1850 | ev_vars.h |
1911 | ev_vars.h |
1851 | ev_wrap.h |
1912 | ev_wrap.h |
1852 | |
1913 | |
1853 | ev_win32.c required on win32 platforms only |
1914 | ev_win32.c required on win32 platforms only |
1854 | |
1915 | |
1855 | ev_select.c only when select backend is enabled (which is by default) |
1916 | ev_select.c only when select backend is enabled (which is enabled by default) |
1856 | ev_poll.c only when poll backend is enabled (disabled by default) |
1917 | ev_poll.c only when poll backend is enabled (disabled by default) |
1857 | ev_epoll.c only when the epoll backend is enabled (disabled by default) |
1918 | ev_epoll.c only when the epoll backend is enabled (disabled by default) |
1858 | ev_kqueue.c only when the kqueue backend is enabled (disabled by default) |
1919 | ev_kqueue.c only when the kqueue backend is enabled (disabled by default) |
1859 | ev_port.c only when the solaris port backend is enabled (disabled by default) |
1920 | ev_port.c only when the solaris port backend is enabled (disabled by default) |
1860 | |
1921 | |
… | |
… | |
1985 | |
2046 | |
1986 | =item EV_USE_DEVPOLL |
2047 | =item EV_USE_DEVPOLL |
1987 | |
2048 | |
1988 | reserved for future expansion, works like the USE symbols above. |
2049 | reserved for future expansion, works like the USE symbols above. |
1989 | |
2050 | |
|
|
2051 | =item EV_USE_INOTIFY |
|
|
2052 | |
|
|
2053 | If defined to be C<1>, libev will compile in support for the Linux inotify |
|
|
2054 | interface to speed up C<ev_stat> watchers. Its actual availability will |
|
|
2055 | be detected at runtime. |
|
|
2056 | |
1990 | =item EV_H |
2057 | =item EV_H |
1991 | |
2058 | |
1992 | The name of the F<ev.h> header file used to include it. The default if |
2059 | The name of the F<ev.h> header file used to include it. The default if |
1993 | undefined is C<< <ev.h> >> in F<event.h> and C<"ev.h"> in F<ev.c>. This |
2060 | undefined is C<< <ev.h> >> in F<event.h> and C<"ev.h"> in F<ev.c>. This |
1994 | can be used to virtually rename the F<ev.h> header file in case of conflicts. |
2061 | can be used to virtually rename the F<ev.h> header file in case of conflicts. |
… | |
… | |
2049 | =item EV_PID_HASHSIZE |
2116 | =item EV_PID_HASHSIZE |
2050 | |
2117 | |
2051 | C<ev_child> watchers use a small hash table to distribute workload by |
2118 | C<ev_child> watchers use a small hash table to distribute workload by |
2052 | pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more |
2119 | pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more |
2053 | than enough. If you need to manage thousands of children you might want to |
2120 | than enough. If you need to manage thousands of children you might want to |
2054 | increase this value. |
2121 | increase this value (I<must> be a power of two). |
|
|
2122 | |
|
|
2123 | =item EV_INOTIFY_HASHSIZE |
|
|
2124 | |
|
|
2125 | C<ev_staz> watchers use a small hash table to distribute workload by |
|
|
2126 | inotify watch id. The default size is C<16> (or C<1> with C<EV_MINIMAL>), |
|
|
2127 | usually more than enough. If you need to manage thousands of C<ev_stat> |
|
|
2128 | watchers you might want to increase this value (I<must> be a power of |
|
|
2129 | two). |
2055 | |
2130 | |
2056 | =item EV_COMMON |
2131 | =item EV_COMMON |
2057 | |
2132 | |
2058 | By default, all watchers have a C<void *data> member. By redefining |
2133 | By default, all watchers have a C<void *data> member. By redefining |
2059 | this macro to a something else you can include more and other types of |
2134 | this macro to a something else you can include more and other types of |
… | |
… | |
2088 | interface) and F<EV.xs> (implementation) files. Only the F<EV.xs> file |
2163 | interface) and F<EV.xs> (implementation) files. Only the F<EV.xs> file |
2089 | will be compiled. It is pretty complex because it provides its own header |
2164 | will be compiled. It is pretty complex because it provides its own header |
2090 | file. |
2165 | file. |
2091 | |
2166 | |
2092 | The usage in rxvt-unicode is simpler. It has a F<ev_cpp.h> header file |
2167 | The usage in rxvt-unicode is simpler. It has a F<ev_cpp.h> header file |
2093 | that everybody includes and which overrides some autoconf choices: |
2168 | that everybody includes and which overrides some configure choices: |
2094 | |
2169 | |
|
|
2170 | #define EV_MINIMAL 1 |
2095 | #define EV_USE_POLL 0 |
2171 | #define EV_USE_POLL 0 |
2096 | #define EV_MULTIPLICITY 0 |
2172 | #define EV_MULTIPLICITY 0 |
2097 | #define EV_PERIODICS 0 |
2173 | #define EV_PERIODIC_ENABLE 0 |
|
|
2174 | #define EV_STAT_ENABLE 0 |
|
|
2175 | #define EV_FORK_ENABLE 0 |
2098 | #define EV_CONFIG_H <config.h> |
2176 | #define EV_CONFIG_H <config.h> |
|
|
2177 | #define EV_MINPRI 0 |
|
|
2178 | #define EV_MAXPRI 0 |
2099 | |
2179 | |
2100 | #include "ev++.h" |
2180 | #include "ev++.h" |
2101 | |
2181 | |
2102 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
2182 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
2103 | |
2183 | |
… | |
… | |
2119 | |
2199 | |
2120 | =item Starting io/check/prepare/idle/signal/child watchers: O(1) |
2200 | =item Starting io/check/prepare/idle/signal/child watchers: O(1) |
2121 | |
2201 | |
2122 | =item Stopping check/prepare/idle watchers: O(1) |
2202 | =item Stopping check/prepare/idle watchers: O(1) |
2123 | |
2203 | |
2124 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % 16)) |
2204 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE)) |
2125 | |
2205 | |
2126 | =item Finding the next timer per loop iteration: O(1) |
2206 | =item Finding the next timer per loop iteration: O(1) |
2127 | |
2207 | |
2128 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
2208 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
2129 | |
2209 | |