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.318 by root, Fri Oct 22 09:40:22 2010 UTC

124this argument. 124this argument.
125 125
126=head2 TIME REPRESENTATION 126=head2 TIME REPRESENTATION
127 127
128Libev represents time as a single floating point number, representing 128Libev represents time as a single floating point number, representing
129the (fractional) number of seconds since the (POSIX) epoch (in practise 129the (fractional) number of seconds since the (POSIX) epoch (in practice
130somewhere near the beginning of 1970, details are complicated, don't 130somewhere near the beginning of 1970, details are complicated, don't
131ask). This type is called C<ev_tstamp>, which is what you should use 131ask). This type is called C<ev_tstamp>, which is what you should use
132too. It usually aliases to the C<double> type in C. When you need to do 132too. It usually aliases to the C<double> type in C. When you need to do
133any calculations on it, you should treat it as some floating point value. 133any calculations on it, you should treat it as some floating point value.
134 134
165 165
166=item ev_tstamp ev_time () 166=item ev_tstamp ev_time ()
167 167
168Returns the current time as libev would use it. Please note that the 168Returns the current time as libev would use it. Please note that the
169C<ev_now> function is usually faster and also often returns the timestamp 169C<ev_now> function is usually faster and also often returns the timestamp
170you actually want to know. 170you actually want to know. Also interetsing is the combination of
171C<ev_update_now> and C<ev_now>.
171 172
172=item ev_sleep (ev_tstamp interval) 173=item ev_sleep (ev_tstamp interval)
173 174
174Sleep for the given interval: The current thread will be blocked until 175Sleep for the given interval: The current thread will be blocked until
175either it is interrupted or the given time interval has passed. Basically 176either it is interrupted or the given time interval has passed. Basically
213 assert (("sorry, no epoll, no sex", 214 assert (("sorry, no epoll, no sex",
214 ev_supported_backends () & EVBACKEND_EPOLL)); 215 ev_supported_backends () & EVBACKEND_EPOLL));
215 216
216=item unsigned int ev_recommended_backends () 217=item unsigned int ev_recommended_backends ()
217 218
218Return the set of all backends compiled into this binary of libev and also 219Return the set of all backends compiled into this binary of libev and
219recommended for this platform. This set is often smaller than the one 220also recommended for this platform, meaning it will work for most file
221descriptor types. This set is often smaller than the one returned by
220returned by C<ev_supported_backends>, as for example kqueue is broken on 222C<ev_supported_backends>, as for example kqueue is broken on most BSDs
221most BSDs and will not be auto-detected unless you explicitly request it 223and will not be auto-detected unless you explicitly request it (assuming
222(assuming you know what you are doing). This is the set of backends that 224you know what you are doing). This is the set of backends that libev will
223libev will probe for if you specify no backends explicitly. 225probe for if you specify no backends explicitly.
224 226
225=item unsigned int ev_embeddable_backends () 227=item unsigned int ev_embeddable_backends ()
226 228
227Returns the set of backends that are embeddable in other event loops. This 229Returns the set of backends that are embeddable in other event loops. This
228is the theoretical, all-platform, value. To find which backends 230is the theoretical, all-platform, value. To find which backends
293=back 295=back
294 296
295=head1 FUNCTIONS CONTROLLING THE EVENT LOOP 297=head1 FUNCTIONS CONTROLLING THE EVENT LOOP
296 298
297An event loop is described by a C<struct ev_loop *> (the C<struct> is 299An 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 300I<not> optional in this case unless libev 3 compatibility is disabled, as
2993 had an C<ev_loop> function colliding with the struct name). 301libev 3 had an C<ev_loop> function colliding with the struct name).
300 302
301The library knows two types of such loops, the I<default> loop, which 303The library knows two types of such loops, the I<default> loop, which
302supports signals and child events, and dynamically created event loops 304supports signals and child events, and dynamically created event loops
303which do not. 305which do not.
304 306
908 910
909=item ev_invoke_pending (loop) 911=item ev_invoke_pending (loop)
910 912
911This call will simply invoke all pending watchers while resetting their 913This call will simply invoke all pending watchers while resetting their
912pending state. Normally, C<ev_run> does this automatically when required, 914pending state. Normally, C<ev_run> does this automatically when required,
913but when overriding the invoke callback this call comes handy. 915but when overriding the invoke callback this call comes handy. This
916function can be invoked from a watcher - this can be useful for example
917when you want to do some lengthy calculation and want to pass further
918event handling to another thread (you still have to make sure only one
919thread executes within C<ev_invoke_pending> or C<ev_run> of course).
914 920
915=item int ev_pending_count (loop) 921=item int ev_pending_count (loop)
916 922
917Returns the number of pending watchers - zero indicates that no watchers 923Returns the number of pending watchers - zero indicates that no watchers
918are pending. 924are pending.
990 996
991In the following description, uppercase C<TYPE> in names stands for the 997In 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 998watcher 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. 999watchers and C<ev_io_start> for I/O watchers.
994 1000
995A watcher is a structure that you create and register to record your 1001A 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 1002your interest in some event. To make a concrete example, imagine you want
997become readable, you would create an C<ev_io> watcher for that: 1003to wait for STDIN to become readable, you would create an C<ev_io> watcher
1004for that:
998 1005
999 static void my_cb (struct ev_loop *loop, ev_io *w, int revents) 1006 static void my_cb (struct ev_loop *loop, ev_io *w, int revents)
1000 { 1007 {
1001 ev_io_stop (w); 1008 ev_io_stop (w);
1002 ev_break (loop, EVBREAK_ALL); 1009 ev_break (loop, EVBREAK_ALL);
1017stack). 1024stack).
1018 1025
1019Each watcher has an associated watcher structure (called C<struct ev_TYPE> 1026Each watcher has an associated watcher structure (called C<struct ev_TYPE>
1020or simply C<ev_TYPE>, as typedefs are provided for all watcher structs). 1027or simply C<ev_TYPE>, as typedefs are provided for all watcher structs).
1021 1028
1022Each watcher structure must be initialised by a call to C<ev_init 1029Each watcher structure must be initialised by a call to C<ev_init (watcher
1023(watcher *, callback)>, which expects a callback to be provided. This 1030*, 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 1031invoked 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 1032time the event loop detects that the file descriptor given is readable
1026is readable and/or writable). 1033and/or writable).
1027 1034
1028Each watcher type further has its own C<< ev_TYPE_set (watcher *, ...) >> 1035Each watcher type further has its own C<< ev_TYPE_set (watcher *, ...) >>
1029macro to configure it, with arguments specific to the watcher type. There 1036macro to configure it, with arguments specific to the watcher type. There
1030is also a macro to combine initialisation and setting in one call: C<< 1037is also a macro to combine initialisation and setting in one call: C<<
1031ev_TYPE_init (watcher *, callback, ...) >>. 1038ev_TYPE_init (watcher *, callback, ...) >>.
1126example it might indicate that a fd is readable or writable, and if your 1133example 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 1134callbacks 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 1135the 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 1136programs, though, as the fd could already be closed and reused for another
1130thing, so beware. 1137thing, so beware.
1138
1139=back
1140
1141=head2 WATCHER STATES
1142
1143There are various watcher states mentioned throughout this manual -
1144active, pending and so on. In this section these states and the rules to
1145transition between them will be described in more detail - and while these
1146rules might look complicated, they usually do "the right thing".
1147
1148=over 4
1149
1150=item initialiased
1151
1152Before a watcher can be registered with the event looop it has to be
1153initialised. This can be done with a call to C<ev_TYPE_init>, or calls to
1154C<ev_init> followed by the watcher-specific C<ev_TYPE_set> function.
1155
1156In this state it is simply some block of memory that is suitable for use
1157in an event loop. It can be moved around, freed, reused etc. at will.
1158
1159=item started/running/active
1160
1161Once a watcher has been started with a call to C<ev_TYPE_start> it becomes
1162property of the event loop, and is actively waiting for events. While in
1163this state it cannot be accessed (except in a few documented ways), moved,
1164freed or anything else - the only legal thing is to keep a pointer to it,
1165and call libev functions on it that are documented to work on active watchers.
1166
1167=item pending
1168
1169If a watcher is active and libev determines that an event it is interested
1170in has occurred (such as a timer expiring), it will become pending. It will
1171stay in this pending state until either it is stopped or its callback is
1172about to be invoked, so it is not normally pending inside the watcher
1173callback.
1174
1175The watcher might or might not be active while it is pending (for example,
1176an expired non-repeating timer can be pending but no longer active). If it
1177is stopped, it can be freely accessed (e.g. by calling C<ev_TYPE_set>),
1178but it is still property of the event loop at this time, so cannot be
1179moved, freed or reused. And if it is active the rules described in the
1180previous item still apply.
1181
1182It is also possible to feed an event on a watcher that is not active (e.g.
1183via C<ev_feed_event>), in which case it becomes pending without being
1184active.
1185
1186=item stopped
1187
1188A watcher can be stopped implicitly by libev (in which case it might still
1189be pending), or explicitly by calling its C<ev_TYPE_stop> function. The
1190latter will clear any pending state the watcher might be in, regardless
1191of whether it was active or not, so stopping a watcher explicitly before
1192freeing it is often a good idea.
1193
1194While stopped (and not pending) the watcher is essentially in the
1195initialised state, that is it can be reused, moved, modified in any way
1196you wish.
1131 1197
1132=back 1198=back
1133 1199
1134=head2 GENERIC WATCHER FUNCTIONS 1200=head2 GENERIC WATCHER FUNCTIONS
1135 1201
4467=head3 C<kqueue> is buggy 4533=head3 C<kqueue> is buggy
4468 4534
4469The kqueue syscall is broken in all known versions - most versions support 4535The kqueue syscall is broken in all known versions - most versions support
4470only sockets, many support pipes. 4536only sockets, many support pipes.
4471 4537
4472Libev tries to work around this by not using C<kqueue> by default on 4538Libev 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 4539rotten platform, but of course you can still ask for it when creating a
4474a loop. 4540loop - embedding a socket-only kqueue loop into a select-based one is
4541probably going to work well.
4475 4542
4476=head3 C<poll> is buggy 4543=head3 C<poll> is buggy
4477 4544
4478Instead of fixing C<kqueue>, Apple replaced their (working) C<poll> 4545Instead of fixing C<kqueue>, Apple replaced their (working) C<poll>
4479implementation by something calling C<kqueue> internally around the 10.5.6 4546implementation by something calling C<kqueue> internally around the 10.5.6
4498 4565
4499=head3 C<errno> reentrancy 4566=head3 C<errno> reentrancy
4500 4567
4501The default compile environment on Solaris is unfortunately so 4568The default compile environment on Solaris is unfortunately so
4502thread-unsafe that you can't even use components/libraries compiled 4569thread-unsafe that you can't even use components/libraries compiled
4503without C<-D_REENTRANT> (as long as they use C<errno>), which, of course, 4570without C<-D_REENTRANT> in a threaded program, which, of course, isn't
4504isn't defined by default. 4571defined by default. A valid, if stupid, implementation choice.
4505 4572
4506If you want to use libev in threaded environments you have to make sure 4573If you want to use libev in threaded environments you have to make sure
4507it's compiled with C<_REENTRANT> defined. 4574it's compiled with C<_REENTRANT> defined.
4508 4575
4509=head3 Event port backend 4576=head3 Event port backend
4510 4577
4511The scalable event interface for Solaris is called "event ports". Unfortunately, 4578The scalable event interface for Solaris is called "event
4512this mechanism is very buggy. If you run into high CPU usage, your program 4579ports". Unfortunately, this mechanism is very buggy in all major
4580releases. 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 4581a 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 4582and latest kernel patches applied. No, I don't know which ones, but there
4515ones, but there are multiple ones. 4583are multiple ones to apply, and afterwards, event ports actually work
4584great.
4516 4585
4517If you can't get it to work, you can try running the program by setting 4586If 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 4587the environment variable C<LIBEV_FLAGS=3> to only allow C<poll> and
4519C<select> backends. 4588C<select> backends.
4520 4589
4521=head2 AIX POLL BUG 4590=head2 AIX POLL BUG
4522 4591
4523AIX unfortunately has a broken C<poll.h> header. Libev works around 4592AIX 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 4593this 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 4594compiled in), which normally isn't a big problem as C<select> works fine
4526with large bitsets, and AIX is dead anyway. 4595with large bitsets on AIX, and AIX is dead anyway.
4527 4596
4528=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS 4597=head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS
4529 4598
4530=head3 General issues 4599=head3 General issues
4531 4600
4808 4877
4809=over 4 4878=over 4
4810 4879
4811=item active 4880=item active
4812 4881
4813A watcher is active as long as it has been started (has been attached to 4882A 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). 4883See L<WATCHER STATES> for details.
4815 4884
4816=item application 4885=item application
4817 4886
4818In this document, an application is whatever is using libev. 4887In this document, an application is whatever is using libev.
4888
4889=item backend
4890
4891The part of the code dealing with the operating system interfaces.
4819 4892
4820=item callback 4893=item callback
4821 4894
4822The address of a function that is called when some event has been 4895The address of a function that is called when some event has been
4823detected. Callbacks are being passed the event loop, the watcher that 4896detected. Callbacks are being passed the event loop, the watcher that
4824received the event, and the actual event bitset. 4897received the event, and the actual event bitset.
4825 4898
4826=item callback invocation 4899=item callback/watcher invocation
4827 4900
4828The act of calling the callback associated with a watcher. 4901The act of calling the callback associated with a watcher.
4829 4902
4830=item event 4903=item event
4831 4904
4850The model used to describe how an event loop handles and processes 4923The model used to describe how an event loop handles and processes
4851watchers and events. 4924watchers and events.
4852 4925
4853=item pending 4926=item pending
4854 4927
4855A watcher is pending as soon as the corresponding event has been detected, 4928A 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 4929detected. 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 4930
4862=item real time 4931=item real time
4863 4932
4864The physical time that is observed. It is apparently strictly monotonic :) 4933The physical time that is observed. It is apparently strictly monotonic :)
4865 4934
4872=item watcher 4941=item watcher
4873 4942
4874A data structure that describes interest in certain events. Watchers need 4943A data structure that describes interest in certain events. Watchers need
4875to be started (attached to an event loop) before they can receive events. 4944to be started (attached to an event loop) before they can receive events.
4876 4945
4877=item watcher invocation
4878
4879The act of calling the callback associated with a watcher.
4880
4881=back 4946=back
4882 4947
4883=head1 AUTHOR 4948=head1 AUTHOR
4884 4949
4885Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson. 4950Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines