--- AnyEvent/lib/AnyEvent.pm 2008/05/10 00:57:31 1.110 +++ AnyEvent/lib/AnyEvent.pm 2008/05/25 23:52:02 1.136 @@ -1,4 +1,4 @@ -=head1 NAME +=head1 => NAME AnyEvent - provide framework for multiple event loops @@ -17,8 +17,8 @@ }); my $w = AnyEvent->condvar; # stores whether a condition was flagged - $w->wait; # enters "main loop" till $condvar gets ->send - $w->send; # wake up current and all future wait's + $w->send; # wake up current and all future recv's + $w->recv; # enters "main loop" till $condvar gets ->send =head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) @@ -59,7 +59,7 @@ In addition to being free of having to use I, AnyEvent also is free of bloat and policy: with POE or similar -modules, you get an enourmous amount of code and strict rules you have to +modules, you get an enormous amount of code and strict rules you have to follow. AnyEvent, on the other hand, is lean and up to the point, by only offering the functionality that is necessary, in as thin as a wrapper as technically possible. @@ -110,7 +110,7 @@ AnyEvent has the central concept of a I, which is an object that stores relevant data for each kind of event you are waiting for, such as -the callback to call, the filehandle to watch, etc. +the callback to call, the file handle to watch, etc. These watchers are normal Perl objects with normal Perl lifetime. After creating a watcher it will immediately "watch" for events and invoke the @@ -239,10 +239,10 @@ presence is undefined and you cannot rely on them. Portable AnyEvent callbacks cannot use arguments passed to signal watcher callbacks. -Multiple signal occurances can be clumped together into one callback -invocation, and callback invocation will be synchronous. synchronous means +Multiple signal occurrences can be clumped together into one callback +invocation, and callback invocation will be synchronous. Synchronous means that it might take a while until the signal gets handled by the process, -but it is guarenteed not to interrupt any other callbacks. +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. @@ -281,8 +281,6 @@ my $done = AnyEvent->condvar; - AnyEvent::detect; # force event module to be initialised - my $pid = fork or exit 5; my $w = AnyEvent->child ( @@ -295,7 +293,7 @@ ); # do something else, then wait for process exit - $done->wait; + $done->recv; =head2 CONDITION VARIABLES @@ -314,13 +312,15 @@ C, which specifies a callback to be called when the condition variable becomes true. -After creation, the conditon variable is "false" until it becomes "true" -by calling the C method. +After creation, the condition variable is "false" until it becomes "true" +by calling the C method (or calling the condition variable as if it +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 outstandign events have been processed. And yet -another way to call them is transations - each condition variable can be +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. @@ -328,15 +328,15 @@ for example, if you write a module that does asynchronous http requests, then a condition variable would be the ideal candidate to signal the availability of results. The user can either act when the callback is -called or can synchronously C<< ->wait >> for the results. +called or can synchronously C<< ->recv >> for the results. You can also use them to simulate traditional event loops - for example, you can block your main program until an event occurs - for example, you -could C<< ->wait >> in your main program until the user clicks the Quit +could C<< ->recv >> in your main program until the user clicks the Quit button of your app, which would C<< ->send >> the "quit" event. Note that condition variables recurse into the event loop - if you have -two pieces of code that call C<< ->wait >> in a round-robbin fashion, you +two pieces of code that call C<< ->recv >> in a round-robin fashion, you lose. Therefore, condition variables are good to export to your caller, but you should avoid making a blocking wait yourself, at least in callbacks, as this asks for trouble. @@ -351,7 +351,7 @@ eventually calls C<< -> send >>, and the "consumer side", which waits for the send to occur. -Example: +Example: wait for a timer. # wait till the result is ready my $result_ready = AnyEvent->condvar; @@ -367,7 +367,14 @@ # this "blocks" (while handling events) till the callback # calls send - $result_ready->wait; + $result_ready->recv; + +Example: wait for a timer, but take advantage of the fact that +condition variables are also code references. + + my $done = AnyEvent->condvar; + my $delay = AnyEvent->timer (after => 5, cb => $done); + $done->recv; =head3 METHODS FOR PRODUCERS @@ -380,19 +387,28 @@ =item $cv->send (...) -Flag the condition as ready - a running C<< ->wait >> and all further -calls to C will (eventually) return after this method has been +Flag the condition as ready - a running C<< ->recv >> and all further +calls to C will (eventually) return after this method has been called. If nobody is waiting the send will be remembered. If a callback has been set on the condition variable, it is called immediately from within send. Any arguments passed to the C call will be returned by all -future C<< ->wait >> calls. +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). =item $cv->croak ($error) -Similar to send, but causes all call's wait C<< ->wait >> to invoke +Similar to send, but causes all call's to C<< ->recv >> to invoke C with the given error message/object/scalar. This can be used to signal any errors to the condition variable @@ -402,6 +418,8 @@ =item $cv->end +These two methods are EXPERIMENTAL and MIGHT CHANGE. + These two methods can be used to combine many transactions/events into one. For example, a function that pings many hosts in parallel might want to use a condition variable for the whole process. @@ -445,7 +463,7 @@ This is the general pattern when you "fan out" into multiple subrequests: use an outer C/C pair to set the callback and ensure C is called at least once, and then, for each subrequest you start, call -C and for eahc subrequest you finish, call C. +C and for each subrequest you finish, call C. =back @@ -456,7 +474,7 @@ =over 4 -=item $cv->wait +=item $cv->recv Wait (blocking if necessary) until the C<< ->send >> or C<< ->croak >> methods have been called on c<$cv>, while servicing other watchers @@ -477,21 +495,21 @@ 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 suppporting blocking waits if the caller so desires). +while still supporting blocking waits if the caller so desires). -Another reason I to C<< ->wait >> in a module is that you cannot -sensibly have two C<< ->wait >>'s in parallel, as that would require +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<< ->wait >> calls perfectly safe as long as they are done from another +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<< -wait >> never blocks by setting a callback and -only calling C<< ->wait >> from within that callback (or at a later +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 waits otherwise. @@ -506,7 +524,7 @@ replaces it before doing so. The callback will be called when the condition becomes "true", i.e. when -C or C are called. Calling C inside the callback +C or C are called. Calling C inside the callback or at any later time is guaranteed not to block. =back @@ -551,15 +569,16 @@ have created an AnyEvent watcher anyway, that is, as late as possible at runtime. -=item $guard = AnyEvent::on_detect { BLOCK } +=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). If called in scalar or list context, then it creates and returns an object -that automatically removes the callback again when it is destroyed. +that automatically removes the callback again when it is destroyed. See +L for a case where this is useful. -=item @AnyEvent::on_detect +=item @AnyEvent::post_detect If there are any code references in this array (you can C to it before or after loading AnyEvent), then they will called directly after @@ -569,7 +588,7 @@ if it contains a true value then the event loop has already been detected, and the array will be ignored. -Best use C instead. +Best use C instead. =back @@ -583,14 +602,14 @@ by calling AnyEvent in your module body you force the user of your module to load the event module first. -Never call C<< ->wait >> on a condition variable unless you I that +Never call C<< ->recv >> on a condition variable unless you I that the C<< ->send >> method has been called on it already. This is because it will stall the whole program, and the whole point of using events is to stay interactive. -It is fine, however, to call C<< ->wait >> when the user of your module +It is fine, however, to call C<< ->recv >> when the user of your module requests it (i.e. if you create a http request object ad have a method -called C that returns the results, it should call C<< ->wait >> +called C that returns the results, it should call C<< ->recv >> freely, as the user of your module knows what she is doing. always). =head1 WHAT TO DO IN THE MAIN PROGRAM @@ -602,17 +621,34 @@ do anything special (it does not need to be event-based) and let AnyEvent decide which implementation to chose if some module relies on it. -If the main program relies on a specific event model. For example, in -Gtk2 programs you have to rely on the Glib module. You should load the +If the main program relies on a specific event model - for example, in +Gtk2 programs you have to rely on the Glib module - you should load the event module before loading AnyEvent or any module that uses it: generally speaking, you should load it as early as possible. The reason is that modules might create watchers when they are loaded, and AnyEvent will decide on the event model to use as soon as it creates watchers, and it might chose the wrong one unless you load the correct one yourself. -You can chose to use a rather inefficient pure-perl implementation by -loading the C module, which gives you similar -behaviour everywhere, but letting AnyEvent chose is generally better. +You can chose to use a pure-perl implementation by loading the +C module, which gives you similar behaviour +everywhere, but letting AnyEvent chose the model is generally better. + +=head2 MAINLOOP EMULATION + +Sometimes (often for short test scripts, or even standalone programs who +only want to use AnyEvent), you do not want to run a specific event loop. + +In that case, you can use a condition variable like this: + + AnyEvent->condvar->recv; + +This has the effect of entering the event loop and looping forever. + +Note that usually your program has some exit condition, in which case +it is better to use the "traditional" approach of storing a condition +variable somewhere, waiting for it, and sending it when the program should +exit cleanly. + =head1 OTHER MODULES @@ -634,16 +670,17 @@ =item L -Provides a means to do non-blocking connects, accepts etc. +Provides various utility functions for (internet protocol) sockets, +addresses and name resolution. Also functions to create non-blocking tcp +connections or tcp servers, with IPv6 and SRV record support and more. -=item L +=item L -Provides a simple web application server framework. +Provides rich asynchronous DNS resolver capabilities. -=item L +=item L -Provides asynchronous DNS resolver capabilities, beyond what -L offers. +Provides a simple web application server framework. =item L @@ -670,19 +707,20 @@ Has special support for AnyEvent via L. -=item L +=item L, L -The lambda approach to I/O - don't ask, look there. Can use AnyEvent. +Truly asynchronous I/O, should be in the toolbox of every event +programmer. AnyEvent::AIO transparently fuses IO::AIO and AnyEvent +together. -=item L +=item L, L -Truly asynchronous I/O, should be in the toolbox of every event -programmer. Can be trivially made to use AnyEvent. +Truly asynchronous Berkeley DB access. AnyEvent::AIO transparently fuses +IO::AIO and AnyEvent together. -=item L +=item L -Truly asynchronous Berkeley DB access. Can be trivially made to use -AnyEvent. +The lambda approach to I/O - don't ask, look there. Can use AnyEvent. =back @@ -695,35 +733,46 @@ use Carp; -our $VERSION = '3.4'; +our $VERSION = '4.03'; our $MODEL; our $AUTOLOAD; our @ISA; +our @REGISTRY; + our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; -our @REGISTRY; +our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred + +{ + my $idx; + $PROTOCOL{$_} = ++$idx + for reverse split /\s*,\s*/, + $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; +} my @models = ( [EV:: => AnyEvent::Impl::EV::], [Event:: => AnyEvent::Impl::Event::], - [Tk:: => AnyEvent::Impl::Tk::], - [Wx:: => AnyEvent::Impl::POE::], - [Prima:: => AnyEvent::Impl::POE::], [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], - # everything below here will not be autoprobed as the pureperl backend should work everywhere - [Glib:: => AnyEvent::Impl::Glib::], + # 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 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza + [Wx:: => AnyEvent::Impl::POE::], + [Prima:: => AnyEvent::Impl::POE::], ); our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); -our @on_detect; +our @post_detect; -sub on_detect(&) { +sub post_detect(&) { my ($cb) = @_; if ($MODEL) { @@ -731,16 +780,16 @@ 1 } else { - push @on_detect, $cb; + push @post_detect, $cb; defined wantarray - ? bless \$cb, "AnyEvent::Util::Guard" + ? bless \$cb, "AnyEvent::Util::PostDetect" : () } } -sub AnyEvent::Util::Guard::DESTROY { - @on_detect = grep $_ != ${$_[0]}, @on_detect; +sub AnyEvent::Util::PostDetect::DESTROY { + @post_detect = grep $_ != ${$_[0]}, @post_detect; } sub detect() { @@ -792,7 +841,7 @@ unshift @ISA, $MODEL; push @{"$MODEL\::ISA"}, "AnyEvent::Base"; - (shift @on_detect)->() while @on_detect; + (shift @post_detect)->() while @post_detect; } $MODEL @@ -812,18 +861,10 @@ package AnyEvent::Base; -# default implementation for ->condvar, ->wait, ->broadcast +# default implementation for ->condvar sub condvar { - bless \my $flag, "AnyEvent::Base::CondVar" -} - -sub AnyEvent::Base::CondVar::broadcast { - ${$_[0]}++; -} - -sub AnyEvent::Base::CondVar::wait { - AnyEvent->one_event while !${$_[0]}; + bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: } # default implementation for ->signal @@ -907,6 +948,66 @@ undef $CHLD_W unless keys %PID_CB; } +package AnyEvent::CondVar; + +our @ISA = AnyEvent::CondVar::Base::; + +package AnyEvent::CondVar::Base; + +use overload + '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, + fallback => 1; + +sub _send { + # nop +} + +sub send { + my $cv = shift; + $cv->{_ae_sent} = [@_]; + (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb}; + $cv->_send; +} + +sub croak { + $_[0]{_ae_croak} = $_[1]; + $_[0]->send; +} + +sub ready { + $_[0]{_ae_sent} +} + +sub _wait { + AnyEvent->one_event while !$_[0]{_ae_sent}; +} + +sub recv { + $_[0]->_wait; + + Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; + wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] +} + +sub cb { + $_[0]{_ae_cb} = $_[1] if @_ > 1; + $_[0]{_ae_cb} +} + +sub begin { + ++$_[0]{_ae_counter}; + $_[0]{_ae_end_cb} = $_[1] if @_ > 1; +} + +sub end { + return if --$_[0]{_ae_counter}; + &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; +} + +# undocumented/compatibility with pre-3.4 +*broadcast = \&send; +*wait = \&_wait; + =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE This is an advanced topic that you do not normally need to use AnyEvent in @@ -972,11 +1073,11 @@ =item C This can be used to specify the event model to be used by AnyEvent, before -autodetection and -probing kicks in. It must be a string consisting +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 -autodetection and -probing. +auto detection and -probing. This functionality might change in future versions. @@ -985,6 +1086,37 @@ PERL_ANYEVENT_MODEL=Perl perl ... +=item C + +Used by both L and L to determine preferences +for IPv4 or IPv6. The default is unspecified (and might change, or be the result +of auto probing). + +Must be set to a comma-separated list of protocols or address families, +current supported: C and C. Only protocols mentioned will be +used, and preference will be given to protocols mentioned earlier in the +list. + +This variable can effectively be used for denial-of-service attacks +against local programs (e.g. when setuid), although the impact is likely +small, as the program has to handle connection errors already- + +Examples: C - prefer IPv4 over IPv6, +but support both and try to use both. C +- only support IPv4, never try to resolve or contact IPv6 +addresses. C support either IPv4 or +IPv6, but prefer IPv6 over IPv4. + +=item C + +Used by L to decide whether to use the EDNS0 extension +for DNS. This extension is generally useful to reduce DNS traffic, but +some (broken) firewalls drop such DNS packets, which is why it is off by +default. + +Setting this variable to C<1> will cause L to announce +EDNS0 in its DNS requests. + =back =head1 EXAMPLE PROGRAM @@ -1004,7 +1136,7 @@ warn "io event <$_[0]>\n"; # will always output chomp (my $input = ); # read a line warn "read: $input\n"; # output what has been read - $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i + $cv->send if $input =~ /^q/i; # quit program if /^q/i }, ); @@ -1019,7 +1151,7 @@ new_timer; # create first timer - $cv->wait; # wait until user enters /^q/i + $cv->recv; # wait until user enters /^q/i =head1 REAL-WORLD EXAMPLE @@ -1079,13 +1211,13 @@ $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); Again, C waits till all data has arrived, and then stores the -result and signals any possible waiters that the request ahs finished: +result and signals any possible waiters that the request has finished: sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; if (end-of-file or data complete) { $txn->{result} = $txn->{buf}; - $txn->{finished}->broadcast; + $txn->{finished}->send; $txb->{cb}->($txn) of $txn->{cb}; # also call callback } @@ -1093,11 +1225,11 @@ request was already finished, it doesn't wait, of course, and returns the data: - $txn->{finished}->wait; + $txn->{finished}->recv; return $txn->{result}; The actual code goes further and collects all errors (Cs, exceptions) -that occured during request processing. The C method detects +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 @@ -1138,10 +1270,10 @@ $fcp->txn_client_get ($url)->cb (sub { ... - $quit->broadcast; + $quit->send; }); - $quit->wait; + $quit->recv; =head1 BENCHMARKS @@ -1153,7 +1285,7 @@ =head2 BENCHMARKING ANYEVENT OVERHEAD Here is a benchmark of various supported event models used natively and -through anyevent. The benchmark creates a lot of timers (with a zero +through AnyEvent. The benchmark creates a lot of timers (with a zero timeout) and I/O watchers (watching STDOUT, a pty, to become writable, which it is), lets them fire exactly once and destroys them again. @@ -1180,7 +1312,7 @@ I is the time, in microseconds, used to invoke a simple callback. The callback simply counts down a Perl variable and after it was -invoked "watcher" times, it would C<< ->broadcast >> a condvar once to +invoked "watcher" times, it would C<< ->send >> a condvar once to signal the end of this phase. I is the time, in microseconds, that it takes to destroy a single @@ -1286,19 +1418,19 @@ =head2 BENCHMARKING THE LARGE SERVER CASE -This benchmark atcually benchmarks the event loop itself. It works by -creating a number of "servers": each server consists of a socketpair, a +This benchmark actually benchmarks the event loop itself. It works by +creating a number of "servers": each server consists of a socket pair, a timeout watcher that gets reset on activity (but never fires), and an I/O watcher waiting for input on one side of the socket. Each time the socket watcher reads a byte it will write that byte to a random other "server". The effect is that there will be a lot of I/O watchers, only part of which are active at any one point (so there is a constant number of active -fds for each loop iterstaion, but which fds these are is random). The +fds for each loop iteration, but which fds these are is random). The timeout is reset each time something is read because that reflects how most timeouts work (and puts extra pressure on the event loops). -In this benchmark, we use 10000 socketpairs (20000 sockets), of which 100 +In this benchmark, we use 10000 socket pairs (20000 sockets), of which 100 (1%) are active. This mirrors the activity of large servers with many connections, most of which are idle at any one point in time. @@ -1310,7 +1442,7 @@ I is the number of sockets, and twice the number of "servers" (as each server has a read and write socket end). -I is the time it takes to create a socketpair (which is +I is the time it takes to create a socket pair (which is nontrivial) and two watchers: an I/O watcher and a timeout watcher. I, the most important value, is the time it takes to handle a @@ -1393,7 +1525,7 @@ EV is again fastest. -Perl again comes second. It is noticably faster than the C-based event +Perl again comes second. It is noticeably faster than the C-based event loops Event and Glib, although the difference is too small to really matter. @@ -1443,6 +1575,8 @@ =head1 SEE ALSO +Utility functions: L. + Event modules: L, L, L, L, L, L, L, L, L, L. @@ -1451,9 +1585,14 @@ L, L, L. +Non-blocking file handles, sockets, TCP clients and +servers: L, L. + +Asynchronous DNS: L. + Coroutine support: L, L, L, L, -Nontrivial usage examples: L, L. +Nontrivial usage examples: L, L, L. =head1 AUTHOR