--- libev/ev.pod 2012/04/02 23:14:41 1.399 +++ libev/ev.pod 2012/05/06 13:42:10 1.416 @@ -84,9 +84,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 @@ -249,7 +249,7 @@ See the description of C watchers for more info. -=item ev_set_allocator (void *(*cb)(void *ptr, long size)) +=item ev_set_allocator (void *(*cb)(void *ptr, long size) throw ()) Sets the allocation function to use (the prototype is similar - the semantics are identical to the C C89/SuS/POSIX function). It is @@ -285,7 +285,7 @@ ... ev_set_allocator (persistent_realloc); -=item ev_set_syserr_cb (void (*cb)(const char *msg)) +=item ev_set_syserr_cb (void (*cb)(const char *msg) throw ()) Set the callback function to call on a retryable system call error (such as failed select, poll, epoll_wait). The message is a printable string @@ -766,7 +766,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) @@ -1018,7 +1018,7 @@ If you want to reset the callback, use C as new callback. -=item ev_set_loop_release_cb (loop, void (*release)(EV_P), void (*acquire)(EV_P)) +=item ev_set_loop_release_cb (loop, void (*release)(EV_P) throw (), void (*acquire)(EV_P) throw ()) Sometimes you want to share the same loop between multiple threads. This can be done relatively simply by putting mutex_lock/unlock calls around @@ -1176,13 +1176,18 @@ =item C -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 -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). +All C watchers are invoked just I C starts to +gather new events, and all C watchers are queued (not invoked) +just after C has gathered them, but before it queues any callbacks +for any received events. That means C watchers are the last +watchers invoked before the event loop sleeps or polls for new events, and +C watchers will be invoked before any other watchers of the same +or lower priority within an event loop iteration. + +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). =item C @@ -1315,7 +1320,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). @@ -1343,7 +1348,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) @@ -1378,7 +1383,7 @@ =back -See also the L and L and L idioms. =head2 WATCHER STATES @@ -1876,7 +1881,7 @@ // calculate when the timeout would happen ev_tstamp after = last_activity - ev_now (EV_A) + timeout; - // if negative, it means we the timeout already occured + // if negative, it means we the timeout already occurred if (after < 0.) { // timeout occurred, take action @@ -1904,7 +1909,7 @@ and simply start the timer with this timeout value. In other words, each time the callback is invoked it will check whether -the timeout cocured. If not, it will simply reschedule itself to check +the timeout occurred. If not, it will simply reschedule itself to check again at the earliest time it could time out. Rinse. Repeat. This scheme causes more callback invocations (about one every 60 seconds @@ -1928,7 +1933,7 @@ When your timeout value changes, then the timeout can be changed by simply providing a new value, stopping the timer and calling the callback, which -will agaion do the right thing (for example, time out immediately :). +will again do the right thing (for example, time out immediately :). timeout = new_value; ev_timer_stop (EV_A_ &timer); @@ -2133,7 +2138,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 *) @@ -2844,6 +2849,20 @@ "pseudo-background processing", or delay processing stuff to after the event loop has handled all outstanding events. +=head3 Abusing an C watcher for its side-effect + +As long as there is at least one active idle watcher, libev will never +sleep unnecessarily. Or in other words, it will loop as fast as possible. +For this to work, the idle watcher doesn't need to be invoked at all - the +lowest priority will do. + +This mode of operation can be useful together with an C watcher, +to do something on each event loop iteration - for example to balance load +between different connections. + +See L for a longer +example. + =head3 Watcher-Specific Functions and Data Members =over 4 @@ -2864,7 +2883,12 @@ static void idle_cb (struct ev_loop *loop, ev_idle *w, int revents) { + // stop the watcher + ev_idle_stop (loop, w); + + // now we can free it free (w); + // now do something you wanted to do when the program has // no longer anything immediate to do. } @@ -2876,7 +2900,7 @@ =head2 C and C - customise your event loop! -Prepare and check watchers are usually (but not always) used in pairs: +Prepare and check watchers are often (but not always) used in pairs: prepare watchers get invoked before the process blocks and check watchers afterwards. @@ -2914,9 +2938,10 @@ loop from blocking if lower-priority coroutines are active, thus mapping low-priority coroutines to idle/background tasks). -It is recommended to give C watchers highest (C) -priority, to ensure that they are being run before any other watchers -after the poll (this doesn't matter for C watchers). +When used for this purpose, it is recommended to give C watchers +highest (C) priority, to ensure that they are being run before +any other watchers after the poll (this doesn't matter for C +watchers). Also, C watchers (and C watchers, too) should not activate ("feed") events into libev. While libev fully supports this, they @@ -2926,6 +2951,26 @@ C watcher ran (always remind yourself to coexist peacefully with others). +=head3 Abusing an C watcher for its side-effect + +C (and less often also C) watchers can also be +useful because they are called once per event loop iteration. For +example, if you want to handle a large number of connections fairly, you +normally only do a bit of work for each active connection, and if there +is more work to do, you wait for the next event loop iteration, so other +connections have a chance of making progress. + +Using an C watcher is almost enough: it will be called on the +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 +will not sleep, and the C watcher makes sure a callback gets +invoked. Neither watcher alone can do that. + =head3 Watcher-Specific Functions and Data Members =over 4 @@ -3315,7 +3360,7 @@ This functionality is very similar to C watchers, as signals, too, are asynchronous in nature, and signals, too, will be compressed (i.e. the number of callback invocations may be less than the number of -C calls). In fact, you could use signal watchers as a kind +C calls). In fact, you could use signal watchers as a kind of "global async watchers" by using a watcher on an otherwise unused signal, and C to signal this watcher from another thread, even without knowing which loop owns the signal. @@ -3832,7 +3877,7 @@ void wait_for_event (ev_watcher *w) { - ev_cb_set (w) = current_coro; + ev_set_cb (w, current_coro); switch_to (libev_coro); } @@ -3845,7 +3890,7 @@ 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 @@ -3899,6 +3944,39 @@ =head1 C++ SUPPORT +=head2 C API + +The normal C API should work fine when used from C++: both ev.h and the +libev sources can be compiled as C++. Therefore, code that uses the C API +will work fine. + +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 +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: + + static void + fatal_error (const char *msg) EV_THROW + { + perror (msg); + abort (); + } + + ... + ev_set_syserr_cb (fatal_error); + +The only API functions that can currently throw exceptions are C, +C, C and C (the latter +because it runs cleanup watchers). + +Throwing exceptions in watcher callbacks is only supported if libev itself +is compiled with a C++ compiler or your C and C++ environments allow +throwing exceptions through C libraries (most do). + +=head2 C++ API + Libev comes with some simplistic wrapper classes for C++ that mainly allow you to use some convenience methods to start/stop watchers and also change the callback model to a model using method callbacks on objects. @@ -4162,6 +4240,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 @@ -4622,7 +4708,7 @@ #define EV_ASYNC_ENABLE 1 The actual value is a bitset, it can be a combination of the following -values: +values (by default, all of these are enabled): =over 4 @@ -4637,6 +4723,9 @@ gcc is recommended, as well as C<-DNDEBUG>, as libev contains a number of assertions. +The default is off when C<__OPTIMIZE_SIZE__> is defined by your compiler +(e.g. gcc with C<-Os>). + =item C<2> - faster/larger data structures Replaces the small 2-heap for timer management by a faster 4-heap, larger @@ -4644,6 +4733,9 @@ and can additionally have an effect on the size of data structures at runtime. +The default is off when C<__OPTIMIZE_SIZE__> is defined by your compiler +(e.g. gcc with C<-Os>). + =item C<4> - full API configuration This enables priorities (sets C=2 and C=-2), and @@ -4911,7 +5003,7 @@ =back -See also L. +See also L. =head3 COROUTINES @@ -5327,7 +5419,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 @@ -5380,7 +5472,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 @@ -5426,7 +5518,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