lib/Parallel/WorkUnit.pm

filter: Statementstmt 76.8% 336 / 437 Branchbran 62.3% 126 / 202 Conditioncond 40.0% 6 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 92.4% 49 / 53 Podpod 100% 11 / 11 totaltotal 72.9% 531 / 728 CC 113 SCAR 26.2
CC
113
Cov
72%
_set_anyevent
132.0
waitall
13.7
async
8.5
CRAP 13.7
« blib/lib/Parallel/WorkUnit.pm ↑ Summary blib/lib/Parallel/WorkUnit/Procedural.pm »
1  
2 # Copyright (C) 2015-2020 Joelle Maslak
3 # All Rights Reserved - See License
4 #
5  
6 package Parallel::WorkUnit;
7 225 $Parallel::WorkUnit::VERSION = '2.202270';
8 225 ▶use v5.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Provide multi-paradigm forking with ability to pass back data
11  
12 225 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 225 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 225 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 225 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 225 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 225 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 225 ▶use IO::Pipely qw(pipely);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 225 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 225 ▶use POSIX ':sys_wait_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 225 ▶use Scalar::Util qw(blessed reftype weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 225 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 225 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 225 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 225 my @ALL_WU; # Holds all active work units so child processes can't
31 # mess with parent work units
32 # Note it holds a reference (strong) to a reference
33 # (weak).
34  
35  
36 sub use_anyevent {
37 4431 ▶ if ( $#_ == 0 ) {
Subroutine use_anyevent: called CC=3 SCAR=19.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 4431 0 4431
elsif ($#_ == 1) { } 0 0 0
38 4431 return shift->{use_anyevent};
39 } elsif ( $#_ == 1 ) {
40 0 my ( $self, $val ) = @_;
41  
42 0 my ($old_val) = $self->{use_anyevent};
43 0 $self->{use_anyevent} = $val;
44  
45 # Trigger
46 0 $self->_set_anyevent( $val, $old_val );
47  
48 0 return $val;
49 } else {
50 0 confess("Invalid call");
51 }
52 }
53  
54 # XXX: Add validation that _cv is a Maybe[AnyEvent::CondVar]
55 sub _cv {
56 3193 ▶ if ( $#_ == 0 ) {
Subroutine _cv: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 2977 216 3193
elsif ($#_ == 1) { } 216 0 216
57 2977 return shift->{_cv};
58 } elsif ( $#_ == 1 ) {
59 216 my ( $self, $val ) = @_;
60 216 $self->{_cv} = $val;
61 216 return $val;
62 } else {
63 0 confess("Invalid call");
64 }
65 }
66  
67 # XXX: Add validation that _last_error is a Maybe[Str]
68 sub _last_error {
69 10296 ▶ if ( $#_ == 0 ) {
Subroutine _last_error: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 10080 216 10296
elsif ($#_ == 1) { } 216 0 216
70 10080 return shift->{_last_error};
71 } elsif ( $#_ == 1 ) {
72 216 my ( $self, $val ) = @_;
73 216 $self->{_last_error} = $val;
74 216 return $val;
75 } else {
76 0 confess("Invalid call");
77 }
78 }
79  
80 # XXX: Add validation that _ordered_count is a non-negative integer
81 sub _ordered_count {
82 5576 ▶ if ( $#_ == 0 ) {
Subroutine _ordered_count: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 2487 3089 5576
elsif ($#_ == 1) { } 3089 0 3089
83 2487 my $self = shift;
84  
85 # Initialize
86 2487 ▶ if ( !exists( $self->{_ordered_count} ) ) { $self->{_ordered_count} = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_ordered_count"}) 136 2351 2487
87  
88 2487 return $self->{_ordered_count};
89 } elsif ( $#_ == 1 ) {
90 3089 my ( $self, $val ) = @_;
91 3089 $self->{_ordered_count} = $val;
92 3089 return $val;
93 } else {
94 0 confess("Invalid call");
95 }
96 }
97  
98 # XXX: Add validation that _ordered_responses is an ArrayRef
99 sub _ordered_responses {
100 2919 ▶ if ( $#_ == 0 ) {
Subroutine _ordered_responses: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 2317 602 2919
elsif ($#_ == 1) { } 602 0 602
101 2317 my $self = shift;
102  
103 # Initialize
104 2317 ▶ if ( !exists( $self->{_ordered_responses} ) ) { $self->{_ordered_responses} = []; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_ordered_responses"}) 163 2154 2317
105  
106 2317 return $self->{_ordered_responses};
107 } elsif ( $#_ == 1 ) {
108 602 my ( $self, $val ) = @_;
109 602 $self->{_ordered_responses} = $val;
110 602 return $val;
111 } else {
112 0 confess("Invalid call");
113 }
114 }
115  
116  
117 sub max_children {
118 8418 ▶ if ( $#_ == 0 ) {
Subroutine max_children: called CC=7 SCAR=19.5
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 7792 626 8418
elsif ($#_ == 1) { } 626 0 626
119 7792 my $self = shift;
120  
121 7792 ▶ if ( !exists( $self->{max_children} ) ) { $self->{max_children} = 5; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"max_children"}) 61 7731 7792
122  
123 7792 return $self->{max_children};
124 } elsif ( $#_ == 1 ) {
125 626 my ( $self, $val ) = @_;
126  
127 # Validate
128 626 ▶ if ( defined($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 554 72 626
129 554 ▶ if ( $val !~ m/^[0-9]+$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /^[0-9]+$/s) 92 462 554
130 92 confess("max_children must be set to a positive integer");
131 }
132 462 ▶ if ( $val <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val <= 0) 46 416 462
133 46 confess("max_children must be set to a positive integer");
134 }
135 }
136  
137 488 $self->{max_children} = $val;
138  
139 # Trigger
140 488 $self->_start_queued_children();
141  
142 474 return $val;
143 } else {
144 0 confess("Invalid call");
145 }
146 }
147  
148 # XXX: Add validation that _subprocs is a HashRef
149 sub _subprocs {
150 28572 ▶ if ( $#_ == 0 ) {
Subroutine _subprocs: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 28356 216 28572
elsif ($#_ == 1) { } 216 0 216
151 28356 my $self = shift;
152  
153 # Initial value
154 28356 ▶ if ( !exists( $self->{_subprocs} ) ) { $self->{_subprocs} = {}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_subprocs"}) 311 28045 28356
155  
156 28356 return $self->{_subprocs};
157 } elsif ( $#_ == 1 ) {
158 216 my ( $self, $val ) = @_;
159 216 $self->{_subprocs} = $val;
160 216 return $val;
161 } else {
162 0 confess("Invalid call");
163 }
164 }
165  
166 # XXX: Add validation that _count is a positive integer
167 # This only gets used on Win32.
168 sub _count {
169 216 ▶ if ( $#_ == 0 ) {
Subroutine _count: called CC=4 SCAR=20.1
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 0 216 216
elsif ($#_ == 1) { } 216 0 216
170 0 my $self = shift;
171  
172 # Initial value
173 0 ▶ if ( !exists( $self->{_count} ) ) { $self->{_count} = 1; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_count"}) 0 0 0
174  
175 0 return $self->{_count};
176 } elsif ( $#_ == 1 ) {
177 216 my ( $self, $val ) = @_;
178 216 $self->{_count} = $val;
179 216 return $val;
180 } else {
181 0 confess("Invalid call");
182 }
183 }
184  
185 # XXX: Add validation that _parent_pid is a positive integer
186 # We also need to initialize always in the parent process.
187 sub _parent_pid {
188 322 ▶ if ( $#_ == 0 ) {
Subroutine _parent_pid: called CC=3 SCAR=12.7
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 0 322 322
elsif ($#_ == 1) { } 322 0 322
189 0 return shift->{_parent_pid};
190 } elsif ( $#_ == 1 ) {
191 322 my ( $self, $val ) = @_;
192 322 $self->{_parent_pid} = $val;
193 322 return $val;
194 } else {
195 0 confess("Invalid call");
196 }
197 }
198  
199 # Children queued
200 # XXX: Add validation that _queued_cildren is an
201 # ArrayRef[ArrayRef[CodeRef]]
202 sub _queued_children {
203 16061 ▶ if ( $#_ == 0 ) {
Subroutine _queued_children: called CC=4 SCAR=14.0
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0) { } 15845 216 16061
elsif ($#_ == 1) { } 216 0 216
204 15845 my $self = shift;
205  
206 15845 ▶ if ( !exists( $self->{_queued_children} ) ) { $self->{_queued_children} = []; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_queued_children"}) 274 15571 15845
207  
208 15845 return $self->{_queued_children};
209 } elsif ( $#_ == 1 ) {
210 216 my ( $self, $val ) = @_;
211 216 $self->{_queued_children} = $val;
212 216 return $val;
213 } else {
214 0 confess("Invalid call");
215 }
216 }
217  
218  
219 sub new {
220 322 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.2
Pod documented
221 322 my $self = {};
222 322 bless $self, $class;
223  
224 # Initialize parent PID
225 322 $self->_parent_pid($$);
226  
227 # Make a weak reference and shove it into the ALL_WU array
228 322 my $weakself = $self;
229 322 weaken $weakself;
230 322 push @ALL_WU, \$weakself;
231  
232 # Do some housekeeping on @ALL_WU, so it is somewhat bounded
233 322 @ALL_WU = grep { defined $$_ } @ALL_WU;
234  
235 # Do we have any arguments?
236 322 ▶ if (scalar(@_) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 0) 92 230 322
237 92 ▶ my %args = (scalar(@_) == 1) ? %{shift()} : @_;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 46 46 92
238  
239 92 ▶ if (exists $args{use_anyevent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"use_anyevent"}) 0 92 92
240 0 $self->use_anyevent($args{use_anyevent});
241 }
242 92 ▶ if (exists $args{max_children}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"max_children"}) 92 0 92
243 92 $self->max_children($args{max_children});
244 }
245 }
246  
247 322 return $self;
248 }
249  
250  
251 sub async {
252 4245 ▶ if ( $#_ < 1 ) { confess 'invalid call'; }
Subroutine async: called CC=8 SCAR=21.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ < 1) 0 4245 4245
253 4245 my $self = shift;
254 4245 my $sub = shift;
255  
256 # Test $sub to make sure it is a code ref or a sub ref
257 4245 ▶ if ( !_codelike($sub) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_codelike($sub)) 7 4238 4245
258 7 confess("Parameter to async() is not a code (or codelike) reference");
259 }
260  
261 4238 my $callback;
262 4238 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 2487 1751 4238
elsif (scalar @_ == 1) { } 1751 0 1751
263 # No callback provided
264  
265 2487 my $cbnum = $self->_ordered_count;
266 2487 $self->_ordered_count( $cbnum + 1 );
267  
268 # We create a callback that populates the ordered responses
269 2487 my $selfref = $self;
270 2487 weaken $selfref;
271 $callback = sub {
272 1931 ▶ if ( defined $selfref ) { # Incase this went away
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if (defined $selfref) 1931 0 1931
273 1931 @{ $selfref->_ordered_responses }[$cbnum] = shift;
274 }
275 2487 };
276 } elsif ( scalar(@_) == 1 ) {
277 # Callback provided
278 1751 $callback = shift;
279 } else {
280 0 confess 'invalid call';
281 }
282  
283 # If there are pending errors, throw that.
284 4238 ▶ if ( defined( $self->_last_error ) ) { die( $self->_last_error ); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_last_error) 0 4238 4238
285  
286 4238 my $pipe = [ pipely() ];
287  
288 4238 my $pid = fork();
289  
290 4238 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 4028 210 4238
291 # We are in the parent process
292  
293 4028 $pipe = $pipe->[0];
294  
295 4028 $self->_subprocs()->{$pid} = {
296 fh => $pipe,
297 anyevent => undef,
298 callback => $callback,
299 caller => [ caller() ],
300 };
301  
302 # Set up anyevent listener if appropriate
303 4028 ▶ if ( $self->use_anyevent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_anyevent) 0 4028 4028
304 0 $self->_add_anyevent_watcher($pid);
305 }
306  
307 4028 return $pid;
308  
309 } else {
310 # We are in the child process
311 210 $pipe = $pipe->[1];
312  
313 210 return $self->_child( $sub, $pipe );
314 }
315 }
316  
317  
318 sub asyncs {
319 79 ▶ if ( $#_ < 2 ) { confess 'invalid call'; }
Subroutine asyncs: called CC=5 SCAR=17.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ < 2) 0 79 79
320 79 my $self = shift;
321 79 my $children = shift;
322 79 my $sub = shift;
323 79 ▶ if ( scalar(@_) > 1 ) { confess("invalid call"); }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 0 79 79
324  
325 79 ▶ if ( $children !~ m/^[1-9][0-9]*$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($children =~ /^[1-9][0-9]*$/s) 0 79 79
326 0 confess("Number of children must be a numeric value > 0");
327 }
328  
329 79 ▶ for ( my $i = 0; $i < $children; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $children) 655 49 704
330 655 ▶ $self->async( sub { return $sub->($i); }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
331 }
332  
333 49 return $children;
334 }
335  
336 sub _child {
337 210 ▶ if ( scalar(@_) != 3 ) { confess 'invalid call'; }
Subroutine _child: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 3) 0 210 210
338 210 my ( $self, $sub, $pipe ) = @_;
339  
340 # Cleanup ALL_WU
341 210 @ALL_WU = grep { defined $$_ } @ALL_WU;
342 210 foreach my $wu ( map { $$_ } @ALL_WU ) {
343 216 $wu->_clear_all();
344 }
345  
346 try {
347 210 ▶ my $result = $sub->();
Subroutine __ANON__: called CC=1 SCAR=0.0
348 203 $self->_send_result( $pipe, $result );
349 } catch {
350 6 ▶ $self->_send_error( $pipe, $_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
351 210 };
352  
353 208 exit();
354 }
355  
356  
357 sub waitall {
358 3363 ▶ if ( $#_ != 0 ) { confess 'invalid call'; }
Subroutine waitall: called CC=7 SCAR=26.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 0) 0 3363 3363
359 3363 my ($self) = @_;
360  
361 # No subprocs? Just return.
362 3363 ▶ if ( scalar( keys %{ $self->_subprocs } ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->_subprocs;} == 0) 386 2977 3363
363 386 ▶ if ( $self->use_anyevent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_anyevent) 0 386 386
364 0 $self->_cv( AnyEvent->condvar );
365 }
366  
367 386 return $self->_get_and_reset_ordered_responses();
368 }
369  
370 # Using AnyEvent?
371 2977 ▶ if ( defined( $self->_cv ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_cv) 0 2977 2977
372 0 $self->_cv->recv();
373 0 ▶ if ( defined( $self->_last_error ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_last_error) 0 0 0
374 0 my $err = $self->_last_error;
375 0 $self->_last_error(undef);
376  
377 0 die($err);
378 }
379  
380 0 return $self->_get_and_reset_ordered_responses();
381 }
382  
383 # Tail recursion
384 2977 ▶ if ( $self->_waitone() ) { goto &waitall }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_waitone) 2946 0 2946
385  
386 0 return @{ $self->_get_and_reset_ordered_responses() };
387 }
388  
389 # Gets the _ordered_responses and returns the reference. Also
390 # resets the _ordered_responses and the _ordered_counts to an
391 # empty list and zero respectively.
392 sub _get_and_reset_ordered_responses {
393 386 ▶ if ( $#_ != 0 ) { confess 'invalid call'; }
Subroutine _get_and_reset_ordered_responses: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 0) 0 386 386
394 386 my $self = shift;
395  
396 386 my (@r) = @{ $self->_ordered_responses() };
397  
398 386 $self->_ordered_responses( [] );
399 386 $self->_ordered_count(0);
400  
401 386 return @r;
402 }
403  
404  
405 sub waitone {
406 372 ▶ if ( $#_ != 0 ) { confess 'invalid call'; }
Subroutine waitone: called CC=3 SCAR=13.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 0) 0 372 372
407 372 my ($self) = @_;
408  
409 372 my $rv = $self->_waitone();
410  
411 # Using AnyEvent?
412 368 ▶ if ( defined( $self->_last_error ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_last_error) 0 368 368
413 0 my $err = $self->_last_error;
414 0 $self->_last_error(undef);
415  
416 0 die($err);
417 }
418  
419 368 return $rv;
420 }
421  
422 # Meat of waitone (but doesn't handle returning an exception when using
423 # anyevent)
424 sub _waitone {
425 3349 ▶ if ( $#_ != 0 ) { confess 'invalid call'; }
Subroutine _waitone: called CC=8 SCAR=21.0
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 0) 0 3349 3349
426 3349 my ($self) = @_;
427  
428 3349 my $sp = $self->_subprocs();
429 3349 weaken $sp; # To avoid some Windows warnings
430 3349 ▶ if ( !keys(%$sp) ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$sp) 8 3341 3349
431  
432 # On everything but Windows
433 #
434 3341 my $s = IO::Select->new();
435 3341 foreach ( keys(%$sp) ) { $s->add( $sp->{$_}{fh} ); }
436  
437 3341 my @ready = $s->can_read();
438  
439 3341 foreach my $fh (@ready) {
440 3341 foreach my $child ( keys(%$sp) ) {
441 9818 ▶ if ( defined( $fh->fileno() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh->fileno) 9818 0 9818
442 9818 ▶ if ( $fh->fileno() == $sp->{$child}{fh}->fileno() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->fileno == $sp->{$child}{"fh"}->fileno) 3341 6477 9818
443 3341 $self->_read_result($child);
444  
445 3324 waitpid( $child, 0 );
446  
447 # Start queued children, if needed
448 3324 $self->_start_queued_children();
449  
450 3306 return 1; # We don't want to read more than one!
451 }
452 }
453 }
454 }
455  
456 # We should never get here
457 0 return;
458 }
459  
460  
461 ## no critic ('Subroutines::ProhibitBuiltinHomonyms')
462 sub wait {
463 8 ▶ if ( $#_ != 1 ) { confess 'invalid call'; }
Subroutine wait: called CC=3 SCAR=13.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 1) 0 8 8
464 8 my ( $self, $pid ) = @_;
465  
466 8 my $rv = $self->_wait($pid);
467  
468 8 ▶ if ( defined( $self->_last_error ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_last_error) 0 8 8
469 0 my $err = $self->_last_error;
470 0 $self->_last_error(undef);
471  
472 0 die($err);
473 }
474  
475 8 return $rv;
476 }
477  
478 # Internal version that doesn't check for AnyEvent die needs
479 sub _wait {
480 8 ▶ if ( $#_ != 1 ) { confess 'invalid call'; }
Subroutine _wait: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 1) 0 8 8
481 8 my ( $self, $pid ) = @_;
482  
483 8 ▶ if ( !exists( $self->_subprocs()->{$pid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_subprocs->{$pid}) 4 4 8
484  
485 # We don't warn/die because it's possible that there is
486 # a race between callback and here, in the main thread.
487 4 return;
488 }
489  
490 4 my $result = $self->_read_result($pid);
491  
492 4 waitpid( $pid, 0 );
493  
494 4 return $result;
495 }
496 ## use critic
497  
498  
499 sub count {
500 7292 ▶ if ( $#_ != 0 ) { confess 'invalid call'; }
Subroutine count: called CC=2 SCAR=7.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 0) 0 7292 7292
501 7292 my ($self) = @_;
502  
503 7292 my $sp = $self->_subprocs();
504 7292 return scalar( keys %$sp );
505 }
506  
507  
508 sub queue {
509 2302 ▶ if ( $#_ < 1 ) { confess 'invalid call'; }
Subroutine queue: called CC=6 SCAR=18.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ < 1) 0 2302 2302
510 2302 my $self = shift;
511 2302 my $sub = shift;
512  
513 # Test $sub to make sure it is a code ref or a sub ref
514 2302 ▶ if ( !_codelike($sub) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_codelike($sub)) 7 2295 2302
515 7 confess("Parameter to queue() is not a code (or codelike) reference");
516 }
517  
518 2295 my $callback;
519 2295 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 1159 1136 2295
elsif (scalar @_ == 1) { } 1136 0 1136
520 # We're okay, don't need to do anything - no callback
521 } elsif ( scalar(@_) == 1 ) {
522 # We have a callback
523 1136 $callback = shift;
524 } else {
525 0 confess 'invalid call';
526 }
527  
528 # If there are pending errors, throw that.
529 2295 ▶ if ( defined( $self->_last_error ) ) { die( $self->_last_error ); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_last_error) 0 2295 2295
530  
531 2295 push @{ $self->_queued_children }, [ $sub, $callback ];
532 2295 return $self->_start_queued_children();
533 }
534  
535 sub _send_result {
536 203 ▶ if ( $#_ != 2 ) { confess 'invalid call'; }
Subroutine _send_result: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 2) 0 203 203
537 203 my ( $self, $fh, $msg ) = @_;
538  
539 203 return $self->_send( $fh, 'RESULT', $msg );
540 }
541  
542 sub _send_error {
543 6 ▶ if ( $#_ != 2 ) { confess 'invalid call'; }
Subroutine _send_error: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 2) 0 6 6
544 6 my ( $self, $fh, $err ) = @_;
545  
546 6 return $self->_send( $fh, 'ERROR', $err );
547 }
548  
549 sub _send {
550 209 ▶ if ( $#_ != 3 ) { confess 'invalid call'; }
Subroutine _send: called CC=3 SCAR=11.3
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 3) 0 209 209
551 209 my ( $self, $fh, $type, $data ) = @_;
552  
553 209 my $msg = Storable::freeze( \$data );
554  
555 208 ▶ if ( !defined($msg) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 208 208
556 0 die 'freeze() returned undef for child return value';
557 }
558  
559 208 $fh->write($type);
560 208 $fh->write("\n");
561  
562 208 $fh->write( length($msg) );
563 208 $fh->write("\n");
564  
565 208 binmode( $fh, ':raw' );
566  
567 208 $fh->write($msg);
568  
569 208 $fh->close();
570 208 return;
571 }
572  
573 sub _read_result {
574 3345 ▶ if ( $#_ != 1 ) { confess 'invalid call'; }
Subroutine _read_result: called CC=8 SCAR=21.2
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 1) 0 3345 3345
575 3345 my ( $self, $child ) = @_;
576  
577 3345 my $cinfo = $self->_subprocs()->{$child};
578 3345 my $fh = $cinfo->{fh};
579  
580 3345 my $type = <$fh>;
581 3345 ▶ if ( !defined($type) ) { die 'Could not read child data'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 3345 3345
582 3345 chomp($type);
583  
584 3345 my $size = <$fh>;
585 3345 chomp($size);
586  
587 3345 binmode($fh);
588  
589 3345 my $result = '';
590  
591 3345 my $ret = 1;
592 3345 ▶ while ( defined($ret) && ( length($result) < $size ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret and length $result < $size) 3345 3345 6690
Condition66%2/3
defined $ret and length $result < $size
!ll&&!rl&&r
033453345
MC/DC50%1/2
defined $ret and length $result < $size
defined $ret length $result < $size
Truth table66%2/3
A and B
A= defined $retB= length $result < $size
ABresult
0X0
100
111
593 3345 my $s = $size - length($result);
594  
595 3345 my $part = '';
596 3345 $ret = $fh->read( $part, $s );
597 3345 ▶ if ( defined($ret) ) { $result .= $part; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) 3345 0 3345
598 }
599  
600 3345 my $data = ${ Storable::thaw($result) };
601  
602 3345 my $caller = $self->_subprocs()->{$child}{caller};
603 3345 delete $self->_subprocs()->{$child};
604 3345 $fh->close();
605  
606 3345 ▶ if ( $type eq 'RESULT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'RESULT') { } 3328 17 3345
607 3328 $cinfo->{callback}->($data);
608 } else {
609 17 my $err =
610 "Child (created at "
611 . $caller->[1]
612 . " line "
613 . $caller->[2]
614 . ") died with error: $data";
615  
616 17 ▶ if ( $self->use_anyevent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_anyevent) { } 0 17 17
617 # Can't throw events with anyevent
618 0 $self->_last_error($err);
619 } else {
620 # Otherwise we do throw it
621 17 die($err);
622 }
623 }
624  
625 3328 return;
626 }
627  
628 # Start queued children, if possible.
629 # Returns 1 if children were started, undef otherwise
630 sub _start_queued_children {
631 6107 ▶ if ( $#_ != 0 ) { confess 'invalid call' }
Subroutine _start_queued_children: called CC=8 SCAR=20.9
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 0) 0 6107 6107
632 6107 my ($self) = @_;
633  
634 6107 ▶ if ( !( @{ $self->_queued_children } ) ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->_queued_children;}) 2936 3171 6107
635 3171 ▶ if ( defined( $self->_last_error ) ) { return; } # Do not queue if there are errors
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_last_error) 0 3171 3171
636  
637 # Can we start a queued process?
638 3171 ▶ while ( scalar @{ $self->_queued_children } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->_queued_children;}) 4011 1249 5260
639 4011 ▶ if ( ( !defined( $self->max_children ) ) || ( $self->count < $self->max_children ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->max_children or $self->count < $self->max_children) { } 2183 1828 4011
Condition100%3/3
not defined $self->max_children or $self->count < $self->max_children
l!l&&r!l&&!r
63015531828
MC/DC100%2/2
not defined $self->max_children or $self->count < $self->max_children
not defined $self->max_children $self->count < $self->max_children
Truth table100%3/3
not(A) or B
A= not defined $self->max_childrenB= $self->count < $self->max_children
ABresult
1X1
011
000
640 # Start queued child
641 2183 my $ele = shift @{ $self->_queued_children };
642 2183 ▶ if ( !defined( $ele->[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ele->[1]) { } 1103 1080 2183
643 1103 $self->async( $ele->[0] );
644 } else {
645 1080 $self->async( $ele->[0], $ele->[1] );
646 }
647 } else {
648 # Can't unqueue
649 1828 return;
650 }
651 }
652  
653 # We started at least one process
654 1249 return 1;
655 }
656  
657 # Sets up AnyEvent or tears it down as needed
658 sub _set_anyevent {
659 0 ▶ if ( $#_ < 1 ) { confess 'invalid call' }
Subroutine _set_anyevent: not called CC=11 SCAR=48.8
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ < 1) 0 0 0
660 0 ▶ if ( $#_ > 2 ) { confess 'invalid call' }
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > 2) 0 0 0
661 0 my ( $self, $new, $old ) = @_;
662  
663 0 ▶ if ( ( !$old ) && $new ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $old and $new) { } 0 0 0
elsif ($old and not $new) { } 0 0 0
Condition0%0/3
not $old and $new
!ll&&!rl&&r
000
MC/DC0%0/2
not $old and $new
not $old $new
Truth table0%0/3
not(A) and B
A= not $oldB= $new
ABresult
0X0
100
111
664 # We are setting up AnyEvent
665 0 require AnyEvent;
666  
667 0 ▶ if ( defined( $self->_subprocs() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_subprocs) 0 0 0
668 0 foreach my $pid ( keys %{ $self->_subprocs() } ) {
669 0 $self->_add_anyevent_watcher($pid);
670 }
671 }
672  
673 0 $self->_cv( AnyEvent->condvar );
674  
675 ▶ } elsif ( $old && ( !$new ) ) {
Condition0%0/3
$old and not $new
!ll&&!rl&&r
000
MC/DC0%0/2
$old and not $new
$old not $new
Truth table0%0/3
A and not(B)
A= $oldB= not $new
ABresult
0X0
100
111
676 # We are tearing down AnyEvent
677  
678 0 ▶ if ( defined( $self->_subprocs() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_subprocs) 0 0 0
679 0 foreach my $pid ( keys %{ $self->_subprocs() } ) {
680 0 my $proc = $self->_subprocs()->{$pid};
681  
682 0 $proc->{anyevent} = undef;
683 }
684 }
685  
686 0 $self->_cv(undef);
687 }
688 0 return;
689 }
690  
691 # Sets up the listener for AnyEvent
692 sub _add_anyevent_watcher {
693 0 ▶ if ( $#_ != 1 ) { confess 'invalid call' }
Subroutine _add_anyevent_watcher: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 1) 0 0 0
694 0 my ( $self, $pid ) = @_;
695  
696 0 my $proc = $self->_subprocs()->{$pid};
697  
698 $proc->{anyevent} = AnyEvent->io(
699 fh => $proc->{fh},
700 poll => 'r',
701 cb => sub {
702 0 ▶ $self->_wait($pid);
Subroutine __ANON__: not called CC=2 SCAR=17.9
703 0 ▶ if ( scalar( keys %{ $self->_subprocs() } ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->_subprocs;} == 0) 0 0 0
704 0 my $oldcv = $self->_cv;
705 0 $self->_cv( AnyEvent->condvar );
706 0 $oldcv->send();
707 }
708  
709 # Start queued children, if needed
710 0 $self->_start_queued_children();
711 },
712 0 );
713  
714 0 return;
715 }
716  
717 # Used to clear all sub-processes, etc, in child process.
718 sub _clear_all {
719 216 ▶ if ( $#_ != 0 ) { confess 'invalid call' }
Subroutine _clear_all: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 0) 0 216 216
720 216 my ( $self ) = @_;
721  
722 216 $self->_cv(undef);
723 216 $self->_last_error(undef);
724 216 $self->_ordered_count(0);
725 216 $self->_ordered_responses( [] );
726 216 $self->_count(1);
727 216 $self->_queued_children( [] );
728  
729 216 do {
730 # Don't warn on AnyEvent in child threads being DESTROYed
731 216 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
732 216 $self->_subprocs( {} );
733 };
734  
735 216 return;
736 }
737  
738  
739 sub start {
740 5 ▶ if ( $#_ != 1 ) { confess 'invalid call'; }
Subroutine start: called CC=4 SCAR=14.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != 1) 0 5 5
741 5 my $self = shift;
742 5 my $sub = shift;
743  
744 # Test $sub to make sure it is a code ref or a sub ref
745 5 ▶ if ( !_codelike($sub) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_codelike($sub)) 0 5 5
746 0 confess("Parameter to start() is not a code (or codelike) reference");
747 }
748  
749 5 my $pid = fork();
750  
751 5 ▶ if ( !$pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 2 3 5
752 # We are in the child process.
753 2 $sub->();
754 2 exit();
755 }
756  
757 3 return;
758 }
759  
760 # Tests to see if something is codelike
761 #
762 # Borrowed from Params::Util (written by Adam Kennedy)
763 sub _codelike {
764 6552 ▶ if ( scalar(@_) != 1 ) { confess 'invalid call' }
Subroutine _codelike: called CC=5 SCAR=18.1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 1) 0 6552 6552
765 6552 my $thing = shift;
766  
767 6552 ▶ if ( reftype($thing) ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($thing)) 6538 14 6552
768 14 ▶ if ( blessed($thing) && overload::Method( $thing, '()' ) ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing) and overload::Method($thing, "()")) 0 14 14
Condition33%1/3
builtin::blessed($thing) and overload::Method($thing, "()")
!ll&&!rl&&r
1400
MC/DC0%0/2
builtin::blessed($thing) and overload::Method($thing, "()")
builtin::blessed($thing) overload::Method($thing, "()")
Truth table33%1/3
A and B
A= builtin::blessed($thing)B= overload::Method($thing, "()")
ABresult
0X0
100
111
769  
770 14 return;
771 }
772  
773 # Destructor emits warning if sub processes are running
774 sub DESTROY {
775 281 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
776  
777 281 ▶ if ( scalar( keys %{ $self->_subprocs } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->_subprocs;}) 1 280 281
778 1 warn "Warning: Subprocesses running when Parallel::WorkUnit object destroyed\n";
779 }
780  
781 281 return;
782 }
783  
784 225 1;
785  
786 __END__
« blib/lib/Parallel/WorkUnit.pm ↑ Summary blib/lib/Parallel/WorkUnit/Procedural.pm »