… | |
… | |
109 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
109 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
110 | warn "timeout\n"; |
110 | warn "timeout\n"; |
111 | }); |
111 | }); |
112 | |
112 | |
113 | # to cancel the timer: |
113 | # to cancel the timer: |
114 | undef $w |
114 | undef $w; |
115 | |
115 | |
116 | =head2 CONDITION WATCHERS |
116 | =head2 CONDITION WATCHERS |
117 | |
117 | |
118 | Condition watchers can be created by calling the C<< AnyEvent->condvar >> |
118 | Condition watchers can be created by calling the C<< AnyEvent->condvar >> |
119 | method without any arguments. |
119 | method without any arguments. |
… | |
… | |
199 | |
199 | |
200 | The known classes so far are: |
200 | The known classes so far are: |
201 | |
201 | |
202 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
202 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
203 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
203 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
204 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
204 | AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. |
205 | AnyEvent::Impl::Event based on Event, also second best choice :) |
205 | AnyEvent::Impl::Event based on Event, also second best choice :) |
206 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
206 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
207 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
207 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
208 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
208 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
209 | |
209 | |
… | |
… | |
252 | no warnings; |
252 | no warnings; |
253 | use strict; |
253 | use strict; |
254 | |
254 | |
255 | use Carp; |
255 | use Carp; |
256 | |
256 | |
257 | our $VERSION = '2.6'; |
257 | our $VERSION = '2.9'; |
258 | our $MODEL; |
258 | our $MODEL; |
259 | |
259 | |
260 | our $AUTOLOAD; |
260 | our $AUTOLOAD; |
261 | our @ISA; |
261 | our @ISA; |
262 | |
262 | |
… | |
… | |
265 | our @REGISTRY; |
265 | our @REGISTRY; |
266 | |
266 | |
267 | my @models = ( |
267 | my @models = ( |
268 | [Coro::EV:: => AnyEvent::Impl::CoroEV::], |
268 | [Coro::EV:: => AnyEvent::Impl::CoroEV::], |
269 | [EV:: => AnyEvent::Impl::EV::], |
269 | [EV:: => AnyEvent::Impl::EV::], |
270 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
270 | [Coro::Event:: => AnyEvent::Impl::CoroEvent::], |
271 | [Event:: => AnyEvent::Impl::Event::], |
271 | [Event:: => AnyEvent::Impl::Event::], |
272 | [Glib:: => AnyEvent::Impl::Glib::], |
272 | [Glib:: => AnyEvent::Impl::Glib::], |
273 | [Tk:: => AnyEvent::Impl::Tk::], |
273 | [Tk:: => AnyEvent::Impl::Tk::], |
274 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
274 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
275 | ); |
275 | ); |
… | |
… | |
373 | |
373 | |
374 | # default implementation for ->child |
374 | # default implementation for ->child |
375 | |
375 | |
376 | our %PID_CB; |
376 | our %PID_CB; |
377 | our $CHLD_W; |
377 | our $CHLD_W; |
|
|
378 | our $CHLD_DELAY_W; |
378 | our $PID_IDLE; |
379 | our $PID_IDLE; |
379 | our $WNOHANG; |
380 | our $WNOHANG; |
380 | |
381 | |
381 | sub _child_wait { |
382 | sub _child_wait { |
382 | while (0 <= (my $pid = waitpid -1, $WNOHANG)) { |
383 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
383 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
384 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
384 | (values %{ $PID_CB{0} || {} }); |
385 | (values %{ $PID_CB{0} || {} }); |
385 | } |
386 | } |
386 | |
387 | |
387 | undef $PID_IDLE; |
388 | undef $PID_IDLE; |
388 | } |
389 | } |
389 | |
390 | |
|
|
391 | sub _sigchld { |
|
|
392 | # make sure we deliver these changes "synchronous" with the event loop. |
|
|
393 | $CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub { |
|
|
394 | undef $CHLD_DELAY_W; |
|
|
395 | &_child_wait; |
|
|
396 | }); |
|
|
397 | } |
|
|
398 | |
390 | sub child { |
399 | sub child { |
391 | my (undef, %arg) = @_; |
400 | my (undef, %arg) = @_; |
392 | |
401 | |
393 | defined (my $pid = $arg{pid} + 0) |
402 | defined (my $pid = $arg{pid} + 0) |
394 | or Carp::croak "required option 'pid' is missing"; |
403 | or Carp::croak "required option 'pid' is missing"; |
… | |
… | |
398 | unless ($WNOHANG) { |
407 | unless ($WNOHANG) { |
399 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
408 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
400 | } |
409 | } |
401 | |
410 | |
402 | unless ($CHLD_W) { |
411 | unless ($CHLD_W) { |
403 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait); |
412 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
404 | # child could be a zombie already |
413 | # child could be a zombie already, so make at least one round |
405 | $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); |
414 | &_sigchld; |
406 | } |
415 | } |
407 | |
416 | |
408 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
417 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
409 | } |
418 | } |
410 | |
419 | |