| 1 | # | ||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | package Rex::Fork::Manager; | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 56 | ▶ | use v5.14.4; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 56 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 56 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 56 | ▶ | use Rex::Fork::Task; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 56 | ▶ | use Time::HiRes qw(sleep); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 138 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 16 | 138 | my $that = shift; | |||||||||||||||||||
| 17 | 138 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||
|
Condition33%1/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||
| 18 | 138 | my $self = {@_}; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 138 | bless( $self, $proto ); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 138 | $self->{'forks'} = []; | |||||||||||||||||||
| 23 | 138 | $self->{'running'} = 0; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 138 | return $self; | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 135 | ▶ | sub add { | ||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 29 | 135 | my ( $self, $coderef ) = @_; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 135 | my $f = Rex::Fork::Task->new( coderef => $coderef ); | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 135 | push( @{ $self->{'forks'} }, $f ); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 135 | $f->start; | |||||||||||||||||||
| 36 | 106 | ++$self->{'running'}; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 106 | ▶ | if ( $self->{'running'} >= $self->{'max'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 106 | $self->wait_for_one; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | ▶ | sub start { | ||||||||||||||||||
|
Subroutine
start: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 44 | 0 | my ($self) = @_; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | my @threads = @{ $self->{'forks'} }; | |||||||||||||||||||
| 47 | 0 | ▶ | for ( my $i = 0 ; $i < scalar(@threads) ; ++$i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | $threads[$i]->start; | |||||||||||||||||||
| 49 | 0 | ++$self->{'running'}; | |||||||||||||||||||
| 50 | 0 | ▶ | if ( $self->{'running'} >= $self->{'max'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | $self->wait_for_one; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | $self->wait_for_all; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 106 | ▶ | sub wait_for_one { | ||||||||||||||||||
|
Subroutine
wait_for_one: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 59 | 106 | my ($self) = @_; | |||||||||||||||||||
| 60 | 106 | $self->wait_for; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 107 | ▶ | sub wait_for_all { | ||||||||||||||||||
|
Subroutine
wait_for_all: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 64 | 107 | my ($self) = @_; | |||||||||||||||||||
| 65 | 107 | $self->wait_for(1); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 213 | ▶ | sub wait_for { | ||||||||||||||||||
|
Subroutine
wait_for: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 69 | 213 | my ( $self, $all ) = @_; | |||||||||||||||||||
| 70 | do { | ||||||||||||||||||||
| 71 | 3242 | ▶ | FORK: for ( my $i = 0 ; $i < scalar( @{ $self->{'forks'} } ) ; $i++ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 3241 | my $thr = $self->{'forks'}->[$i]; | |||||||||||||||||||
| 73 | 3241 | ▶ | unless ( $thr->{'running'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 106 | next FORK; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 3135 | my $kid; | |||||||||||||||||||
| 78 | 3135 | $kid = $thr->wait; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 3135 | ▶ | if ( $kid == -1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 106 | $thr = undef; | |||||||||||||||||||
| 82 | 106 | $thr->{running} = 0; | |||||||||||||||||||
| 83 | 106 | --$self->{'running'}; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 106 | ▶ | return 1 unless $all; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | 3029 | sleep Rex::Config->get_waitpid_blocking_sleep_time; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | 213 | ▶ | } until $self->{'running'} == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 56 | 1; | |||||||||||||||||||