AnyEvent

 view release on metacpan or  search on metacpan

lib/AnyEvent/Impl/IOAsync.pm  view on Meta::CPAN

   my $cb = $arg{cb};

   my $id;

   if (my $ival = $arg{interval}) {
      my $ival_cb; $ival_cb = sub {
         $id = $LOOP->enqueue_timer (delay => $ival, code => $ival_cb);
         &$cb;
      };
      $id = $LOOP->enqueue_timer (delay => $arg{after}, code => $ival_cb);

      # we have to weaken afterwards, but when enqueue dies, we have a memleak.
      # still, we do anything for speed...
      Scalar::Util::weaken $ival_cb;

   } else {
      # IO::Async has problems with overloaded objects
      $id = $LOOP->enqueue_timer (delay => $arg{after}, code => sub {
         undef $id; # IO::Async <= 0.43 bug workaround
         &$cb;
      });
   }

   bless \\$id, "AnyEvent::Impl::IOAsync::timer"
}

sub AnyEvent::Impl::IOAsync::timer::DESTROY {
   # Need to be well-behaved during global destruction
   $LOOP->cancel_timer (${${$_[0]}})
      if defined ${${$_[0]}}; # IO::Async <= 0.43 bug workaround
}

sub io {
   my ($class, %arg) = @_;

   # Ensure we have a real IO handle, and not just a UNIX fd integer
   my ($fh) = AnyEvent::_dupfh $arg{poll}, $arg{fh};

   my $event = $arg{poll} eq "r" ? "on_read_ready" : "on_write_ready";

   $LOOP->watch_io (
      handle => $fh,
      $event => $arg{cb},
   );

   bless [$fh, $event], "AnyEvent::Impl::IOAsync::io"
}

sub AnyEvent::Impl::IOAsync::io::DESTROY {
   $LOOP->unwatch_io (
      handle => $_[0][0],
      $_[0][1] => 1,
   );
}

sub signal {
   my ($class, %arg) = @_;

   my $signal = $arg{signal};

   my $id = $LOOP->attach_signal ($arg{signal}, $arg{cb});
   bless [$signal, $id], "AnyEvent::Impl::IOAsync::signal"
}

sub AnyEvent::Impl::IOAsync::signal::DESTROY {
   $LOOP->detach_signal (@{ $_[0] });
}

our %pid_cb;

sub child {
   my ($class, %arg) = @_;

   my $pid = $arg{pid};

   $LOOP->watch_child ($pid, $arg{cb});
   bless [$pid], "AnyEvent::Impl::IOAsync::child"
}

sub child {
   my ($class, %arg) = @_;

   my $pid = $arg{pid};
   my $cb  = $arg{cb};

   unless (%{ $pid_cb{$pid} }) {
      $LOOP->watch_child ($pid, sub {
         $_->($_[0], $_[1])
            for values %{ $pid_cb{$pid} };
      });
   }

   $pid_cb{$pid}{$cb+0} = $cb;

   bless [$pid, $cb+0], "AnyEvent::Impl::IOAsync::child"
}

sub AnyEvent::Impl::IOAsync::child::DESTROY {
   my ($pid, $icb) = @{ $_[0] };

   delete $pid_cb{$pid}{$icb};

   unless (%{ $pid_cb{$pid} }) {
      delete $pid_cb{$pid};
      $LOOP->unwatch_child ($pid);
   }
}

#sub loop {
#   $LOOP->loop_forever;
#}

sub _poll {
   $LOOP->loop_once;
}

sub AnyEvent::CondVar::Base::_wait {
   $LOOP->loop_once until exists $_[0]{_ae_sent};
}

=head1 SEE ALSO



( run in 0.478 second using v1.01-cache-2.11-cpan-df04353d9ac )