--- AnyEvent/lib/AnyEvent.pm 2011/08/02 20:02:44 1.350 +++ AnyEvent/lib/AnyEvent.pm 2011/08/17 02:32:01 1.369 @@ -2,8 +2,8 @@ AnyEvent - the DBI of event loop programming -EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, Qt -and POE are various supported event loops/environments. +EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, Qt, +FLTK and POE are various supported event loops/environments. =head1 SYNOPSIS @@ -123,11 +123,11 @@ During the first call of any watcher-creation method, the module tries to detect the currently loaded event loop by probing whether one of the -following modules is already loaded: L, L, +following modules is already loaded: L, L, L, L, L, L, L, L. The first one found is used. If none are detected, the module tries to load the first four modules in the order given; but note that if L is not -available, the pure-perl L should always work, so +available, the pure-perl L should always work, so the other two are not normally tried. Because AnyEvent first checks for modules that are already loaded, loading @@ -144,9 +144,9 @@ 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 -explicitly and enjoy the high availability of that event loop :) +The pure-perl implementation of AnyEvent is called C. Like +other event modules you can load it explicitly and enjoy the high +availability of that event loop :) =head1 WATCHERS @@ -358,9 +358,9 @@ =item AnyEvent->now_update -Some event loops (such as L or L) cache -the current time for each loop iteration (see the discussion of L<< -AnyEvent->now >>, above). +Some event loops (such as L or L) cache the current +time for each loop iteration (see the discussion of L<< AnyEvent->now >>, +above). When a callback runs for a long time (or when the process sleeps), then this "current" time will differ substantially from the real time, which @@ -484,8 +484,8 @@ 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. +emulated by AnyEvent in most cases, in which case the latency and race +problems mentioned in the description of signal watchers apply. Example: fork a process and wait for it @@ -862,7 +862,7 @@ AnyEvent itself. AnyEvent::Impl::EV based on EV (interface to libev, best choice). - AnyEvent::Impl::Perl pure-perl implementation, fast and portable. + AnyEvent::Impl::Perl pure-perl AnyEvent::Loop, fast and portable. =item Backends that are transparently being picked up when they are used. @@ -880,7 +880,7 @@ AnyEvent::Impl::Irssi used when running within irssi. AnyEvent::Impl::IOAsync based on IO::Async. AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. - AnyEvent::Impl::FLTK based on FLTK. + AnyEvent::Impl::FLTK2 based on FLTK (fltk 2 binding). =item Backends with special needs. @@ -936,6 +936,10 @@ have created an AnyEvent watcher anyway, that is, as late as possible at runtime, and not e.g. during initialisation of your module. +The effect of calling this function is as if a watcher had been created +(specifically, actions that happen "when the first watcher is created" +happen when calling detetc as well). + If you need to do some initialisation before AnyEvent watchers are created, use C. @@ -1007,6 +1011,56 @@ push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; } +=item AnyEvent::postpone { BLOCK } + +Arranges for the block to be executed as soon as possible, but not before +the call itself returns. In practise, the block will be executed just +before the event loop polls for new events, or shortly afterwards. + +This function never returns anything (to make the C idiom more useful. + +To understand the usefulness of this function, consider a function that +asynchronously does something for you and returns some transaction +object or guard to let you cancel the operation. For example, +C: + + # start a conenction attempt unless one is active + $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub { + delete $self->{connect_guard}; + ... + }; + +Imagine that this function could instantly call the callback, for +example, because it detects an obvious error such as a negative port +number. Invoking the callback before the function returns causes problems +however: the callback will be called and will try to delete the guard +object. But since the function hasn't returned yet, there is nothing to +delete. When the function eventually returns it will assign the guard +object to C<< $self->{connect_guard} >>, where it will likely never be +deleted, so the program thinks it is still trying to connect. + +This is where C should be used. Instead of calling the +callback directly on error: + + $cb->(undef), return # signal error to callback, BAD! + if $some_error_condition; + +It should use C: + + AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later + if $some_error_condition; + +=item AnyEvent::log $level, $msg[, @args] + +Log the given C<$msg> at the given C<$level>. + +Loads AnyEvent::Log on first use and calls C - +consequently, look at the L documentation for details. + +If you want to sprinkle loads of logging calls around your code, consider +creating a logger callback with the C module, which gives you similar behaviour +C module, which gives you similar behaviour everywhere, but letting AnyEvent chose the model is generally better. =head2 MAINLOOP EMULATION @@ -1072,9 +1126,12 @@ =head1 OTHER MODULES 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 as part of AnyEvent, the others 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 as part of AnyEvent, the others are available via CPAN (see +L for +a longer non-exhaustive list), and the list is heavily biased towards +modules of the AnyEvent author himself :) =over 4 @@ -1164,10 +1221,9 @@ use Carp (); -our $VERSION = '5.34'; +our $VERSION = '6.01'; our $MODEL; -our $AUTOLOAD; our @ISA; our @REGISTRY; @@ -1183,7 +1239,6 @@ if ${^TAINT}; $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; - } our $MAX_SIGNAL_LATENCY = 10; @@ -1197,11 +1252,51 @@ $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; } -my @models = ( +our @post_detect; + +sub post_detect(&) { + my ($cb) = @_; + + push @post_detect, $cb; + + defined wantarray + ? bless \$cb, "AnyEvent::Util::postdetect" + : () +} + +sub AnyEvent::Util::postdetect::DESTROY { + @post_detect = grep $_ != ${$_[0]}, @post_detect; +} + +our $POSTPONE_W; +our @POSTPONE; + +sub _postpone_exec { + undef $POSTPONE_W; + + &{ shift @POSTPONE } + while @POSTPONE; +} + +sub postpone(&) { + push @POSTPONE, shift; + + $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); + + () +} + +sub log($$;@) { + require AnyEvent::Log; + # AnyEvent::Log overwrites this function + goto &log; +} + +our @models = ( [EV:: => AnyEvent::Impl::EV:: , 1], - [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl:: , 1], + [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], # everything below here will not (normally) be autoprobed - # as the pureperl backend should work everywhere + # as the pure perl backend should work everywhere # and is usually faster [Event:: => AnyEvent::Impl::Event::, 1], [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers @@ -1212,44 +1307,61 @@ [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza [Wx:: => AnyEvent::Impl::POE::], [Prima:: => AnyEvent::Impl::POE::], - [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], + [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], - [FLTK:: => AnyEvent::Impl::FLTK::], + [FLTK:: => AnyEvent::Impl::FLTK2::], ); -our %method = map +($_ => 1), - qw(io timer time now now_update signal child idle condvar DESTROY); +our @isa_hook; -our @post_detect; +sub _isa_set { + my @pkg = ("AnyEvent", (map $_->[0], grep defined, @isa_hook), $MODEL); -sub post_detect(&) { - my ($cb) = @_; + @{"$pkg[$_-1]::ISA"} = $pkg[$_] + for 1 .. $#pkg; - push @post_detect, $cb; - - defined wantarray - ? bless \$cb, "AnyEvent::Util::postdetect" - : () + grep $_ && $_->[1], @isa_hook + and AE::_reset (); } -sub AnyEvent::Util::postdetect::DESTROY { - @post_detect = grep $_ != ${$_[0]}, @post_detect; +# used for hooking AnyEvent::Strict and AnyEvent::Debug::Wrap into the class hierarchy +sub _isa_hook($$;$) { + my ($i, $pkg, $reset_ae) = @_; + + $isa_hook[$i] = $pkg ? [$pkg, $reset_ae] : undef; + + _isa_set; } +# all autoloaded methods reserve the complete glob, not just the method slot. +# due to bugs in perls method cache implementation. +our @methods = qw(io timer time now now_update signal child idle condvar); + sub detect() { - # free some memory - *detect = sub () { $MODEL }; + return $MODEL if $MODEL; # some programs keep references to detect local $!; # for good measure - local $SIG{__DIE__}; + local $SIG{__DIE__}; # we use eval - if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { - my $model = "AnyEvent::Impl::$1"; + # free some memory + *detect = sub () { $MODEL }; + # undef &func doesn't correctly update the method cache. grmbl. + # so we delete the whole glob. grmbl. + # otoh, perl doesn't let me undef an active usb, but it lets me free + # a glob with an active sub. hrm. i hope it works, but perl is + # usually buggy in this department. sigh. + delete @{"AnyEvent::"}{@methods}; + undef @methods; + + if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { + my $model = $1; + $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; if (eval "require $model") { $MODEL = $model; - warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; + AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it." + if $VERBOSE >= 7; } else { - warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; + AnyEvent::log warn => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; } } @@ -1260,7 +1372,8 @@ if (${"$package\::VERSION"} > 0) { if (eval "require $model") { $MODEL = $model; - warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; + AnyEvent::log 7 => "autodetected model '$model', using it." + if $VERBOSE >= 7; last; } } @@ -1277,35 +1390,59 @@ and eval "require $model" ) { $MODEL = $model; - warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; + AnyEvent::log 7 => "autoloaded model '$model', using it." + if $VERBOSE >= 7; last; } } $MODEL - or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; + or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; } } - @models = (); # free probe data + # free memory only needed for probing + undef @models; + undef @REGISTRY; push @{"$MODEL\::ISA"}, "AnyEvent::Base"; - unshift @ISA, $MODEL; # now nuke some methods that are overridden by the backend. - # SUPER is not allowed. + # SUPER usage is not allowed in these. for (qw(time signal child idle)) { undef &{"AnyEvent::Base::$_"} if defined &{"$MODEL\::$_"}; } + _isa_set; + + # we're officially open! + if ($ENV{PERL_ANYEVENT_STRICT}) { - eval { require AnyEvent::Strict }; - warn "AnyEvent: cannot load AnyEvent::Strict: $@" - if $@ && $VERBOSE; + require AnyEvent::Strict; + } + + if ($ENV{PERL_ANYEVENT_DEBUG_WRAP}) { + require AnyEvent::Debug; + AnyEvent::Debug::wrap ($ENV{PERL_ANYEVENT_DEBUG_WRAP}); } + if (length $ENV{PERL_ANYEVENT_DEBUG_SHELL}) { + require AnyEvent::Socket; + require AnyEvent::Debug; + + my $shell = $ENV{PERL_ANYEVENT_DEBUG_SHELL}; + $shell =~ s/\$\$/$$/g; + + my ($host, $service) = AnyEvent::Socket::parse_hostport ($shell); + $AnyEvent::Debug::SHELL = AnyEvent::Debug::shell ($host, $service); + } + + # now the anyevent environment is set up as the user told us to, so + # call the actual user code - post detects + (shift @post_detect)->() while @post_detect; + undef @post_detect; *post_detect = sub(&) { shift->(); @@ -1316,16 +1453,14 @@ $MODEL } -sub AUTOLOAD { - (my $func = $AUTOLOAD) =~ s/.*://; - - $method{$func} - or Carp::croak "$func: not a valid AnyEvent class method"; - - detect; - - my $class = shift; - $class->$func (@_); +for my $name (@methods) { + *$name = sub { + detect; + # we use goto because + # a) it makes the thunk more transparent + # b) it allows us to delete the thunk later + goto &{ UNIVERSAL::can AnyEvent => "SUPER::$name" } + }; } # utility function to dup a filehandle. this is used by many backends @@ -1359,45 +1494,55 @@ our $VERSION = $AnyEvent::VERSION; -# fall back to the main API by default - backends and AnyEvent::Base -# implementations can overwrite these. +sub _reset() { + eval q{ + # 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]) -} + sub io($$$) { + AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) + } -sub timer($$$) { - AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) -} + sub timer($$$) { + AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) + } -sub signal($$) { - AnyEvent->signal (signal => $_[0], cb => $_[1]) -} + sub signal($$) { + AnyEvent->signal (signal => $_[0], cb => $_[1]) + } -sub child($$) { - AnyEvent->child (pid => $_[0], cb => $_[1]) -} + sub child($$) { + AnyEvent->child (pid => $_[0], cb => $_[1]) + } -sub idle($) { - AnyEvent->idle (cb => $_[0]) -} + sub idle($) { + AnyEvent->idle (cb => $_[0]); + } -sub cv(;&) { - AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) -} + sub cv(;&) { + AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) + } -sub now() { - AnyEvent->now -} + sub now() { + AnyEvent->now + } -sub now_update() { - AnyEvent->now_update -} + sub now_update() { + AnyEvent->now_update + } -sub time() { - AnyEvent->time + sub time() { + AnyEvent->time + } + + *postpone = \&AnyEvent::postpone; + *log = \&AnyEvent::log; + }; + die if $@; } +BEGIN { _reset } + package AnyEvent::Base; # default implementations for many methods @@ -1406,15 +1551,18 @@ 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; + AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy." + if $AnyEvent::VERBOSE >= 8; + *time = sub { Time::HiRes::time () }; + *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 + AnyEvent::log critical => "using built-in time(), WARNING, no sub-second resolution!"; + *time = sub { CORE::time }; + *AE::time = sub (){ CORE::time }; } - *time = sub { AE::time }; # different prototypes + *now = \&time; }; die if $@; @@ -1422,10 +1570,14 @@ } *now = \&time; - sub now_update { } +sub _poll { + Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; +} + # default implementation for ->condvar +# in fact, the default should not be overwritten sub condvar { eval q{ # poor man's autoloading {} @@ -1513,13 +1665,15 @@ eval q{ # poor man's autoloading {} # probe for availability of Async::Interrupt if (_have_async_interrupt) { - warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; + AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling." + if $AnyEvent::VERBOSE >= 8; $SIGPIPE_R = new Async::Interrupt::EventPipe; $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; } else { - warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; + AnyEvent::log 8 => "using emulated perl signal handling with latency timer." + if $AnyEvent::VERBOSE >= 8; if (AnyEvent::WIN32) { require AnyEvent::Util; @@ -1605,7 +1759,7 @@ while (%SIG_EV) { for (keys %SIG_EV) { delete $SIG_EV{$_}; - $_->() for values %{ $SIG_CB{$_} || {} }; + &$_ for values %{ $SIG_CB{$_} || {} }; } } }; @@ -1642,10 +1796,10 @@ *child = sub { my (undef, %arg) = @_; - defined (my $pid = $arg{pid} + 0) - or Carp::croak "required option 'pid' is missing"; + my $pid = $arg{pid}; + my $cb = $arg{cb}; - $PID_CB{$pid}{$arg{cb}} = $arg{cb}; + $PID_CB{$pid}{$cb+0} = $cb; unless ($CHLD_W) { $CHLD_W = AE::signal CHLD => \&_sigchld; @@ -1653,13 +1807,13 @@ &_sigchld; } - bless [$pid, $arg{cb}], "AnyEvent::Base::child" + bless [$pid, $cb+0], "AnyEvent::Base::child" }; *AnyEvent::Base::child::DESTROY = sub { - my ($pid, $cb) = @{$_[0]}; + my ($pid, $icb) = @{$_[0]}; - delete $PID_CB{$pid}{$cb}; + delete $PID_CB{$pid}{$icb}; delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; undef $CHLD_W unless keys %PID_CB; @@ -1682,9 +1836,9 @@ $rcb = sub { if ($cb) { - $w = _time; + $w = AE::time; &$cb; - $w = _time - $w; + $w = AE::time - $w; # never use more then 50% of the time for the idle watcher, # within some limits @@ -1742,7 +1896,7 @@ } sub _wait { - Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; + AnyEvent->_poll until $_[0]{_ae_sent}; } sub send { @@ -1764,7 +1918,7 @@ sub recv { unless ($_[0]{_ae_sent}) { $WAITING - and Carp::croak "AnyEvent::CondVar: recursive blocking wait detected"; + and Carp::croak "AnyEvent::CondVar: recursive blocking wait attempted"; local $WAITING = 1; $_[0]->_wait; @@ -1838,11 +1992,11 @@ conditions. You can set this environment variable to make AnyEvent more talkative. -When set to C<1> or higher, causes AnyEvent to warn about unexpected +When set to C<5> or higher, causes AnyEvent to warn about unexpected conditions, such as not being able to load the event model specified by C. -When set to C<2> or higher, cause AnyEvent to report to STDERR which event +When set to C<7> 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 @@ -1863,18 +2017,44 @@ C in your environment while developing programs can be very useful, however. +=item C + +If this env variable is set, then its contents will be interpreted by +C (after replacing every occurance of +C<$$> by the process pid) and an C is bound on +that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>. + +This takes place when the first watcher is created. + +For example, to bind a debug shell on a unix domain socket in +F<< /tmp/debug.sock >>, you could use this: + + PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog + +Note that creating sockets in F is very unsafe on multiuser +systems. + +=item C + +Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for +debugging purposes. See C for details. + =item C This can be used to specify the event model to be used by AnyEvent, before -auto detection and -probing kicks in. It must be a string consisting -entirely of ASCII letters. The string C gets prepended -and the resulting module name is loaded and if the load was successful, -used as event model. If it fails to load AnyEvent will proceed with +auto detection and -probing kicks in. + +It normally is a string consisting entirely of ASCII letters (e.g. C +or C). The string C gets prepended and the +resulting module name is loaded and - if the load was successful - used as +event model backend. If it fails to load then AnyEvent will proceed with auto detection and -probing. -This functionality might change in future versions. +If the string ends with C<::> instead (e.g. C) then +nothing gets prepended and the module name is used as-is (hint: C<::> at +the end of a string designates a module name and quotes it appropriately). -For example, to force the pure perl model (L) you +For example, to force the pure perl model (L) you could start your program like this: PERL_ANYEVENT_MODEL=Perl perl ... @@ -2274,7 +2454,7 @@ =item * The overhead AnyEvent adds is usually much smaller than the overhead of the actual event loop, only with extremely fast event loops such as EV -adds AnyEvent significant overhead. +does AnyEvent add significant overhead. =item * You should avoid POE like the plague if you want performance or reasonable memory usage. @@ -2583,7 +2763,7 @@ 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 of its own. The -pure-perl event loop (L) will additionally use it to +pure-perl event loop (L) will additionally load it to try to use a monotonic clock for timing stability. =back @@ -2659,17 +2839,23 @@ FAQ: L. -Utility functions: L. +Utility functions: L (misc. grab-bag), L +(simply logging). + +Development/Debugging: L (stricter checking), +L (interactive shell, watcher tracing). -Event modules: L, L, L, L, L, -L, L, L, L, L. +Supported event modules: L, L, L, +L, L, L, L, L, L, +L, L, L. Implementations: L, L, L, L, L, L, L, -L, L, L. +L, L, L, +L. -Non-blocking file handles, sockets, TCP clients and +Non-blocking handles, pipes, stream sockets, TCP clients and servers: L, L, L. Asynchronous DNS: L.