--- libev/ev.pod 2010/10/21 09:23:21 1.309 +++ libev/ev.pod 2010/10/21 12:32:47 1.310 @@ -28,8 +28,8 @@ // with its corresponding stop function. ev_io_stop (EV_A_ w); - // this causes all nested ev_loop's to stop iterating - ev_unloop (EV_A_ EVUNLOOP_ALL); + // this causes all nested ev_run's to stop iterating + ev_break (EV_A_ EVBREAK_ALL); } // another callback, this time for a time-out @@ -37,8 +37,8 @@ timeout_cb (EV_P_ ev_timer *w, int revents) { puts ("timeout"); - // this causes the innermost ev_loop to stop iterating - ev_unloop (EV_A_ EVUNLOOP_ONE); + // this causes the innermost ev_run to stop iterating + ev_break (EV_A_ EVBREAK_ONE); } int @@ -58,7 +58,7 @@ ev_timer_start (loop, &timeout_watcher); // now wait for events to arrive - ev_loop (loop, 0); + ev_run (loop, 0); // unloop was called, so exit return 0; @@ -294,13 +294,13 @@ =head1 FUNCTIONS CONTROLLING THE EVENT LOOP -An event loop is described by a C (the C -is I optional in this case, as there is also an C -I). +An event loop is described by a C (the C is +I optional in case unless libev 3 compatibility is disabled, as libev +3 had an C function colliding with the struct name). The library knows two types of such loops, the I loop, which -supports signals and child events, and dynamically created loops which do -not. +supports signals and child events, and dynamically created event loops +which do not. =over 4 @@ -608,12 +608,12 @@ =item ev_default_fork () -This function sets a flag that causes subsequent C iterations +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 (or both child and parent, but that again makes little sense). You I call it in the child before using any of the libev -functions, and it will only take effect at the next C iteration. +functions, and it will only take effect at the next C iteration. Again, you I to call it on I loop that you want to re-use after a fork, I. This is @@ -621,9 +621,11 @@ during fork. On the other hand, you only need to call this function in the child -process if and only if you want to use the event loop in the child. If you -just fork+exec or create a new loop in the child, you don't have to call -it at all. +process if and only if you want to use the event loop in the child. If +you just fork+exec or create a new loop in the child, you don't have to +call it at all (in fact, C is so badly broken that it makes a +difference, but libev will usually detect this case on its own and do a +costly reset of the backend). The function itself is quite fast and it's usually not a problem to call it just in case after a fork. To make this easy, the function will fit in @@ -645,9 +647,9 @@ =item unsigned int ev_iteration (loop) -Returns the current iteration count for the loop, which is identical to -the number of times libev did poll for new events. It starts at C<0> and -happily wraps around with enough iterations. +Returns the current iteration count for the event loop, which is identical +to the number of times libev did poll for new events. It starts at C<0> +and happily wraps around with enough iterations. This value can sometimes be useful as a generation counter of sorts (it "ticks" the number of loop iterations), as it roughly corresponds with @@ -656,16 +658,16 @@ =item unsigned int ev_depth (loop) -Returns the number of times C was entered minus the number of -times C was exited, in other words, the recursion depth. +Returns the number of times C was entered minus the number of +times C was exited, in other words, the recursion depth. -Outside C, this number is zero. In a callback, this number is -C<1>, unless C was invoked recursively (or from another thread), +Outside C, this number is zero. In a callback, this number is +C<1>, unless C was invoked recursively (or from another thread), in which case it is higher. -Leaving C abnormally (setjmp/longjmp, cancelling the thread +Leaving C abnormally (setjmp/longjmp, cancelling the thread etc.), doesn't count as "exit" - consider this as a hint to avoid such -ungentleman behaviour unless it's really convenient. +ungentleman-like behaviour unless it's really convenient. =item unsigned int ev_backend (loop) @@ -684,7 +686,7 @@ Establishes the current time by querying the kernel, updating the time returned by C in the progress. This is a costly operation and -is usually done automatically within C. +is usually done automatically within C. This function is rarely useful, but when some event callback runs for a very long time without entering the event loop, updating libev's idea of @@ -696,8 +698,8 @@ =item ev_resume (loop) -These two functions suspend and resume a loop, for use when the loop is -not used for a while and timeouts should not be processed. +These two functions suspend and resume an event loop, for use when the +loop is not used for a while and timeouts should not be processed. A typical use case would be an interactive program such as a game: When the user presses C<^Z> to suspend the game and resumes it an hour later it @@ -718,28 +720,32 @@ Calling C/C has the side effect of updating the event loop time (see C). -=item ev_loop (loop, int flags) +=item ev_run (loop, int flags) Finally, this is it, the event handler. This function usually is called after you have initialised all your watchers and you want to start -handling events. +handling events. It will ask the operating system for any new events, call +the watcher callbacks, an then repeat the whole process indefinitely: This +is why event loops are called I. -If the flags argument is specified as C<0>, it will not return until -either no event watchers are active anymore or C was called. +If the flags argument is specified as C<0>, it will keep handling events +until either no event watchers are active anymore or C was +called. -Please note that an explicit C is usually better than +Please note that an explicit C is usually better than relying on all watchers to be stopped when deciding when a program has finished (especially in interactive programs), but having a program that automatically loops as long as it has to and no longer by virtue of relying on its watchers stopping correctly, that is truly a thing of beauty. -A flags value of C will look for new events, will handle -those events and any already outstanding ones, but will not block your -process in case there are no events and will return after one iteration of -the loop. +A flags value of C will look for new events, will handle +those events and any already outstanding ones, but will not wait and +block your process in case there are no events and will return after one +iteration of the loop. This is sometimes useful to poll and handle new +events while doing lengthy calculations, to keep the program responsive. -A flags value of C will look for new events (waiting if +A flags value of C will look for new events (waiting if necessary) and will handle those and any already outstanding ones. It will block your process until at least one new event arrives (which could be an event internal to libev itself, so there is no guarantee that a @@ -748,55 +754,64 @@ This is useful if you are waiting for some external event in conjunction with something not expressible using other libev watchers (i.e. "roll your -own C"). However, a pair of C/C watchers is +own C"). However, a pair of C/C watchers is usually a better approach for this kind of thing. -Here are the gory details of what C does: +Here are the gory details of what C does: + - Increment loop depth. + - Reset the ev_break status. - Before the first iteration, call any pending watchers. - * If EVFLAG_FORKCHECK was used, check for a fork. + LOOP: + - If EVFLAG_FORKCHECK was used, check for a fork. - If a fork was detected (by any means), queue and call all fork watchers. - Queue and call all prepare watchers. + - If ev_break was called, goto FINISH. - If we have been forked, detach and recreate the kernel state as to not disturb the other process. - Update the kernel state with all outstanding changes. - Update the "event loop time" (ev_now ()). - Calculate for how long to sleep or block, if at all - (active idle watchers, EVLOOP_NONBLOCK or not having + (active idle watchers, EVRUN_NOWAIT or not having any active watchers at all will result in not sleeping). - Sleep if the I/O and timer collect interval say so. + - Increment loop iteration counter. - Block the process, waiting for any events. - Queue all outstanding I/O (fd) events. - Update the "event loop time" (ev_now ()), and do time jump adjustments. - Queue all expired timers. - Queue all expired periodics. - - Unless any events are pending now, queue all idle watchers. + - Queue all idle watchers with priority higher than that of pending events. - Queue all check watchers. - Call all queued watchers in reverse order (i.e. check watchers first). Signals and child watchers are implemented as I/O watchers, and will be handled here by queueing them when their watcher gets executed. - - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK - were used, or there are no active watchers, return, otherwise - continue with step *. + - If ev_break has been called, or EVRUN_ONCE or EVRUN_NOWAIT + were used, or there are no active watchers, goto FINISH, otherwise + continue with step LOOP. + FINISH: + - Reset the ev_break status iff it was EVBREAK_ONE. + - Decrement the loop depth. + - Return. Example: Queue some jobs and then loop until no events are outstanding anymore. ... queue jobs here, make sure they register event watchers as long ... as they still have work to do (even an idle watcher will do..) - ev_loop (my_loop, 0); + ev_run (my_loop, 0); ... jobs done or somebody called unloop. yeah! -=item ev_unloop (loop, how) +=item ev_break (loop, how) -Can be used to make a call to C return early (but only after it +Can be used to make a call to C return early (but only after it has processed all outstanding events). The C argument must be either -C, which will make the innermost C call return, or -C, which will make all nested C calls return. +C, which will make the innermost C call return, or +C, which will make all nested C calls return. -This "unloop state" will be cleared when entering C again. +This "unloop state" will be cleared when entering C again. -It is safe to call C from outside any C calls. +It is safe to call C from outside any C calls. ##TODO## =item ev_ref (loop) @@ -804,15 +819,15 @@ Ref/unref can be used to add or remove a reference count on the event loop: Every watcher keeps one reference, and as long as the reference -count is nonzero, C will not return on its own. +count is nonzero, C will not return on its own. This is useful when you have a watcher that you never intend to -unregister, but that nevertheless should not keep C from +unregister, but that nevertheless should not keep C from returning. In such a case, call C after starting, and C before stopping it. As an example, libev itself uses this for its internal signal pipe: It -is not visible to the libev user and should not keep C from +is not visible to the libev user and should not keep C from exiting if no event watchers registered by it are active. It is also an excellent way to do this for generic recurring timers or from within third-party libraries. Just remember to I and I in the callback). -Example: Create a signal watcher, but keep it from keeping C +Example: Create a signal watcher, but keep it from keeping C running when nothing else is active. ev_signal exitsig; @@ -894,7 +909,7 @@ =item ev_invoke_pending (loop) This call will simply invoke all pending watchers while resetting their -pending state. Normally, C does this automatically when required, +pending state. Normally, C does this automatically when required, but when overriding the invoke callback this call comes handy. =item int ev_pending_count (loop) @@ -905,7 +920,7 @@ =item ev_set_invoke_pending_cb (loop, void (*invoke_pending_cb)(EV_P)) This overrides the invoke pending functionality of the loop: Instead of -invoking all pending watchers when there are any, C will call +invoking all pending watchers when there are any, C will call this callback instead. This is useful, for example, when you want to invoke the actual watchers inside another context (another thread etc.). @@ -918,10 +933,10 @@ can be done relatively simply by putting mutex_lock/unlock calls around each call to a libev function. -However, C can run an indefinite time, so it is not feasible to -wait for it to return. One way around this is to wake up the loop via -C and C, another way is to set these I -and I callbacks on the loop. +However, C can run an indefinite time, so it is not feasible +to wait for it to return. One way around this is to wake up the event +loop via C and C, another way is to set these +I and I callbacks on the loop. When set, then C will be called just before the thread is suspended waiting for new events, and C is called just @@ -934,10 +949,10 @@ C and C (that's their only purpose after all), no modifications done will affect the event loop, i.e. adding watchers will have no effect on the set of file descriptors being watched, or the time -waited. Use an C watcher to wake up C when you want it +waited. Use an C watcher to wake up C when you want it to take note of any changes you made. -In theory, threads executing C will be async-cancel safe between +In theory, threads executing C will be async-cancel safe between invocations of C and C. See also the locking example in the C section later in this @@ -984,7 +999,7 @@ static void my_cb (struct ev_loop *loop, ev_io *w, int revents) { ev_io_stop (w); - ev_unloop (loop, EVUNLOOP_ALL); + ev_break (loop, EVBREAK_ALL); } struct ev_loop *loop = ev_default_loop (0); @@ -995,7 +1010,7 @@ ev_io_set (&stdin_watcher, STDIN_FILENO, EV_READ); ev_io_start (loop, &stdin_watcher); - ev_loop (loop, 0); + ev_run (loop, 0); As you can see, you are responsible for allocating the memory for your watcher structures (and it is I a bad idea to do this on the @@ -1069,13 +1084,13 @@ =item C -All C watchers are invoked just I C starts +All C watchers are invoked just I C starts to gather new events, and all C watchers are invoked just after -C has gathered them, but before it invokes any callbacks for any +C has gathered them, but before it invokes any callbacks for any received events. Callbacks of both watcher types can start and stop as many watchers as they want, and all of them will be taken into account (for example, a C watcher might start an idle watcher to keep -C from blocking). +C from blocking). =item C @@ -1626,7 +1641,7 @@ ev_io stdin_readable; ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ); ev_io_start (loop, &stdin_readable); - ev_loop (loop, 0); + ev_run (loop, 0); =head2 C - relative and optionally repeating timeouts @@ -1645,7 +1660,7 @@ might introduce a small delay). If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones of the same priority with later time-out values (but this is -no longer true when a callback calls C recursively). +no longer true when a callback calls C recursively). =head3 Be smart about timeouts @@ -1826,7 +1841,7 @@ Establishing the current time is a costly operation (it usually takes at least two system calls): EV therefore updates its idea of the current -time only before and after C collects new events, which causes a +time only before and after C collects new events, which causes a growing difference between C and C when handling lots of events in one iteration. @@ -1953,7 +1968,7 @@ ev_timer mytimer; ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */ ev_timer_again (&mytimer); /* start timer */ - ev_loop (loop, 0); + ev_run (loop, 0); // and in some piece of code that gets executed on any "activity": // reset the timeout to start ticking again at 10 seconds @@ -1989,7 +2004,7 @@ point in time where it is supposed to trigger has passed. If multiple timers become ready during the same loop iteration then the ones with earlier time-out values are invoked before ones with later time-out values -(but this is no longer true when a callback calls C recursively). +(but this is no longer true when a callback calls C recursively). =head3 Watcher-Specific Functions and Data Members @@ -2237,7 +2252,7 @@ static void sigint_cb (struct ev_loop *loop, ev_signal *w, int revents) { - ev_unloop (loop, EVUNLOOP_ALL); + ev_break (loop, EVBREAK_ALL); } ev_signal signal_watcher; @@ -2633,7 +2648,7 @@ prepare watchers get invoked before the process blocks and check watchers afterwards. -You I call C or similar functions that enter +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 @@ -2811,7 +2826,7 @@ // create/start timer // poll - ev_loop (EV_A_ 0); + ev_run (EV_A_ 0); // stop timer again if (timeout >= 0) @@ -2899,7 +2914,7 @@ =item ev_embed_sweep (loop, ev_embed *) Make a single, non-blocking sweep over the embedded loop. This works -similarly to C, but in the most +similarly to C, but in the most appropriate way for embedded loops. =item struct ev_loop *other [read-only] @@ -3015,7 +3030,7 @@ =head2 C - how to wake up an event loop -In general, you cannot use an C from multiple threads or other +In general, you cannot use an C from multiple threads or other asynchronous sources such as signal handlers (as opposed to multiple event loops - those are of course safe to use in different threads). @@ -3532,7 +3547,7 @@ ev_unref (EV_A); ev_timer_add (EV_A_ watcher); - ev_loop (EV_A_ 0); + ev_run (EV_A_ 0); It assumes the variable C of type C is in scope, which is often provided by the following macro. @@ -3582,7 +3597,7 @@ ev_check check; ev_check_init (&check, check_cb); ev_check_start (EV_DEFAULT_ &check); - ev_loop (EV_DEFAULT_ 0); + ev_run (EV_DEFAULT_ 0); =head1 EMBEDDING @@ -3684,6 +3699,22 @@ =over 4 +=item EV_COMPAT3 (h) + +Backwards compatibility is a major concern for libev. This is why this +release of libev comes with wrappers for the functions and symbols that +have been renamed between libev version 3 and 4. + +You can disable these wrappers (to test compatibility with future +versions) by defining C to C<0> when compiling your +sources. This has the additional advantage that you can drop the C +from C declarations, as libev will provide an C +typedef in that case. + +In some future version, the default for C will become C<0>, +and in some even more future version the compatibility code will be +removed completely. + =item EV_STANDALONE (h) Must always be C<1> if you do not use autoconf configuration, which @@ -4267,7 +4298,7 @@ } The event loop thread first acquires the mutex, and then jumps straight -into C: +into C: void * l_run (void *thr_arg) @@ -4276,7 +4307,7 @@ l_acquire (EV_A); pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); - ev_loop (EV_A_ 0); + ev_run (EV_A_ 0); l_release (EV_A); return 0; @@ -4338,13 +4369,13 @@ Libev is very accommodating to coroutines ("cooperative threads"): libev fully supports nesting calls to its functions from different -coroutines (e.g. you can call C on the same loop from two +coroutines (e.g. you can call C on the same loop from two different coroutines, and switch freely between both coroutines running the loop, as long as you don't confuse yourself). The only exception is that you must not do this from C reschedule callbacks. Care has been taken to ensure that libev does not keep local state inside -C, and other calls do not usually allow for coroutine switches as +C, and other calls do not usually allow for coroutine switches as they do not call any callbacks. =head2 COMPILER WARNINGS @@ -4731,25 +4762,38 @@ =over 4 -=item C renamed to C +=item function/symbol renames + +A number of functions and symbols have been renamed: + + ev_loop => ev_run + EVLOOP_NONBLOCK => EVRUN_NOWAIT + EVLOOP_ONESHOT => EVRUN_ONCE + + ev_unloop => ev_break + EVUNLOOP_CANCEL => EVBREAK_CANCEL + EVUNLOOP_ONE => EVBREAK_ONE + EVUNLOOP_ALL => EVBREAK_ALL -=item C renamed to C + EV_TIMEOUT => EV_TIMER -=item C renamed to C + ev_loop_count => ev_iteration + ev_loop_depth => ev_depth + ev_loop_verify => ev_verify Most functions working on C objects don't have an -C prefix, so it was removed. Note that C is -still called C because it would otherwise clash with the -C typedef. - -=item C renamed to C in C - -This is a simple rename - all other watcher types use their name -as revents flag, and now C does, too. - -Both C and C symbols were present in 3.x versions -and continue to be present for the foreseeable future, so this is mostly a -documentation change. +C prefix, so it was removed; C, C and +associated constants have been renamed to not collide with the C anymore and C now follows the same naming scheme +as all other watcher types. Note that C is still called +C because it would otherwise clash with the C +typedef. + +=item C backwards compatibility mechanism + +The backward compatibility mechanism can be controlled by +C. See L in the L +section. =item C mechanism replaced by C