lib/Proc/Spawner/Loop/POSIX.pm

filter: Statementstmt 98.9% 92 / 93 Branchbran 77.2% 34 / 44 Conditioncond 47.6% 10 / 21 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 10 totaltotal 78.1% 154 / 197 CC 36 SCAR 27.6
CC
36
Cov
86%
wait
29.6
reap
5.0
new
3.0
CRAP 15.8
« blib/lib/Proc/Spawner/Loop/Win32.pm ↑ Summary lib/Proc/ShellSubset/Small.pm »
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 524 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 524 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 524 ▶use Fcntl qw(F_SETFD FD_CLOEXEC F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 524 ▶use Proc::Spawner (); # (its XS: chld_to_fd, chld_restore)
Subroutine BEGIN: called CC=1 SCAR=0.0
11 524 our $VERSION = '0.12';
12  
13 524 my $CHLD_C = defined &Proc::Spawner::chld_to_fd;
14 524 ▶our $TICK = $CHLD_C ? undef : 0.05; # the longest wait without a wake-up for SIGCHLD
Branch: 1 branch
BranchTrueFalseTotal
$CHLD_C ? : 524 0 524
15  
16 524 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
17 524 my $self = bless { rd => {}, wr => {}, kid => {} }, shift;
18 524 ▶ CORE::pipe($self->{self_r}, $self->{self_w}) or die "pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::pipe $self->{"self_r"}, $self->{"self_w"} or die "pipe: $!" 524 0 524
19 524 for (@$self{qw(self_r self_w)}) { fcntl($_, F_SETFD, FD_CLOEXEC); fcntl($_, F_SETFL, fcntl($_, F_GETFL, 0) | O_NONBLOCK) }
20 524 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 524 $self->{chld}->safe(1); # the fallback: deferred; POSIX::sigaction's immediate default corrupts the heap (S2)
23 524 $self }
24 578 ▶sub begin { my $self = shift; $self->{old} = POSIX::SigAction->new;
Subroutine begin: called CC=2 SCAR=7.1
Pod undocumented
25 578 ▶ $CHLD_C ? Proc::Spawner::chld_to_fd(fileno $self->{self_w}) : POSIX::sigaction(POSIX::SIGCHLD(), $self->{chld}, $self->{old}) }
Branch: 1 branch
BranchTrueFalseTotal
$CHLD_C ? : 578 0 578
26 578 ▶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
BranchTrueFalseTotal
$CHLD_C ? : 578 0 578
27  
28 1846 ▶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 851 ▶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 851 my ($self, $id, $fd, $bytes, $chunk) = @_;
31 851 ▶ fcntl($fd, F_SETFL, fcntl($fd, F_GETFL, 0) | O_NONBLOCK) if ref $fd;
Branch: 1 branch
BranchTrueFalseTotal
if ref $fd 821 30 851
32 851 $self->{wr}{$id} = [Proc::Spawner::Loop::fd_of($fd), $bytes, $chunk] }
33 335 ▶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 6534 ▶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 6534 my $self = shift; my @e;
38 6534 for my $id (sort { $a <=> $b } keys %{$self->{kid}}) {
39 6034 ▶ my $pid = $self->{kid}{$id}; my $r = waitpid $pid, POSIX::WNOHANG(); next unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 5699 335 6034
40 335 ▶ my $w = $r > 0 ? $? : undef; delete $self->{kid}{$id};
Branch: 1 branch
BranchTrueFalseTotal
$r > 0 ? : 334 1 335
41 335 ▶ Proc::Spawner::Loop::tr_("reaped $pid: ", defined $w ? "wait status $w" : 'status taken by other code');
Branch: 1 branch
BranchTrueFalseTotal
defined $w ? : 334 1 335
42 335 push @e, [$id, $w] }
43 6534 @e }
44  
45 3310 ▶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 3310 my ($self, $timeout) = @_;
47 3310 ▶ my @e = $self->reap; return @e if @e;
Branch: 1 branch
BranchTrueFalseTotal
if @e 86 3224 3310
48 3224 ▶ $timeout = $TICK if defined $TICK && (!defined $timeout || $timeout > $TICK);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Proc::Spawner::Loop::POSIX::TICK and !defined($timeout) || $timeout > $Proc::Spawner::Loop::POSIX::TICK 0 3224 3224
Condition16%1/6
defined $Proc::Spawner::Loop::POSIX::TICK and !defined($timeout) || $timeout > $Proc::Spawner::Loop::POSIX::TICK
!ll&&!rl&&r
322400
not defined $timeout or $timeout > $Proc::Spawner::Loop::POSIX::TICK
l!l&&r!l&&!r
000
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
ABCresult
0XX0
1000
11X1
1011
49 3224 my ($rd, $wr) = @$self{qw(rd wr)};
50 3224 my $rin = ''; vec($rin, $_, 1) = 1 for fileno $self->{self_r}, map $_->[0], values %$rd;
51 3224 my $win = ''; vec($win, $_->[0], 1) = 1 for values %$wr;
52 3224 my $n = select my $rout = $rin, my $wout = $win, undef, $timeout;
53 3224 ▶ 1 while sysread $self->{self_r}, my $buf, 512;
Branch: 1 branch
BranchTrueFalseTotal
sysread $self->{"self_r"}, my $buf, 512 and do { '???' } 268 3224 3492
54 3224 ▶ return $self->reap if $n <= 0; # timeout, or EINTR (SIGCHLD): the sets say nothing
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 0 237 2987 3224
55 2987 my %served; # one reader per fd and round, the oldest first
56 2987 for my $id (sort { $a <=> $b } keys %$rd) {
57 2225 my ($fd, $len) = @{$rd->{$id}};
58 2225 ▶ next if $served{$fd}++ or !vec($rout, $fd, 1);
Branch: 1 branch
BranchTrueFalseTotal
if $served{$fd}++ or not vec($rout, $fd, 1) 381 1844 2225
Condition100%3/3
$served{$fd}++ or not vec($rout, $fd, 1)
l!l&&r!l&&!r
323491844
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)
ABresult
1X1
011
000
59 1844 my $got = POSIX::read($fd, my $bytes, $len);
60 1844 ▶ next if !defined $got && $! == POSIX::EAGAIN();
Branch: 1 branch
BranchTrueFalseTotal
if not defined $got and $! == 11 0 1844 1844
Condition33%1/3
not defined $got and $! == 11
!ll&&!rl&&r
184400
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
ABresult
0X0
100
111
61 1844 delete $rd->{$id};
62 1844 ▶ push @e, [$id, defined $got && $got + 0 ? $bytes : undef, defined $got ? () : $! + 0] } # ("0 but true": end of file)
Branch: 2 branches
BranchTrueFalseTotal
defined $got && $got + 0 ? : 1773 71 1844
defined $got ? : 1844 0 1844
Condition66%2/3
defined $got and $got + 0
!ll&&!rl&&r
0711773
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
ABresult
0X0
100
111
63 2987 for my $id (sort { $a <=> $b } keys %$wr) {
64 1296 ▶ my $w = $wr->{$id}; next unless vec($wout, $w->[0], 1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $wout, $w->[0], 1 140 1156 1296
65 1156 ▶ my $did = POSIX::write($w->[0], $w->[1], $w->[2] && $w->[2] < length $w->[1] ? $w->[2] : length $w->[1]);
Branch: 1 branch
BranchTrueFalseTotal
$w->[2] && $w->[2] < length $w->[1] ? : 0 1156 1156
Condition33%1/3
$w->[2] and $w->[2] < length $w->[1]
!ll&&!rl&&r
115600
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]
ABresult
0X0
100
111
66 1156 ▶ next if !defined $did && $! == POSIX::EAGAIN();
Branch: 1 branch
BranchTrueFalseTotal
if not defined $did and $! == 11 0 1156 1156
Condition66%2/3
not defined $did and $! == 11
!ll&&!rl&&r
115240
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
ABresult
0X0
100
111
67 1156 ▶ if (!defined $did) { delete $wr->{$id}; push @e, [$id, $! + 0]; next } # (the reader is gone: the rest is dropped)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $did) 4 1152 1156
68 1152 substr($w->[1], 0, $did) = '';
69 1152 ▶ delete $wr->{$id}, push @e, [$id] if $w->[1] eq '' }
Branch: 1 branch
BranchTrueFalseTotal
if $w->[1] eq "" 847 305 1152
70 2987 (@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
BranchTrueFalseTotal
CORE::pipe my $r, my $w or return 6 0 6
73  
74 524 1;
« blib/lib/Proc/Spawner/Loop/Win32.pm ↑ Summary lib/Proc/ShellSubset/Small.pm »