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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines