--- AnyEvent/lib/AnyEvent.pm 2009/08/09 16:05:11 1.279 +++ AnyEvent/lib/AnyEvent.pm 2010/07/11 05:44:22 1.329 @@ -9,7 +9,10 @@ use AnyEvent; - # file descriptor readable + # if you prefer function calls, look at the AE manpage for + # an alternative API. + + # file handle or descriptor readable my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); # one-shot or repeating timers @@ -137,8 +140,9 @@ # .. AnyEvent will likely default to Tk The I means that, if any module loads another event model and -starts using it, all bets are off. Maybe you should tell their authors to -use AnyEvent so their modules work together with others seamlessly... +starts using it, all bets are off - this case should be very rare though, +as very few modules hardcode event loops without announcing this very +loudly. The pure-perl implementation of AnyEvent is called C. Like other event modules you can load it @@ -365,6 +369,13 @@ When this is the case, you can call this method, which will update the event loop's idea of "current time". +A typical example would be a script in a web server (e.g. C) - +when mod_perl executes the script, then the event loop will have the wrong +idea about the "current time" (being potentially far in the past, when the +script ran the last time). In that case you should arrange a call to C<< +AnyEvent->now_update >> each time the web server process wakes up again +(e.g. at the start of your script, or in a handler). + Note that updating the time I cause some events to be handled. =back @@ -398,6 +409,23 @@ my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); +=head3 Restart Behaviour + +While restart behaviour is up to the event loop implementation, most will +not restart syscalls (that includes L and AnyEvent's +pure perl implementation). + +=head3 Safe/Unsafe Signals + +Perl signals can be either "safe" (synchronous to opcode handling) or +"unsafe" (asynchronous) - the former might get delayed indefinitely, the +latter might corrupt your memory. + +AnyEvent signal handlers are, in addition, synchronous to the event loop, +i.e. they will not interrupt your running perl program but will only be +called as part of the normal event handling (just like timer, I/O etc. +callbacks, too). + =head3 Signal Races, Delays and Workarounds Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching @@ -481,16 +509,19 @@ $w = AnyEvent->idle (cb => ); -Sometimes there is a need to do something, but it is not so important -to do it instantly, but only when there is nothing better to do. This -"nothing better to do" is usually defined to be "no other events need -attention by the event loop". - -Idle watchers ideally get invoked when the event loop has nothing -better to do, just before it would block the process to wait for new -events. Instead of blocking, the idle watcher is invoked. +Repeatedly invoke the callback after the process becomes idle, until +either the watcher is destroyed or new events have been detected. + +Idle watchers are useful when there is a need to do something, but it +is not so important (or wise) to do it instantly. The callback will be +invoked only when there is "nothing better to do", which is usually +defined as "all outstanding events have been handled and no new events +have been detected". That means that idle watchers ideally get invoked +when the event loop has just polled for new events but none have been +detected. Instead of blocking to wait for more events, the idle watchers +will be invoked. -Most event loops unfortunately do not really support idle watchers (only +Unfortunately, most event loops do not really support idle watchers (only EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent will simply call the callback "from time to time". @@ -528,8 +559,8 @@ 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. +The tool 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. @@ -544,13 +575,29 @@ were a callback, read about the caveats in the description for the C<< ->send >> method). -Condition variables are similar to callbacks, except that you can -optionally wait for them. They can also be called merge points - points -in time where multiple outstanding events have been processed. And yet -another way to call them is transactions - each condition variable can be -used to represent a transaction, which finishes at some point and delivers -a result. And yet some people know them as "futures" - a promise to -compute/deliver something that you can wait for. +Since condition variables are the most complex part of the AnyEvent API, here are +some different mental models of what they are - pick the ones you can connect to: + +=over 4 + +=item * Condition variables are like callbacks - you can call them (and pass them instead +of callbacks). Unlike callbacks however, you can also wait for them to be called. + +=item * Condition variables are signals - one side can emit or send them, +the other side can wait for them, or install a handler that is called when +the signal fires. + +=item * Condition variables are like "Merge Points" - points in your program +where you merge multiple independent results/control flows into one. + +=item * Condition variables represent a transaction - function that start +some kind of transaction can return them, leaving the caller the choice +between waiting in a blocking fashion, or setting a callback. + +=item * Condition variables represent future values, or promises to deliver +some result, long before the result is available. + +=back Condition variables are very useful to signal that something has finished, for example, if you write a module that does asynchronous http requests, @@ -581,21 +628,21 @@ Example: wait for a timer. - # wait till the result is ready - my $result_ready = AnyEvent->condvar; + # condition: "wait till the timer is fired" + my $timer_fired = AnyEvent->condvar; - # do something such as adding a timer - # or socket watcher the calls $result_ready->send - # when the "result" is ready. + # create the timer - we could wait for, say + # a handle becomign ready, or even an + # AnyEvent::HTTP request to finish, but # in this case, we simply use a timer: my $w = AnyEvent->timer ( after => 1, - cb => sub { $result_ready->send }, + cb => sub { $timer_fired->send }, ); # this "blocks" (while handling events) till the callback - # calls -recv; + # calls ->send + $timer_fired->recv; Example: wait for a timer, but take advantage of the fact that condition variables are also callable directly. @@ -668,9 +715,10 @@ Every call to C<< ->begin >> will increment a counter, and every call to C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end ->>, the (last) callback passed to C will be executed. That callback -is I to call C<< ->send >>, but that is not required. If no -callback was set, C will be called without any arguments. +>>, the (last) callback passed to C will be executed, passing the +condvar as first argument. That callback is I to call C<< ->send +>>, but that is not required. If no group callback was set, C will +be called without any arguments. You can think of C<< $cv->send >> giving you an OR condition (one call sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND @@ -707,7 +755,7 @@ my $cv = AnyEvent->condvar; my %result; - $cv->begin (sub { $cv->send (\%result) }); + $cv->begin (sub { shift->send (\%result) }); for my $host (@list_of_hosts) { $cv->begin; @@ -943,13 +991,26 @@ array will be ignored. Best use C when your application allows -it,as it takes care of these details. +it, as it takes care of these details. 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. +Example: To load Coro::AnyEvent whenever Coro and AnyEvent are used +together, you could put this into Coro (this is the actual code used by +Coro to accomplish this): + + if (defined $AnyEvent::MODEL) { + # AnyEvent already initialised, so load Coro::AnyEvent + require Coro::AnyEvent; + } else { + # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent + # as soon as it is + push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; + } + =back =head1 WHAT TO DO IN A MODULE @@ -1015,7 +1076,7 @@ The following is a non-exhaustive list of additional modules that use 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. +come as part of AnyEvent, the others are available via CPAN. =over 4 @@ -1040,60 +1101,48 @@ Provides rich asynchronous DNS resolver capabilities. -=item L - -A simple-to-use HTTP library that is capable of making a lot of concurrent -HTTP requests. - -=item L +=item L, L, L, L, L, L -Provides a simple web application server framework. - -=item L - -The fastest ping in the west. +Implement event-based interfaces to the protocols of the same name (for +the curious, IGS is the International Go Server and FCP is the Freenet +Client Protocol). + +=item L + +Here be danger! + +As Pauli would put it, "Not only is it not right, it's not even wrong!" - +there are so many things wrong with AnyEvent::Handle::UDP, most notably +it's use of a stream-based API with a protocol that isn't streamable, that +the only way to improve it is to delete it. + +It features data corruption (but typically only under load) and general +confusion. On top, the author is not only clueless about UDP but also +fact-resistant - some gems of his understanding: "connect doesn't work +with UDP", "UDP packets are not IP packets", "UDP only has datagrams, not +packets", "I don't need to implement proper error checking as UDP doesn't +support error checking" and so on - he doesn't even understand what's +wrong with his module when it is explained to him. =item L -Executes L requests asynchronously in a proxy process. +Executes L requests asynchronously in a proxy process for you, +notifying you in an event-bnased way when the operation is finished. =item L -Truly asynchronous I/O, should be in the toolbox of every event -programmer. AnyEvent::AIO transparently fuses L and AnyEvent -together. - -=item L - -Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses -L and AnyEvent together. - -=item L - -A non-blocking interface to gpsd, a daemon delivering GPS information. - -=item L +Truly asynchronous (as opposed to non-blocking) I/O, should be in the +toolbox of every event programmer. AnyEvent::AIO transparently fuses +L and AnyEvent together, giving AnyEvent access to event-based +file I/O, and much more. -AnyEvent based IRC client module family (replacing the older Net::IRC3). - -=item L - -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 +=item L -AnyEvent-based implementation of the Freenet Client Protocol, birthplace -of AnyEvent. +A simple embedded webserver. -=item L +=item L -High level API for event-based execution flow control. +The fastest ping in the west. =item L @@ -1107,9 +1156,9 @@ # basically a tuned-down version of common::sense sub common_sense { - # no warnings - ${^WARNING_BITS} ^= ${^WARNING_BITS}; - # use strict vars subs + # from common:.sense 1.0 + ${^WARNING_BITS} = "\xfc\x3f\x33\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x00"; + # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) $^H |= 0x00000600; } @@ -1117,7 +1166,7 @@ use Carp (); -our $VERSION = '5.0'; +our $VERSION = '5.271'; our $MODEL; our $AUTOLOAD; @@ -1125,13 +1174,12 @@ our @REGISTRY; -our $WIN32; - our $VERBOSE; BEGIN { - eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; - eval "sub TAINT(){ " . (${^TAINT}*1) . " }"; + require "AnyEvent/constants.pl"; + + eval "sub TAINT (){" . (${^TAINT}*1) . "}"; delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} if ${^TAINT}; @@ -1184,17 +1232,11 @@ sub post_detect(&) { my ($cb) = @_; - if ($MODEL) { - $cb->(); - - undef - } else { - push @post_detect, $cb; + push @post_detect, $cb; - defined wantarray - ? bless \$cb, "AnyEvent::Util::postdetect" - : () - } + defined wantarray + ? bless \$cb, "AnyEvent::Util::postdetect" + : () } sub AnyEvent::Util::postdetect::DESTROY { @@ -1202,62 +1244,78 @@ } sub detect() { - unless ($MODEL) { - local $SIG{__DIE__}; + # free some memory + *detect = sub () { $MODEL }; - 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 \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; - } else { - warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; + local $!; # for good measure + 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 \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; + } else { + warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; + } + } + + # check for already loaded models + unless ($MODEL) { + for (@REGISTRY, @models) { + my ($package, $model) = @$_; + if (${"$package\::VERSION"} > 0) { + if (eval "require $model") { + $MODEL = $model; + warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; + last; + } } } - # check for already loaded models unless ($MODEL) { + # try to autoload a model for (@REGISTRY, @models) { - my ($package, $model) = @$_; - if (${"$package\::VERSION"} > 0) { - if (eval "require $model") { - $MODEL = $model; - warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; - last; - } + my ($package, $model, $autoload) = @$_; + if ( + $autoload + and eval "require $package" + and ${"$package\::VERSION"} > 0 + and eval "require $model" + ) { + $MODEL = $model; + warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; + last; } } - unless ($MODEL) { - # try to autoload a model - for (@REGISTRY, @models) { - my ($package, $model, $autoload) = @$_; - if ( - $autoload - and eval "require $package" - and ${"$package\::VERSION"} > 0 - and eval "require $model" - ) { - $MODEL = $model; - warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; - last; - } - } - - $MODEL - or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; - } + $MODEL + or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; } + } - push @{"$MODEL\::ISA"}, "AnyEvent::Base"; - - unshift @ISA, $MODEL; + @models = (); # free probe data - require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; + push @{"$MODEL\::ISA"}, "AnyEvent::Base"; + unshift @ISA, $MODEL; - (shift @post_detect)->() while @post_detect; + # now nuke some methods that are overriden by the backend. + # SUPER is not allowed. + for (qw(time signal child idle)) { + undef &{"AnyEvent::Base::$_"} + if defined &{"$MODEL\::$_"}; } + require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; + + (shift @post_detect)->() while @post_detect; + + *post_detect = sub(&) { + shift->(); + + undef + }; + $MODEL } @@ -1265,9 +1323,9 @@ (my $func = $AUTOLOAD) =~ s/.*://; $method{$func} - or Carp::croak "$func: not a valid method for AnyEvent objects"; + or Carp::croak "$func: not a valid AnyEvent class method"; - detect unless $MODEL; + detect; my $class = shift; $class->$func (@_); @@ -1294,7 +1352,7 @@ Starting with version 5.0, AnyEvent officially supports a second, much simpler, API that is designed to reduce the calling, typing and memory -overhead. +overhead by using function call syntax and a fixed number of parameters. See the L manpage for details. @@ -1304,6 +1362,9 @@ our $VERSION = $AnyEvent::VERSION; +# fall back to the main API by default - backends and AnyEvent::Base +# implementations can overwrite these. + sub io($$$) { AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) } @@ -1344,28 +1405,44 @@ # default implementations for many methods -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 - } +sub time { + eval q{ # poor man's autoloading {} + # 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; + *AE::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; + *AE::time = sub (){ time }; # epic fail + } + + *time = sub { AE::time }; # different prototypes + }; + die if $@; - &_time + &time } -sub time { _time } -sub now { _time } +*now = \&time; + sub now_update { } # default implementation for ->condvar sub condvar { - bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" + eval q{ # poor man's autoloading {} + *condvar = sub { + bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" + }; + + *AE::cv = sub (;&) { + bless { @_ ? (_ae_cb => shift) : () }, "AnyEvent::CondVar" + }; + }; + die if $@; + + &condvar } # default implementation for ->signal @@ -1374,7 +1451,7 @@ sub _have_async_interrupt() { $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} - && eval "use Async::Interrupt 1.0 (); 1") + && eval "use Async::Interrupt 1.02 (); 1") unless defined $HAVE_ASYNC_INTERRUPT; $HAVE_ASYNC_INTERRUPT @@ -1384,20 +1461,8 @@ our (%SIG_ASY, %SIG_ASY_W); our ($SIG_COUNT, $SIG_TW); -sub _signal_exec { - $HAVE_ASYNC_INTERRUPT - ? $SIGPIPE_R->drain - : sysread $SIGPIPE_R, my $dummy, 9; - - while (%SIG_EV) { - for (keys %SIG_EV) { - delete $SIG_EV{$_}; - $_->() for values %{ $SIG_CB{$_} || {} }; - } - } -} - # install a dummy wakeup watcher to reduce signal catching latency +# used by Impls sub _sig_add() { unless ($SIG_COUNT++) { # try to align timer on a full-second boundary, if possible @@ -1417,7 +1482,7 @@ } our $_sig_name_init; $_sig_name_init = sub { - eval q{ # poor man's autoloading + eval q{ # poor man's autoloading {} undef $_sig_name_init; if (_have_async_interrupt) { @@ -1459,8 +1524,6 @@ } else { warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; - require Fcntl; - if (AnyEvent::WIN32) { require AnyEvent::Util; @@ -1469,12 +1532,12 @@ AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case } else { pipe $SIGPIPE_R, $SIGPIPE_W; - fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; - fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case + fcntl $SIGPIPE_R, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_R; + fcntl $SIGPIPE_W, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_W; # just in case # not strictly required, as $^F is normally 2, but let's make sure... - fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; - fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; + fcntl $SIGPIPE_R, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; + fcntl $SIGPIPE_W, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; } $SIGPIPE_R @@ -1483,45 +1546,43 @@ $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; } - *signal = sub { - my (undef, %arg) = @_; + *signal = $HAVE_ASYNC_INTERRUPT + ? sub { + my (undef, %arg) = @_; + + # async::interrupt + my $signal = sig2num $arg{signal}; + $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; + + $SIG_ASY{$signal} ||= new Async::Interrupt + cb => sub { undef $SIG_EV{$signal} }, + signal => $signal, + pipe => [$SIGPIPE_R->filenos], + pipe_autodrain => 0, + ; + + bless [$signal, $arg{cb}], "AnyEvent::Base::signal" + } + : sub { + my (undef, %arg) = @_; + + # pure perl + my $signal = sig2name $arg{signal}; + $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; + + $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; - my $signal = uc $arg{signal} - or Carp::croak "required option 'signal' is missing"; - - if ($HAVE_ASYNC_INTERRUPT) { - # async::interrupt - - $signal = sig2num $signal; - $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; - - $SIG_ASY{$signal} ||= new Async::Interrupt - cb => sub { undef $SIG_EV{$signal} }, - signal => $signal, - pipe => [$SIGPIPE_R->filenos], - pipe_autodrain => 0, - ; - - } else { - # pure perl - - # AE::Util has been loaded in signal - $signal = sig2name $signal; - $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; - - $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" - }; + bless [$signal, $arg{cb}], "AnyEvent::Base::signal" + } + ; *AnyEvent::Base::signal::DESTROY = sub { my ($signal, $cb) = @{$_[0]}; @@ -1538,8 +1599,22 @@ undef $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; }; + + *_signal_exec = sub { + $HAVE_ASYNC_INTERRUPT + ? $SIGPIPE_R->drain + : sysread $SIGPIPE_R, (my $dummy), 9; + + while (%SIG_EV) { + for (keys %SIG_EV) { + delete $SIG_EV{$_}; + $_->() for values %{ $SIG_CB{$_} || {} }; + } + } + }; }; die if $@; + &signal } @@ -1550,6 +1625,7 @@ our $CHLD_DELAY_W; our $WNOHANG; +# used by many Impl's sub _emit_childstatus($$) { my (undef, $rpid, $rstatus) = @_; @@ -1558,78 +1634,92 @@ values %{ $PID_CB{0} || {} }; } -sub _sigchld { - my $pid; +sub child { + eval q{ # poor man's autoloading {} + *_sigchld = sub { + my $pid; - AnyEvent->_emit_childstatus ($pid, $?) - while ($pid = waitpid -1, $WNOHANG) > 0; -} + AnyEvent->_emit_childstatus ($pid, $?) + while ($pid = waitpid -1, $WNOHANG) > 0; + }; -sub child { - my (undef, %arg) = @_; + *child = sub { + my (undef, %arg) = @_; - defined (my $pid = $arg{pid} + 0) - or Carp::croak "required option 'pid' is missing"; + defined (my $pid = $arg{pid} + 0) + or Carp::croak "required option 'pid' is missing"; - $PID_CB{$pid}{$arg{cb}} = $arg{cb}; + $PID_CB{$pid}{$arg{cb}} = $arg{cb}; - # 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 = AE::signal CHLD => \&_sigchld; - # child could be a zombie already, so make at least one round - &_sigchld; - } + # 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 = AE::signal CHLD => \&_sigchld; + # child could be a zombie already, so make at least one round + &_sigchld; + } - bless [$pid, $arg{cb}], "AnyEvent::Base::child" -} + bless [$pid, $arg{cb}], "AnyEvent::Base::child" + }; + + *AnyEvent::Base::child::DESTROY = sub { + my ($pid, $cb) = @{$_[0]}; -sub AnyEvent::Base::child::DESTROY { - my ($pid, $cb) = @{$_[0]}; + delete $PID_CB{$pid}{$cb}; + delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; - delete $PID_CB{$pid}{$cb}; - delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; + undef $CHLD_W unless keys %PID_CB; + }; + }; + die if $@; - undef $CHLD_W unless keys %PID_CB; + &child } # idle emulation is done by simply using a timer, regardless # of whether the process is idle or not, and not letting # the callback use more than 50% of the time. sub idle { - my (undef, %arg) = @_; + eval q{ # poor man's autoloading {} + *idle = sub { + my (undef, %arg) = @_; - my ($cb, $w, $rcb) = $arg{cb}; + my ($cb, $w, $rcb) = $arg{cb}; - $rcb = sub { - if ($cb) { - $w = _time; - &$cb; - $w = _time - $w; - - # never use more then 50% of the time for the idle watcher, - # within some limits - $w = 0.0001 if $w < 0.0001; - $w = 5 if $w > 5; + $rcb = sub { + if ($cb) { + $w = _time; + &$cb; + $w = _time - $w; + + # never use more then 50% of the time for the idle watcher, + # within some limits + $w = 0.0001 if $w < 0.0001; + $w = 5 if $w > 5; + + $w = AE::timer $w, 0, $rcb; + } else { + # clean up... + undef $w; + undef $rcb; + } + }; - $w = AE::timer $w, 0, $rcb; - } else { - # clean up... - undef $w; - undef $rcb; - } - }; + $w = AE::timer 0.05, 0, $rcb; - $w = AE::timer 0.05, 0, $rcb; + bless \\$cb, "AnyEvent::Base::idle" + }; - bless \\$cb, "AnyEvent::Base::idle" -} + *AnyEvent::Base::idle::DESTROY = sub { + undef $${$_[0]}; + }; + }; + die if $@; -sub AnyEvent::Base::idle::DESTROY { - undef $${$_[0]}; + &idle } package AnyEvent::CondVar; @@ -1913,16 +2003,9 @@ }, ); - my $time_watcher; # can only be used once - - sub new_timer { - $timer = AnyEvent->timer (after => 1, cb => sub { - warn "timeout\n"; # print 'timeout' about every second - &new_timer; # and restart the time - }); - } - - new_timer; # create first timer + my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub { + warn "timeout\n"; # print 'timeout' at most every second + }); $cv->recv; # wait until user enters /^q/i @@ -2005,7 +2088,7 @@ that occurred during request processing. The C method detects whether an exception as thrown (it is stored inside the $txn object) and just throws the exception, which means connection errors and other -problems get reported tot he code that tries to use the result, not in a +problems get reported to the code that tries to use the result, not in a random callback. All of this enables the following usage styles: @@ -2376,13 +2459,13 @@ backend easily beats IO::Lambda and POE. And even the 100% non-blocking version written using the high-level (and -slow :) L abstraction beats both POE and IO::Lambda by a -large margin, even though it does all of DNS, tcp-connect and socket I/O -in a non-blocking way. +slow :) L abstraction beats both POE and IO::Lambda +higher level ("unoptimised") abstractions by a large margin, even though +it does all of DNS, tcp-connect and socket I/O in a non-blocking way. The two AnyEvent benchmarks programs can be found as F and F in the AnyEvent distribution, the remaining benchmarks are -part of the IO::lambda distribution and were used without any changes. +part of the IO::Lambda distribution and were used without any changes. =head1 SIGNALS @@ -2434,8 +2517,8 @@ 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. +This section explains which additional modules will be used, and how they +affect AnyEvent's operation. =over 4 @@ -2450,7 +2533,7 @@ 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 +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 @@ -2472,6 +2555,9 @@ C, and is the fastest backend I. You can even embed L/L in it (or vice versa, see L and L). +If you only use backends that rely on another event loop (e.g. C), +then this module will do nothing for you. + =item L The guard module, when used, will be used to implement @@ -2481,13 +2567,10 @@ =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 +One of these modules is required when you want to read or write JSON data +via L. L 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 @@ -2507,13 +2590,31 @@ =head1 FORK Most event libraries are not fork-safe. The ones who are usually are -because they rely on inefficient but fork-safe C or C calls +- higher performance APIs such as BSD's kqueue or the dreaded Linux epoll +are usually badly thought-out hacks that are incompatible with fork in +one way or another. Only L is fully fork-aware and ensures that you +continue event-processing in both parent and child (or both, if you know +what you are doing). + +This means that, in general, you cannot fork and do event processing in +the child if the event library was initialised before the fork (which +usually happens when the first AnyEvent watcher is created, or the library +is loaded). 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 OR you must do something completely out of the scope of AnyEvent. +The problem of doing event processing in the parent I the child +is much more complicated: even for backends that I fork-aware or +fork-safe, their behaviour is not usually what you want: fork clones all +watchers, that means all timers, I/O watchers etc. are active in both +parent and child, which is almost never what you want. USing C +to start worker children from some kind of manage rprocess is usually +preferred, because it is much easier and cleaner, at the expense of having +to have another binary. + =head1 SECURITY CONSIDERATIONS