--- AnyEvent/lib/AnyEvent.pm 2009/07/30 03:41:56 1.266 +++ AnyEvent/lib/AnyEvent.pm 2009/08/09 00:24:35 1.275 @@ -401,18 +401,22 @@ =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 +callbacks to signals in a generic way, which is a pity, as you cannot +do race-free signal handling in perl, requiring C libraries for +this. AnyEvent will try to do it's best, which means 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. This variable determines how often +AnyEvent polls for signals (in case a wake-up was missed). 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. +saving. + +All these problems can be avoided by installing the optional +L module, which works with most event loops. It 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). For those, you just have to suffer the delays. =head2 CHILD PROCESS WATCHERS @@ -788,10 +792,10 @@ This is a mutator function that returns the callback set and optionally replaces it before doing so. -The callback will be called when the condition becomes "true", i.e. when -C or C are called, with the only argument being the condition -variable itself. Calling C inside the callback or at any later time -is guaranteed not to block. +The callback will be called when the condition becomes (or already was) +"true", i.e. when C or C are called (or were called), with +the only argument being the condition variable itself. Calling C +inside the callback or at any later time is guaranteed not to block. =back @@ -1113,7 +1117,7 @@ use Carp (); -our $VERSION = 4.881; +our $VERSION = 4.92; our $MODEL; our $AUTOLOAD; @@ -1285,6 +1289,50 @@ ($fh2, $rw) } +############################################################################# +# "new" API, currently only emulation of it +############################################################################# + +package AE; + +our $VERSION = $AnyEvent::VERSION; + +sub io($$$) { + AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) +} + +sub timer($$$) { + AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]); +} + +sub signal($$) { + AnyEvent->signal (signal => $_[0], cb => $_[1]); +} + +sub child($$) { + AnyEvent->child (pid => $_[0], cb => $_[1]); +} + +sub idle($) { + AnyEvent->idle (cb => $_[0]); +} + +sub cv(;&) { + AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) +} + +sub now() { + AnyEvent->now +} + +sub now_update() { + AnyEvent->now_update +} + +sub time() { + AnyEvent->time +} + package AnyEvent::Base; # default implementations for many methods @@ -1346,13 +1394,13 @@ sub _sig_add() { unless ($SIG_COUNT++) { # try to align timer on a full-second boundary, if possible - my $NOW = AnyEvent->now; + my $NOW = AE::now; - $SIG_TW = AnyEvent->timer ( - after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), - interval => $MAX_SIGNAL_LATENCY, - cb => sub { }, # just for the PERL_ASYNC_CHECK - ); + $SIG_TW = AE::timer + $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), + $MAX_SIGNAL_LATENCY, + sub { } # just for the PERL_ASYNC_CHECK + ; } } @@ -1399,7 +1447,7 @@ warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; $SIGPIPE_R = new Async::Interrupt::EventPipe; - $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); + $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; @@ -1425,7 +1473,7 @@ $SIGPIPE_R or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; - $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); + $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; } *signal = sub { @@ -1524,7 +1572,7 @@ : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; unless ($CHLD_W) { - $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); + $CHLD_W = AE::signal CHLD => \&_sigchld; # child could be a zombie already, so make at least one round &_sigchld; } @@ -1560,7 +1608,7 @@ $w = 0.0001 if $w < 0.0001; $w = 5 if $w > 5; - $w = AnyEvent->timer (after => $w, cb => $rcb); + $w = AE::timer $w, 0, $rcb; } else { # clean up... undef $w; @@ -1568,7 +1616,7 @@ } }; - $w = AnyEvent->timer (after => 0.05, cb => $rcb); + $w = AE::timer 0.05, 0, $rcb; bless \\$cb, "AnyEvent::Base::idle" } @@ -1632,8 +1680,14 @@ } sub cb { - $_[0]{_ae_cb} = $_[1] if @_ > 1; - $_[0]{_ae_cb} + my $cv = shift; + + @_ + and $cv->{_ae_cb} = shift + and $cv->{_ae_sent} + and (delete $cv->{_ae_cb})->($cv); + + $cv->{_ae_cb} } sub begin { @@ -1650,48 +1704,6 @@ *broadcast = \&send; *wait = \&_wait; -############################################################################# -# "new" API, currently only emulation of it -############################################################################# - -package AE; - -sub io($$$) { - AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) -} - -sub timer($$$) { - AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]); -} - -sub signal($$) { - AnyEvent->signal (signal => $_[0], cb => $_[1]); -} - -sub child($$) { - AnyEvent->child (pid => $_[0], cb => $_[1]); -} - -sub idle($) { - AnyEvent->idle (cb => $_[0]); -} - -sub cv() { - AnyEvent->condvar -} - -sub now() { - AnyEvent->now -} - -sub now_update() { - AnyEvent->now_update -} - -sub time() { - AnyEvent->time -} - =head1 ERROR AND EXCEPTION HANDLING In general, AnyEvent does not do any error handling - it relies on the