lib/Proc/Spawner/Loop.pm

filter: Statementstmt 99.0% 103 / 104 Branchbran 80.9% 34 / 42 Conditioncond 55.5% 10 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 95.6% 22 / 23 Podpod 50.0% 8 / 16 totaltotal 84.5% 181 / 214 CC 28 SCAR 16.3
CC
28
Cov
90%
turn
12.3
wait_until
6.0
poll
5.2
CRAP 5.1
« lib/Proc/ShellSpawn/Builtins.pm ↑ Summary t/try.pl »
1 package Proc::Spawner::Loop;
2 # EXPERIMENTAL (W5, post-0.10). The one event loop of the process: queued calls, timers, reads, writes and ended
3 # children, with one interface on POSIX and on Windows. The front end here keeps the queue and the rules; a backend
4 # waits for the events (Proc::Spawner::Loop::POSIX: select and SIGCHLD; Proc::Spawner::Loop::Win32: an I/O completion
5 # port). See the POD at the end.
6 #
7 # The rules (decisions R1, D1-D3 of the page "Child Exit Wake-up"): every callback runs from the queue, one at a time,
8 # never inside a signal or inside another callback (R1); a callback never waits: wait_until called from one dies (D2);
9 # a backend may take process-wide state only while wait_until runs (D3: SIGCHLD on POSIX).
10 536 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 536 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 536 our $VERSION = '0.12';
13  
14 536 our $TRACE; # undef, or a code ref given one line per operation (Proc::Spawner's $TRACE is this one)
15 536 our $CAUSE; # the caller's: a queued call keeps the value current when it was queued, and runs with it
16 536 our $REACTING = 0; # true while a queued call runs
17 536 our $DEPTH = 0; # nesting of wait_until and poll
18 536 ▶our $BACKEND = $^O eq 'MSWin32' ? 'Proc::Spawner::Loop::Win32' : 'Proc::Spawner::Loop::POSIX';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 536 536
19 335 ▶sub tr_ { $TRACE->(join '', @_) if $TRACE }
Subroutine tr_: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $Proc::Spawner::Loop::TRACE 0 335 335
20  
21 536 my $LOOP;
22 6604 ▶sub new { # the loop (there is one per process: every call gives the same object)
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
23 6604 ▶ return $LOOP if $LOOP;
Branch: 1 branch
BranchTrueFalseTotal
if $LOOP 6080 524 6604
24 524 (my $f = "$BACKEND.pm") =~ s{::}{/}g; require $f;
25 524 $LOOP = bless { q => [], watch => {}, timers => [], id => 0, backend => $BACKEND->new }, shift }
26  
27 # ------------------------------------------------------------------ what a caller asks for
28 6071 ▶sub defer { my $l = shift; push @{$l->{q}}, [$CAUSE, @_]; return }
Subroutine defer: called CC=1 SCAR=0.0
Pod documented
29 1846 ▶sub read { my ($l, $fd, $max, $cb) = @_; $l->watch('read', $cb, $fd, $max) } # (FD: a number or a handle)
Subroutine read: called CC=1 SCAR=0.0
Pod documented
30 851 ▶sub write { my ($l, $fd, $bytes, $cb, %o) = @_; $l->watch('write', $cb, $fd, $bytes, $o{chunk}) }
Subroutine write: called CC=1 SCAR=0.0
Pod documented
31 335 ▶sub child { my ($l, $pid, $cb) = @_; $l->watch('child', $cb, $pid) }
Subroutine child: called CC=1 SCAR=0.0
Pod documented
32 7 ▶sub after {
Subroutine after: called CC=2 SCAR=7.1
Pod documented
33 7 my ($l, $s, $cb) = @_; my $w = $l->watch('timer', $cb);
34 7 my $at = now() + $s; my $t = $l->{timers};
35 7 ▶ @$t = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @$t, [$at, $w->id]; $w }
Condition33%1/3
$Proc::Spawner::Loop::a->[0] <=> $Proc::Spawner::Loop::b->[0] || $Proc::Spawner::Loop::a->[1] <=> $Proc::Spawner::Loop::b->[1]
l!l&&r!l&&!r
400
MC/DC0%0/2
$Proc::Spawner::Loop::a->[0] <=> $Proc::Spawner::Loop::b->[0] || $Proc::Spawner::Loop::a->[1] <=> $Proc::Spawner::Loop::b->[1]
$Proc::Spawner::Loop::a->[0] <=> $Proc::Spawner::Loop::b->[0] $Proc::Spawner::Loop::a->[1] <=> $Proc::Spawner::Loop::b->[1]
Truth table33%1/3
A || B
A= $Proc::Spawner::Loop::a->[0] <=> $Proc::Spawner::Loop::b->[0]B= $Proc::Spawner::Loop::a->[1] <=> $Proc::Spawner::Loop::b->[1]
ABresult
1X1
011
000
36 6 ▶sub pipe { my ($l, %o) = @_; $l->{backend}->pipe(%o) } # (READ, WRITE); overlapped => 'r', 'w', 'rw' (Windows)
Subroutine pipe: called CC=1 SCAR=0.0
Pod documented
37 2697 ▶sub fd_of { ref $_[0] ? fileno $_[0] : $_[0] }
Subroutine fd_of: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 904 1793 2697
38 19 ▶sub now { require Time::HiRes; Time::HiRes::time() }
Subroutine now: called CC=1 SCAR=0.0
Pod undocumented
39  
40 3039 ▶sub watch { # a new watch: its callback here, the event asked of the backend (not for timers)
Subroutine watch: called CC=2 SCAR=6.9
Pod undocumented
41 3039 my ($l, $kind, $cb, @a) = @_; my $id = ++$l->{id};
42 3039 $l->{watch}{$id} = [$kind, $cb];
43 3039 ▶ $l->{backend}->$kind($id, @a) unless $kind eq 'timer';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "timer" 3032 7 3039
44 3039 bless [$l, $id, $kind], 'Proc::Spawner::Loop::Watch' }
45 3 ▶sub cancel {
Subroutine cancel: called CC=3 SCAR=11.0
Pod undocumented
46 3 ▶ my ($l, $id) = @_; my $w = delete $l->{watch}{$id} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $w = delete $l->{"watch"}{$id} or return 0 3 0 3
47 3 ▶ $w->[0] eq 'timer' ? (@{$l->{timers}} = grep $_->[1] != $id, @{$l->{timers}}) : $l->{backend}->cancel($id); 1 }
Branch: 1 branch
BranchTrueFalseTotal
$w->[0] eq 'timer' ? : 1 2 3
48 46 ▶sub pending { my $l = shift; @{$l->{q}} + keys %{$l->{watch}} }
Subroutine pending: called CC=1 SCAR=0.0
Pod undocumented
49  
50 # ------------------------------------------------------------------ running
51 579 ▶sub wait_until {
Subroutine wait_until: called CC=6 SCAR=18.0
Pod documented
52 579 my ($l, $done) = @_;
53 579 ▶ die "Proc::Spawner: wait inside a reaction\n" if $REACTING; # D2
Branch: 1 branch
BranchTrueFalseTotal
if $Proc::Spawner::Loop::REACTING 1 578 579
54 578 ▶ $l->{backend}->begin unless $DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $Proc::Spawner::Loop::DEPTH 578 0 578
55 578 ▶ my $ok = eval { local $DEPTH = $DEPTH + 1; $l->turn until $done->(); 1 };
Branch: 1 branch
BranchTrueFalseTotal
&$done() or do { $l->turn } 577 9349 9926
56 578 ▶ $l->{backend}->end unless $DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $Proc::Spawner::Loop::DEPTH 578 0 578
57 578 ▶ die $@ unless $ok }
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 577 578
58 23 ▶sub poll { # what is ready now, without waiting; nothing inside wait_until or a callback (K3, post-0.04)
Subroutine poll: called CC=5 SCAR=16.6
Pod documented
59 23 ▶ my $l = shift; return if $REACTING || $DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
if $Proc::Spawner::Loop::REACTING or $Proc::Spawner::Loop::DEPTH 0 23 23
Condition33%1/3
$Proc::Spawner::Loop::REACTING or $Proc::Spawner::Loop::DEPTH
l!l&&r!l&&!r
0023
MC/DC0%0/2
$Proc::Spawner::Loop::REACTING or $Proc::Spawner::Loop::DEPTH
$Proc::Spawner::Loop::REACTING $Proc::Spawner::Loop::DEPTH
Truth table33%1/3
A or B
A= $Proc::Spawner::Loop::REACTINGB= $Proc::Spawner::Loop::DEPTH
ABresult
1X1
011
000
60 23 local $DEPTH = $DEPTH + 1; # (a callback's own wait_until still dies: D2)
61 23 ▶ 1 while $l->pending && $l->turn(0) }
Branch: 1 branch
BranchTrueFalseTotal
$l->pending and $l->turn(0) and do { 1 } 23 23 46
Condition100%3/3
$l->pending and $l->turn(0)
!ll&&!rl&&r
71623
MC/DC100%2/2
$l->pending and $l->turn(0)
$l->pending $l->turn(0)
Truth table100%3/3
A and B
A= $l->pendingB= $l->turn(0)
ABresult
0X0
100
111
62 9388 ▶sub turn { # one queued call, or one round of waiting (at most TIMEOUT seconds; undef: as long as it takes): false if
Subroutine turn: called CC=12 SCAR=25.1
Pod undocumented
63 9388 my ($l, $timeout) = @_; # nothing happened
64 9388 ▶ if (my $c = shift @{$l->{q}}) { my ($cause, $k, @a) = @$c; local ($CAUSE, $REACTING) = ($cause, 1); $k->(@a); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = shift @{$$l{"q"};}) 6071 3317 9388
65 3317 my $t = $l->{timers};
66 3317 ▶ if (@$t) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$t) 12 3305 3317
67 12 my $now = now();
68 12 ▶ while (@$t && $t->[0][0] <= $now) { my $id = (shift @$t)->[1]; my $w = delete $l->{watch}{$id}; $l->defer($w->[1]) }
Branch: 1 branch
BranchTrueFalseTotal
if (@$t and $t->[0][0] <= $now) 6 12 18
Condition100%3/3
@$t and $t->[0][0] <= $now
!ll&&!rl&&r
576
MC/DC100%2/2
@$t and $t->[0][0] <= $now
@$t $t->[0][0] <= $now
Truth table100%3/3
A and B
A= @$tB= $t->[0][0] <= $now
ABresult
0X0
100
111
69 12 ▶ return 1 if @{$l->{q}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$l->{"q"};} 6 6 12
70 6 ▶ $timeout = $t->[0][0] - $now if @$t && (!defined $timeout || $t->[0][0] - $now < $timeout) }
Branch: 1 branch
BranchTrueFalseTotal
if @$t and !defined($timeout) || $t->[0][0] - $now < $timeout 6 0 6
Condition33%2/6
@$t and !defined($timeout) || $t->[0][0] - $now < $timeout
!ll&&!rl&&r
006
not defined $timeout or $t->[0][0] - $now < $timeout
l!l&&r!l&&!r
600
MC/DC0%0/3
@$t and !defined($timeout) || $t->[0][0] - $now < $timeout
@$t not defined $timeout $t->[0][0] - $now < $timeout
Truth table25%1/4
A and not(B) or C
A= @$tB= not defined $timeoutC= $t->[0][0] - $now < $timeout
ABCresult
0XX0
1000
11X1
1011
71 3311 ▶ die "Proc::Spawner: nothing left to wait for\n" unless %{$l->{watch}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$l->{"watch"};} 1 3310 3311
72 3310 my $n = 0;
73 3310 for my $e ($l->{backend}->wait($timeout)) { # [ID, ARGS...] per event, the oldest first
74 3030 ▶ my ($id, @a) = @$e; my $w = delete $l->{watch}{$id} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $w = delete $l->{"watch"}{$id} or next 3030 0 3030
75 3030 $l->defer($w->[1], @a); $n++ }
76 3310 $n }
77  
78 package Proc::Spawner::Loop::Watch; # [LOOP, ID, KIND]
79 7 ▶sub id { $_[0][1] }
Subroutine id: called CC=1 SCAR=0.0
80 0 ▶sub kind { $_[0][2] }
Subroutine kind: not called CC=1 SCAR=6.9
81 3 ▶sub cancel { my $w = shift; $w->[0]->cancel($w->[1]) }
Subroutine cancel: called CC=1 SCAR=0.0
82 1 ▶sub active { my $w = shift; exists $w->[0]{watch}{$w->[1]} }
Subroutine active: called CC=1 SCAR=0.0
83  
84 536 1;
85  
86 __END__
« lib/Proc/ShellSpawn/Builtins.pm ↑ Summary t/try.pl »