| 1 | package Proc::Spawner::Loop::POSIX; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # EXPERIMENTAL (W5, post-0.10). The POSIX backend of Proc::Spawner::Loop: select(2) on the fds, and SIGCHLD while | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # wait_until runs (D3): a C handler (Proc::Spawner::chld_to_fd) writes one byte into a pipe the loop selects on (D1), | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # so no Perl code runs inside the signal and no wake-up is lost. Without it, a deferred Perl handler, and select wakes | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # up every $TICK seconds (B1). Background: perl-signals-findings.md. The interface (new, begin, end, read, write, | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # child, cancel, wait) is the one every backend has. | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 478 | ▶ | use strict; use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 478 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 478 | ▶ | use Fcntl qw(F_SETFD FD_CLOEXEC F_GETFL F_SETFL O_NONBLOCK); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 478 | ▶ | use Proc::Spawner (); # (its XS: chld_to_fd, chld_restore) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 478 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 478 | my $CHLD_C = defined &Proc::Spawner::chld_to_fd; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 478 | ▶ | our $TICK = $CHLD_C ? undef : 0.05; # the longest wait without a wake-up for SIGCHLD | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 478 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 478 | my $self = bless { rd => {}, wr => {}, kid => {} }, shift; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 478 | ▶ | CORE::pipe($self->{self_r}, $self->{self_w}) or die "pipe: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 478 | for (@$self{qw(self_r self_w)}) { fcntl($_, F_SETFD, FD_CLOEXEC); fcntl($_, F_SETFL, fcntl($_, F_GETFL, 0) | O_NONBLOCK) } | |||||||||||||||||||||||||||||||||||||||||
| 20 | 478 | my $w = $self->{self_w}; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $self->{chld} = POSIX::SigAction->new(sub { syswrite $w, 'x' }, POSIX::SigSet->new, POSIX::SA_RESTART()); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 478 | $self->{chld}->safe(1); # the fallback: deferred; POSIX::sigaction's immediate default corrupts the heap (S2) | |||||||||||||||||||||||||||||||||||||||||
| 23 | 478 | $self } | |||||||||||||||||||||||||||||||||||||||||
| 24 | 527 | ▶ | sub begin { my $self = shift; $self->{old} = POSIX::SigAction->new; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
begin: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 527 | ▶ | $CHLD_C ? Proc::Spawner::chld_to_fd(fileno $self->{self_w}) : POSIX::sigaction(POSIX::SIGCHLD(), $self->{chld}, $self->{old}) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 527 | ▶ | sub end { my $self = shift; $CHLD_C ? Proc::Spawner::chld_restore() : POSIX::sigaction(POSIX::SIGCHLD(), $self->{old}) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
end: called CC=2 SCAR=7.2
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1891 | ▶ | sub read { my ($self, $id, $fd, $max) = @_; $self->{rd}{$id} = [Proc::Spawner::Loop::fd_of($fd), $max] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 774 | ▶ | sub write { # a handle is made non-blocking; an fd number is left as it is (CHUNK: at most so many bytes per write) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 774 | my ($self, $id, $fd, $bytes, $chunk) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 774 | ▶ | fcntl($fd, F_SETFL, fcntl($fd, F_GETFL, 0) | O_NONBLOCK) if ref $fd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 774 | $self->{wr}{$id} = [Proc::Spawner::Loop::fd_of($fd), $bytes, $chunk] } | |||||||||||||||||||||||||||||||||||||||||
| 33 | 324 | ▶ | sub child { my ($self, $id, $pid) = @_; $self->{kid}{$id} = $pid } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
child: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | sub cancel { my ($self, $id) = @_; delete $self->{$_}{$id} for qw(rd wr kid) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cancel: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 6486 | ▶ | sub reap { # our children only: waitpid(-1) would take statuses that are not ours (C2) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reap: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6486 | my $self = shift; my @e; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 6486 | for my $id (sort { $a <=> $b } keys %{$self->{kid}}) { | |||||||||||||||||||||||||||||||||||||||||
| 39 | 6245 | ▶ | my $pid = $self->{kid}{$id}; my $r = waitpid $pid, POSIX::WNOHANG(); next unless $r; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 324 | ▶ | my $w = $r > 0 ? $? : undef; delete $self->{kid}{$id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 324 | ▶ | Proc::Spawner::Loop::tr_("reaped $pid: ", defined $w ? "wait status $w" : 'status taken by other code'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 324 | push @e, [$id, $w] } | |||||||||||||||||||||||||||||||||||||||||
| 43 | 6486 | @e } | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 3289 | ▶ | sub wait { # the events of one round: [ID, ARGS...], at most TIMEOUT seconds (undef: until there is one) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wait: called CC=25 SCAR=33.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 3289 | my ($self, $timeout) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 3289 | ▶ | my @e = $self->reap; return @e if @e; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 3197 | ▶ | $timeout = $TICK if defined $TICK && (!defined $timeout || $timeout > $TICK); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
defined $Proc::Spawner::Loop::POSIX::TICK and !defined($timeout) || $timeout > $Proc::Spawner::Loop::POSIX::TICK
not defined $timeout or $timeout > $Proc::Spawner::Loop::POSIX::TICK
MC/DC0%0/3
defined $Proc::Spawner::Loop::POSIX::TICK and !defined($timeout) || $timeout > $Proc::Spawner::Loop::POSIX::TICK
defined $Proc::Spawner::Loop::POSIX::TICK
not defined $timeout
$timeout > $Proc::Spawner::Loop::POSIX::TICK
Truth table25%1/4
A and not(B) or C
A= defined $Proc::Spawner::Loop::POSIX::TICKB= not defined $timeoutC= $timeout > $Proc::Spawner::Loop::POSIX::TICK
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 3197 | my ($rd, $wr) = @$self{qw(rd wr)}; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 3197 | my $rin = ''; vec($rin, $_, 1) = 1 for fileno $self->{self_r}, map $_->[0], values %$rd; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 3197 | my $win = ''; vec($win, $_->[0], 1) = 1 for values %$wr; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 3197 | my $n = select my $rout = $rin, my $wout = $win, undef, $timeout; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 3197 | ▶ | 1 while sysread $self->{self_r}, my $buf, 512; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 3197 | ▶ | return $self->reap if $n <= 0; # timeout, or EINTR (SIGCHLD): the sets say nothing | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2978 | my %served; # one reader per fd and round, the oldest first | |||||||||||||||||||||||||||||||||||||||||
| 56 | 2978 | for my $id (sort { $a <=> $b } keys %$rd) { | |||||||||||||||||||||||||||||||||||||||||
| 57 | 2288 | my ($fd, $len) = @{$rd->{$id}}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 2288 | ▶ | next if $served{$fd}++ or !vec($rout, $fd, 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$served{$fd}++ or not vec($rout, $fd, 1)
MC/DC100%2/2
$served{$fd}++ or not vec($rout, $fd, 1)
$served{$fd}++
not vec($rout, $fd, 1)
Truth table100%3/3
A or not(B)
A= $served{$fd}++B= not vec($rout, $fd, 1)
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1889 | my $got = POSIX::read($fd, my $bytes, $len); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1889 | ▶ | next if !defined $got && $! == POSIX::EAGAIN(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $got and $! == 11
MC/DC0%0/2
not defined $got and $! == 11
not defined $got
$! == 11
Truth table33%1/3
not(A) and B
A= not defined $gotB= $! == 11
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1889 | delete $rd->{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 1889 | ▶ | push @e, [$id, defined $got && $got + 0 ? $bytes : undef, defined $got ? () : $! + 0] } # ("0 but true": end of file) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $got and $got + 0
MC/DC50%1/2
defined $got and $got + 0
defined $got
$got + 0
Truth table66%2/3
A and B
A= defined $gotB= $got + 0
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 2978 | for my $id (sort { $a <=> $b } keys %$wr) { | |||||||||||||||||||||||||||||||||||||||||
| 64 | 1249 | ▶ | my $w = $wr->{$id}; next unless vec($wout, $w->[0], 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1097 | ▶ | my $did = POSIX::write($w->[0], $w->[1], $w->[2] && $w->[2] < length $w->[1] ? $w->[2] : length $w->[1]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$w->[2] and $w->[2] < length $w->[1]
MC/DC0%0/2
$w->[2] and $w->[2] < length $w->[1]
$w->[2]
$w->[2] < length $w->[1]
Truth table33%1/3
A and B
A= $w->[2]B= $w->[2] < length $w->[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1097 | ▶ | next if !defined $did && $! == POSIX::EAGAIN(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $did and $! == 11
MC/DC0%0/2
not defined $did and $! == 11
not defined $did
$! == 11
Truth table66%2/3
not(A) and B
A= not defined $didB= $! == 11
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1097 | ▶ | if (!defined $did) { delete $wr->{$id}; push @e, [$id, $! + 0]; next } # (the reader is gone: the rest is dropped) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1093 | substr($w->[1], 0, $did) = ''; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 1093 | ▶ | delete $wr->{$id}, push @e, [$id] if $w->[1] eq '' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 2978 | (@e, $self->reap) } | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | sub pipe { CORE::pipe(my $r, my $w) or return; ($r, $w) } # (overlapped => ...: nothing to do here) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pipe: called CC=2 SCAR=7.2
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 478 | 1; | |||||||||||||||||||||||||||||||||||||||||