lib/Parallel/WorkUnit.pm

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