| 1 | package Parallel::Pipes; | ||||||||||||||
| 2 | 34 | ▶ | use 5.008001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 34 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 34 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 34 | ▶ | use IO::Handle; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 34 | ▶ | use IO::Select; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 34 | ▶ | use constant WIN32 => $^O eq 'MSWin32'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 34 | our $VERSION = '0.200'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | { | ||||||||||||||
| 13 | package Parallel::Pipes::Impl; | ||||||||||||||
| 14 | 34 | ▶ | use Storable (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | sub new { | ||||||||||||||
| 16 | 164 | ▶ | my ($class, %option) = @_; | ||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
| |||||||||||||||
| 17 | 164 | ▶ | my $read_fh = delete $option{read_fh} or die; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 18 | 164 | ▶ | my $write_fh = delete $option{write_fh} or die; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 19 | 164 | $write_fh->autoflush(1); | |||||||||||||
| 20 | 164 | bless { %option, read_fh => $read_fh, write_fh => $write_fh, buf => '' }, $class; | |||||||||||||
| 21 | } | ||||||||||||||
| 22 | sub read :method { | ||||||||||||||
| 23 | 433 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
read: called CC=2 SCAR=6.9
| |||||||||||||||
| 24 | 433 | ▶ | my $_size = $self->_read(4) or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 404 | my $size = unpack 'I', $_size; | |||||||||||||
| 26 | 404 | my $freezed = $self->_read($size); | |||||||||||||
| 27 | 404 | Storable::thaw($freezed); | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | sub write :method { | ||||||||||||||
| 30 | 404 | ▶ | my ($self, $data) = @_; | ||||||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 404 | my $freezed = Storable::freeze({data => $data}); | |||||||||||||
| 32 | 404 | my $size = pack 'I', length($freezed); | |||||||||||||
| 33 | 404 | $self->_write("$size$freezed"); | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | sub _read { | ||||||||||||||
| 36 | 837 | ▶ | my ($self, $size) = @_; | ||||||||||||
|
Subroutine
_read: called CC=4 SCAR=13.9
| |||||||||||||||
| 37 | 837 | my $fh = $self->{read_fh}; | |||||||||||||
| 38 | 837 | my $offset = length $self->{buf}; | |||||||||||||
| 39 | 837 | ▶ | while ($offset < $size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 436 | my $len = sysread $fh, $self->{buf}, 65536, $offset; | |||||||||||||
| 41 | 436 | ▶ | if (!defined $len) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | 0 | die $!; | |||||||||||||
| 43 | } elsif ($len == 0) { | ||||||||||||||
| 44 | 29 | last; | |||||||||||||
| 45 | } else { | ||||||||||||||
| 46 | 407 | $offset += $len; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | } | ||||||||||||||
| 49 | 837 | return substr $self->{buf}, 0, $size, ''; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | sub _write { | ||||||||||||||
| 52 | 404 | ▶ | my ($self, $data) = @_; | ||||||||||||
|
Subroutine
_write: called CC=4 SCAR=14.3
| |||||||||||||||
| 53 | 404 | my $fh = $self->{write_fh}; | |||||||||||||
| 54 | 404 | my $size = length $data; | |||||||||||||
| 55 | 404 | my $offset = 0; | |||||||||||||
| 56 | 404 | ▶ | while ($size) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 404 | my $len = syswrite $fh, $data, $size, $offset; | |||||||||||||
| 58 | 404 | ▶ | if (!defined $len) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 59 | 0 | die $!; | |||||||||||||
| 60 | } elsif ($len == 0) { | ||||||||||||||
| 61 | 0 | last; | |||||||||||||
| 62 | } else { | ||||||||||||||
| 63 | 404 | $size -= $len; | |||||||||||||
| 64 | 404 | $offset += $len; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | } | ||||||||||||||
| 67 | 404 | $size; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | { | ||||||||||||||
| 71 | 34 | package Parallel::Pipes::Here; | |||||||||||||
| 72 | 34 | our @ISA = qw(Parallel::Pipes::Impl); | |||||||||||||
| 73 | 34 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 74 | sub new { | ||||||||||||||
| 75 | 135 | ▶ | my ($class, %option) = @_; | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||
| 76 | 135 | $class->SUPER::new(%option, _written => 0); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | sub is_written { | ||||||||||||||
| 79 | 1258 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
is_written: called CC=1 SCAR=0.0
| |||||||||||||||
| 80 | 1258 | $self->{_written} == 1; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | sub read :method { | ||||||||||||||
| 83 | 292 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
read: called CC=3 SCAR=11.8
| |||||||||||||||
| 84 | 292 | ▶ | if (!$self->is_written) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 0 | Carp::croak("This pipe has not been written; you cannot read it"); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | 292 | $self->{_written}--; | |||||||||||||
| 88 | 292 | ▶ | return unless my $read = $self->SUPER::read; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | 292 | $read->{data}; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | sub write :method { | ||||||||||||||
| 92 | 292 | ▶ | my ($self, $task) = @_; | ||||||||||||
|
Subroutine
write: called CC=2 SCAR=7.4
| |||||||||||||||
| 93 | 292 | ▶ | if ($self->is_written) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 0 | Carp::croak("This pipe has already been written; you must read it first"); | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | 292 | $self->{_written}++; | |||||||||||||
| 97 | 292 | $self->SUPER::write($task); | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | } | ||||||||||||||
| 100 | { | ||||||||||||||
| 101 | 34 | package Parallel::Pipes::There; | |||||||||||||
| 102 | 34 | our @ISA = qw(Parallel::Pipes::Impl); | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | { | ||||||||||||||
| 105 | 34 | package Parallel::Pipes::Impl::NoFork; | |||||||||||||
| 106 | 34 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 107 | sub new { | ||||||||||||||
| 108 | 24 | ▶ | my ($class, %option) = @_; | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||
| 109 | 24 | bless {%option}, $class; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | sub is_written { | ||||||||||||||
| 112 | 2172 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
is_written: called CC=1 SCAR=0.0
| |||||||||||||||
| 113 | 2172 | exists $self->{_result}; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | sub read :method { | ||||||||||||||
| 116 | 714 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
read: called CC=2 SCAR=7.6
| |||||||||||||||
| 117 | 714 | ▶ | if (!$self->is_written) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 0 | Carp::croak("This pipe has not been written; you cannot read it"); | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | 714 | delete $self->{_result}; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | sub write :method { | ||||||||||||||
| 123 | 714 | ▶ | my ($self, $task) = @_; | ||||||||||||
|
Subroutine
write: called CC=2 SCAR=7.4
| |||||||||||||||
| 124 | 714 | ▶ | if ($self->is_written) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 125 | 0 | Carp::croak("This pipe has already been written; you must read it first"); | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | 714 | my $result = $self->{work}->($task); | |||||||||||||
| 128 | 714 | $self->{_result} = $result; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 34 | sub new { | |||||||||||||
| 133 | 69 | ▶ | my ($class, $number, $work, $option) = @_; | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 134 | 69 | if (WIN32 and $number != 1) { | |||||||||||||
| 135 | die "The number of pipes must be 1 under WIN32 environment.\n"; | ||||||||||||||
| 136 | } | ||||||||||||||
| 137 | 69 | ▶ | my $self = bless { | ||||||||||||
|
Condition100%2/2
$option || {}
MC/DC100%1/1
$option || {}
$option
Truth table100%2/2
A
A= $option
| |||||||||||||||
| 138 | work => $work, | ||||||||||||||
| 139 | number => $number, | ||||||||||||||
| 140 | no_fork => $number == 1, | ||||||||||||||
| 141 | pipes => {}, | ||||||||||||||
| 142 | option => $option || {}, | ||||||||||||||
| 143 | }, $class; | ||||||||||||||
| 144 | |||||||||||||||
| 145 | 69 | ▶ | if ($self->no_fork) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | 24 | $self->{pipes}{-1} = Parallel::Pipes::Impl::NoFork->new(pid => -1, work => $self->{work}); | |||||||||||||
| 147 | } else { | ||||||||||||||
| 148 | 45 | $self->_fork for 1 .. $number; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | 40 | $self; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 1118 | ▶ | sub no_fork { shift->{no_fork} } | ||||||||||||
|
Subroutine
no_fork: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 154 | |||||||||||||||
| 155 | sub _fork { | ||||||||||||||
| 156 | 164 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_fork: called CC=6 SCAR=17.9
| |||||||||||||||
| 157 | 164 | my $work = $self->{work}; | |||||||||||||
| 158 | 164 | pipe my $read_fh1, my $write_fh1; | |||||||||||||
| 159 | 164 | pipe my $read_fh2, my $write_fh2; | |||||||||||||
| 160 | 164 | my $pid = fork; | |||||||||||||
| 161 | 164 | ▶ | die "fork failed" unless defined $pid; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | 164 | ▶ | if ($pid == 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | 29 | srand; | |||||||||||||
| 164 | 29 | close $_ for $read_fh1, $write_fh2, map { ($_->{read_fh}, $_->{write_fh}) } $self->pipes; | |||||||||||||
| 165 | 29 | my $there = Parallel::Pipes::There->new(read_fh => $read_fh2, write_fh => $write_fh1); | |||||||||||||
| 166 | 29 | ▶ | while (my $read = $there->read) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | 112 | $there->write( $work->($read->{data}) ); | |||||||||||||
| 168 | } | ||||||||||||||
| 169 | 29 | exit; | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | 135 | close $_ for $write_fh1, $read_fh2; | |||||||||||||
| 172 | 135 | $self->{pipes}{$pid} = Parallel::Pipes::Here->new( | |||||||||||||
| 173 | pid => $pid, read_fh => $read_fh1, write_fh => $write_fh2, | ||||||||||||||
| 174 | ); | ||||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | sub pipes { | ||||||||||||||
| 178 | 1075 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
pipes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 179 | 1075 | map { $self->{pipes}{$_} } sort { $a <=> $b } keys %{$self->{pipes}}; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | sub is_ready { | ||||||||||||||
| 183 | 1010 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
is_ready: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||
| 184 | 1010 | ▶ | return $self->pipes if $self->no_fork; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 185 | |||||||||||||||
| 186 | 272 | ▶ | my @pipes = @_ ? @_ : $self->pipes; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 187 | 272 | ▶ | if (my @ready = grep { $_->{_written} == 0 } @pipes) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 188 | 59 | return @ready; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | 213 | my $select = IO::Select->new(map { $_->{read_fh} } @pipes); | |||||||||||||
| 192 | 213 | my @ready; | |||||||||||||
| 193 | 213 | ▶ | if (my $tick = $self->{option}{idle_tick}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 194 | 22 | while (1) { | |||||||||||||
| 195 | 36 | ▶ | if (my @r = $select->can_read($tick)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 196 | 22 | @ready = @r, last; | |||||||||||||
| 197 | } | ||||||||||||||
| 198 | 14 | $self->{option}{idle_work}->(); | |||||||||||||
| 199 | } | ||||||||||||||
| 200 | } else { | ||||||||||||||
| 201 | 191 | @ready = $select->can_read; | |||||||||||||
| 202 | } | ||||||||||||||
| 203 | |||||||||||||||
| 204 | 213 | my @return; | |||||||||||||
| 205 | 213 | for my $pipe (@pipes) { | |||||||||||||
| 206 | 969 | ▶ | if (grep { $pipe->{read_fh} == $_ } @ready) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | 288 | push @return, $pipe; | |||||||||||||
| 208 | } | ||||||||||||||
| 209 | } | ||||||||||||||
| 210 | 213 | return @return; | |||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | sub is_written { | ||||||||||||||
| 214 | 67 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
is_written: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 215 | 67 | grep { $_->is_written } $self->pipes; | |||||||||||||
| 216 | } | ||||||||||||||
| 217 | |||||||||||||||
| 218 | sub close :method { | ||||||||||||||
| 219 | 39 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
close: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||
| 220 | 39 | ▶ | return if $self->no_fork; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 221 | |||||||||||||||
| 222 | 15 | close $_ for map { ($_->{write_fh}, $_->{read_fh}) } $self->pipes; | |||||||||||||
| 223 | 15 | ▶ | while (%{$self->{pipes}}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 224 | 75 | my $pid = wait; | |||||||||||||
| 225 | 75 | ▶ | if (delete $self->{pipes}{$pid}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 226 | # OK | ||||||||||||||
| 227 | } else { | ||||||||||||||
| 228 | 0 | warn "wait() unexpectedly returns $pid\n"; | |||||||||||||
| 229 | } | ||||||||||||||
| 230 | } | ||||||||||||||
| 231 | } | ||||||||||||||
| 232 | |||||||||||||||
| 233 | 34 | 1; | |||||||||||||
| 234 | __END__ | ||||||||||||||