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