MCE/Child.pm

filter: Statementstmt 71.7% 386 / 538 Branchbran 46.4% 198 / 426 Conditioncond 31.1% 51 / 164 MC/DCmcdc 3.3% 3 / 91 Subroutinesub 74.3% 55 / 74 Podpod 100% 23 / 23 totaltotal 54.4% 716 / 1316 SCAR 87.9
CC
274
Cov
56%
create
154.7
init
111.0
_trap
90.0
CRAP 6541.8
« MCE/Core/Manager.pm ↑ Summary MCE/Queue.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## A threads-like parallelization module compatible with Perl 5.8.
4 ##
5 ###############################################################################
6  
7 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶no warnings qw( threads recursion uninitialized once redefine );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package MCE::Child;
13  
14 12 our $VERSION = '1.889';
15  
16 ## no critic (BuiltinFunctions::ProhibitStringyEval)
17 ## no critic (Subroutines::ProhibitExplicitReturnUndef)
18 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
19 ## no critic (TestingAndDebugging::ProhibitNoStrict)
20  
21 12 ▶use MCE::Signal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use MCE::Mutex ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶use MCE::Channel ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use Time::HiRes 'sleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 use overload (
27 q(==) => \&equal,
28 0 ▶ q(!=) => sub { !equal(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 12 fallback => 1
30 12 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 sub import {
33 12 ▶ if (caller !~ /^MCE::/) {
Subroutine import: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ((caller) =~ /^MCE::/) 12 0 12
34 12 ▶ no strict 'refs'; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
35 12 *{ caller().'::mce_child' } = \&mce_child;
36 }
37 12 return;
38 }
39  
40 ## The POSIX module has many symbols. Try not loading it simply
41 ## to have WNOHANG. The following covers most platforms.
42  
43 use constant {
44 12 ▶ _WNOHANG => ( $INC{'POSIX.pm'} )
Branch: 2 branches
BranchTrueFalseTotal
$INC{'POSIX.pm'} ? : 0 12 12
$^O eq 'solaris' ? : 0 12 12
45 ? &POSIX::WNOHANG : ( $^O eq 'solaris' ) ? 64 : 1
46 12 ▶};
Subroutine BEGIN: called CC=3 SCAR=11.0
47  
48 12 my ( $_MNGD, $_DATA, $_DELY, $_LIST ) = ( {}, {}, {}, {} );
49  
50 12 ▶my $_is_MSWin32 = ( $^O eq 'MSWin32' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 12 12
51 12 ▶my $_tid = ( $INC{'threads.pm'} ) ? threads->tid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 12 12
52  
53 sub CLONE {
54 0 ▶ $_tid = threads->tid(), &_clear() if $INC{'threads.pm'};
Subroutine CLONE: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 0 0
55 }
56  
57 sub _clear {
58 0 ▶ %{ $_LIST } = ();
Subroutine _clear: not called CC=1 SCAR=6.9
59 }
60  
61 sub _max_workers {
62 10 ▶ my ( $cpus ) = @_;
Subroutine _max_workers: called CC=5 SCAR=16.1
63 10 ▶ if ( $cpus eq 'auto' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cpus eq 'auto') { } 1 9 10
elsif ($cpus =~ /^([0-9.]+)%$/) { } 6 3 9
64 1 $cpus = MCE::Util::get_ncpu();
65 }
66 elsif ( $cpus =~ /^([0-9.]+)%$/ ) {
67 6 my ( $percent, $ncpu ) = ( $1 / 100, MCE::Util::get_ncpu() );
68 6 $cpus = $ncpu * $percent + 0.5;
69 }
70 10 ▶ $cpus = 1 if $cpus !~ /^[\d\.]+$/ || $cpus < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $cpus =~ /^[\d\.]+$/ or $cpus < 1 3 7 10
Condition66%2/3
not $cpus =~ /^[\d\.]+$/ or $cpus < 1
l!l&&r!l&&!r
037
MC/DC50%1/2
not $cpus =~ /^[\d\.]+$/ or $cpus < 1
not $cpus =~ /^[\d\.]+$/ $cpus < 1
Truth table66%2/3
not(A) or B
A= not $cpus =~ /^[\d\.]+$/B= $cpus < 1
ABresult
1X1
011
000
71 10 return int($cpus);
72 }
73  
74 ###############################################################################
75 ## ----------------------------------------------------------------------------
76 ## Init routine.
77 ##
78 ###############################################################################
79  
80 12 bless my $_SELF = { MGR_ID => "$$.$_tid", WRK_ID => $$ }, __PACKAGE__;
81  
82 sub init {
83 19 ▶ shift if ( defined $_[0] && $_[0] eq __PACKAGE__ );
Subroutine init: called CC=34 SCAR=47.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq __PACKAGE__ 8 11 19
Condition66%2/3
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
1108
MC/DC50%1/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
84  
85 # -- options ----------------------------------------------------------
86 # max_workers child_timeout posix_exit on_start on_finish void_context
87 # ---------------------------------------------------------------------
88  
89 19 ▶ my $pkg = "$$.$_tid.".( caller eq __PACKAGE__ ? caller(1) : caller );
Branch: 1 branch
BranchTrueFalseTotal
caller eq __PACKAGE__ ? : 11 8 19
90 19 ▶ my $mngd = $_MNGD->{$pkg} = ( ref $_[0] eq 'HASH' ) ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 19 19
91  
92 19 @_ = ();
93  
94 $mngd->{MGR_ID} = "$$.$_tid", $mngd->{PKG} = $pkg,
95 19 $mngd->{WRK_ID} = $$;
96  
97 19 ▶ &_force_reap($pkg), $_DATA->{$pkg}->clear() if ( exists $_LIST->{$pkg} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $_LIST->{$pkg} 7 12 19
98  
99 19 ▶ if ( !exists $_LIST->{$pkg} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_LIST->{$pkg}) 12 7 19
100 12 ▶ $MCE::_GMUTEX->lock() if ( $_tid && $MCE::_GMUTEX );
Branch: 1 branch
BranchTrueFalseTotal
if $_tid and $MCE::_GMUTEX 0 12 12
Condition33%1/3
$_tid and $MCE::_GMUTEX
!ll&&!rl&&r
1200
MC/DC0%0/2
$_tid and $MCE::_GMUTEX
$_tid $MCE::_GMUTEX
Truth table33%1/3
A and B
A= $_tidB= $MCE::_GMUTEX
ABresult
0X0
100
111
101 12 ▶ sleep 0.015 if $_tid;
Branch: 1 branch
BranchTrueFalseTotal
if $_tid 0 12 12
102  
103 # Start the shared-manager process if not running.
104 12 ▶ MCE::Shared->start() if $INC{'MCE/Shared.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"MCE/Shared.pm"} 0 12 12
105  
106 12 my $chnl = MCE::Channel->new( impl => 'Mutex' );
107 12 $_LIST->{ $pkg } = MCE::Child::_ordhash->new();
108 12 $_DELY->{ $pkg } = MCE::Child::_delay->new( $chnl );
109 12 $_DATA->{ $pkg } = MCE::Child::_hash->new( $chnl );
110 12 $_DATA->{"$pkg:seed"} = int(rand() * 1e9);
111 12 $_DATA->{"$pkg:id" } = 0;
112  
113 12 ▶ $MCE::_GMUTEX->unlock() if ( $_tid && $MCE::_GMUTEX );
Branch: 1 branch
BranchTrueFalseTotal
if $_tid and $MCE::_GMUTEX 0 12 12
Condition33%1/3
$_tid and $MCE::_GMUTEX
!ll&&!rl&&r
1200
MC/DC0%0/2
$_tid and $MCE::_GMUTEX
$_tid $MCE::_GMUTEX
Truth table33%1/3
A and B
A= $_tidB= $MCE::_GMUTEX
ABresult
0X0
100
111
114 }
115  
116 19 ▶ if ( !exists $mngd->{posix_exit} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $mngd->{"posix_exit"}) 19 0 19
117 $mngd->{posix_exit} = 1 if (
118 $^S || $_tid || $INC{'Mojo/IOLoop.pm'} ||
119 $INC{'Coro.pm'} || $INC{'LWP/UserAgent.pm'} || $INC{'stfl.pm'} ||
120 $INC{'Curses.pm'} || $INC{'CGI.pm'} || $INC{'FCGI.pm'} ||
121 $INC{'Tk.pm'} || $INC{'Wx.pm'} || $INC{'Win32/GUI.pm'} ||
122 19 ▶ $INC{'Gearman/Util.pm'} || $INC{'Gearman/XS.pm'}
Branch: 1 branch
BranchTrueFalseTotal
if $^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"} or $INC{"FCGI.pm"} or $INC{"Tk.pm"} or $INC{"Wx.pm"} or $INC{"Win32/GUI.pm"} or $INC{"Gearman/Util.pm"} or $INC{"Gearman/XS.pm"} 0 19 19
Condition33%13/39
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"} or $INC{"FCGI.pm"} or $INC{"Tk.pm"} or $INC{"Wx.pm"} or $INC{"Win32/GUI.pm"} or $INC{"Gearman/Util.pm"} or $INC{"Gearman/XS.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"} or $INC{"FCGI.pm"} or $INC{"Tk.pm"} or $INC{"Wx.pm"} or $INC{"Win32/GUI.pm"} or $INC{"Gearman/Util.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"} or $INC{"FCGI.pm"} or $INC{"Tk.pm"} or $INC{"Wx.pm"} or $INC{"Win32/GUI.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"} or $INC{"FCGI.pm"} or $INC{"Tk.pm"} or $INC{"Wx.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"} or $INC{"FCGI.pm"} or $INC{"Tk.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"} or $INC{"FCGI.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid or $INC{"Mojo/IOLoop.pm"}
l!l&&r!l&&!r
0019
$^S or $_tid
l!l&&r!l&&!r
0019
MC/DC0%0/14
$^S or $_tid or $INC{"Mojo/IOLoop.pm"} or $INC{"Coro.pm"} or $INC{"LWP/UserAgent.pm"} or $INC{"stfl.pm"} or $INC{"Curses.pm"} or $INC{"CGI.pm"} or $INC{"FCGI.pm"} or $INC{"Tk.pm"} or $INC{"Wx.pm"} or $INC{"Win32/GUI.pm"} or $INC{"Gearman/Util.pm"} or $INC{"Gearman/XS.pm"}
$^S $_tid $INC{"Mojo/IOLoop.pm"} $INC{"Coro.pm"} $INC{"LWP/UserAgent.pm"} $INC{"stfl.pm"} $INC{"Curses.pm"} $INC{"CGI.pm"} $INC{"FCGI.pm"} $INC{"Tk.pm"} $INC{"Wx.pm"} $INC{"Win32/GUI.pm"} $INC{"Gearman/Util.pm"} $INC{"Gearman/XS.pm"}
Truth table6%1/15
A or B or C or D or E or F or G or H or I or J or K or L or M or N
A= $^SB= $_tidC= $INC{"Mojo/IOLoop.pm"}D= $INC{"Coro.pm"}E= $INC{"LWP/UserAgent.pm"}F= $INC{"stfl.pm"}G= $INC{"Curses.pm"}H= $INC{"CGI.pm"}I= $INC{"FCGI.pm"}J= $INC{"Tk.pm"}K= $INC{"Wx.pm"}L= $INC{"Win32/GUI.pm"}M= $INC{"Gearman/Util.pm"}N= $INC{"Gearman/XS.pm"}
ABCDEFGHIJKLMNresult
1XXXXXXXXXXXXX1
01XXXXXXXXXXXX1
001XXXXXXXXXXX1
0001XXXXXXXXXX1
00001XXXXXXXXX1
000001XXXXXXXX1
0000001XXXXXXX1
00000001XXXXXX1
000000001XXXXX1
0000000001XXXX1
00000000001XXX1
000000000001XX1
0000000000001X1
000000000000011
000000000000000
123 );
124 }
125  
126 19 ▶ if ( defined $mngd->{max_workers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mngd->{"max_workers"}) 3 16 19
127 3 $mngd->{max_workers} = _max_workers($mngd->{max_workers});
128 }
129  
130 19 ▶ if ( $INC{'LWP/UserAgent.pm'} && !$INC{'Net/HTTP.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"LWP/UserAgent.pm"} and not $INC{"Net/HTTP.pm"}) 0 19 19
Condition33%1/3
$INC{"LWP/UserAgent.pm"} and not $INC{"Net/HTTP.pm"}
!ll&&!rl&&r
1900
MC/DC0%0/2
$INC{"LWP/UserAgent.pm"} and not $INC{"Net/HTTP.pm"}
$INC{"LWP/UserAgent.pm"} not $INC{"Net/HTTP.pm"}
Truth table33%1/3
A and not(B)
A= $INC{"LWP/UserAgent.pm"}B= not $INC{"Net/HTTP.pm"}
ABresult
0X0
100
111
131 0 local $@; eval 'require Net::HTTP; require Net::HTTPS';
132 }
133  
134 require POSIX
135 19 ▶ if ( $mngd->{on_finish} && !$INC{'POSIX.pm'} && !$_is_MSWin32 );
Branch: 1 branch
BranchTrueFalseTotal
if $mngd->{"on_finish"} and not $INC{"POSIX.pm"} and not $_is_MSWin32 5 14 19
Condition66%4/6
$mngd->{"on_finish"} and not $INC{"POSIX.pm"} and not $_is_MSWin32
!ll&&!rl&&r
1405
$mngd->{"on_finish"} and not $INC{"POSIX.pm"}
!ll&&!rl&&r
1405
MC/DC33%1/3
$mngd->{"on_finish"} and not $INC{"POSIX.pm"} and not $_is_MSWin32
$mngd->{"on_finish"} not $INC{"POSIX.pm"} not $_is_MSWin32
Truth table50%2/4
A and not(B) and not(C)
A= $mngd->{"on_finish"}B= not $INC{"POSIX.pm"}C= not $_is_MSWin32
ABCresult
0XX0
10X0
1100
1111
136  
137 19 return;
138 }
139  
140 ###############################################################################
141 ## ----------------------------------------------------------------------------
142 ## 'new', 'mce_child', and 'create' for threads-like similarity.
143 ##
144 ###############################################################################
145  
146 ## 'new' and 'tid' are aliases for 'create' and 'pid' respectively.
147  
148 12 *new = \&create, *tid = \&pid;
149  
150 ## Use "goto" trick to avoid pad problems from 5.8.1 (fixed in 5.8.2)
151 ## Tip found in threads::async.
152  
153 sub mce_child (&;@) {
154 0 ▶ goto &create;
Subroutine mce_child: not called CC=1 SCAR=6.9
Pod documented
155 }
156  
157 sub create {
158 65 ▶ my $mngd = $_MNGD->{ "$$.$_tid.".caller() } || do {
Subroutine create: called CC=37 SCAR=50.4
Pod documented
Condition66%2/3
$$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
l!l&&r!l&&!r
54110
MC/DC0%0/2
$$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
$$_MNGD{"${$}.$_tid." . (caller())} do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
Truth table66%2/3
A || B
A= $$_MNGD{"${$}.$_tid." . (caller())}B= do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
ABresult
1X1
011
000
159 # construct mngd internally on first use unless defined
160 11 init(); $_MNGD->{ "$$.$_tid.".caller() };
161 };
162  
163 65 ▶ shift if ( $_[0] eq __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 65 0 65
164  
165 # ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~
166  
167 65 ▶ my $self = bless ref $_[0] eq 'HASH' ? { %{ shift() } } : { }, __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 65 65
168  
169 65 ▶ $self->{IGNORE} = 1 if $SIG{CHLD} eq 'IGNORE';
Branch: 1 branch
BranchTrueFalseTotal
if $SIG{"CHLD"} eq "IGNORE" 0 65 65
170 65 $self->{MGR_ID} = $mngd->{MGR_ID}, $self->{PKG} = $mngd->{PKG};
171 65 ▶ $self->{ident } = shift if ( !ref $_[0] && ref $_[1] eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if not ref $_[0] and ref $_[1] eq "CODE" 0 65 65
Condition33%1/3
not ref $_[0] and ref $_[1] eq "CODE"
!ll&&!rl&&r
6500
MC/DC0%0/2
not ref $_[0] and ref $_[1] eq "CODE"
not ref $_[0] ref $_[1] eq "CODE"
Truth table33%1/3
not(A) and B
A= not ref $_[0]B= ref $_[1] eq "CODE"
ABresult
0X0
100
111
172  
173 65 ▶ my $func = shift; $func = caller().'::'.$func
Branch: 1 branch
BranchTrueFalseTotal
if not ref $func and length $func and index($func, ":") == -1 0 65 65
Condition33%2/6
not ref $func and length $func and index($func, ":") == -1
!ll&&!rl&&r
6500
not ref $func and length $func
!ll&&!rl&&r
6500
MC/DC0%0/3
not ref $func and length $func and index($func, ":") == -1
not ref $func length $func index($func, ":") == -1
Truth table25%1/4
not(A) and B and C
A= not ref $funcB= length $funcC= index($func, ":") == -1
ABCresult
0XX0
10X0
1100
1111
174 if ( !ref $func && length $func && index($func,':') < 0 );
175  
176 65 ▶ if ( !defined $func ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $func) 0 65 65
177 0 local $\; print {*STDERR} "code function is not specified or valid\n";
178 0 return undef;
179 }
180  
181 my ( $list, $max_workers, $pkg ) = (
182 $_LIST->{ $mngd->{PKG} }, $mngd->{max_workers}, $mngd->{PKG}
183 65 );
184  
185 65 ▶ $_DATA->{"$pkg:id"} = 10000 if ( ( my $id = ++$_DATA->{"$pkg:id"} ) >= 2e9 );
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = ++$$_DATA{"${pkg}:id"}) >= 2000000000 0 65 65
186  
187 {
188 # Reap completed child processes.
189 65 local ($SIG{CHLD}, $!, $?, $_); map {
190 4 waitpid($_, 0); _reap_child($list->del($_), 0); ();
191 }
192 65 $_DATA->{$pkg}->reap_data;
193 }
194  
195 65 ▶ if ( $max_workers || $self->{IGNORE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_workers or $self->{"IGNORE"}) 0 65 65
Condition33%1/3
$max_workers or $self->{"IGNORE"}
l!l&&r!l&&!r
0065
MC/DC0%0/2
$max_workers or $self->{"IGNORE"}
$max_workers $self->{"IGNORE"}
Truth table33%1/3
A or B
A= $max_workersB= $self->{"IGNORE"}
ABresult
1X1
011
000
196 # Wait for a slot if saturated.
197 0 ▶ if ( $max_workers && $list->len() >= $max_workers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_workers and $list->len >= $max_workers) 0 0 0
Condition0%0/3
$max_workers and $list->len >= $max_workers
!ll&&!rl&&r
000
MC/DC0%0/2
$max_workers and $list->len >= $max_workers
$max_workers $list->len >= $max_workers
Truth table0%0/3
A and B
A= $max_workersB= $list->len >= $max_workers
ABresult
0X0
100
111
198 0 my $count = $list->len() - $max_workers + 1;
199 0 _wait_one($pkg) for 1 .. $count;
200 }
201 }
202  
203 # ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~
204  
205 65 ▶ $MCE::_GMUTEX->lock() if ( $_tid && $MCE::_GMUTEX );
Branch: 1 branch
BranchTrueFalseTotal
if $_tid and $MCE::_GMUTEX 0 65 65
Condition33%1/3
$_tid and $MCE::_GMUTEX
!ll&&!rl&&r
6500
MC/DC0%0/2
$_tid and $MCE::_GMUTEX
$_tid $MCE::_GMUTEX
Truth table33%1/3
A and B
A= $_tidB= $MCE::_GMUTEX
ABresult
0X0
100
111
206  
207 65 my @args = @_; @_ = (); # To avoid (Scalars leaked: N) messages
208 65 my ( $killed, $pid );
209  
210 {
211 65 ▶ local $SIG{TERM} = local $SIG{INT} = sub { $killed = $_[0] }
Subroutine __ANON__: not called CC=1 SCAR=1.2
212 65 ▶ if ( !$_is_MSWin32 && $] ge '5.010001' );
Branch: 1 branch
BranchTrueFalseTotal
if not $_is_MSWin32 and $] ge "5.010001" 65 0 65
Condition33%1/3
not $_is_MSWin32 and $] ge "5.010001"
!ll&&!rl&&r
0065
MC/DC0%0/2
not $_is_MSWin32 and $] ge "5.010001"
not $_is_MSWin32 $] ge "5.010001"
Truth table33%1/3
not(A) and B
A= not $_is_MSWin32B= $] ge "5.010001"
ABresult
0X0
100
111
213  
214 local $SIG{TTIN}, local $SIG{TTOU}, local $SIG{WINCH}
215 65 ▶ if ( !$_is_MSWin32 );
Branch: 1 branch
BranchTrueFalseTotal
unless $_is_MSWin32 65 0 65
216  
217 65 $pid = fork();
218  
219 65 ▶ if ( !defined $pid ) { # error
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 65 65
elsif ($pid) { } 55 10 65
220 0 local $\; print {*STDERR} "fork error: $!\n";
221 }
222 elsif ( $pid ) { # parent
223 55 $self->{WRK_ID} = $pid;
224 55 $list->set($pid, $self);
225 55 ▶ $mngd->{on_start}->($pid, $self->{ident}) if $mngd->{on_start};
Branch: 1 branch
BranchTrueFalseTotal
if $mngd->{"on_start"} 10 45 55
226 }
227 else { # child
228 10 %{ $_LIST } = (), $_SELF = $self;
229  
230 local $SIG{TERM} = local $SIG{INT} = local $SIG{ABRT} = \&_trap,
231 local $SIG{SEGV} = local $SIG{HUP} = \&_trap,
232 10 local $SIG{QUIT} = \&_quit;
233 10 local $SIG{CHLD};
234  
235 10 ▶ MCE::Shared::init() if $INC{'MCE/Shared.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"MCE/Shared.pm"} 0 10 10
236 10 ▶ $_DATA->{ $_SELF->{PKG} }->set('S'.$$, '') unless $self->{IGNORE};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"IGNORE"} 10 0 10
237 10 ▶ CORE::kill($killed, $$) if $killed;
Branch: 1 branch
BranchTrueFalseTotal
if $killed 0 10 10
238  
239 # Sets the seed of the base generator uniquely between workers.
240 # The new seed is computed using the current seed and ID value.
241 # One may set the seed at the application level for predictable
242 # results. Ditto for Math::Prime::Util, Math::Random, and
243 # Math::Random::MT::Auto.
244  
245 10 srand( abs($_DATA->{"$pkg:seed"} - ($id * 100000)) % 2147483560 );
246  
247 10 ▶ if ( $INC{'Math/Prime/Util.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Math/Prime/Util.pm"}) 0 10 10
248 Math::Prime::Util::srand(
249 0 abs($_DATA->{"$pkg:seed"} - ($id * 100000)) % 2147483560
250 );
251 }
252  
253 10 ▶ if ( $INC{'Math/Random.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Math/Random.pm"}) 0 10 10
254 0 my $cur_seed = Math::Random::random_get_seed();
255 0 ▶ my $new_seed = ($cur_seed < 1073741781)
Branch: 1 branch
BranchTrueFalseTotal
$cur_seed < 1073741781 ? : 0 0 0
256 ? $cur_seed + ((abs($id) * 10000) % 1073741780)
257 : $cur_seed - ((abs($id) * 10000) % 1073741780);
258  
259 0 Math::Random::random_set_seed($new_seed, $new_seed);
260 }
261  
262 10 ▶ if ( $INC{'Math/Random/MT/Auto.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Math/Random/MT/Auto.pm"}) 0 10 10
263 0 my $cur_seed = Math::Random::MT::Auto::get_seed()->[0];
264 0 ▶ my $new_seed = ($cur_seed < 1073741781)
Branch: 1 branch
BranchTrueFalseTotal
$cur_seed < 1073741781 ? : 0 0 0
265 ? $cur_seed + ((abs($id) * 10000) % 1073741780)
266 : $cur_seed - ((abs($id) * 10000) % 1073741780);
267  
268 0 Math::Random::MT::Auto::set_seed($new_seed);
269 }
270  
271 10 _dispatch($mngd, $func, \@args);
272 }
273 }
274  
275 55 ▶ $MCE::_GMUTEX->unlock() if ( $_tid && $MCE::_GMUTEX );
Branch: 1 branch
BranchTrueFalseTotal
if $_tid and $MCE::_GMUTEX 0 55 55
Condition33%1/3
$_tid and $MCE::_GMUTEX
!ll&&!rl&&r
5500
MC/DC0%0/2
$_tid and $MCE::_GMUTEX
$_tid $MCE::_GMUTEX
Truth table33%1/3
A and B
A= $_tidB= $MCE::_GMUTEX
ABresult
0X0
100
111
276  
277 55 ▶ CORE::kill($killed, $$) if $killed;
Branch: 1 branch
BranchTrueFalseTotal
if $killed 0 55 55
278  
279 55 ▶ return $pid ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pid ? : 55 0 55
280 }
281  
282 ###############################################################################
283 ## ----------------------------------------------------------------------------
284 ## Public methods.
285 ##
286 ###############################################################################
287  
288 sub equal {
289 0 ▶ return 0 unless ( ref $_[0] && ref $_[1] );
Subroutine equal: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] and ref $_[1] 0 0 0
Condition0%0/3
ref $_[0] and ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[1]
ref $_[0] ref $_[1]
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[1]
ABresult
0X0
100
111
290 0 ▶ $_[0]->{WRK_ID} == $_[1]->{WRK_ID} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'WRK_ID'} == $_[1]{'WRK_ID'} ? : 0 0 0
291 }
292  
293 sub error {
294 31 ▶ _croak('Usage: $child->error()') unless ref( my $self = $_[0] );
Subroutine error: called CC=4 SCAR=15.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref(my $self = $_[0]) 0 31 31
295 31 ▶ $self->join() unless $self->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"REAPED"} 0 31 31
296 31 ▶ $self->{ERROR} || undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ERROR"} or undef 31 0 31
297 }
298  
299 sub exit {
300 10 ▶ shift if ( defined $_[0] && $_[0] eq __PACKAGE__ );
Subroutine exit: called CC=15 SCAR=40.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq __PACKAGE__ 0 10 10
Condition33%1/3
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
0100
MC/DC0%0/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
301  
302 10 ▶ my ( $self ) = ( ref $_[0] ? shift : $_SELF );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 10 0 10
303 10 my ( $pkg, $wrk_id ) = ( $self->{PKG}, $self->{WRK_ID} );
304  
305 10 ▶ if ( $wrk_id == $$ && $self->{MGR_ID} eq "$$.$_tid" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wrk_id == $$ and $self->{'MGR_ID'} eq "${$}.$_tid") { } 0 10 10
elsif ($wrk_id == $$) { } 0 10 10
Condition33%1/3
$wrk_id == $$ and $self->{"MGR_ID"} eq "${$}.$_tid"
!ll&&!rl&&r
1000
MC/DC0%0/2
$wrk_id == $$ and $self->{"MGR_ID"} eq "${$}.$_tid"
$wrk_id == $$ $self->{"MGR_ID"} eq "${$}.$_tid"
Truth table33%1/3
A and B
A= $wrk_id == $$B= $self->{"MGR_ID"} eq "${$}.$_tid"
ABresult
0X0
100
111
306 0 MCE::Child->finish('MCE'); CORE::exit(@_);
307 }
308 elsif ( $wrk_id == $$ ) {
309 0 ▶ alarm 0; my ( $exit_status, @res ) = @_; $? = $exit_status || 0;
Condition0%0/2
$exit_status || 0
l!l
00
MC/DC0%0/1
$exit_status || 0
$exit_status
Truth table0%0/2
A
A= $exit_status
Aresult
11
00
310 0 ▶ $_DATA->{$pkg}->set('R'.$wrk_id, @res ? \@res : '');
Branch: 1 branch
BranchTrueFalseTotal
@res ? : 0 0 0
311 0 die "Child exited ($?)\n";
312 0 _exit($?); # not reached
313 }
314  
315 10 ▶ return $self if $self->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"REAPED"} 0 10 10
316  
317 10 ▶ if ( exists $_DATA->{$pkg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_DATA->{$pkg}) { } 10 0 10
318 10 ▶ sleep 0.015 until $_DATA->{$pkg}->exists('S'.$wrk_id);
Branch: 1 branch
BranchTrueFalseTotal
$_DATA->{$pkg}->exists('S' . $wrk_id) or do { &MCE::Child::sleep("0.015") } 0 10 10
319 } else {
320 0 sleep 0.030;
321 }
322  
323 10 ▶ if ($_is_MSWin32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_is_MSWin32) { } 0 10 10
324 0 ▶ CORE::kill('KILL', $wrk_id) if CORE::kill('ZERO', $wrk_id);
Branch: 1 branch
BranchTrueFalseTotal
if CORE::kill "ZERO", $wrk_id 0 0 0
325 } else {
326 10 ▶ CORE::kill('QUIT', $wrk_id) if CORE::kill('ZERO', $wrk_id);
Branch: 1 branch
BranchTrueFalseTotal
if CORE::kill "ZERO", $wrk_id 10 0 10
327 }
328  
329 10 $self;
330 }
331  
332 sub finish {
333 26 ▶ _croak('Usage: MCE::Child->finish()') if ref($_[0]);
Subroutine finish: called CC=10 SCAR=23.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 26 26
334 26 ▶ shift if ( defined $_[0] && $_[0] eq __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq __PACKAGE__ 26 0 26
Condition33%1/3
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
335  
336 26 ▶ my $pkg = defined($_[0]) ? $_[0] : caller();
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 25 1 26
337  
338 26 ▶ if ( $pkg eq 'MCE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pkg eq 'MCE') { } 23 3 26
elsif (exists $_LIST->{$pkg}) { } 2 1 3
339 23 for my $key ( keys %{ $_LIST } ) { MCE::Child->finish($key); }
340 }
341 elsif ( exists $_LIST->{$pkg} ) {
342 2 ▶ return if $MCE::Signal::KILLED;
Branch: 1 branch
BranchTrueFalseTotal
if $MCE::Signal::KILLED 0 2 2
343  
344 2 ▶ if ( exists $_DELY->{$pkg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_DELY->{$pkg}) 2 0 2
345 2 &_force_reap($pkg);
346 delete($_DELY->{$pkg}), delete($_DATA->{"$pkg:seed"}),
347 delete($_LIST->{$pkg}), delete($_DATA->{"$pkg:id"}),
348 2 delete($_MNGD->{$pkg}), delete($_DATA->{ $pkg });
349 }
350 }
351  
352 26 @_ = ();
353  
354 26 return;
355 }
356  
357 sub is_joinable {
358 24 ▶ _croak('Usage: $child->is_joinable()') unless ref( my $self = $_[0] );
Subroutine is_joinable: called CC=7 SCAR=25.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref(my $self = $_[0]) 0 24 24
359 24 my ( $wrk_id, $pkg ) = ( $self->{WRK_ID}, $self->{PKG} );
360  
361 24 ▶ if ( $wrk_id == $$ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wrk_id == $$) { } 0 24 24
elsif ($self->{'MGR_ID'} eq "${$}.$_tid") { } 24 0 24
362 0 '';
363 }
364 elsif ( $self->{MGR_ID} eq "$$.$_tid" ) {
365 24 ▶ return '' if $self->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"REAPED"} 0 24 24
366 24 local $!; $_DATA->{$pkg}->reap_data;
367 24 ▶ ( waitpid($wrk_id, _WNOHANG) == 0 ) ? '' : do {
Branch: 1 branch
BranchTrueFalseTotal
waitpid($wrk_id, 1) == 0 ? : 24 0 24
368 0 ▶ _reap_child($self, 0) unless $self->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"REAPED"} 0 0 0
369 0 1;
370 };
371 }
372 else {
373 # limitation for MCE::Child only; allowed for MCE::Hobo
374 0 _croak('Error: $child->is_joinable() not called by managed process');
375 }
376 }
377  
378 sub is_running {
379 24 ▶ _croak('Usage: $child->is_running()') unless ref( my $self = $_[0] );
Subroutine is_running: called CC=7 SCAR=25.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref(my $self = $_[0]) 0 24 24
380 24 my ( $wrk_id, $pkg ) = ( $self->{WRK_ID}, $self->{PKG} );
381  
382 24 ▶ if ( $wrk_id == $$ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wrk_id == $$) { } 0 24 24
elsif ($self->{'MGR_ID'} eq "${$}.$_tid") { } 24 0 24
383 0 1;
384 }
385 elsif ( $self->{MGR_ID} eq "$$.$_tid" ) {
386 24 ▶ return '' if $self->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"REAPED"} 0 24 24
387 24 local $!; $_DATA->{$pkg}->reap_data;
388 24 ▶ ( waitpid($wrk_id, _WNOHANG) == 0 ) ? 1 : do {
Branch: 1 branch
BranchTrueFalseTotal
waitpid($wrk_id, 1) == 0 ? : 24 0 24
389 0 ▶ _reap_child($self, 0) unless $self->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"REAPED"} 0 0 0
390 0 '';
391 };
392 }
393 else {
394 # limitation for MCE::Child only; allowed for MCE::Hobo
395 0 _croak('Error: $child->is_running() not called by managed process');
396 }
397 }
398  
399 sub join {
400 40 ▶ _croak('Usage: $child->join()') unless ref( my $self = $_[0] );
Subroutine join: called CC=12 SCAR=35.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref(my $self = $_[0]) 0 40 40
401 40 my ( $wrk_id, $pkg ) = ( $self->{WRK_ID}, $self->{PKG} );
402  
403 40 ▶ if ( $self->{REAPED} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"REAPED"}) 0 40 40
404 0 ▶ _croak('Child already joined') unless exists( $self->{RESULT} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"RESULT"} 0 0 0
405 0 ▶ $_LIST->{$pkg}->del($wrk_id) if ( exists $_LIST->{$pkg} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $_LIST->{$pkg} 0 0 0
406  
407 return ( defined wantarray )
408 0 ▶ ? wantarray ? @{ delete $self->{RESULT} } : delete( $self->{RESULT} )->[-1]
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
wantarray ? : 0 0 0
409 : ();
410 }
411  
412 40 ▶ if ( $wrk_id == $$ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wrk_id == $$) { } 0 40 40
elsif ($self->{'MGR_ID'} eq "${$}.$_tid") { } 40 0 40
413 0 _croak('Cannot join self');
414 }
415 elsif ( $self->{MGR_ID} eq "$$.$_tid" ) {
416 # remove from list after reaping
417 40 local $SIG{CHLD};
418 40 _reap_child($self, 1);
419 40 $_LIST->{$pkg}->del($wrk_id);
420 }
421 else {
422 # limitation for MCE::Child only; allowed for MCE::Hobo
423 0 _croak('Error: $child->join() not called by managed process');
424 }
425  
426 40 ▶ return unless ( exists $self->{RESULT} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"RESULT"} 0 40 40
427  
428 ( defined wantarray )
429 40 ▶ ? wantarray ? @{ delete $self->{RESULT} } : delete( $self->{RESULT} )->[-1]
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 39 1 40
wantarray ? : 0 39 39
430 : ();
431 }
432  
433 sub kill {
434 5 ▶ _croak('Usage: $child->kill()') unless ref( my $self = $_[0] );
Subroutine kill: called CC=10 SCAR=29.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref(my $self = $_[0]) 0 5 5
435 5 my ( $wrk_id, $pkg, $signal ) = ( $self->{WRK_ID}, $self->{PKG}, $_[1] );
436  
437 5 ▶ if ( $wrk_id == $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrk_id == $$) 0 5 5
438 0 ▶ CORE::kill($signal || 'INT', $$);
Condition0%0/2
$signal || 'INT'
l!l
00
MC/DC0%0/1
$signal || 'INT'
$signal
Truth table0%0/2
A
A= $signal
Aresult
11
00
439 0 return $self;
440 }
441 5 ▶ if ( $self->{MGR_ID} eq "$$.$_tid" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MGR_ID"} eq "${$}.$_tid") 5 0 5
442 5 ▶ return $self if $self->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"REAPED"} 0 5 5
443 5 ▶ if ( exists $_DATA->{$pkg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_DATA->{$pkg}) { } 5 0 5
444 5 ▶ sleep 0.015 until $_DATA->{$pkg}->exists('S'.$wrk_id);
Branch: 1 branch
BranchTrueFalseTotal
$_DATA->{$pkg}->exists('S' . $wrk_id) or do { &MCE::Child::sleep("0.015") } 0 5 5
445 } else {
446 0 sleep 0.030;
447 }
448 }
449  
450 5 ▶ CORE::kill($signal || 'INT', $wrk_id) if CORE::kill('ZERO', $wrk_id);
Branch: 1 branch
BranchTrueFalseTotal
if CORE::kill "ZERO", $wrk_id 5 0 5
Condition50%1/2
$signal || 'INT'
l!l
50
MC/DC0%0/1
$signal || 'INT'
$signal
Truth table50%1/2
A
A= $signal
Aresult
11
00
451  
452 5 $self;
453 }
454  
455 sub list {
456 8 ▶ _croak('Usage: MCE::Child->list()') if ref($_[0]);
Subroutine list: called CC=3 SCAR=11.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 8 8
457 8 my $pkg = "$$.$_tid.".caller();
458  
459 8 ▶ ( exists $_LIST->{$pkg} ) ? $_LIST->{$pkg}->vals() : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $_LIST->{$pkg} ? : 8 0 8
460 }
461  
462 sub list_pids {
463 8 ▶ _croak('Usage: MCE::Child->list_pids()') if ref($_[0]);
Subroutine list_pids: called CC=3 SCAR=11.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 8 8
464 8 my $pkg = "$$.$_tid.".caller(); local $_;
465  
466 8 ▶ ( exists $_LIST->{$pkg} ) ? map { $_->pid } $_LIST->{$pkg}->vals() : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $_LIST->{$pkg} ? : 8 0 8
467 }
468  
469 sub list_joinable {
470 8 ▶ _croak('Usage: MCE::Child->list_joinable()') if ref($_[0]);
Subroutine list_joinable: called CC=5 SCAR=19.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 8 8
471 8 my $pkg = "$$.$_tid.".caller();
472  
473 8 ▶ return () unless ( my $list = $_LIST->{$pkg} );
Branch: 1 branch
BranchTrueFalseTotal
unless my $list = $_LIST->{$pkg} 0 8 8
474 8 local ($!, $?, $_);
475  
476 8 $_DATA->{$pkg}->reap_data;
477  
478 map {
479 8 ▶ ( waitpid($_->{WRK_ID}, _WNOHANG) == 0 ) ? () : do {
Branch: 1 branch
BranchTrueFalseTotal
waitpid($_->{'WRK_ID'}, 1) == 0 ? : 24 0 24
480 0 ▶ _reap_child($_, 0) unless $_->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"REAPED"} 0 0 0
481 0 $_;
482 };
483 }
484 $list->vals();
485 }
486  
487 sub list_running {
488 8 ▶ _croak('Usage: MCE::Child->list_running()') if ref($_[0]);
Subroutine list_running: called CC=5 SCAR=19.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 8 8
489 8 my $pkg = "$$.$_tid.".caller();
490  
491 8 ▶ return () unless ( my $list = $_LIST->{$pkg} );
Branch: 1 branch
BranchTrueFalseTotal
unless my $list = $_LIST->{$pkg} 0 8 8
492 8 local ($!, $?, $_);
493  
494 8 $_DATA->{$pkg}->reap_data;
495  
496 map {
497 8 ▶ ( waitpid($_->{WRK_ID}, _WNOHANG) == 0 ) ? $_ : do {
Branch: 1 branch
BranchTrueFalseTotal
waitpid($_->{'WRK_ID'}, 1) == 0 ? : 24 0 24
498 0 ▶ _reap_child($_, 0) unless $_->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"REAPED"} 0 0 0
499 0 ();
500 };
501 }
502 $list->vals();
503 }
504  
505 sub max_workers {
506 17 ▶ _croak('Usage: MCE::Child->max_workers()') if ref($_[0]);
Subroutine max_workers: called CC=5 SCAR=18.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 17 17
507 17 ▶ my $mngd = $_MNGD->{ "$$.$_tid.".caller() } || do {
Condition33%1/3
$$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
l!l&&r!l&&!r
1700
MC/DC0%0/2
$$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
$$_MNGD{"${$}.$_tid." . (caller())} do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
Truth table33%1/3
A || B
A= $$_MNGD{"${$}.$_tid." . (caller())}B= do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
ABresult
1X1
011
000
508 # construct mngd internally on first use unless defined
509 0 init(); $_MNGD->{ "$$.$_tid.".caller() };
510 };
511 17 ▶ shift if ( $_[0] eq __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 17 0 17
512  
513 17 ▶ $mngd->{max_workers} = _max_workers(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 7 10 17
514 17 $mngd->{max_workers};
515 }
516  
517 sub pending {
518 8 ▶ _croak('Usage: MCE::Child->pending()') if ref($_[0]);
Subroutine pending: called CC=3 SCAR=11.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 8 8
519 8 my $pkg = "$$.$_tid.".caller();
520  
521 8 ▶ ( exists $_LIST->{$pkg} ) ? $_LIST->{$pkg}->len() : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $_LIST->{$pkg} ? : 8 0 8
522 }
523  
524 sub pid {
525 31 ▶ ref($_[0]) ? $_[0]->{WRK_ID} : $_SELF->{WRK_ID};
Subroutine pid: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 31 0 31
526 }
527  
528 sub result {
529 7 ▶ _croak('Usage: $child->result()') unless ref( my $self = $_[0] );
Subroutine result: called CC=5 SCAR=18.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref(my $self = $_[0]) 0 7 7
530 7 ▶ return $self->join() unless $self->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"REAPED"} 0 7 7
531  
532 7 ▶ _croak('Child already joined') unless exists( $self->{RESULT} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"RESULT"} 0 7 7
533 7 ▶ wantarray ? @{ delete $self->{RESULT} } : delete( $self->{RESULT} )->[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 7 7
534 }
535  
536 sub self {
537 0 ▶ ref($_[0]) ? $_[0] : $_SELF;
Subroutine self: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
538 }
539  
540 sub wait_all {
541 1 ▶ _croak('Usage: MCE::Child->wait_all()') if ref($_[0]);
Subroutine wait_all: called CC=6 SCAR=22.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 1 1
542 1 my $pkg = "$$.$_tid.".caller();
543  
544 return wantarray ? () : 0
545 1 ▶ if ( !exists $_LIST->{$pkg} || !$_LIST->{$pkg}->len() );
Branch: 2 branches
BranchTrueFalseTotal
unless exists $_LIST->{$pkg} and $_LIST->{$pkg}->len 0 1 1
wantarray ? : 0 0 0
Condition33%1/3
exists $_LIST->{$pkg} and $_LIST->{$pkg}->len
!ll&&!rl&&r
001
MC/DC0%0/2
exists $_LIST->{$pkg} and $_LIST->{$pkg}->len
exists $_LIST->{$pkg} $_LIST->{$pkg}->len
Truth table33%1/3
A and B
A= exists $_LIST->{$pkg}B= $_LIST->{$pkg}->len
ABresult
0X0
100
111
546  
547 1 local $_; ( wantarray )
548 0 ? map { $_->join(); $_ } $_LIST->{$pkg}->vals()
549 1 ▶ : map { $_->join(); () } $_LIST->{$pkg}->vals();
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
550 }
551  
552 12 *waitall = \&wait_all; # compatibility
553  
554 sub wait_one {
555 4 ▶ _croak('Usage: MCE::Child->wait_one()') if ref($_[0]);
Subroutine wait_one: called CC=4 SCAR=15.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 4 4
556 4 my $pkg = "$$.$_tid.".caller();
557  
558 return undef
559 4 ▶ if ( !exists $_LIST->{$pkg} || !$_LIST->{$pkg}->len() );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_LIST->{$pkg} and $_LIST->{$pkg}->len 0 4 4
Condition33%1/3
exists $_LIST->{$pkg} and $_LIST->{$pkg}->len
!ll&&!rl&&r
004
MC/DC0%0/2
exists $_LIST->{$pkg} and $_LIST->{$pkg}->len
exists $_LIST->{$pkg} $_LIST->{$pkg}->len
Truth table33%1/3
A and B
A= exists $_LIST->{$pkg}B= $_LIST->{$pkg}->len
ABresult
0X0
100
111
560  
561 4 _wait_one($pkg);
562 }
563  
564 12 *waitone = \&wait_one; # compatibility
565  
566 sub yield {
567 0 ▶ _croak('Usage: MCE::Child->yield()') if ref($_[0]);
Subroutine yield: not called CC=7 SCAR=40.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
568 0 ▶ shift if ( defined $_[0] && $_[0] eq __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
defined $_[0] and $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and $_[0] eq __PACKAGE__
defined $_[0] $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] eq __PACKAGE__
ABresult
0X0
100
111
569  
570 0 ▶ my $pkg = $_SELF->{PKG} || do {
Condition0%0/3
$_SELF->{'PKG'} || do { my $mngd = $$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }; $mngd->{'PKG'} }
l!l&&r!l&&!r
000
MC/DC0%0/2
$_SELF->{'PKG'} || do { my $mngd = $$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }; $mngd->{'PKG'} }
$_SELF->{'PKG'} do { my $mngd = $$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }; $mngd->{'PKG'} }
Truth table0%0/3
A || B
A= $_SELF->{'PKG'}B= do { my $mngd = $$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }; $mngd->{'PKG'} }
ABresult
1X1
011
000
571 0 ▶ my $mngd = $_MNGD->{ "$$.$_tid.".caller() } || do {
Condition0%0/3
$$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$_MNGD{"${$}.$_tid." . (caller())} || do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
$$_MNGD{"${$}.$_tid." . (caller())} do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
Truth table0%0/3
A || B
A= $$_MNGD{"${$}.$_tid." . (caller())}B= do { init(); $$_MNGD{"${$}.$_tid." . (caller())} }
ABresult
1X1
011
000
572 # construct mngd internally on first use unless defined
573 0 init(); $_MNGD->{ "$$.$_tid.".caller() };
574 };
575 0 $mngd->{PKG};
576 };
577  
578 0 ▶ return unless $_DELY->{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
unless $_DELY->{$pkg} 0 0 0
579 0 my $seconds = $_DELY->{$pkg}->seconds(@_);
580  
581 0 MCE::Util::_sleep( $seconds );
582 }
583  
584 ###############################################################################
585 ## ----------------------------------------------------------------------------
586 ## Private methods.
587 ##
588 ###############################################################################
589  
590 sub _croak {
591 0 ▶ if ( $INC{'MCE.pm'} ) {
Subroutine _croak: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'MCE.pm'}) { } 0 0 0
592 0 goto &MCE::_croak;
593 }
594 else {
595 0 $SIG{__DIE__} = \&MCE::Signal::_die_handler;
596 0 $SIG{__WARN__} = \&MCE::Signal::_warn_handler;
597  
598 0 $\ = undef; goto &Carp::croak;
599 }
600 }
601  
602 sub _dispatch {
603 10 ▶ my ( $mngd, $func, $args ) = @_;
Subroutine _dispatch: called CC=20 SCAR=42.2
604  
605 10 $mngd->{WRK_ID} = $_SELF->{WRK_ID} = $$, $? = 0;
606 10 ▶ $ENV{PERL_MCE_IPC} = 'win32' if $_is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $_is_MSWin32 0 10 10
607  
608 {
609 10 local $!;
610 10 ▶ (*STDERR)->autoflush(1) if defined( fileno *STDERR );
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno STDERR 10 0 10
611 9 ▶ (*STDOUT)->autoflush(1) if defined( fileno *STDOUT );
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno STDOUT 9 0 9
612 }
613  
614 # Run task.
615 my $child_timeout = ( exists $_SELF->{child_timeout} )
616 9 ▶ ? $_SELF->{child_timeout} : $mngd->{child_timeout};
Branch: 1 branch
BranchTrueFalseTotal
exists $_SELF->{'child_timeout'} ? : 0 9 9
617  
618 my $void_context = ( exists $_SELF->{void_context} )
619 9 ▶ ? $_SELF->{void_context} : $mngd->{void_context};
Branch: 1 branch
BranchTrueFalseTotal
exists $_SELF->{'void_context'} ? : 0 9 9
620  
621 9 my @res; my $timed_out = 0;
622  
623 local $SIG{'ALRM'} = sub {
624 0 ▶ alarm 0; $timed_out = 1; $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
625 0 die "Child timed out\n";
626 9 };
627  
628 9 ▶ if ( $void_context || $_SELF->{IGNORE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($void_context or $_SELF->{'IGNORE'}) { } 0 9 9
Condition33%1/3
$void_context or $_SELF->{"IGNORE"}
l!l&&r!l&&!r
009
MC/DC0%0/2
$void_context or $_SELF->{"IGNORE"}
$void_context $_SELF->{"IGNORE"}
Truth table33%1/3
A or B
A= $void_contextB= $_SELF->{"IGNORE"}
ABresult
1X1
011
000
629 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
630 0 ▶ eval { alarm($child_timeout || 0); $func->(@{ $args }) };
Condition0%0/2
$child_timeout || 0
l!l
00
MC/DC0%0/1
$child_timeout || 0
$child_timeout
Truth table0%0/2
A
A= $child_timeout
Aresult
11
00
631 }
632 else {
633 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
634 9 ▶ @res = eval { alarm($child_timeout || 0); $func->(@{ $args }) };
Condition50%1/2
$child_timeout || 0
l!l
09
MC/DC0%0/1
$child_timeout || 0
$child_timeout
Truth table50%1/2
A
A= $child_timeout
Aresult
11
00
635 }
636  
637 7 alarm 0;
638 7 ▶ $@ = "Child timed out" if $timed_out;
Branch: 1 branch
BranchTrueFalseTotal
if $timed_out 0 7 7
639  
640 7 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 7 7
641 0 ▶ _exit($?) if ( $@ =~ /^Child exited \(\S+\)$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /^Child exited \(\S+\)$/ 0 0 0
642 0 my $err = $@; $? = 1; $err =~ s/, <__ANONIO__> line \d+//;
643  
644 0 ▶ if ( ! $_SELF->{IGNORE} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_SELF->{"IGNORE"}) 0 0 0
645 $_DATA->{ $_SELF->{PKG} }->set('S'.$$, $err),
646 0 $_DATA->{ $_SELF->{PKG} }->set('R'.$$, '');
647 }
648  
649 0 ▶ if ( !$timed_out && !$mngd->{on_finish} && !$INC{'MCE/Simple.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timed_out or $mngd->{"on_finish"} or $INC{"MCE/Simple.pm"}) 0 0 0
Condition0%0/6
$timed_out or $mngd->{"on_finish"} or $INC{"MCE/Simple.pm"}
l!l&&r!l&&!r
000
$timed_out or $mngd->{"on_finish"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$timed_out or $mngd->{"on_finish"} or $INC{"MCE/Simple.pm"}
$timed_out $mngd->{"on_finish"} $INC{"MCE/Simple.pm"}
Truth table0%0/4
A or B or C
A= $timed_outB= $mngd->{"on_finish"}C= $INC{"MCE/Simple.pm"}
ABCresult
1XX1
01X1
0011
0000
650 12 ▶ use bytes; warn "Child $$ terminated abnormally: reason $err\n";
Subroutine BEGIN: called CC=1 SCAR=0.2
651 }
652 }
653 else {
654 7 ▶ shift(@res) if ref($res[0]) =~ /^MCE::(?:Barrier|Semaphore)::_guard/s;
Branch: 1 branch
BranchTrueFalseTotal
if ref($res[0]) =~ /^MCE::(?:Barrier|Semaphore)::_guard/s 0 7 7
655 $_DATA->{ $_SELF->{PKG} }->set('R'.$$, @res ? \@res : '')
656 7 ▶ if ( ! $_SELF->{IGNORE} );
Branch: 2 branches
BranchTrueFalseTotal
unless $_SELF->{"IGNORE"} 7 0 7
@res ? : 7 0 7
657 }
658  
659 7 _exit($?);
660 }
661  
662 sub _exit {
663 10 ▶ my ( $exit_status ) = @_;
Subroutine _exit: called CC=11 SCAR=32.2
664  
665 # Check for nested workers not yet joined.
666 10 ▶ MCE::Child->finish('MCE') if ( !$_SELF->{SIGNALED} && keys %{ $_LIST } );
Branch: 1 branch
BranchTrueFalseTotal
if not $_SELF->{"SIGNALED"} and keys %{$_LIST;} 0 10 10
Condition66%2/3
not $_SELF->{"SIGNALED"} and keys %{$_LIST;}
!ll&&!rl&&r
370
MC/DC0%0/2
not $_SELF->{"SIGNALED"} and keys %{$_LIST;}
not $_SELF->{"SIGNALED"} keys %{$_LIST;}
Truth table66%2/3
not(A) and B
A= not $_SELF->{"SIGNALED"}B= keys %{$_LIST;}
ABresult
0X0
100
111
667  
668 # Exit child process.
669 10 ▶ $SIG{__DIE__} = sub {} unless $_tid;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless $_tid 10 0 10
670 10 ▶ $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
671  
672 10 ▶ threads->exit($exit_status) if ( $INC{'threads.pm'} && $_is_MSWin32 );
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} and $_is_MSWin32 0 10 10
Condition33%1/3
$INC{"threads.pm"} and $_is_MSWin32
!ll&&!rl&&r
1000
MC/DC0%0/2
$INC{"threads.pm"} and $_is_MSWin32
$INC{"threads.pm"} $_is_MSWin32
Truth table33%1/3
A and B
A= $INC{"threads.pm"}B= $_is_MSWin32
ABresult
0X0
100
111
673  
674 my $posix_exit = ( exists $_SELF->{posix_exit} )
675 10 ▶ ? $_SELF->{posix_exit} : $_MNGD->{ $_SELF->{PKG} }{posix_exit};
Branch: 1 branch
BranchTrueFalseTotal
exists $_SELF->{'posix_exit'} ? : 0 10 10
676  
677 10 ▶ if ( $posix_exit && !$_SELF->{SIGNALED} && !$_is_MSWin32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posix_exit and not $_SELF->{"SIGNALED"} and not $_is_MSWin32) 0 10 10
Condition33%2/6
$posix_exit and not $_SELF->{"SIGNALED"} and not $_is_MSWin32
!ll&&!rl&&r
1000
$posix_exit and not $_SELF->{"SIGNALED"}
!ll&&!rl&&r
1000
MC/DC0%0/3
$posix_exit and not $_SELF->{"SIGNALED"} and not $_is_MSWin32
$posix_exit not $_SELF->{"SIGNALED"} not $_is_MSWin32
Truth table25%1/4
A and not(B) and not(C)
A= $posix_exitB= not $_SELF->{"SIGNALED"}C= not $_is_MSWin32
ABCresult
0XX0
10X0
1100
1111
678 0 eval { MCE::Mutex::Channel::_destroy() };
679 0 ▶ POSIX::_exit($exit_status) if $INC{'POSIX.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"POSIX.pm"} 0 0 0
680 0 CORE::kill('KILL', $$);
681 }
682  
683 10 CORE::exit($exit_status);
684 }
685  
686 sub _force_reap {
687 9 ▶ my ( $count, $pkg ) = ( 0, @_ );
Subroutine _force_reap: called CC=9 SCAR=40.6
688 9 ▶ return unless ( exists $_LIST->{$pkg} && $_LIST->{$pkg}->len() );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_LIST->{$pkg} and $_LIST->{$pkg}->len 9 0 9
Condition33%1/3
exists $_LIST->{$pkg} and $_LIST->{$pkg}->len
!ll&&!rl&&r
090
MC/DC0%0/2
exists $_LIST->{$pkg} and $_LIST->{$pkg}->len
exists $_LIST->{$pkg} $_LIST->{$pkg}->len
Truth table33%1/3
A and B
A= exists $_LIST->{$pkg}B= $_LIST->{$pkg}->len
ABresult
0X0
100
111
689  
690 0 for my $child ( $_LIST->{$pkg}->vals() ) {
691 0 ▶ next if $child->{IGNORE};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"IGNORE"} 0 0 0
692  
693 0 ▶ if ( $child->is_running() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->is_running) 0 0 0
694 0 ▶ sleep(0.015), CORE::kill('KILL', $child->pid())
Branch: 1 branch
BranchTrueFalseTotal
if CORE::kill "ZERO", $child->pid 0 0 0
695 if CORE::kill('ZERO', $child->pid());
696 0 $count++;
697 }
698 }
699  
700 0 $_LIST->{$pkg}->clear();
701  
702 0 ▶ warn "Finished with active child processes [$pkg] ($count)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $count and not $_is_MSWin32 0 0 0
Condition0%0/3
$count and not $_is_MSWin32
!ll&&!rl&&r
000
MC/DC0%0/2
$count and not $_is_MSWin32
$count not $_is_MSWin32
Truth table0%0/3
A and not(B)
A= $countB= not $_is_MSWin32
ABresult
0X0
100
111
703 if ( $count && !$_is_MSWin32 );
704  
705 0 return;
706 }
707  
708 sub _quit {
709 3 ▶ return MCE::Signal::defer($_[0]) if $MCE::Signal::IPC;
Subroutine _quit: called CC=4 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if $MCE::Signal::IPC 0 3 3
710  
711 3 alarm 0; my ( $name ) = @_;
712 3 $_SELF->{SIGNALED} = 1, $name =~ s/^SIG//;
713  
714 ▶ $SIG{$name} = sub {}, CORE::kill($name, -$$)
Subroutine __ANON__: not called CC=1 SCAR=0.0
715 3 ▶ if ( exists $SIG{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $SIG{$name} 3 0 3
716  
717 3 ▶ if ( ! $_SELF->{IGNORE} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_SELF->{"IGNORE"}) 3 0 3
718 3 my ( $pkg, $wrk_id ) = ( $_SELF->{PKG}, $_SELF->{WRK_ID} );
719 3 $_DATA->{$pkg}->set('R'.$wrk_id, '');
720 }
721  
722 3 _exit(0);
723 }
724  
725 sub _reap_child {
726 48 ▶ my ( $child, $wait_flag ) = @_;
Subroutine _reap_child: called CC=14 SCAR=36.9
727 48 ▶ return unless $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 48 48
728  
729 48 local @_ = $_DATA->{ $child->{PKG} }->get( $child->{WRK_ID}, $wait_flag );
730  
731 48 ▶ ( $child->{ERROR}, $child->{RESULT}, $child->{REAPED} ) =
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] ? : 33 15 48
Condition50%1/2
pop() || ''
l!l
048
MC/DC0%0/1
pop() || ''
pop()
Truth table50%1/2
A
A= pop()
Aresult
11
00
732 ( pop || '', length $_[0] ? pop : [], 1 );
733  
734 48 ▶ return if $child->{IGNORE};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"IGNORE"} 0 48 48
735  
736 48 ▶ my ( $exit, $err ) = ( $? || 0, $child->{ERROR} );
Condition50%1/2
$? || 0
l!l
048
MC/DC0%0/1
$? || 0
$?
Truth table50%1/2
A
A= $?
Aresult
11
00
737 48 my ( $code, $sig ) = ( $exit >> 8, $exit & 0x7f );
738  
739 48 ▶ if ( $code > 100 && !$err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code > 100 and not $err) 0 48 48
Condition33%1/3
$code > 100 and not $err
!ll&&!rl&&r
4800
MC/DC0%0/2
$code > 100 and not $err
$code > 100 not $err
Truth table33%1/3
A and not(B)
A= $code > 100B= not $err
ABresult
0X0
100
111
740 0 ▶ $code = 2, $sig = 1, $err = 'Child received SIGHUP' if $code == 101;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 101 0 0 0
741 0 ▶ $code = 2, $sig = 2, $err = 'Child received SIGINT' if $code == 102;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 102 0 0 0
742 0 ▶ $code = 2, $sig = 6, $err = 'Child received SIGABRT' if $code == 106;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 106 0 0 0
743 0 ▶ $code = 2, $sig = 11, $err = 'Child received SIGSEGV' if $code == 111;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 111 0 0 0
744 0 ▶ $code = 2, $sig = 15, $err = 'Child received SIGTERM' if $code == 115;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 115 0 0 0
745  
746 0 $child->{ERROR} = $err;
747 }
748  
749 48 ▶ if ( my $on_finish = $_MNGD->{ $child->{PKG} }{on_finish} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_finish = $$_MNGD{$child->{"PKG"}}{"on_finish"}) 9 39 48
750 $on_finish->(
751 $child->{WRK_ID}, $code, $child->{ident}, $sig, $err,
752 9 @{ $child->{RESULT} }
753 );
754 }
755  
756 48 return;
757 }
758  
759 sub _trap {
760 0 ▶ return MCE::Signal::defer($_[0]) if $MCE::Signal::IPC;
Subroutine _trap: not called CC=9 SCAR=45.0
Branch: 1 branch
BranchTrueFalseTotal
if $MCE::Signal::IPC 0 0 0
761  
762 0 alarm 0; my ( $exit_status, $name ) = ( 2, @_ );
763 0 $_SELF->{SIGNALED} = 1, $name =~ s/^SIG//;
764  
765 ▶ $SIG{$name} = sub {}, CORE::kill($name, -$$)
Subroutine __ANON__: not called CC=1 SCAR=0.0
766 0 ▶ if ( exists $SIG{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $SIG{$name} 0 0 0
767  
768 0 ▶ if ( $name eq 'HUP' ) { $exit_status = 101 }
Branch: 5 branches
BranchTrueFalseTotal
if ($name eq 'HUP') { } 0 0 0
elsif ($name eq 'INT') { } 0 0 0
elsif ($name eq 'ABRT') { } 0 0 0
elsif ($name eq 'SEGV') { } 0 0 0
elsif ($name eq 'TERM') { } 0 0 0
769 0 elsif ( $name eq 'INT' ) { $exit_status = 102 }
770 0 elsif ( $name eq 'ABRT' ) { $exit_status = 106 }
771 0 elsif ( $name eq 'SEGV' ) { $exit_status = 111 }
772 0 elsif ( $name eq 'TERM' ) { $exit_status = 115 }
773  
774 0 ▶ if ( ! $_SELF->{IGNORE} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_SELF->{"IGNORE"}) 0 0 0
775 0 my ( $pkg, $wrk_id ) = ( $_SELF->{PKG}, $_SELF->{WRK_ID} );
776 0 $_DATA->{$pkg}->set('R'.$wrk_id, '');
777 }
778  
779 0 _exit($exit_status);
780 }
781  
782 sub _wait_one {
783 4 ▶ my ( $pkg ) = @_;
Subroutine _wait_one: called CC=5 SCAR=16.1
784 4 my ( $list, $self, $wrk_id ) = ( $_LIST->{$pkg} ); local $!;
785  
786 4 while () {
787 124 $_DATA->{$pkg}->reap_data;
788 124 for my $child ( $list->vals() ) {
789 124 $wrk_id = $child->{WRK_ID};
790 124 ▶ return $list->del($wrk_id) if $child->{REAPED};
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"REAPED"} 0 124 124
791 124 ▶ $self = $list->del($wrk_id), last if waitpid($wrk_id, _WNOHANG);
Branch: 1 branch
BranchTrueFalseTotal
if waitpid $wrk_id, 1 4 120 124
792 }
793 124 ▶ last if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 4 120 124
794 120 sleep 0.030;
795 }
796  
797 4 _reap_child($self, 0);
798  
799 4 $self;
800 }
801  
802 ###############################################################################
803 ## ----------------------------------------------------------------------------
804 ## Delay implementation suited for MCE::Child.
805 ##
806 ###############################################################################
807  
808 package # hide from rpm
809 MCE::Child::_delay;
810  
811 sub new {
812 12 ▶ my ( $class, $chnl, $delay ) = @_;
Subroutine new: called CC=3 SCAR=11.3
813  
814 12 ▶ if ( !defined $delay ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $delay) 12 0 12
815 12 ▶ $delay = ($^O =~ /mswin|mingw|msys|cygwin/i) ? 0.015 : 0.008;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /mswin|mingw|msys|cygwin/i ? : 0 12 12
816 }
817  
818 12 $chnl->send(undef);
819  
820 12 bless [ $delay, $chnl ], $class;
821 }
822  
823 sub seconds {
824 0 ▶ my ( $self, $how_long ) = @_;
Subroutine seconds: not called CC=5 SCAR=34.0
825 0 ▶ my $delay = defined($how_long) ? $how_long : $self->[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $how_long ? : 0 0 0
826 0 my $lapse = $self->[1]->recv();
827 0 my $time = MCE::Util::_time();
828  
829 0 ▶ if ( !$delay || !defined $lapse ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $delay && defined $lapse) { } 0 0 0
elsif ($lapse + $delay - $time < 0) { } 0 0 0
Condition0%0/3
$delay && defined $lapse
!ll&&!rl&&r
000
MC/DC0%0/2
$delay && defined $lapse
$delay defined $lapse
Truth table0%0/3
A && B
A= $delayB= defined $lapse
ABresult
0X0
100
111
830 0 $lapse = $time;
831 }
832 elsif ( $lapse + $delay - $time < 0 ) {
833 0 $lapse += int( abs($time - $lapse) / $delay + 0.5 ) * $delay;
834 }
835  
836 0 $self->[1]->send( $lapse += $delay );
837  
838 0 return $lapse - $time;
839 }
840  
841 ###############################################################################
842 ## ----------------------------------------------------------------------------
843 ## Hash and ordhash implementations suited for MCE::Child.
844 ##
845 ###############################################################################
846  
847 package # hide from rpm
848 MCE::Child::_hash;
849  
850 12 ▶use Time::HiRes 'sleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
851  
852 use constant {
853 12 ▶ _WNOHANG => ( $INC{'POSIX.pm'} )
Branch: 2 branches
BranchTrueFalseTotal
$INC{'POSIX.pm'} ? : 0 12 12
$^O eq 'solaris' ? : 0 12 12
854 ? &POSIX::WNOHANG : ( $^O eq 'solaris' ) ? 64 : 1
855 12 ▶};
Subroutine BEGIN: called CC=3 SCAR=11.0
856  
857 sub new {
858 12 ▶ my ( $class, $chnl ) = @_;
Subroutine new: called CC=1 SCAR=0.0
859  
860 12 bless [ {}, $chnl ], shift;
861 }
862  
863 sub clear {
864 7 ▶ my ( $self ) = @_;
Subroutine clear: called CC=2 SCAR=7.0
865  
866 7 ▶ 1 while ( $self->[1]->recv2_nb() );
Branch: 1 branch
BranchTrueFalseTotal
$self->[1]->recv2_nb and do { '???' } 0 7 7
867  
868 7 %{ $self->[0] } = ();
869 }
870  
871 sub exists {
872 15 ▶ my ( $self, $key ) = @_;
Subroutine exists: called CC=2 SCAR=6.9
873  
874 15 ▶ while ( my $data = $self->[1]->recv2_nb() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->[1]->recv2_nb) 5 15 20
875 5 $self->[0]{ $data->[0] } = $data->[1];
876 }
877  
878 15 CORE::exists $self->[0]{ $key };
879 }
880  
881 sub get {
882 48 ▶ my ( $self, $wrk_id, $wait_flag ) = @_;
Subroutine get: called CC=12 SCAR=25.5
883  
884 48 ▶ if ( !CORE::exists $self->[0]{ 'R'.$wrk_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[0]{'R' . $wrk_id}) 27 21 48
885 27 ▶ while ( my $data = $self->[1]->recv2_nb() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->[1]->recv2_nb) 13 27 40
886 13 $self->[0]{ $data->[0] } = $data->[1];
887 }
888 }
889  
890 48 ▶ if ( $wait_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait_flag) 40 8 48
891 40 local $!;
892 40 ▶ ( CORE::exists $self->[0]{ 'R'.$wrk_id } ) ? waitpid($wrk_id, 0) : do {
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[0]{'R' . $wrk_id} ? : 26 14 40
893 14 while () {
894 42 my $data = $self->[1]->recv2_nb();
895 42 ▶ if ( !defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 14 28 42
896 14 ▶ last if waitpid($wrk_id, _WNOHANG);
Branch: 1 branch
BranchTrueFalseTotal
if waitpid $wrk_id, 1 0 14 14
897 14 sleep(0.0009), next;
898 }
899 28 $self->[0]{ $data->[0] } = $data->[1];
900 28 ▶ waitpid($wrk_id, 0), last if $data->[0] eq 'R'.$wrk_id;
Branch: 1 branch
BranchTrueFalseTotal
if $data->[0] eq 'R' . $wrk_id 14 14 28
901 }
902 14 ▶ if ( !CORE::exists $self->[0]{ 'R'.$wrk_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[0]{'R' . $wrk_id}) 0 14 14
903 0 ▶ while ( my $data = $self->[1]->recv2_nb() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->[1]->recv2_nb) 0 0 0
904 0 $self->[0]{ $data->[0] } = $data->[1];
905 }
906 }
907 };
908 }
909  
910 48 my $result = delete $self->[0]{ 'R'.$wrk_id };
911 48 my $error = delete $self->[0]{ 'S'.$wrk_id };
912  
913 48 ▶ $result = '' unless ( defined $result );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 48 48
914 48 ▶ $error = '' unless ( defined $error );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 0 48 48
915  
916 48 return ( $result, $error );
917 }
918  
919 sub reap_data {
920 253 ▶ my ( $self ) = @_;
Subroutine reap_data: called CC=5 SCAR=16.1
921  
922 253 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 65 188 253
923 65 my @ret;
924 65 ▶ while ( my $data = $self->[1]->recv2_nb() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->[1]->recv2_nb) 39 65 104
925 39 ▶ push @ret, substr($data->[0], 1) if substr($data->[0], 0, 1) eq 'R';
Branch: 1 branch
BranchTrueFalseTotal
if substr($data->[0], 0, 1) eq "R" 4 35 39
926 39 $self->[0]{ $data->[0] } = $data->[1];
927 }
928 65 return @ret;
929 }
930  
931 188 ▶ while ( my $data = $self->[1]->recv2_nb() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->[1]->recv2_nb) 16 188 204
932 16 $self->[0]{ $data->[0] } = $data->[1];
933 }
934  
935 188 return;
936 }
937  
938 sub set {
939 20 ▶ $_[0]->[1]->send2([ $_[1], $_[2] ]);
Subroutine set: called CC=1 SCAR=0.0
940 }
941  
942 package # hide from rpm
943 MCE::Child::_ordhash;
944  
945 12 ▶sub new { bless [ {}, [], {}, 0 ], shift; } # data, keys, indx, gcnt
Subroutine new: called CC=1 SCAR=0.0
946 0 ▶sub exists { CORE::exists $_[0]->[0]{ $_[1] }; }
Subroutine exists: not called CC=1 SCAR=6.9
947 0 ▶sub get { $_[0]->[0]{ $_[1] }; }
Subroutine get: not called CC=1 SCAR=6.9
948 22 ▶sub len { scalar keys %{ $_[0]->[0] }; }
Subroutine len: called CC=1 SCAR=0.0
949  
950 sub clear {
951 0 ▶ my ( $self ) = @_;
Subroutine clear: not called CC=1 SCAR=6.9
952 0 %{ $self->[0] } = @{ $self->[1] } = %{ $self->[2] } = (), $self->[3] = 0;
953  
954 0 return;
955 }
956  
957 sub del {
958 48 ▶ my ( $self, $key ) = @_;
Subroutine del: called CC=5 SCAR=16.1
959 48 ▶ return undef unless defined( my $off = delete $self->[2]{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $off = delete $self->[2]{$key}) 0 48 48
960  
961 # tombstone
962 48 $self->[1][$off] = undef;
963  
964 # GC keys and refresh index
965 48 ▶ if ( ++$self->[3] > @{ $self->[1] } * 0.667 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->[3] > @{$self->[1];} * "0.667") 20 28 48
966 20 my ( $keys, $indx ) = ( $self->[1], $self->[2] );
967 20 my $i; $i = $self->[3] = 0;
968 20 for my $k ( @{ $keys } ) {
969 48 ▶ $keys->[$i] = $k, $indx->{$k} = $i++ if defined($k);
Branch: 1 branch
BranchTrueFalseTotal
if defined $k 0 48 48
970 }
971 20 splice @{ $keys }, $i;
972 }
973  
974 48 delete $self->[0]{$key};
975 }
976  
977 sub set {
978 55 ▶ my ( $self, $key ) = @_;
Subroutine set: called CC=2 SCAR=7.0
979 55 ▶ $self->[0]{$key} = $_[2], return 1 if exists($self->[0]{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->[0]{$key} 0 55 55
980  
981 55 $self->[2]{$key} = @{ $self->[1] }; push @{ $self->[1] }, $key;
982 55 $self->[0]{$key} = $_[2];
983  
984 55 return 1;
985 }
986  
987 sub vals {
988 157 ▶ my ( $self ) = @_;
Subroutine vals: called CC=2 SCAR=7.9
989 $self->[3]
990 0 ? @{ $self->[0] }{ grep defined($_), @{ $self->[1] } }
991 157 ▶ : @{ $self->[0] }{ @{ $self->[1] } };
Branch: 1 branch
BranchTrueFalseTotal
$self->[3] ? : 0 157 157
992 }
993  
994 12 1;
995  
996 __END__
« MCE/Core/Manager.pm ↑ Summary MCE/Queue.pm »