| 1 | package Parallel::SubArray; | ||||||||||||||||||||||||||||
| 2 | 15 | require v5.8.6; | |||||||||||||||||||||||||||
| 3 | 15 | our $VERSION = 0.6; | |||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use Storable qw(store_fd fd_retrieve); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | our @EXPORT_OK = qw(par); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 21 | ▶ | sub par { | ||||||||||||||||||||||||||
|
Subroutine
par: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 21 | my( $timeout ) = @_; | |||||||||||||||||||||||||||
| 11 | 29 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 12 | 29 | my @subs = @{ shift(@_) }; | |||||||||||||||||||||||||||
| 13 | 29 | my %rets; | |||||||||||||||||||||||||||
| 14 | 29 | my $c = 0; | |||||||||||||||||||||||||||
| 15 | 29 | for my $sub ( @subs ) { | |||||||||||||||||||||||||||
| 16 | 105 | $c++; | |||||||||||||||||||||||||||
| 17 | 105 | my( $parent_w, $child_r ); | |||||||||||||||||||||||||||
| 18 | 105 | pipe( $child_r, $parent_w ); | |||||||||||||||||||||||||||
| 19 | 105 | select((select($parent_w ), $| = 1)[0]); | |||||||||||||||||||||||||||
| 20 | 105 | ▶ | if( my $pid = fork ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 87 | close $parent_w; | |||||||||||||||||||||||||||
| 22 | 87 | $rets{ $pid } = { fd => $child_r, | |||||||||||||||||||||||||||
| 23 | ord => $c | ||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||
| 26 | 18 | ▶ | die "Cannot fork: $!" unless defined $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 18 | close $child_r; | |||||||||||||||||||||||||||
| 28 | 14 | ▶ | my $exit = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 14 | my( $save_val, $exit_val ) = @_; | |||||||||||||||||||||||||||
| 30 | 14 | store_fd $save_val, $parent_w; | |||||||||||||||||||||||||||
| 31 | 14 | close $parent_w; | |||||||||||||||||||||||||||
| 32 | 14 | exit $exit_val; | |||||||||||||||||||||||||||
| 33 | 18 | }; | |||||||||||||||||||||||||||
| 34 | 18 | ▶ | local $SIG{ALRM} = sub { $exit->( ['TIMEOUT'], 1 ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 18 | ▶ | alarm( $timeout || 0 ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$timeout || 0
MC/DC100%1/1
$timeout || 0
$timeout
Truth table100%2/2
A
A= $timeout
| |||||||||||||||||||||||||||||
| 36 | 18 | my $ret = eval{ $sub->() }; | |||||||||||||||||||||||||||
| 37 | 12 | my $err = $@; | |||||||||||||||||||||||||||
| 38 | 12 | alarm( 0 ); | |||||||||||||||||||||||||||
| 39 | 12 | ▶ | $exit->( [$err], 1 ) if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 10 | $exit->( $ret , 0 ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 11 | while(1) { | |||||||||||||||||||||||||||
| 44 | 60 | my $pid = wait(); | |||||||||||||||||||||||||||
| 45 | 60 | my $err = $?; | |||||||||||||||||||||||||||
| 46 | 60 | ▶ | last if( $pid == -1 or not @subs ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pid == -1 or not @subs
MC/DC50%1/2
$pid == -1 or not @subs
$pid == -1
not @subs
Truth table66%2/3
A or not(B)
A= $pid == -1B= not @subs
| |||||||||||||||||||||||||||||
| 47 | 49 | ▶ | next if not exists $rets{ $pid }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 49 | ▶ | if( $err ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 18 | $rets{ $pid }->{err} = fd_retrieve( $rets{ $pid }->{fd} )->[0]; | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 31 | $rets{ $pid }->{val} = fd_retrieve( $rets{ $pid }->{fd} ); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 49 | close $rets{ $pid }->{fd}; | |||||||||||||||||||||||||||
| 54 | 49 | pop @subs; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 12 | ▶ | my $r = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 12 | my( $key ) = @_; | |||||||||||||||||||||||||||
| 58 | # can be optimized | ||||||||||||||||||||||||||||
| 59 | 54 | [ map { $rets{$_}->{ $key } } | |||||||||||||||||||||||||||
| 60 | 12 | sort { $rets{$a}->{ord} <=> $rets{$b}->{ord} } | |||||||||||||||||||||||||||
| 61 | keys %rets | ||||||||||||||||||||||||||||
| 62 | ]; | ||||||||||||||||||||||||||||
| 63 | 11 | }; | |||||||||||||||||||||||||||
| 64 | 11 | ▶ | return wantarray ? ( $r->('val'), $r->('err') ) : $r->('val'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 21 | } | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 15 | 1; | |||||||||||||||||||||||||||
| 69 | __END__ | ||||||||||||||||||||||||||||