… | |
… | |
293 | =back |
293 | =back |
294 | |
294 | |
295 | =head1 FUNCTIONS CONTROLLING THE EVENT LOOP |
295 | =head1 FUNCTIONS CONTROLLING THE EVENT LOOP |
296 | |
296 | |
297 | An event loop is described by a C<struct ev_loop *> (the C<struct> is |
297 | An event loop is described by a C<struct ev_loop *> (the C<struct> is |
298 | I<not> optional in case unless libev 3 compatibility is disabled, as libev |
298 | I<not> optional in this case unless libev 3 compatibility is disabled, as |
299 | 3 had an C<ev_loop> function colliding with the struct name). |
299 | libev 3 had an C<ev_loop> function colliding with the struct name). |
300 | |
300 | |
301 | The library knows two types of such loops, the I<default> loop, which |
301 | The library knows two types of such loops, the I<default> loop, which |
302 | supports signals and child events, and dynamically created event loops |
302 | supports signals and child events, and dynamically created event loops |
303 | which do not. |
303 | which do not. |
304 | |
304 | |
… | |
… | |
908 | |
908 | |
909 | =item ev_invoke_pending (loop) |
909 | =item ev_invoke_pending (loop) |
910 | |
910 | |
911 | This call will simply invoke all pending watchers while resetting their |
911 | This call will simply invoke all pending watchers while resetting their |
912 | pending state. Normally, C<ev_run> does this automatically when required, |
912 | pending state. Normally, C<ev_run> does this automatically when required, |
913 | but when overriding the invoke callback this call comes handy. |
913 | but when overriding the invoke callback this call comes handy. This |
|
|
914 | function can be invoked from a watcher - this can be useful for example |
|
|
915 | when you want to do some lengthy calculation and want to pass further |
|
|
916 | event handling to another thread (you still have to make sure only one |
|
|
917 | thread executes within C<ev_invoke_pending> or C<ev_run> of course). |
914 | |
918 | |
915 | =item int ev_pending_count (loop) |
919 | =item int ev_pending_count (loop) |
916 | |
920 | |
917 | Returns the number of pending watchers - zero indicates that no watchers |
921 | Returns the number of pending watchers - zero indicates that no watchers |
918 | are pending. |
922 | are pending. |
… | |
… | |
990 | |
994 | |
991 | In the following description, uppercase C<TYPE> in names stands for the |
995 | In the following description, uppercase C<TYPE> in names stands for the |
992 | watcher type, e.g. C<ev_TYPE_start> can mean C<ev_timer_start> for timer |
996 | watcher type, e.g. C<ev_TYPE_start> can mean C<ev_timer_start> for timer |
993 | watchers and C<ev_io_start> for I/O watchers. |
997 | watchers and C<ev_io_start> for I/O watchers. |
994 | |
998 | |
995 | A watcher is a structure that you create and register to record your |
999 | A watcher is an opaque structure that you allocate and register to record |
996 | interest in some event. For instance, if you want to wait for STDIN to |
1000 | your interest in some event. To make a concrete example, imagine you want |
997 | become readable, you would create an C<ev_io> watcher for that: |
1001 | to wait for STDIN to become readable, you would create an C<ev_io> watcher |
|
|
1002 | for that: |
998 | |
1003 | |
999 | static void my_cb (struct ev_loop *loop, ev_io *w, int revents) |
1004 | static void my_cb (struct ev_loop *loop, ev_io *w, int revents) |
1000 | { |
1005 | { |
1001 | ev_io_stop (w); |
1006 | ev_io_stop (w); |
1002 | ev_break (loop, EVBREAK_ALL); |
1007 | ev_break (loop, EVBREAK_ALL); |
… | |
… | |
1017 | stack). |
1022 | stack). |
1018 | |
1023 | |
1019 | Each watcher has an associated watcher structure (called C<struct ev_TYPE> |
1024 | Each watcher has an associated watcher structure (called C<struct ev_TYPE> |
1020 | or simply C<ev_TYPE>, as typedefs are provided for all watcher structs). |
1025 | or simply C<ev_TYPE>, as typedefs are provided for all watcher structs). |
1021 | |
1026 | |
1022 | Each watcher structure must be initialised by a call to C<ev_init |
1027 | Each watcher structure must be initialised by a call to C<ev_init (watcher |
1023 | (watcher *, callback)>, which expects a callback to be provided. This |
1028 | *, callback)>, which expects a callback to be provided. This callback is |
1024 | callback gets invoked each time the event occurs (or, in the case of I/O |
1029 | invoked each time the event occurs (or, in the case of I/O watchers, each |
1025 | watchers, each time the event loop detects that the file descriptor given |
1030 | time the event loop detects that the file descriptor given is readable |
1026 | is readable and/or writable). |
1031 | and/or writable). |
1027 | |
1032 | |
1028 | Each watcher type further has its own C<< ev_TYPE_set (watcher *, ...) >> |
1033 | Each watcher type further has its own C<< ev_TYPE_set (watcher *, ...) >> |
1029 | macro to configure it, with arguments specific to the watcher type. There |
1034 | macro to configure it, with arguments specific to the watcher type. There |
1030 | is also a macro to combine initialisation and setting in one call: C<< |
1035 | is also a macro to combine initialisation and setting in one call: C<< |
1031 | ev_TYPE_init (watcher *, callback, ...) >>. |
1036 | ev_TYPE_init (watcher *, callback, ...) >>. |
… | |
… | |
1126 | example it might indicate that a fd is readable or writable, and if your |
1131 | example it might indicate that a fd is readable or writable, and if your |
1127 | callbacks is well-written it can just attempt the operation and cope with |
1132 | callbacks is well-written it can just attempt the operation and cope with |
1128 | the error from read() or write(). This will not work in multi-threaded |
1133 | the error from read() or write(). This will not work in multi-threaded |
1129 | programs, though, as the fd could already be closed and reused for another |
1134 | programs, though, as the fd could already be closed and reused for another |
1130 | thing, so beware. |
1135 | thing, so beware. |
|
|
1136 | |
|
|
1137 | =back |
|
|
1138 | |
|
|
1139 | =head2 WATCHER STATES |
|
|
1140 | |
|
|
1141 | There are various watcher states mentioned throughout this manual - |
|
|
1142 | active, pending and so on. In this section these states and the rules to |
|
|
1143 | transition between them will be described in more detail - and while these |
|
|
1144 | rules might look complicated, they usually do "the right thing". |
|
|
1145 | |
|
|
1146 | =over 4 |
|
|
1147 | |
|
|
1148 | =item initialiased |
|
|
1149 | |
|
|
1150 | Before a watcher can be registered with the event looop it has to be |
|
|
1151 | initialised. This can be done with a call to C<ev_TYPE_init>, or calls to |
|
|
1152 | C<ev_init> followed by the watcher-specific C<ev_TYPE_set> function. |
|
|
1153 | |
|
|
1154 | In this state it is simply some block of memory that is suitable for use |
|
|
1155 | in an event loop. It can be moved around, freed, reused etc. at will. |
|
|
1156 | |
|
|
1157 | =item started/running/active |
|
|
1158 | |
|
|
1159 | Once a watcher has been started with a call to C<ev_TYPE_start> it becomes |
|
|
1160 | property of the event loop, and is actively waiting for events. While in |
|
|
1161 | this state it cannot be accessed (except in a few documented ways), moved, |
|
|
1162 | freed or anything else - the only legal thing is to keep a pointer to it, |
|
|
1163 | and call libev functions on it that are documented to work on active watchers. |
|
|
1164 | |
|
|
1165 | =item pending |
|
|
1166 | |
|
|
1167 | If a watcher is active and libev determines that an event it is interested |
|
|
1168 | in has occurred (such as a timer expiring), it will become pending. It will |
|
|
1169 | stay in this pending state until either it is stopped or its callback is |
|
|
1170 | about to be invoked, so it is not normally pending inside the watcher |
|
|
1171 | callback. |
|
|
1172 | |
|
|
1173 | The watcher might or might not be active while it is pending (for example, |
|
|
1174 | an expired non-repeating timer can be pending but no longer active). If it |
|
|
1175 | is stopped, it can be freely accessed (e.g. by calling C<ev_TYPE_set>), |
|
|
1176 | but it is still property of the event loop at this time, so cannot be |
|
|
1177 | moved, freed or reused. And if it is active the rules described in the |
|
|
1178 | previous item still apply. |
|
|
1179 | |
|
|
1180 | It is also possible to feed an event on a watcher that is not active (e.g. |
|
|
1181 | via C<ev_feed_event>), in which case it becomes pending without being |
|
|
1182 | active. |
|
|
1183 | |
|
|
1184 | =item stopped |
|
|
1185 | |
|
|
1186 | A watcher can be stopped implicitly by libev (in which case it might still |
|
|
1187 | be pending), or explicitly by calling its C<ev_TYPE_stop> function. The |
|
|
1188 | latter will clear any pending state the watcher might be in, regardless |
|
|
1189 | of whether it was active or not, so stopping a watcher explicitly before |
|
|
1190 | freeing it is often a good idea. |
|
|
1191 | |
|
|
1192 | While stopped (and not pending) the watcher is essentially in the |
|
|
1193 | initialised state, that is it can be reused, moved, modified in any way |
|
|
1194 | you wish. |
1131 | |
1195 | |
1132 | =back |
1196 | =back |
1133 | |
1197 | |
1134 | =head2 GENERIC WATCHER FUNCTIONS |
1198 | =head2 GENERIC WATCHER FUNCTIONS |
1135 | |
1199 | |
… | |
… | |
4467 | =head3 C<kqueue> is buggy |
4531 | =head3 C<kqueue> is buggy |
4468 | |
4532 | |
4469 | The kqueue syscall is broken in all known versions - most versions support |
4533 | The kqueue syscall is broken in all known versions - most versions support |
4470 | only sockets, many support pipes. |
4534 | only sockets, many support pipes. |
4471 | |
4535 | |
4472 | Libev tries to work around this by not using C<kqueue> by default on |
4536 | Libev tries to work around this by not using C<kqueue> by default on this |
4473 | this rotten platform, but of course you can still ask for it when creating |
4537 | rotten platform, but of course you can still ask for it when creating a |
4474 | a loop. |
4538 | loop - embedding a socket-only kqueue loop into a select-based one is |
|
|
4539 | probably going to work well. |
4475 | |
4540 | |
4476 | =head3 C<poll> is buggy |
4541 | =head3 C<poll> is buggy |
4477 | |
4542 | |
4478 | Instead of fixing C<kqueue>, Apple replaced their (working) C<poll> |
4543 | Instead of fixing C<kqueue>, Apple replaced their (working) C<poll> |
4479 | implementation by something calling C<kqueue> internally around the 10.5.6 |
4544 | implementation by something calling C<kqueue> internally around the 10.5.6 |
… | |
… | |
4498 | |
4563 | |
4499 | =head3 C<errno> reentrancy |
4564 | =head3 C<errno> reentrancy |
4500 | |
4565 | |
4501 | The default compile environment on Solaris is unfortunately so |
4566 | The default compile environment on Solaris is unfortunately so |
4502 | thread-unsafe that you can't even use components/libraries compiled |
4567 | thread-unsafe that you can't even use components/libraries compiled |
4503 | without C<-D_REENTRANT> (as long as they use C<errno>), which, of course, |
4568 | without C<-D_REENTRANT> in a threaded program, which, of course, isn't |
4504 | isn't defined by default. |
4569 | defined by default. A valid, if stupid, implementation choice. |
4505 | |
4570 | |
4506 | If you want to use libev in threaded environments you have to make sure |
4571 | If you want to use libev in threaded environments you have to make sure |
4507 | it's compiled with C<_REENTRANT> defined. |
4572 | it's compiled with C<_REENTRANT> defined. |
4508 | |
4573 | |
4509 | =head3 Event port backend |
4574 | =head3 Event port backend |
4510 | |
4575 | |
4511 | The scalable event interface for Solaris is called "event ports". Unfortunately, |
4576 | The scalable event interface for Solaris is called "event |
4512 | this mechanism is very buggy. If you run into high CPU usage, your program |
4577 | ports". Unfortunately, this mechanism is very buggy in all major |
|
|
4578 | releases. If you run into high CPU usage, your program freezes or you get |
4513 | freezes or you get a large number of spurious wakeups, make sure you have |
4579 | a large number of spurious wakeups, make sure you have all the relevant |
4514 | all the relevant and latest kernel patches applied. No, I don't know which |
4580 | and latest kernel patches applied. No, I don't know which ones, but there |
4515 | ones, but there are multiple ones. |
4581 | are multiple ones to apply, and afterwards, event ports actually work |
|
|
4582 | great. |
4516 | |
4583 | |
4517 | If you can't get it to work, you can try running the program by setting |
4584 | If you can't get it to work, you can try running the program by setting |
4518 | the environment variable C<LIBEV_FLAGS=3> to only allow C<poll> and |
4585 | the environment variable C<LIBEV_FLAGS=3> to only allow C<poll> and |
4519 | C<select> backends. |
4586 | C<select> backends. |
4520 | |
4587 | |
4521 | =head2 AIX POLL BUG |
4588 | =head2 AIX POLL BUG |
4522 | |
4589 | |
4523 | AIX unfortunately has a broken C<poll.h> header. Libev works around |
4590 | AIX unfortunately has a broken C<poll.h> header. Libev works around |
4524 | this by trying to avoid the poll backend altogether (i.e. it's not even |
4591 | this by trying to avoid the poll backend altogether (i.e. it's not even |
4525 | compiled in), which normally isn't a big problem as C<select> works fine |
4592 | compiled in), which normally isn't a big problem as C<select> works fine |
4526 | with large bitsets, and AIX is dead anyway. |
4593 | with large bitsets on AIX, and AIX is dead anyway. |
4527 | |
4594 | |
4528 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
4595 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
4529 | |
4596 | |
4530 | =head3 General issues |
4597 | =head3 General issues |
4531 | |
4598 | |
… | |
… | |
4808 | |
4875 | |
4809 | =over 4 |
4876 | =over 4 |
4810 | |
4877 | |
4811 | =item active |
4878 | =item active |
4812 | |
4879 | |
4813 | A watcher is active as long as it has been started (has been attached to |
4880 | A watcher is active as long as it has been started and not yet stopped. |
4814 | an event loop) but not yet stopped (disassociated from the event loop). |
4881 | See L<WATCHER STATES> for details. |
4815 | |
4882 | |
4816 | =item application |
4883 | =item application |
4817 | |
4884 | |
4818 | In this document, an application is whatever is using libev. |
4885 | In this document, an application is whatever is using libev. |
|
|
4886 | |
|
|
4887 | =item backend |
|
|
4888 | |
|
|
4889 | The part of the code dealing with the operating system interfaces. |
4819 | |
4890 | |
4820 | =item callback |
4891 | =item callback |
4821 | |
4892 | |
4822 | The address of a function that is called when some event has been |
4893 | The address of a function that is called when some event has been |
4823 | detected. Callbacks are being passed the event loop, the watcher that |
4894 | detected. Callbacks are being passed the event loop, the watcher that |
4824 | received the event, and the actual event bitset. |
4895 | received the event, and the actual event bitset. |
4825 | |
4896 | |
4826 | =item callback invocation |
4897 | =item callback/watcher invocation |
4827 | |
4898 | |
4828 | The act of calling the callback associated with a watcher. |
4899 | The act of calling the callback associated with a watcher. |
4829 | |
4900 | |
4830 | =item event |
4901 | =item event |
4831 | |
4902 | |
… | |
… | |
4850 | The model used to describe how an event loop handles and processes |
4921 | The model used to describe how an event loop handles and processes |
4851 | watchers and events. |
4922 | watchers and events. |
4852 | |
4923 | |
4853 | =item pending |
4924 | =item pending |
4854 | |
4925 | |
4855 | A watcher is pending as soon as the corresponding event has been detected, |
4926 | A watcher is pending as soon as the corresponding event has been |
4856 | and stops being pending as soon as the watcher will be invoked or its |
4927 | detected. See L<WATCHER STATES> for details. |
4857 | pending status is explicitly cleared by the application. |
|
|
4858 | |
|
|
4859 | A watcher can be pending, but not active. Stopping a watcher also clears |
|
|
4860 | its pending status. |
|
|
4861 | |
4928 | |
4862 | =item real time |
4929 | =item real time |
4863 | |
4930 | |
4864 | The physical time that is observed. It is apparently strictly monotonic :) |
4931 | The physical time that is observed. It is apparently strictly monotonic :) |
4865 | |
4932 | |
… | |
… | |
4872 | =item watcher |
4939 | =item watcher |
4873 | |
4940 | |
4874 | A data structure that describes interest in certain events. Watchers need |
4941 | A data structure that describes interest in certain events. Watchers need |
4875 | to be started (attached to an event loop) before they can receive events. |
4942 | to be started (attached to an event loop) before they can receive events. |
4876 | |
4943 | |
4877 | =item watcher invocation |
|
|
4878 | |
|
|
4879 | The act of calling the callback associated with a watcher. |
|
|
4880 | |
|
|
4881 | =back |
4944 | =back |
4882 | |
4945 | |
4883 | =head1 AUTHOR |
4946 | =head1 AUTHOR |
4884 | |
4947 | |
4885 | Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson. |
4948 | Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson. |