--- libev/ev.pod 2012/05/03 16:02:52 1.407 +++ libev/ev.pod 2017/01/31 09:31:43 1.440 @@ -1,3 +1,5 @@ +=encoding utf-8 + =head1 NAME libev - a high performance full-featured event loop written in C @@ -84,9 +86,9 @@ This manual tries to be very detailed, but unfortunately, this also makes it very long. If you just want to know the basics of libev, I suggest -reading L, then the L above and -look up the missing functions in L and the C and -C sections in L. +reading L, then the L above and +look up the missing functions in L and the C and +C sections in L. =head1 ABOUT LIBEV @@ -398,8 +400,10 @@ or setgid) then libev will I look at the environment variable C. Otherwise (the default), this environment variable will override the flags completely if it is found in the environment. This is -useful to try out specific backends to test their performance, or to work -around bugs. +useful to try out specific backends to test their performance, to work +around bugs, or to make libev threadsafe (accessing environment variables +cannot be done in a threadsafe way, but usually it works if no other +thread modifies them). =item C @@ -414,8 +418,8 @@ C which is even faster). The big advantage of this flag is that you can forget about fork (and -forget about forgetting to tell libev about forking) when you use this -flag. +forget about forgetting to tell libev about forking, although you still +have to ignore C) when you use this flag. This flag setting cannot be overridden or specified in the C environment variable. @@ -571,7 +575,7 @@ cause an extra system call as with C, it still adds up to two event changes per incident. Support for C is very bad (you might have to leak fd's on fork, but it's more sane than epoll) and it -drops fds silently in similarly hard-to-detect cases +drops fds silently in similarly hard-to-detect cases. This backend usually performs well under most conditions. @@ -680,13 +684,17 @@ =item ev_loop_fork (loop) -This function sets a flag that causes subsequent C iterations to -reinitialise the kernel state for backends that have one. Despite the -name, you can call it anytime, but it makes most sense after forking, in -the child process. You I call it (or use C) in the -child before resuming or calling C. +This function sets a flag that causes subsequent C iterations +to reinitialise the kernel state for backends that have one. Despite +the name, you can call it anytime you are allowed to start or stop +watchers (except inside an C callback), but it makes most +sense after forking, in the child process. You I call it (or use +C) in the child before resuming or calling C. + +In addition, if you want to reuse a loop (via this function or +C), you I have to ignore C. -Again, you I to call it on I loop that you want to re-use after +Again, you I to call it on I loop that you want to re-use after a fork, I. This is because some kernel interfaces *cough* I *cough* do funny things during fork. @@ -766,7 +774,7 @@ very long time without entering the event loop, updating libev's idea of the current time is a good idea. -See also L in the C section. +See also L in the C section. =item ev_suspend (loop) @@ -1320,7 +1328,7 @@ Returns the callback currently set on the watcher. -=item ev_cb_set (ev_TYPE *watcher, callback) +=item ev_set_cb (ev_TYPE *watcher, callback) Change the callback. You can change the callback at virtually any time (modulo threads). @@ -1348,7 +1356,7 @@ The default priority used by watchers when no priority has been set is always C<0>, which is supposed to not be too high and not be too low :). -See L, below, for a more thorough treatment of +See L, below, for a more thorough treatment of priorities. =item ev_invoke (loop, ev_TYPE *watcher, int revents) @@ -1383,7 +1391,7 @@ =back -See also the L and L and L idioms. =head2 WATCHER STATES @@ -1395,7 +1403,7 @@ =over 4 -=item initialiased +=item initialised Before a watcher can be registered with the event loop it has to be initialised. This can be done with a call to C, or calls to @@ -2026,13 +2034,15 @@ time. This is usually the right thing as this timestamp refers to the time of the event triggering whatever timeout you are modifying/starting. If you suspect event processing to be delayed and you I to base the -timeout on the current time, use something like this to adjust for this: +timeout on the current time, use something like the following to adjust +for it: - ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); + ev_timer_set (&timer, after + (ev_time () - ev_now ()), 0.); If the event loop is suspended for a long time, you can also force an update of the time returned by C by calling C. +()>, although that will push the event time of all outstanding events +further into the future. =head3 The special problem of unsynchronised clocks @@ -2138,7 +2148,7 @@ =back -This sounds a bit complicated, see L, above, for a +This sounds a bit complicated, see L, above, for a usage example. =item ev_tstamp ev_timer_remaining (loop, ev_timer *) @@ -2201,7 +2211,7 @@ Unlike C, periodic watchers are not based on real time (or relative time, the physical time that passes) but on wall clock time -(absolute time, the thing you can read on your calender or clock). The +(absolute time, the thing you can read on your calendar or clock). The difference is that wall clock time can run faster or slower than real time, and time jumps are not uncommon (e.g. when you adjust your wrist-watch). @@ -2391,7 +2401,7 @@ ev_periodic_init (&hourly_tick, clock_cb, fmod (ev_now (loop), 3600.), 3600., 0); ev_periodic_start (loop, &hourly_tick); - + =head2 C - signal me when a signal gets signalled! @@ -2411,9 +2421,9 @@ C in both the default loop and another loop at the same time. At the moment, C is permanently tied to the default loop. -When the first watcher gets started will libev actually register something -with the kernel (thus it coexists with your own signal handlers as long as -you don't register any with libev for the same signal). +Only after the first watcher for a signal is started will libev actually +register something with the kernel. It thus coexists with your own signal +handlers as long as you don't register any with libev for the same signal. If possible and supported, libev will install its handlers with C (or equivalent) behaviour enabled, so system calls should @@ -2608,8 +2618,9 @@ This watches a file system path for attribute changes. That is, it calls C on that path in regular intervals (or when the OS says it changed) -and sees if it changed compared to the last time, invoking the callback if -it did. +and sees if it changed compared to the last time, invoking the callback +if it did. Starting the watcher C's the file, so only changes that +happen after the watcher has been started will be reported. The path does not need to exist: changing from "path exists" to "path does not exist" is a status change like any other. The condition "path does not @@ -2860,7 +2871,7 @@ to do something on each event loop iteration - for example to balance load between different connections. -See L watcher for its side-effect> for a longer +See L for a longer example. =head3 Watcher-Specific Functions and Data Members @@ -2904,13 +2915,13 @@ prepare watchers get invoked before the process blocks and check watchers afterwards. -You I call C or similar functions that enter -the current event loop from either C or C -watchers. Other loops than the current one are fine, however. The -rationale behind this is that you do not need to check for recursion in -those watchers, i.e. the sequence will always be C, blocking, -C so if you have one watcher of each kind they will always be -called in pairs bracketing the blocking call. +You I call C (or similar functions that enter the +current event loop) or C from either C or +C watchers. Other loops than the current one are fine, +however. The rationale behind this is that you do not need to check +for recursion in those watchers, i.e. the sequence will always be +C, blocking, C so if you have one watcher of each +kind they will always be called in pairs bracketing the blocking call. Their main purpose is to integrate other event mechanisms into libev and their use is somewhat advanced. They could be used, for example, to track @@ -2964,7 +2975,6 @@ next event loop iteration. However, that isn't as soon as possible - without external events, your C watcher will not be invoked. - This is where C watchers come in handy - all you need is a single global idle watcher that is active as long as you have one active C watcher. The C watcher makes sure the event loop @@ -3180,7 +3190,7 @@ =item ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop) -=item ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop) +=item ev_embed_set (ev_embed *, struct ev_loop *embedded_loop) Configures the watcher to embed the given loop, which must be embeddable. If the callback is C<0>, then C will be @@ -3211,7 +3221,7 @@ struct ev_loop *loop_hi = ev_default_init (0); struct ev_loop *loop_lo = 0; ev_embed embed; - + // see if there is a chance of getting one that works // (remember that a flags value of 0 means autodetection) loop_lo = ev_embeddable_backends () & ev_recommended_backends () @@ -3235,7 +3245,7 @@ struct ev_loop *loop = ev_default_init (0); struct ev_loop *loop_socket = 0; ev_embed embed; - + if (ev_supported_backends () & ~ev_recommended_backends () & EVBACKEND_KQUEUE) if ((loop_socket = ev_loop_new (EVBACKEND_KQUEUE)) { @@ -3253,15 +3263,15 @@ Fork watchers are called when a C was detected (usually because whoever is a good citizen cared to tell libev about it by calling -C or C). The invocation is done before the -event loop blocks next and before C watchers are being called, -and only in the child after the fork. If whoever good citizen calling -C cheats and calls it in the wrong process, the fork -handlers will be invoked, too, of course. +C). The invocation is done before the event loop blocks next +and before C watchers are being called, and only in the child +after the fork. If whoever good citizen calling C cheats +and calls it in the wrong process, the fork handlers will be invoked, too, +of course. =head3 The special problem of life after fork - how is it possible? -Most uses of C consist of forking, then some simple calls to set +Most uses of C consist of forking, then some simple calls to set up/change the process environment, followed by a call to C. This sequence should be handled by libev without any problems. @@ -3661,9 +3671,9 @@ A common way around all these issues is to make sure that C I returns before the callback is invoked. If C immediately knows the result, it can artificially -delay invoking the callback by e.g. using a C or C watcher -for example, or more sneakily, by reusing an existing (stopped) watcher -and pushing it into the pending queue: +delay invoking the callback by using a C or C watcher for +example, or more sneakily, by reusing an existing (stopped) watcher and +pushing it into the pending queue: ev_set_cb (watcher, callback); ev_feed_event (EV_A_ watcher, 0); @@ -3681,7 +3691,7 @@ main C call, but not the nested one (e.g. user clicked "Quit", but a modal "Are you sure?" dialog is still waiting), or just the nested one and not the main one (e.g. user clocked "Ok" in a modal dialog), or some -other combination: In these cases, C will not work alone. +other combination: In these cases, a simple C will not work. The solution is to maintain "break this loop" variable for each C invocation, and use a loop around C until the condition is @@ -3877,7 +3887,7 @@ void wait_for_event (ev_watcher *w) { - ev_cb_set (w) = current_coro; + ev_set_cb (w, current_coro); switch_to (libev_coro); } @@ -3890,12 +3900,12 @@ switching to a coroutine, you push the watcher onto the queue and notify any waiters. -To embed libev, see L, but in short, it's easiest to create two +To embed libev, see L, but in short, it's easiest to create two files, F and F that include the respective libev files: // my_ev.h #define EV_CB_DECLARE(type) struct my_coro *cb; - #define EV_CB_INVOKE(watcher) switch_to ((watcher)->cb); + #define EV_CB_INVOKE(watcher) switch_to ((watcher)->cb) #include "../libev/ev.h" // my_ev.c @@ -3952,7 +3962,7 @@ Proper exception specifications might have to be added to callbacks passed to libev: exceptions may be thrown only from watcher callbacks, all -other callbacks (allocator, syserr, loop acquire/release and periodioc +other callbacks (allocator, syserr, loop acquire/release and periodic reschedule callbacks) must not throw exceptions, and might need a C specification. If you have code that needs to be compiled as both C and C++ you can use the C macro for this: @@ -3982,7 +3992,7 @@ the callback model to a model using method callbacks on objects. To use it, - + #include This automatically includes F and puts all of its definitions (many @@ -4095,7 +4105,7 @@ ... } } - + myfunctor f; ev::io w; @@ -4123,10 +4133,14 @@ =item w->set ([arguments]) -Basically the same as C, with the same arguments. Either this -method or a suitable start method must be called at least once. Unlike the -C counterpart, an active watcher gets automatically stopped and restarted -when reconfiguring it with this method. +Basically the same as C (except for C watchers>), +with the same arguments. Either this method or a suitable start method +must be called at least once. Unlike the C counterpart, an active watcher +gets automatically stopped and restarted when reconfiguring it with this +method. + +For C watchers this method is called C, to avoid +clashing with the C method. =item w->start () @@ -4240,6 +4254,14 @@ time of this writing, only C and C), to be found at L. +=item Javascript + +Node.js (L) uses libev as the underlying event library. + +=item Others + +There are others, and I stopped counting. + =back @@ -4366,11 +4388,11 @@ ev_win32.c required on win32 platforms only - ev_select.c only when select backend is enabled (which is enabled by default) - ev_poll.c only when poll backend is enabled (disabled by default) - ev_epoll.c only when the epoll backend is enabled (disabled by default) - ev_kqueue.c only when the kqueue backend is enabled (disabled by default) - ev_port.c only when the solaris port backend is enabled (disabled by default) + ev_select.c only when select backend is enabled + ev_poll.c only when poll backend is enabled + ev_epoll.c only when the epoll backend is enabled + ev_kqueue.c only when the kqueue backend is enabled + ev_port.c only when the solaris port backend is enabled F includes the backend files directly when enabled, so you only need to compile this single file. @@ -4548,6 +4570,13 @@ file descriptors again. Note that the replacement function has to close the underlying OS handle. +=item EV_USE_WSASOCKET + +If defined to be C<1>, libev will use C to create its internal +communication socket, which works better in some environments. Otherwise, +the normal C function will be used, which works better in other +environments. + =item EV_USE_POLL If defined to be C<1>, libev will compile in support for the C(2) @@ -4601,23 +4630,22 @@ =item EV_NO_THREADS -If defined to be C<1>, libev will assume that it will never be called -from different threads, which is a stronger assumption than C, -above. This reduces dependencies and makes libev faster. +If defined to be C<1>, libev will assume that it will never be called from +different threads (that includes signal handlers), which is a stronger +assumption than C, above. This reduces dependencies and makes +libev faster. =item EV_ATOMIC_T Libev requires an integer type (suitable for storing C<0> or C<1>) whose -access is atomic and serialised with respect to other threads or signal -contexts. No such type is easily found in the C language, so you can -provide your own type that you know is safe for your purposes. It is used -both for signal handler "locking" as well as for signal and thread safety -in C watchers. +access is atomic with respect to other threads or signal contexts. No +such type is easily found in the C language, so you can provide your own +type that you know is safe for your purposes. It is used both for signal +handler "locking" as well as for signal and thread safety in C +watchers. In the absence of this define, libev will use C -(from F), which is usually good enough on most platforms, -although strictly speaking using a type that also implies a memory fence -is required. +(from F), which is usually good enough on most platforms. =item EV_H (h) @@ -4995,7 +5023,7 @@ =back -See also L. +See also L. =head3 COROUTINES @@ -5274,6 +5302,11 @@ callback: The watcher callbacks have different type signatures, but libev calls them using an C internally. +=item null pointers and integer zero are represented by 0 bytes + +Libev uses C to initialise structs and arrays to C<0> bytes, and +relies on this setting pointers and integers to null. + =item pointer accesses must be thread-atomic Accessing a pointer value must be atomic, it must both be readable and @@ -5296,8 +5329,8 @@ C could complicate things, however. The most portable way to handle signals is to block signals in all threads -except the initial one, and run the default loop in the initial thread as -well. +except the initial one, and run the signal handling loop in the initial +thread as well. =item C must be large enough for common memory allocation sizes @@ -5411,7 +5444,7 @@ =item C backwards compatibility mechanism The backward compatibility mechanism can be controlled by -C. See L in the L +C. See L in the L section. =item C and C have been removed @@ -5464,7 +5497,7 @@ =item active A watcher is active as long as it has been started and not yet stopped. -See L for details. +See L for details. =item application @@ -5510,7 +5543,7 @@ =item pending A watcher is pending as soon as the corresponding event has been -detected. See L for details. +detected. See L for details. =item real time