--- libev/ev.pod 2010/10/22 10:50:24 1.321 +++ libev/ev.pod 2010/10/24 17:58:41 1.322 @@ -45,7 +45,7 @@ main (void) { // use the default event loop unless you have special needs - struct ev_loop *loop = ev_default_loop (0); + struct ev_loop *loop = EV_DEFAULT; // initialise an io watcher, then start it // this one will watch for stdin to become readable @@ -295,7 +295,7 @@ =back -=head1 FUNCTIONS CONTROLLING THE EVENT LOOP +=head1 FUNCTIONS CONTROLLING EVENT LOOPS An event loop is described by a C (the C is I optional in this case unless libev 3 compatibility is disabled, as @@ -309,24 +309,56 @@ =item struct ev_loop *ev_default_loop (unsigned int flags) -This will initialise the default event loop if it hasn't been initialised -yet and return it. If the default loop could not be initialised, returns -false. If it already was initialised it simply returns it (and ignores the -flags. If that is troubling you, check C afterwards). +This returns the "default" event loop object, which is what you should +normally use when you just need "the event loop". Event loop objects and +the C parameter are described in more detail in the entry for +C. + +If the default loop is already initialised then this function simply +returns it (and ignores the flags. If that is troubling you, check +C afterwards). Otherwise it will create it with the given +flags, which should almost always be C<0>, unless the caller is also the +one calling C or otherwise qualifies as "the main program". If you don't know what event loop to use, use the one returned from this -function. +function (or via the C macro). Note that this function is I thread-safe, so if you want to use it -from multiple threads, you have to lock (note also that this is unlikely, -as loops cannot be shared easily between threads anyway). +from multiple threads, you have to employ some kind of mutex (note also +that this case is unlikely, as loops cannot be shared easily between +threads anyway). + +The default loop is the only loop that can handle C watchers, +and to do this, it always registers a handler for C. If this is +a problem for your application you can either create a dynamic loop with +C which doesn't do that, or you can simply overwrite the +C signal handler I calling C. -The default loop is the only loop that can handle C and -C watchers, and to do this, it always registers a handler -for C. If this is a problem for your application you can either -create a dynamic loop with C that doesn't do that, or you -can simply overwrite the C signal handler I calling -C. +Example: This is the most typical usage. + + if (!ev_default_loop (0)) + fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); + +Example: Restrict libev to the select and poll backends, and do not allow +environment settings to be taken into account: + + ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); + +Example: Use whatever libev has to offer, but make sure that kqueue is +used if available (warning, breaks stuff, best use only with your own +private event loop and only if you know the OS supports your types of +fds): + + ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); + +=item struct ev_loop *ev_loop_new (unsigned int flags) + +This will create and initialise a new event loop object. If the loop +could not be initialised, returns false. + +Note that this function I thread-safe, and one common way to use +libev with threads is indeed to create one loop per thread, and using the +default loop in the "main" or "initial" thread. The flags argument can be used to specify special behaviour or specific backends to use, and is usually specified as C<0> (or C). @@ -554,69 +586,42 @@ here). If none are specified, all backends in C will be tried. -Example: This is the most typical usage. - - if (!ev_default_loop (0)) - fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); - -Example: Restrict libev to the select and poll backends, and do not allow -environment settings to be taken into account: - - ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); - -Example: Use whatever libev has to offer, but make sure that kqueue is -used if available (warning, breaks stuff, best use only with your own -private event loop and only if you know the OS supports your types of -fds): - - ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); - -=item struct ev_loop *ev_loop_new (unsigned int flags) - -Similar to C, but always creates a new event loop that is -always distinct from the default loop. - -Note that this function I thread-safe, and one common way to use -libev with threads is indeed to create one loop per thread, and using the -default loop in the "main" or "initial" thread. - Example: Try to create a event loop that uses epoll and nothing else. struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); if (!epoller) fatal ("no epoll found here, maybe it hides under your chair"); -=item ev_default_destroy () +=item ev_loop_destroy (loop) -Destroys the default loop (frees all memory and kernel state etc.). None -of the active event watchers will be stopped in the normal sense, so -e.g. C might still return true. It is your responsibility to -either stop all watchers cleanly yourself I calling this function, -or cope with the fact afterwards (which is usually the easiest thing, you -can just ignore the watchers and/or C them for example). +Destroys an event loop object (frees all memory and kernel state +etc.). None of the active event watchers will be stopped in the normal +sense, so e.g. C might still return true. It is your +responsibility to either stop all watchers cleanly yourself I +calling this function, or cope with the fact afterwards (which is usually +the easiest thing, you can just ignore the watchers and/or C them +for example). Note that certain global state, such as signal state (and installed signal handlers), will not be freed by this function, and related watchers (such as signal and child watchers) would need to be stopped manually. -In general it is not advisable to call this function except in the -rare occasion where you really need to free e.g. the signal handling -pipe fds. If you need dynamically allocated loops it is better to use -C and C. - -=item ev_loop_destroy (loop) - -Like C, but destroys an event loop created by an -earlier call to C. +This function is normally used on loop objects allocated by +C, but it can also be used on the default loop returned by +C, in which case it is not thread-safe. + +Note that it is not advisable to call this function on the default loop +except in the rare occasion where you really need to free it's resources. +If you need dynamically allocated loops it is better to use C +and C. -=item ev_default_fork () +=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 +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. +the child process. You I call it (or use C) in the +child before resuming or calling C. Again, you I to call it on I loop that you want to re-use after a fork, I. This is @@ -631,17 +636,19 @@ 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 -quite nicely into a call to C: +it just in case after a fork. - pthread_atfork (0, 0, ev_default_fork); +Example: Automate calling C on the default loop when +using pthreads. -=item ev_loop_fork (loop) + static void + post_fork_child (void) + { + ev_loop_fork (EV_DEFAULT); + } -Like C, but acts on an event loop created by -C. Yes, you have to call this on every allocated event loop -after fork that you want to re-use in the child, and how you keep track of -them is entirely your own problem. + ... + pthread_atfork (0, 0, post_fork_child); =item int ev_is_default_loop (loop) @@ -3077,10 +3084,11 @@ When this is not possible, or you want to use the default loop for other reasons, then in the process that wants to start "fresh", call -C followed by C. Destroying -the default loop will "orphan" (not stop) all registered watchers, so you -have to be careful not to execute code that modifies those watchers. Note -also that in that case, you have to re-register any signal watchers. +C followed by C. +Destroying the default loop will "orphan" (not stop) all registered +watchers, so you have to be careful not to execute code that modifies +those watchers. Note also that in that case, you have to re-register any +signal watchers. =head3 Watcher-Specific Functions and Data Members @@ -4832,6 +4840,13 @@ =over 4 +=item C and C have been removed + +These calls can be replaced easily by their C counterparts: + + ev_loop_destroy (EV_DEFAULT); + ev_loop_fork (EV_DEFAULT); + =item function/symbol renames A number of functions and symbols have been renamed: