blib/lib/Ekorn/Pipeline/Workers.pm

filter: Statementstmt 99.0% 202 / 204 Branchbran 98.6% 75 / 76 Conditioncond 92.8% 52 / 56 MC/DCmcdc 84.8% 28 / 33 Subroutinesub 100% 36 / 36 Podpod 100% 3 / 3 totaltotal 97.0% 396 / 408 CC 63 SCAR 17.1
CC
63
Cov
98%
_check_name
13.0
__ANON__
8.0
_check_pool_args
8.0
CRAP 5.5
« blib/lib/Ekorn/Pipeline/Workers/Worker.pm ↑ Summary blib/lib/Ekorn/Pipeline/Workers/Wire.pm »
1 203 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Workers;
4  
5 # Stages running a function for each value in a pool of
6 # workers, each a new perl, started fresh, not a clone of this one, and
7 # talked to on a socket of its own, so they run alike on Unix and on
8 # Windows. As a worker is a new perl, the block is named, as
9 # 'Module::function', which the worker loads, never a closure; so are
10 # init and done.
11 #
12 # - in_workers($n, $block): results in the order the values came;
13 # - in_workers_as_ready($n, $block): results as they finish;
14 # - once_per($key, $n, $block, join => ...): the block run once per key,
15 # in the workers, each value joined with its key's results.
16 #
17 # All three are made on Ekorn::Pipeline::Workers::Pool, below, which
18 # knows nothing of order or keys: it takes values tagged by its caller,
19 # and gives back what each finished with, tagged.
20  
21 203 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 203 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 203 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 203 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 203 ▶use Ekorn::Pipeline;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 203 ▶use Ekorn::Pipeline::Context qw(kept_origin);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 203 ▶use Ekorn::Pipeline::Errors ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 203 ▶use Ekorn::Pipeline::Gather qw(gather);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 203 ▶use Ekorn::Pipeline::Stop ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 203 ▶use Ekorn::Pipeline::Workers::Pool ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 203 ▶use Ekorn::Pipeline::Workers::Wire qw(resolve);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 203 our $VERSION = 'v0.1.0';
34  
35 # Its whole purpose is its three stages, so all three are exported by
36 # default, as Futures' are (Ekorn-Pipeline's 2026-0064, D12).
37 203 our @EXPORT = qw(in_workers in_workers_as_ready once_per); ## no critic (Modules::ProhibitAutomaticExportation)
38  
39 # Croak at the line that called Ekorn::Pipeline.
40 our @CARP_NOT
41 203 = (@Ekorn::Pipeline::Errors::INTERNAL, 'Ekorn::Pipeline::Workers::Pool');
42  
43 # For tests: the pids of every worker started, and of every worker
44 # reaped, with waitpid's answer, which the pool keeps: kill 0 can't say
45 # on Windows, where a pid is soon another process's. A worker the pool
46 # killed, of which Windows leaves waitpid nothing (-1), as Perl's kill
47 # there has already collected it, is put down by its pid: it's gone.
48 203 our @FORKED;
49 203 our %REAPED;
50  
51  
52 # Check the stage's arguments, and give back the block and the options
53 # with the block, init and done as names, which is how the pool sends
54 # them to its workers.
55 # The number of workers, checked first, as the window's default is made
56 # from it.
57 5141 ▶sub _check_n ($n) {
Subroutine _check_n: called CC=3 SCAR=11.0
58 5141 ▶ croak 'Expected a number of workers of 1 or more'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ / \A [1-9] \d* \z /ux 74 5067 5141
Condition100%3/3
defined $n and $n =~ / \A [1-9] \d* \z /ux
!ll&&!rl&&r
37375067
MC/DC100%2/2
defined $n and $n =~ / \A [1-9] \d* \z /ux
defined $n $n =~ / \A [1-9] \d* \z /ux
Truth table100%3/3
A and B
A= defined $nB= $n =~ / \A [1-9] \d* \z /ux
ABresult
0X0
100
111
59 unless defined $n && $n =~ m( \A [1-9] \d* \z )x;
60 5067 return;
61 }
62  
63  
64 # The options each stage takes, so that one misspelt croaks, rather
65 # than giving the default (2026-0065's D19).
66 203 my %OPTIONS = map { $_ => 1 } qw(init init_args done window);
67 203 my %ONCE_PER_OPTIONS = (%OPTIONS, map { $_ => 1 } qw(join hold ordered));
68  
69  
70 5067 ▶sub _check_options ($known, %options) {
Subroutine _check_options: called CC=5 SCAR=16.1
71 5067 my($unknown) = sort grep { !$known->{$_} } keys %options;
72 5067 ▶ croak "Expected no option $unknown" if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 819 4248 5067
73 4248 for my $count (qw(window hold)) {
74 croak "Expected $count to be a whole number"
75 8314 ▶ if defined $options{$count} && $options{$count} !~ m( \A \d+ \z )x;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{$count} and not $options{$count} =~ / \A \d+ \z /ux 273 8041 8314
Condition100%3/3
defined $options{$count} and not $options{$count} =~ / \A \d+ \z /ux
!ll&&!rl&&r
7808233273
MC/DC100%2/2
defined $options{$count} and not $options{$count} =~ / \A \d+ \z /ux
defined $options{$count} not $options{$count} =~ / \A \d+ \z /ux
Truth table100%3/3
A and not(B)
A= defined $options{$count}B= not $options{$count} =~ / \A \d+ \z /ux
ABresult
0X0
100
111
76 }
77 3975 return;
78 }
79  
80  
81 3901 ▶sub _check_pool_args ($n, $block, $window, %options) {
Subroutine _check_pool_args: called CC=8 SCAR=20.8
82 3901 $block = _check_name('the function' => $block);
83 2917 ▶ croak 'Expected a window at least the number of workers' if $window < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $window < $n 128 2789 2917
84 2789 for my $part (qw(init done)) {
85 $options{$part} = _check_name($part => $options{$part})
86 5396 ▶ if defined $options{$part};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{$part} 722 4674 5396
87 }
88 croak 'Expected init_args to be an array reference'
89 2516 ▶ if defined $options{init_args} && ref $options{init_args} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"init_args"} and ref $options{"init_args"} ne "ARRAY" 37 2479 2516
Condition100%3/3
defined $options{"init_args"} and ref $options{"init_args"} ne "ARRAY"
!ll&&!rl&&r
199148837
MC/DC100%2/2
defined $options{"init_args"} and ref $options{"init_args"} ne "ARRAY"
defined $options{"init_args"} ref $options{"init_args"} ne "ARRAY"
Truth table100%3/3
A and B
A= defined $options{"init_args"}B= ref $options{"init_args"} ne "ARRAY"
ABresult
0X0
100
111
90 # Without an init, they'd be given to nothing (2026-0065's D23).
91 croak 'Expected init with init_args'
92 2479 ▶ if defined $options{init_args} && !defined $options{init};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"init_args"} and not defined $options{"init"} 182 2297 2479
Condition100%3/3
defined $options{"init_args"} and not defined $options{"init"}
!ll&&!rl&&r
1991306182
MC/DC100%2/2
defined $options{"init_args"} and not defined $options{"init"}
defined $options{"init_args"} not defined $options{"init"}
Truth table100%3/3
A and not(B)
A= defined $options{"init_args"}B= not defined $options{"init"}
ABresult
0X0
100
111
93 2297 return ($block, %options);
94 }
95  
96  
97 # A block, init or done names a function a new perl can load: as a
98 # string, 'Module::function', or a code reference to a named sub in a
99 # module, \&Module::function, which gives its name (D7, as amended). An
100 # anonymous sub has no name a worker could load, and may close over
101 # what's only in this perl; a sub of the program's script, in main, is in
102 # no module a worker loads. It's loaded here too, so a name that's wrong
103 # croaks as the stage is made; and a worker loads a function's module by
104 # the function's package, so a sub whose package wasn't loaded from a
105 # file of its name, defined in the program's script, or in a second
106 # package of a module's file, croaks here too, rather than in every
107 # worker (2026-0065's D21).
108 4623 ▶sub _check_name ($part, $given) {
Subroutine _check_name: called CC=13 SCAR=25.7
109 4623 my $name = $given;
110 4623 ▶ if (ref $given eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $given eq "CODE") 980 3643 4623
111 980 $name = Sub::Util::subname($given);
112 980 ▶ croak "Expected $part to be a named sub in a module, not an anonymous one: "
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ / :: __ANON__ \z /ux 182 798 980
113 . 'a worker is a new perl, which it can\'t reach'
114 if $name =~ m( :: __ANON__ \z )x;
115 798 ▶ croak "Expected $part to be a sub in a module, not the program's $name: "
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ / \A main :: /ux 91 707 798
116 . 'a worker is a new perl, which loads modules, not the program'
117 if $name =~ m( \A main :: )x;
118 }
119 4350 ▶ croak "Expected $part to be given by name, as 'Module::function', "
Branch: 1 branch
BranchTrueFalseTotal
if ref $name or not defined $name or not $name =~ / \A \w+ (?: :: \w+ )+ \z /ux 128 4222 4350
Condition83%5/6
ref $name or not defined $name or not $name =~ / \A \w+ (?: :: \w+ )+ \z /ux
l!l&&r!l&&!r
91374222
ref $name or not defined $name
l!l&&r!l&&!r
9104259
MC/DC66%2/3
ref $name or not defined $name or not $name =~ / \A \w+ (?: :: \w+ )+ \z /ux
ref $name not defined $name not $name =~ / \A \w+ (?: :: \w+ )+ \z /ux
Truth table75%3/4
A or not(B) or not(C)
A= ref $nameB= not defined $nameC= not $name =~ / \A \w+ (?: :: \w+ )+ \z /ux
ABCresult
1XX1
01X1
0011
0000
120 . 'or as a code reference to one: a worker is a new perl, which a closure in this one can\'t reach'
121 if ref $name || !defined $name || $name !~ m( \A \w+ (?: :: \w+ )+ \z )x;
122 4222 my($module) = $name =~ m( \A (.+) :: \w+ \z )x;
123 4222 my $code = eval { resolve($name) };
124 4222 chomp(my $why = $@);
125 4222 ▶ croak "Expected ${part}'s module, $module, to load: $why"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code or $why eq "" 219 4003 4222
Condition100%3/3
defined $code or $why eq ""
l!l&&r!l&&!r
3821182219
MC/DC100%2/2
defined $code or $why eq ""
defined $code $why eq ""
Truth table100%3/3
A or B
A= defined $codeB= $why eq ""
ABresult
1X1
011
000
126 unless defined $code || $why eq '';
127 4003 ▶ croak "Expected $part to exist, but there's no $name" unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 182 3821 4003
128 3821 (my $file = "$module.pm") =~ s( :: )(/)gx;
129 3821 ▶ unless ($INC{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$file}) 455 3366 3821
130 455 ▶ croak "Expected $part to be a sub in a module, not the program's $name: "
Branch: 1 branch
BranchTrueFalseTotal
if $module eq "main" or B::svref_2object($code)->FILE eq $0 364 91 455
Condition100%3/3
$module eq "main" or B::svref_2object($code)->FILE eq $0
l!l&&r!l&&!r
9127391
MC/DC100%2/2
$module eq "main" or B::svref_2object($code)->FILE eq $0
$module eq "main" B::svref_2object($code)->FILE eq $0
Truth table100%3/3
A or B
A= $module eq "main"B= B::svref_2object($code)->FILE eq $0
ABresult
1X1
011
000
131 . 'a worker is a new perl, which loads modules, not the program'
132 if $module eq 'main' || B::svref_2object($code)->FILE eq $0;
133 91 croak "Expected $part to be in a module of its own name, $module, "
134 . 'which a worker can load';
135 }
136 3366 return $name;
137 }
138  
139  
140 # As in_workers, but passing each value's results on as soon as they're
141 # back, in the order they finish, so one slow value holds up none of the
142 # rest.
143 746 ▶sub in_workers_as_ready ($n, $block, %options) {
Subroutine in_workers_as_ready: called CC=1 SCAR=0.0
Pod documented
144 746 return _in_workers($n, $block, 0, %options);
145 }
146  
147  
148 3520 ▶sub in_workers ($n, $block, %options) {
Subroutine in_workers: called CC=1 SCAR=0.0
Pod documented
149 3520 return _in_workers($n, $block, 1, %options);
150 }
151  
152  
153 # Both: ordered, or as ready, which is the stage's to say, not an
154 # option (2026-0065's D20).
155 4266 ▶sub _in_workers ($n, $block, $ordered, %options) {
Subroutine _in_workers: called CC=2 SCAR=6.9
156 4266 _check_n($n);
157 4192 _check_options(\%OPTIONS, %options);
158 3282 ▶ my $window = $options{window} // 2 * $n;
Condition100%2/2
$options{"window"} // 2 * $n
l!l
1343148
MC/DC100%1/1
$options{"window"} // 2 * $n
$options{"window"}
Truth table100%2/2
A
A= $options{"window"}
Aresult
11
00
159 3282 ($block, %options) = _check_pool_args($n, $block, $window, %options);
160 1969 ▶ return gather {
Subroutine __ANON__: called CC=3 SCAR=11.0
161 1969 ▶ my $pool = Ekorn::Pipeline::Workers::Pool->new(
Branch: 1 branch
BranchTrueFalseTotal
$ordered ? : 1587 382 1969
162 $n, $block, %options,
163 stage => $ordered ? 'in_workers' : 'in_workers_as_ready',
164 );
165 1969 my @pending; # in order: {seq, state => sent|done, kind, payload}
166 my @finished; # done, in the order they finished
167 1969 my %by_seq;
168 1969 my $next_seq = 0;
169  
170 22224 ▶ my $collect = sub ($timeout) {
Subroutine __ANON__: called CC=2 SCAR=6.9
171 22224 for my $reply ($pool->poll($timeout)) {
172 12644 my($seq, $kind, $payload) = @$reply;
173 12644 my $job = $by_seq{$seq};
174 12644 @$job{qw(state kind payload)} = ('done', $kind, $payload);
175 12644 push @finished, $job;
176 }
177 1969 };
178  
179 # The next finished job whose results may go on: ordered, the
180 # oldest, once it's done; unordered, the first to have finished.
181 my $next_done
182 = $ordered
183 35041 ▶ ? sub () { @pending && $pending[0]{state} eq 'done' ? $pending[0] : undef }
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@pending && $pending[0]{'state'} eq 'done' ? : 13065 21976 35041
Condition100%3/3
@pending and $pending[0]{"state"} eq "done"
!ll&&!rl&&r
8512112513065
MC/DC100%2/2
@pending and $pending[0]{"state"} eq "done"
@pending $pending[0]{"state"} eq "done"
Truth table100%3/3
A and B
A= @pendingB= $pending[0]{"state"} eq "done"
ABresult
0X0
100
111
184 1969 ▶ : sub () { $finished[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$ordered ? : 1587 382 1969
185  
186 # Take the finished jobs that may go on, adding their results to
187 # @$results, which a flush's takes share; a stop carries all of
188 # them (2026-0065's D17). An error stops the taking; it's thrown
189 # once nothing this take took is left to pass on. In a flush,
190 # what earlier takes took is lost with it, as a flush can't pass
191 # values on and then die (Ekorn-Pipeline's 2026-0061).
192 18328 ▶ my $take = sub ($results) {
Subroutine __ANON__: called CC=6 SCAR=17.9
193 18328 my $before = @$results;
194 18328 ▶ while (my $job = $next_done->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $job = &$next_done()) 12519 17860 30379
195 12519 ▶ return if $job->{kind} eq 'err' && @$results > $before;
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"kind"} eq "err" and @$results > $before 7 12512 12519
Condition100%3/3
$job->{"kind"} eq "err" and @$results > $before
!ll&&!rl&&r
122043087
MC/DC100%2/2
$job->{"kind"} eq "err" and @$results > $before
$job->{"kind"} eq "err" @$results > $before
Truth table100%3/3
A and B
A= $job->{"kind"} eq "err"B= @$results > $before
ABresult
0X0
100
111
196 12512 @pending = grep { $_ != $job } @pending;
197 12512 @finished = grep { $_ != $job } @finished;
198 12512 delete $by_seq{$job->{seq}};
199 12512 ▶ die $job->{payload} if $job->{kind} eq 'err'; ## no critic (ErrorHandling::RequireCarping): the worker's error as it was
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"kind"} eq "err" 308 12204 12512
200 12204 ▶ if ($job->{kind} eq 'stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"kind"} eq "stop") 153 12051 12204
201 153 @pending = @finished = ();
202 Ekorn::Pipeline::Stop->throw(
203 @$results,
204 153 $job->{origin}->gives(@{ $job->{payload} }),
205 );
206 }
207 12051 push @$results, $job->{origin}->gives(@{ $job->{payload} });
208 }
209 17860 return;
210 1969 };
211  
212 13823 ▶ my $step = sub ($value) {
Subroutine __ANON__: called CC=3 SCAR=11.0
213 13823 my $job = {seq => $next_seq++, state => 'sent', origin => kept_origin};
214 13823 push @pending, $job;
215 13823 $by_seq{$job->{seq}} = $job;
216 13823 $pool->submit($job->{seq}, $value, _where());
217 13631 $collect->(0);
218 # Wait while the window's full and nothing may go on yet.
219 13631 ▶ $collect->(undef) while @pending >= $window && !$next_done->();
Branch: 1 branch
BranchTrueFalseTotal
@pending >= $window and not &$next_done() and do { &$collect(undef) } 3897 13631 17528
Condition100%3/3
@pending >= $window and not &$next_done()
!ll&&!rl&&r
1060930223897
MC/DC100%2/2
@pending >= $window and not &$next_done()
@pending >= $window not &$next_done()
Truth table100%3/3
A and not(B)
A= @pending >= $windowB= not &$next_done()
ABresult
0X0
100
111
220 13631 $take->(\my @results);
221 13389 return @results;
222 1969 };
223  
224 1451 ▶ my $flush = sub () {
Subroutine __ANON__: called CC=3 SCAR=11.0
225 1451 my @results;
226 1451 ▶ while (@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 4697 1232 5929
227 4697 ▶ $collect->(undef) unless $next_done->();
Branch: 1 branch
BranchTrueFalseTotal
unless &$next_done() 4696 1 4697
228 4697 $take->(\@results);
229 }
230 1232 return @results;
231 1969 };
232  
233 1836 ▶ my $end = sub ($how) {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 1836 $pool->shut_down($how);
235 1836 @pending = @finished = ();
236 1836 %by_seq = ();
237 1969 };
238  
239 1969 return {step => $step, flush => $flush, end => $end, holds => 1};
240 1769 };
241 }
242  
243  
244 # The block, given each distinct key once, in the workers; each value
245 # passed on as join gives it, from the value and its key's results. A
246 # key asked for while it's still being looked up waits for that lookup,
247 # rather than starting another. Keys are kept, with their results, for
248 # the run.
249 875 ▶sub once_per ($key_of, $n, $block, %options) {
Subroutine once_per: called CC=7 SCAR=19.5
Pod documented
250 875 _check_n($n);
251 875 _check_options(\%ONCE_PER_OPTIONS, %options);
252 693 ▶ my $window = $options{window} // 2 * $n; # keys in flight at most
Condition50%1/2
$options{"window"} // 2 * $n
l!l
0693
MC/DC0%0/1
$options{"window"} // 2 * $n
$options{"window"}
Truth table50%1/2
A
A= $options{"window"}
Aresult
11
00
253 693 ▶ my $hold = $options{hold} // 1000; # values waiting, before we wait
Condition100%2/2
$options{"hold"} // 1000
l!l
99594
MC/DC100%1/1
$options{"hold"} // 1000
$options{"hold"}
Truth table100%2/2
A
A= $options{"hold"}
Aresult
11
00
254 693 ▶ my $ordered = $options{ordered} // 1;
Condition100%2/2
$options{"ordered"} // 1
l!l
114579
MC/DC100%1/1
$options{"ordered"} // 1
$options{"ordered"}
Truth table100%2/2
A
A= $options{"ordered"}
Aresult
11
00
255 693 ▶ my $join = $options{join} // sub ($value, @found) { @found };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$options{"join"} // sub ($value, @found) { @found; }
l!l&&r!l&&!r
2014920
MC/DC0%0/2
$options{"join"} // sub ($value, @found) { @found; }
$options{"join"} sub ($value, @found) { @found; }
Truth table66%2/3
A // B
A= $options{"join"}B= sub ($value, @found) { @found; }
ABresult
1X1
011
000
256 693 ▶ croak 'Expected a code reference for the key' unless ref $key_of eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key_of eq "CODE" 37 656 693
257 656 ▶ croak 'Expected a code reference to join' unless ref $join eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $join eq "CODE" 37 619 656
258 619 delete @options{qw(hold ordered join)};
259 619 ($block, %options) = _check_pool_args($n, $block, $window, %options);
260 513 ▶ return gather {
Subroutine __ANON__: called CC=1 SCAR=0.0
261 513 my $pool = Ekorn::Pipeline::Workers::Pool->new(
262 $n, $block, %options,
263 stage => 'once_per',
264 what => 'key',
265 );
266 513 my %found; # key => [kind, payload], once its lookup's back
267 my %asked; # key => 1, while its lookup's in flight
268 513 my @waiting; # in order: [value, key]
269  
270 10524 ▶ my $collect = sub ($timeout) {
Subroutine __ANON__: called CC=2 SCAR=6.9
271 10524 for my $reply ($pool->poll($timeout)) {
272 1258 my($key, $kind, $payload) = @$reply;
273 1258 $found{$key} = [$kind, $payload];
274 1258 delete $asked{$key};
275 }
276 513 };
277  
278 # The waiting values whose keys are back, joined, added to
279 # @$results, as in_workers' take adds them: ordered, from the
280 # oldest, stopping at the first still waiting; unordered, all of
281 # them. A key's error is thrown once nothing this take joined is
282 # left to pass on; its stop, with all of @$results, its values
283 # as they were, not joined.
284 10774 ▶ my $take = sub ($results) {
Subroutine __ANON__: called CC=7 SCAR=19.5
285 10774 my $before = @$results;
286 10774 my @still;
287 10774 ▶ while (my $wait = shift @waiting) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wait = shift @waiting) 80139 4983 85122
288 80139 my($value, $key, $origin) = @$wait;
289 80139 my $found = $found{$key};
290 80139 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 70690 9449 80139
291 70690 ▶ if ($ordered) { unshift @waiting, $wait; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($ordered) 5729 64961 70690
292 64961 push @still, $wait;
293 64961 next;
294 }
295 9449 my($kind, $payload) = @$found;
296 9449 ▶ if ($kind eq 'err') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "err") 42 9407 9449
297 0 ▶ if (@$results > $before) { unshift @waiting, $wait; last }
Branch: 1 branch
BranchTrueFalseTotal
if (@$results > $before) 0 42 42
298 42 die $payload; ## no critic (ErrorHandling::RequireCarping): the worker's error as it was
299 }
300 9407 ▶ if ($kind eq 'stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "stop") 20 9387 9407
301 20 @waiting = @still = ();
302 20 Ekorn::Pipeline::Stop->throw(@$results, $origin->gives(@$payload));
303 }
304 9387 local $_ = $value;
305 9387 push @$results, $origin->gives($join->($value, @$payload));
306 }
307 10712 unshift @waiting, @still;
308 10712 return;
309 513 };
310  
311 9734 ▶ my $step = sub ($value) {
Subroutine __ANON__: called CC=8 SCAR=20.8
312 9734 my $key = do { local $_ = $value; $key_of->($value) };
313 9734 ▶ croak 'Expected a key for each value, not undef' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 37 9697 9734
314 9697 push @waiting, [$value, $key, kept_origin];
315 9697 ▶ unless ($found{$key} || $asked{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found{$key} or $asked{$key}) 1493 8204 9697
Condition100%3/3
$found{$key} or $asked{$key}
l!l&&r!l&&!r
354746571493
MC/DC100%2/2
$found{$key} or $asked{$key}
$found{$key} $asked{$key}
Truth table100%3/3
A or B
A= $found{$key}B= $asked{$key}
ABresult
1X1
011
000
316 1493 $asked{$key} = 1;
317 1493 $pool->submit($key, $key, _where());
318 }
319 9605 $collect->(0);
320 # Wait while the window's full of keys in flight, or too many
321 # values wait behind one whose key isn't back.
322 $collect->(undef)
323 while %asked
324 && (keys %asked >= $window
325 9605 ▶ || (@waiting >= $hold && !$found{$waiting[0][1]}));
Branch: 1 branch
BranchTrueFalseTotal
%asked and keys %asked >= $window || @waiting >= $hold && !$found{$waiting[0][1]} and do { &$collect(undef) } 88 9605 9693
Condition88%8/9
%asked and keys %asked >= $window || @waiting >= $hold && !$found{$waiting[0][1]}
!ll&&!rl&&r
2706689988
keys %asked >= $window or @waiting >= $hold and not $found{$waiting[0][1]}
l!l&&r!l&&!r
8266899
@waiting >= $hold and not $found{$waiting[0][1]}
!ll&&!rl&&r
689906
MC/DC75%3/4
%asked and keys %asked >= $window || @waiting >= $hold && !$found{$waiting[0][1]}
%asked keys %asked >= $window @waiting >= $hold not $found{$waiting[0][1]}
Truth table80%4/5
A and B or C and not(D)
A= %askedB= keys %asked >= $windowC= @waiting >= $holdD= not $found{$waiting[0][1]}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
326 9605 $take->(\my @results);
327 9580 return @results;
328 513 };
329  
330 # Take as the keys come back, so the flush gives its values in
331 # the order they could go on. An error after values taken here
332 # is thrown at the next take, and they're lost with the run: a
333 # flush can't pass values on and then die. A stop carries them.
334 344 ▶ my $flush = sub () {
Subroutine __ANON__: called CC=3 SCAR=11.0
335 344 my @results;
336 344 ▶ while (@waiting) {
Branch: 1 branch
BranchTrueFalseTotal
if (@waiting) 1169 307 1476
337 1169 $take->(\@results);
338 1132 ▶ $collect->(undef) if @waiting;
Branch: 1 branch
BranchTrueFalseTotal
if @waiting 831 301 1132
339 }
340 307 return @results;
341 513 };
342  
343 452 ▶ my $end = sub ($how) {
Subroutine __ANON__: called CC=1 SCAR=0.0
344 452 $pool->shut_down($how);
345 452 %found = %asked = ();
346 452 @waiting = ();
347 513 };
348  
349 513 return {step => $step, flush => $flush, end => $end, holds => 1};
350 528 };
351 }
352  
353  
354 # Where the value being given to the stage stands in its run, as the
355 # block would see it, run in the stage: the run's context, whether a
356 # flush's values are going through, and the result's number and
357 # original. Asking for the original calls the source's original part,
358 # which a run otherwise calls only when a stage asks.
359 15316 ▶sub _where () {
Subroutine _where: called CC=1 SCAR=0.0
360 return {
361 15316 context => Ekorn::Pipeline::run_context(),
362 ending => Ekorn::Pipeline::ending(),
363 number => Ekorn::Pipeline::Context::result_number(),
364 original => Ekorn::Pipeline::Context::result_original(),
365 };
366 }
367  
368 203 1;
369  
370 __END__
« blib/lib/Ekorn/Pipeline/Workers/Worker.pm ↑ Summary blib/lib/Ekorn/Pipeline/Workers/Wire.pm »