ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.310 by root, Thu Oct 21 12:32:47 2010 UTC vs.
Revision 1.316 by root, Fri Oct 22 09:34:01 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines