--- AnyEvent/lib/AnyEvent.pm 2009/07/08 02:01:12 1.229 +++ AnyEvent/lib/AnyEvent.pm 2009/07/18 22:27:10 1.248 @@ -363,15 +363,33 @@ but it is guaranteed not to interrupt any other callbacks. The main advantage of using these watchers is that you can share a signal -between multiple watchers. +between multiple watchers, and AnyEvent will ensure that signals will not +interrupt your program at bad times. -This watcher might use C<%SIG>, so programs overwriting those signals -directly will likely not work correctly. +This watcher might use C<%SIG> (depending on the event loop used), +so programs overwriting those signals directly will likely not work +correctly. Example: exit on SIGINT my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); +=head3 Signal Races, Delays and Workarounds + +Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching +callbacks to signals in a generic way, which is a pity, as you cannot do +race-free signal handling in perl. AnyEvent will try to do it's best, but +in some cases, signals will be delayed. The maximum time a signal might +be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 +seconds). This variable can be changed only before the first signal +watcher is created, and should be left alone otherwise. Higher values +will cause fewer spurious wake-ups, which is better for power and CPU +saving. All these problems can be avoided by installing the optional +L module. This will not work with inherently broken +event loops such as L or L (and not with L +currently, as POE does it's own workaround with one-second latency). With +those, you just have to suffer the delays. + =head2 CHILD PROCESS WATCHERS You can also watch on a child process exit and catch its exit status. @@ -406,6 +424,10 @@ watcher before you C the child (alternatively, you can call C). +As most event loops do not support waiting for child events, they will be +emulated by AnyEvent in most cases, in which the latency and race problems +mentioned in the description of signal watchers apply. + Example: fork a process and wait for it my $done = AnyEvent->condvar; @@ -465,15 +487,16 @@ require you to run some blocking "loop", "run" or similar function that will actively watch for new events and call your callbacks. -AnyEvent is different, it expects somebody else to run the event loop and -will only block when necessary (usually when told by the user). +AnyEvent is slightly different: it expects somebody else to run the event +loop and will only block when necessary (usually when told by the user). The instrument to do that is called a "condition variable", so called because they represent a condition that must become true. +Now is probably a good time to look at the examples further below. + Condition variables can be created by calling the C<< AnyEvent->condvar >> method, usually without arguments. The only argument pair allowed is - C, which specifies a callback to be called when the condition variable becomes true, with the condition variable as the first argument (but not the results). @@ -532,11 +555,11 @@ ); # this "blocks" (while handling events) till the callback - # calls send + # calls -recv; -Example: wait for a timer, but take advantage of the fact that -condition variables are also code references. +Example: wait for a timer, but take advantage of the fact that condition +variables are also callable directly. my $done = AnyEvent->condvar; my $delay = AnyEvent->timer (after => 5, cb => $done); @@ -552,7 +575,7 @@ my @info = $couchdb->info->recv; -And this is how you would just ste a callback to be called whenever the +And this is how you would just set a callback to be called whenever the results are available: $couchdb->info->cb (sub { @@ -580,14 +603,9 @@ Any arguments passed to the C call will be returned by all future C<< ->recv >> calls. -Condition variables are overloaded so one can call them directly -(as a code reference). Calling them directly is the same as calling -C. Note, however, that many C-based event loops do not handle -overloading, so as tempting as it may be, passing a condition variable -instead of a callback does not work. Both the pure perl and EV loops -support overloading, however, as well as all functions that use perl to -invoke a callback (as in L and L for -example). +Condition variables are overloaded so one can call them directly (as if +they were a code reference). Calling them directly is the same as calling +C. =item $cv->croak ($error) @@ -595,7 +613,11 @@ C with the given error message/object/scalar. This can be used to signal any errors to the condition variable -user/consumer. +user/consumer. Doing it this way instead of calling C directly +delays the error detetcion, but has the overwhelmign advantage that it +diagnoses the error at the place where the result is expected, and not +deep in some event clalback without connection to the actual code causing +the problem. =item $cv->begin ([group callback]) @@ -701,25 +723,21 @@ In list context, all parameters passed to C will be returned, in scalar context only the first one will be returned. +Note that doing a blocking wait in a callback is not supported by any +event loop, that is, recursive invocation of a blocking C<< ->recv +>> is not allowed, and the C call will C if such a +condition is detected. This condition can be slightly loosened by using +L, which allows you to do a blocking C<< ->recv >> from +any thread that doesn't run the event loop itself. + Not all event models support a blocking wait - some die in that case (programs might want to do that to stay interactive), so I, but let the +using this from a module, never require a blocking wait>. Instead, let the caller decide whether the call will block or not (for example, by coupling condition variables with some kind of request results and supporting callbacks so the caller knows that getting the result will not block, while still supporting blocking waits if the caller so desires). -Another reason I to C<< ->recv >> in a module is that you cannot -sensibly have two C<< ->recv >>'s in parallel, as that would require -multiple interpreters or coroutines/threads, none of which C -can supply. - -The L module, however, I and I supply coroutines and, in -fact, L replaces AnyEvent's condvars by coroutine-safe -versions and also integrates coroutines into AnyEvent, making blocking -C<< ->recv >> calls perfectly safe as long as they are done from another -coroutine (one that doesn't run the event loop). - You can ensure that C<< -recv >> never blocks by setting a callback and only calling C<< ->recv >> from within that callback (or at a later time). This will work even when the event loop does not support blocking @@ -742,55 +760,117 @@ =back -=head1 GLOBAL VARIABLES AND FUNCTIONS +=head1 SUPPORTED EVENT LOOPS/BACKENDS -=over 4 +The available backend classes are (every class has its own manpage): -=item $AnyEvent::MODEL +=over 4 -Contains C until the first watcher is being created. Then it -contains the event model that is being used, which is the name of the -Perl class implementing the model. This class is usually one of the -C modules, but can be any other class in the case -AnyEvent has been extended at runtime (e.g. in I). +=item Backends that are autoprobed when no other event loop can be found. -The known classes so far are: +EV is the preferred backend when no other event loop seems to be in +use. If EV is not installed, then AnyEvent will try Event, and, failing +that, will fall back to its own pure-perl implementation, which is +available everywhere as it comes with AnyEvent itself. - AnyEvent::Impl::EV based on EV (an interface to libev, best choice). - AnyEvent::Impl::Event based on Event, second best choice. + AnyEvent::Impl::EV based on EV (interface to libev, best choice). + AnyEvent::Impl::Event based on Event, very stable, few glitches. AnyEvent::Impl::Perl pure-perl implementation, fast and portable. - AnyEvent::Impl::Glib based on Glib, third-best choice. - AnyEvent::Impl::Tk based on Tk, very bad choice. - AnyEvent::Impl::Qt based on Qt, cannot be autoprobed (see its docs). + +=item Backends that are transparently being picked up when they are used. + +These will be used when they are currently loaded when the first watcher +is created, in which case it is assumed that the application is using +them. This means that AnyEvent will automatically pick the right backend +when the main program loads an event module before anything starts to +create watchers. Nothing special needs to be done by the main program. + + AnyEvent::Impl::Glib based on Glib, slow but very stable. + AnyEvent::Impl::Tk based on Tk, very broken. AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. - AnyEvent::Impl::POE based on POE, not generic enough for full support. + AnyEvent::Impl::POE based on POE, very slow, some limitations. - # warning, support for IO::Async is only partial, as it is too broken - # and limited toe ven support the AnyEvent API. See AnyEvent::Impl::Async. - AnyEvent::Impl::IOAsync based on IO::Async, cannot be autoprobed (see its docs). - -There is no support for WxWidgets, as WxWidgets has no support for -watching file handles. However, you can use WxWidgets through the -POE Adaptor, as POE has a Wx backend that simply polls 20 times per -second, which was considered to be too horrible to even consider for -AnyEvent. Likewise, other POE backends can be used by AnyEvent by using -it's adaptor. +=item Backends with special needs. + +Qt requires the Qt::Application to be instantiated first, but will +otherwise be picked up automatically. As long as the main program +instantiates the application before any AnyEvent watchers are created, +everything should just work. + + AnyEvent::Impl::Qt based on Qt. + +Support for IO::Async can only be partial, as it is too broken and +architecturally limited to even support the AnyEvent API. It also +is the only event loop that needs the loop to be set explicitly, so +it can only be used by a main program knowing about AnyEvent. See +L for the gory details. + + AnyEvent::Impl::IOAsync based on IO::Async, cannot be autoprobed. + +=item Event loops that are indirectly supported via other backends. + +Some event loops can be supported via other modules: + +There is no direct support for WxWidgets (L) or L. + +B has no support for watching file handles. However, you can +use WxWidgets through the POE adaptor, as POE has a Wx backend that simply +polls 20 times per second, which was considered to be too horrible to even +consider for AnyEvent. + +B is not supported as nobody seems to be using it, but it has a POE +backend, so it can be supported through POE. + +AnyEvent knows about both L and L, however, and will try to +load L when detecting them, in the hope that POE will pick them up, +in which case everything will be automatic. + +=back + +=head1 GLOBAL VARIABLES AND FUNCTIONS -AnyEvent knows about L and L and will try to use L when -autodetecting them. +These are not normally required to use AnyEvent, but can be useful to +write AnyEvent extension modules. + +=over 4 + +=item $AnyEvent::MODEL + +Contains C until the first watcher is being created, before the +backend has been autodetected. + +Afterwards it contains the event model that is being used, which is the +name of the Perl class implementing the model. This class is usually one +of the C modules, but can be any other class in the +case AnyEvent has been extended at runtime (e.g. in I it +will be C). =item AnyEvent::detect Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if necessary. You should only call this function right before you would have created an AnyEvent watcher anyway, that is, as late as possible at -runtime. +runtime, and not e.g. while initialising of your module. + +If you need to do some initialisation before AnyEvent watchers are +created, use C. =item $guard = AnyEvent::post_detect { BLOCK } Arranges for the code block to be executed as soon as the event model is autodetected (or immediately if this has already happened). +The block will be executed I the actual backend has been detected +(C<$AnyEvent::MODEL> is set), but I any watchers have been +created, so it is possible to e.g. patch C<@AnyEvent::ISA> or do +other initialisations - see the sources of L or +L to see how this is used. + +The most common usage is to create some global watchers, without forcing +event module detection too early, for example, L creates +and installs the global L watcher in a C block to +avoid autodetecting the event module at load time. + If called in scalar or list context, then it creates and returns an object that automatically removes the callback again when it is destroyed. See L for a case where this is useful. @@ -802,10 +882,16 @@ the event loop has been chosen. You should check C<$AnyEvent::MODEL> before adding to this array, though: -if it contains a true value then the event loop has already been detected, -and the array will be ignored. +if it is defined then the event loop has already been detected, and the +array will be ignored. + +Best use C when your application allows +it,as it takes care of these details. -Best use C instead. +This variable is mainly useful for modules that can do something useful +when AnyEvent is used and thus want to know when it is initialised, but do +not need to even load it by default. This array provides the means to hook +into AnyEvent passively, without loading it. =back @@ -870,9 +956,9 @@ =head1 OTHER MODULES The following is a non-exhaustive list of additional modules that use -AnyEvent and can therefore be mixed easily with other AnyEvent modules -in the same program. Some of the modules come with AnyEvent, some are -available via CPAN. +AnyEvent as a client and can therefore be mixed easily with other AnyEvent +modules and other event loops in the same program. Some of the modules +come with AnyEvent, most are available via CPAN. =over 4 @@ -891,7 +977,7 @@ Provide read and write buffers, manages watchers for reads and writes, supports raw and formatted I/O, I/O queued and fully transparent and -non-blocking SSL/TLS. +non-blocking SSL/TLS (via L. =item L @@ -929,18 +1015,19 @@ A non-blocking interface to gpsd, a daemon delivering GPS information. -=item L - -A non-blocking interface to the Internet Go Server protocol (used by -L). - =item L AnyEvent based IRC client module family (replacing the older Net::IRC3). -=item L +=item L -AnyEvent based XMPP (Jabber protocol) module family. +AnyEvent based XMPP (Jabber protocol) module family (replacing the older +Net::XMPP2>. + +=item L + +A non-blocking interface to the Internet Go Server protocol (used by +L). =item L @@ -955,22 +1042,25 @@ Has special support for AnyEvent via L. -=item L - -The lambda approach to I/O - don't ask, look there. Can use AnyEvent. - =back =cut package AnyEvent; -no warnings; -use strict qw(vars subs); +# basically a tuned-down version of common::sense +sub common_sense { + # no warnings + ${^WARNING_BITS} ^= ${^WARNING_BITS}; + # use strict vars subs + $^H |= 0x00000600; +} + +BEGIN { AnyEvent::common_sense } -use Carp; +use Carp (); -our $VERSION = 4.8; +our $VERSION = 4.85; our $MODEL; our $AUTOLOAD; @@ -980,15 +1070,20 @@ our $WIN32; +our $VERBOSE; + BEGIN { eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; eval "sub TAINT(){ " . (${^TAINT}*1) . " }"; delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} if ${^TAINT}; + + $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; + } -our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; +our $MAX_SIGNAL_LATENCY = 10; our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred @@ -1006,14 +1101,14 @@ # everything below here will not be autoprobed # as the pureperl backend should work everywhere # and is usually faster - [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy + [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles [Qt:: => AnyEvent::Impl::Qt::], # requires special main program [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza [Wx:: => AnyEvent::Impl::POE::], [Prima:: => AnyEvent::Impl::POE::], - # IO::Async is just too broken - we would need workaorunds for its + # IO::Async is just too broken - we would need workarounds for its # byzantine signal and broken child handling, among others. # IO::Async is rather hard to detect, as it doesn't have any # obvious default class. @@ -1049,16 +1144,15 @@ sub detect() { unless ($MODEL) { - no strict 'refs'; local $SIG{__DIE__}; if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { my $model = "AnyEvent::Impl::$1"; if (eval "require $model") { $MODEL = $model; - warn "AnyEvent: loaded model '$model' (forced by \$PERL_ANYEVENT_MODEL), using it.\n" if $verbose > 1; + warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; } else { - warn "AnyEvent: unable to load model '$model' (from \$PERL_ANYEVENT_MODEL):\n$@" if $verbose; + warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; } } @@ -1069,7 +1163,7 @@ if (${"$package\::VERSION"} > 0) { if (eval "require $model") { $MODEL = $model; - warn "AnyEvent: autodetected model '$model', using it.\n" if $verbose > 1; + warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; last; } } @@ -1084,7 +1178,7 @@ and ${"$package\::VERSION"} > 0 and eval "require $model") { $MODEL = $model; - warn "AnyEvent: autoprobed model '$model', using it.\n" if $verbose > 1; + warn "AnyEvent: autoprobed model '$model', using it.\n" if $VERBOSE >= 2; last; } } @@ -1110,7 +1204,7 @@ (my $func = $AUTOLOAD) =~ s/.*://; $method{$func} - or croak "$func: not a valid method for AnyEvent objects"; + or Carp::croak "$func: not a valid method for AnyEvent objects"; detect unless $MODEL; @@ -1125,9 +1219,9 @@ my ($poll, $fh, $r, $w) = @_; # cygwin requires the fh mode to be matching, unix doesn't - my ($rw, $mode) = $poll eq "r" ? ($r, "<") : ($w, ">"); + my ($rw, $mode) = $poll eq "r" ? ($r, "<&") : ($w, ">&"); - open my $fh2, "$mode&", $fh + open my $fh2, $mode, $fh or die "AnyEvent->io: cannot dup() filehandle in mode '$poll': $!,"; # we assume CLOEXEC is already set by perl in all important cases @@ -1139,13 +1233,18 @@ # default implementations for many methods -BEGIN { +sub _time { + # probe for availability of Time::HiRes if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { + warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; *_time = \&Time::HiRes::time; # if (eval "use POSIX (); (POSIX::times())... } else { + warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; *_time = sub { time }; # epic fail } + + &_time } sub time { _time } @@ -1160,10 +1259,15 @@ # default implementation for ->signal +our $HAVE_ASYNC_INTERRUPT; our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); +our (%SIG_ASY, %SIG_ASY_W); +our ($SIG_COUNT, $SIG_TW); sub _signal_exec { - sysread $SIGPIPE_R, my $dummy, 4; + $HAVE_ASYNC_INTERRUPT + ? $SIGPIPE_R->drain + : sysread $SIGPIPE_R, my $dummy, 9; while (%SIG_EV) { for (keys %SIG_EV) { @@ -1173,10 +1277,76 @@ } } -sub signal { +# install a dumym wakeupw atcher to reduce signal catching latency +sub _sig_add() { + unless ($SIG_COUNT++) { + # try to align timer on a full-second boundary, if possible + my $NOW = AnyEvent->now; + + $SIG_TW = AnyEvent->timer ( + after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), + interval => $MAX_SIGNAL_LATENCY, + cb => sub { }, # just for the PERL_ASYNC_CHECK + ); + } +} + +sub _sig_del { + undef $SIG_TW + unless --$SIG_COUNT; +} + +sub _signal { my (undef, %arg) = @_; - unless ($SIGPIPE_R) { + my $signal = uc $arg{signal} + or Carp::croak "required option 'signal' is missing"; + + $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; + + if ($HAVE_ASYNC_INTERRUPT) { + # async::interrupt + + $SIG_ASY{$signal} ||= do { + my $asy = new Async::Interrupt + cb => sub { undef $SIG_EV{$signal} }, + signal => $signal, + pipe => [$SIGPIPE_R->filenos], + ; + $asy->pipe_autodrain (0); + + $asy + }; + + } else { + # pure perl + + $SIG{$signal} ||= sub { + local $!; + syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; + undef $SIG_EV{$signal}; + }; + + # can't do signal processing without introducing races in pure perl, + # so limit the signal latency. + _sig_add; + } + + bless [$signal, $arg{cb}], "AnyEvent::Base::signal" +} + +sub signal { + # probe for availability of Async::Interrupt + if (!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} && eval "use Async::Interrupt 0.6 (); 1") { + warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; + + $HAVE_ASYNC_INTERRUPT = 1; + $SIGPIPE_R = new Async::Interrupt::EventPipe; + $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); + + } else { + warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; + require Fcntl; if (AnyEvent::WIN32) { @@ -1201,28 +1371,24 @@ $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); } - my $signal = uc $arg{signal} - or Carp::croak "required option 'signal' is missing"; - - $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; - $SIG{$signal} ||= sub { - local $!; - syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; - undef $SIG_EV{$signal}; - }; - - bless [$signal, $arg{cb}], "AnyEvent::Base::signal" + *signal = \&_signal; + &signal } sub AnyEvent::Base::signal::DESTROY { my ($signal, $cb) = @{$_[0]}; + _sig_del; + delete $SIG_CB{$signal}{$cb}; - # delete doesn't work with older perls - they then - # print weird messages, or just unconditionally exit - # instead of getting the default action. - undef $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; + $HAVE_ASYNC_INTERRUPT + ? delete $SIG_ASY{$signal} + : # delete doesn't work with older perls - they then + # print weird messages, or just unconditionally exit + # instead of getting the default action. + undef $SIG{$signal} + unless keys %{ $SIG_CB{$signal} }; } # default implementation for ->child @@ -1234,8 +1400,9 @@ sub _sigchld { while (0 < (my $pid = waitpid -1, $WNOHANG)) { - $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), - (values %{ $PID_CB{0} || {} }); + $_->($pid, $?) + for values %{ $PID_CB{$pid} || {} }, + values %{ $PID_CB{0} || {} }; } } @@ -1247,7 +1414,10 @@ $PID_CB{$pid}{$arg{cb}} = $arg{cb}; - $WNOHANG ||= eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; + # WNOHANG is almost cetrainly 1 everywhere + $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ + ? 1 + : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; unless ($CHLD_W) { $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); @@ -1309,9 +1479,17 @@ package AnyEvent::CondVar::Base; -use overload - '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, - fallback => 1; +#use overload +# '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, +# fallback => 1; + +# save 300+ kilobytes by dirtily hardcoding overloading +${"AnyEvent::CondVar::Base::OVERLOAD"}{dummy}++; # Register with magic by touching. +*{'AnyEvent::CondVar::Base::()'} = sub { }; # "Make it findable via fetchmethod." +*{'AnyEvent::CondVar::Base::(&{}'} = sub { my $self = shift; sub { $self->send (@_) } }; # &{} +${'AnyEvent::CondVar::Base::()'} = 1; # fallback + +our $WAITING; sub _send { # nop @@ -1334,6 +1512,11 @@ } sub _wait { + $WAITING + and !$_[0]{_ae_sent} + and Carp::croak "AnyEvent::CondVar: recursive blocking wait detected"; + + local $WAITING = 1; AnyEvent->one_event while !$_[0]{_ae_sent}; } @@ -1405,6 +1588,9 @@ When set to C<2> or higher, cause AnyEvent to report to STDERR which event model it chooses. +When set to C<8> or higher, then AnyEvent will report extra information on +which optional modules it loads and how it implements certain features. + =item C AnyEvent does not do much argument checking by default, as thorough @@ -1415,9 +1601,10 @@ In other words, enables "strict" mode. -Unlike C, it is definitely recommended to keep it off in -production. Keeping C in your environment while -developing programs can be very useful, however. +Unlike C (or it's modern cousin, C<< use L +>>, it is definitely recommended to keep it off in production. Keeping +C in your environment while developing programs +can be very useful, however. =item C @@ -1490,6 +1677,11 @@ variables exist, they will be used to specify CA certificate locations instead of a system-dependent default. +=item C and C + +When these are set to C<1>, then the respective modules are not +loaded. Mostly good for testing AnyEvent itself. + =back =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE @@ -2037,8 +2229,8 @@ emulation for event loops that do not support them natively. Also, some event loops install a similar handler. -If, when AnyEvent is loaded, SIGCHLD is set to IGNORE, then AnyEvent will -reset it to default, to avoid losing child exit statuses. +Additionally, when AnyEvent is loaded and SIGCHLD is set to IGNORE, then +AnyEvent will reset it to default, to avoid losing child exit statuses. =item SIGPIPE @@ -2066,6 +2258,84 @@ $SIG{PIPE} = sub { } unless defined $SIG{PIPE}; +=head1 RECOMMENDED/OPTIONAL MODULES + +One of AnyEvent's main goals is to be 100% Pure-Perl(tm): only perl (and +it's built-in modules) are required to use it. + +That does not mean that AnyEvent won't take advantage of some additional +modules if they are installed. + +This section epxlains which additional modules will be used, and how they +affect AnyEvent's operetion. + +=over 4 + +=item L + +This slightly arcane module is used to implement fast signal handling: To +my knowledge, there is no way to do completely race-free and quick +signal handling in pure perl. To ensure that signals still get +delivered, AnyEvent will start an interval timer to wake up perl (and +catch the signals) with some delay (default is 10 seconds, look for +C<$AnyEvent::MAX_SIGNAL_LATENCY>). + +If this module is available, then it will be used to implement signal +catching, which means that signals will not be delayed, and the event loop +will not be interrupted regularly, which is more efficient (And good for +battery life on laptops). + +This affects not just the pure-perl event loop, but also other event loops +that have no signal handling on their own (e.g. Glib, Tk, Qt). + +Some event loops (POE, Event, Event::Lib) offer signal watchers natively, +and either employ their own workarounds (POE) or use AnyEvent's workaround +(using C<$AnyEvent::MAX_SIGNAL_LATENCY>). Installing L +does nothing for those backends. + +=item L + +This module isn't really "optional", as it is simply one of the backend +event loops that AnyEvent can use. However, it is simply the best event +loop available in terms of features, speed and stability: It supports +the AnyEvent API optimally, implements all the watcher types in XS, does +automatic timer adjustments even when no monotonic clock is available, +can take avdantage of advanced kernel interfaces such as C and +C, and is the fastest backend I. You can even embed +L/L in it (or vice versa, see L and L). + +=item L + +The guard module, when used, will be used to implement +C. This speeds up guards considerably (and uses a +lot less memory), but otherwise doesn't affect guard operation much. It is +purely used for performance. + +=item L and L + +This module is required when you want to read or write JSON data via +L. It is also written in pure-perl, but can take +advantage of the ultra-high-speed L module when it is installed. + +In fact, L will use L by default if it is +installed. + +=item L + +Implementing TLS/SSL in Perl is certainly interesting, but not very +worthwhile: If this module is installed, then L (with +the help of L), gains the ability to do TLS/SSL. + +=item L + +This module is part of perl since release 5.008. It will be used when the +chosen event library does not come with a timing source on it's own. The +pure-perl event loop (L) will additionally use it to +try to use a monotonic clock for timing stability. + +=back + + =head1 FORK Most event libraries are not fork-safe. The ones who are usually are @@ -2073,7 +2343,8 @@ calls. Only L is fully fork-aware. If you have to fork, you must either do so I creating your first -watcher OR you must not use AnyEvent at all in the child. +watcher OR you must not use AnyEvent at all in the child OR you must do +something completely out of the scope of AnyEvent. =head1 SECURITY CONSIDERATIONS @@ -2121,16 +2392,18 @@ Implementations: L, L, L, L, L, L, L, -L. +L, L. Non-blocking file handles, sockets, TCP clients and -servers: L, L. +servers: L, L, L. Asynchronous DNS: L. -Coroutine support: L, L, L, L, +Coroutine support: L, L, L, +L, -Nontrivial usage examples: L, L, L. +Nontrivial usage examples: L, L, +L. =head1 AUTHOR