Pipeline::Segment::Async::IThreads - ithread model for asynchronous pipeline segments


Pipeline documentation Contained in the Pipeline distribution.

Index


Code Index:

NAME

Top

Pipeline::Segment::Async::IThreads - ithread model for asynchronous pipeline segments

DESCRIPTION

Top

Pipeline::Segment::Async::IThreads provides asynchronous segments under Perl's ithreads model.

SEE ALSO

Top

Pipeline::Segment::Async, Pipeline::Segment::Async::Handler, Pipeline::Segment::Async::Fork

AUTHOR

Top

James A. Duncan <jduncan@fotango.com>

COPYRIGHT

Top


Pipeline documentation Contained in the Pipeline distribution.

package Pipeline::Segment::Async::IThreads;

use strict;
use warnings;
our $VERSION = "3.12";

BEGIN {
  use Config;
  if ($Config{useithreads}) {
    $Pipeline::Segment::Async::IThreads::AVAILABLE = 1;
    require threads;
    threads->import;
  } else {
    $Pipeline::Segment::Async::IThreads::AVAILABLE = 0;
  }
}

use Config;
use Pipeline::Segment::Async::Handler;
use base qw( Pipeline::Segment::Async::Handler );

sub canop {
  my $self = shift;
  $Pipeline::Segment::Async::IThreads::AVAILABLE
}

sub run {
  my $self = shift;
  my $sub  = shift;
  my @args = @_;
  $self->thread( threads->create( $sub, @args ) );
}

sub thread {
  my $self   = shift;
  my $thread = shift;
  if (defined( $thread )) {
    $self->{ thread } = $thread;
    return $self;
  } else {
    return $self->{ thread };
  }
}

sub reattach {
  my $self = shift;
  return $self->thread->join();
}

sub discard {
  my $self = shift;
  $self->thread->detach();
}

1;

__END__