lib/Proc/Spawner/Loop.pm

filter: Statementstmt 100% 114 / 114 Branchbran 75.0% 36 / 48 Conditioncond 55.5% 10 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 24 / 24 Podpod 47.0% 8 / 17 totaltotal 84.4% 196 / 232 CC 31 SCAR 17.3
CC
31
Cov
89%
turn
12.3
wait_until
9.5
poll
5.2
CRAP 5.6
« 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 561 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 583 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 554 our $VERSION = '0.13';
13  
14 554 our $TRACE; # undef, or a code ref given one line per operation (Proc::Spawner's $TRACE is this one)
15 554 our $CAUSE; # the caller's: a queued call keeps the value current when it was queued, and runs with it
16 554 our $REACTING = 0; # true while a queued call runs
17 554 our $DEPTH = 0; # nesting of wait_until and poll
18 554 ▶our $BACKEND = $^O eq 'MSWin32' ? 'Proc::Spawner::Loop::Win32' : 'Proc::Spawner::Loop::POSIX';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 555 555
19 336 ▶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 343 343
20  
21 554 my $LOOP;
22 6776 ▶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 6775 ▶ return $LOOP if $LOOP;
Branch: 1 branch
BranchTrueFalseTotal
if $LOOP 6284 541 6825
24 541 (my $f = "$BACKEND.pm") =~ s{::}{/}g; require $f;
25 547 $LOOP = bless { q => [], watch => {}, timers => [], id => 0, backend => $BACKEND->new }, shift }
26  
27 # ------------------------------------------------------------------ what a caller asks for
28 6224 ▶sub defer { my $l = shift; push @{$l->{q}}, [$CAUSE, @_]; return }
Subroutine defer: called CC=1 SCAR=0.0
Pod documented
29 1914 ▶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 869 ▶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 347 ▶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 8 my ($l, $s, $cb) = @_; my $w = $l->watch('timer', $cb);
34 7 my $at = now() + $s; my $t = $l->{timers};
35 15 ▶ @$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 14 ▶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 2783 ▶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] ? : 922 1862 2784
38 19 ▶sub now { require Time::HiRes; Time::HiRes::time() }
Subroutine now: called CC=1 SCAR=0.0
Pod undocumented
39  
40 3137 ▶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 3137 my ($l, $kind, $cb, @a) = @_; my $id = ++$l->{id};
42 3174 $l->{watch}{$id} = [$kind, $cb];
43 3174 ▶ $l->{backend}->$kind($id, @a) unless $kind eq 'timer';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "timer" 3138 7 3145
44 3174 bless [$l, $id, $kind], 'Proc::Spawner::Loop::Watch' }
45 10 ▶sub cancel {
Subroutine cancel: called CC=3 SCAR=11.0
Pod undocumented
46 40 ▶ 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 20 ▶sub pending { my $l = shift; @{$l->{q}} + keys %{$l->{watch}} }
Subroutine pending: called CC=1 SCAR=0.0
Pod undocumented
49 16 ▶sub queued { scalar @{$_[0]{q}} } # the calls queued, not yet run
Subroutine queued: called CC=1 SCAR=0.0
Pod undocumented
50  
51 # ------------------------------------------------------------------ running
52 596 ▶sub wait_until { # true when CODE->() came true; with TIMEOUT (seconds; post-0.12, K5): false if the time ran out first
Subroutine wait_until: called CC=9 SCAR=22.5
Pod documented
53 610 my ($l, $done, $timeout) = @_;
54 596 ▶ die "Proc::Spawner: wait inside a reaction\n" if $REACTING; # D2
Branch: 1 branch
BranchTrueFalseTotal
if $Proc::Spawner::Loop::REACTING 1 602 603
55 595 ▶ $l->{backend}->begin unless $DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $Proc::Spawner::Loop::DEPTH 602 0 602
56 609 ▶ my $end = defined $timeout ? now() + $timeout : undef; my $met;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 0 602 602
57 602 my $ok = eval { local $DEPTH = $DEPTH + 1;
58 602 ▶ until ($met = !!$done->()) { my $left; if (defined $end) { $left = $end - now(); last if $left <= 0 } $l->turn($left) } 1 };
Branch: 3 branches
BranchTrueFalseTotal
unless ($met = !!&$done()) 9632 601 10233
if (defined $end) 0 9632 9632
if $left <= 0 0 0 0
59 639 ▶ $l->{backend}->end unless $DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $Proc::Spawner::Loop::DEPTH 602 0 602
60 624 ▶ die $@ unless $ok; $met }
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 601 602
61 10 ▶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
62 39 ▶ my $l = shift; return if $REACTING || $DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
if $Proc::Spawner::Loop::REACTING or $Proc::Spawner::Loop::DEPTH 0 24 24
Condition33%1/3
$Proc::Spawner::Loop::REACTING or $Proc::Spawner::Loop::DEPTH
l!l&&r!l&&!r
0024
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
63 25 local $DEPTH = $DEPTH + 1; # (a callback's own wait_until still dies: D2)
64 25 ▶ 1 while $l->pending && $l->turn(0) }
Branch: 1 branch
BranchTrueFalseTotal
$l->pending and $l->turn(0) and do { 1 } 26 24 50
Condition100%3/3
$l->pending and $l->turn(0)
!ll&&!rl&&r
81626
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
65 9630 ▶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
66 9630 my ($l, $timeout) = @_; # nothing happened
67 9630 ▶ 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"};}) 6246 3428 9674
68 3413 my $t = $l->{timers};
69 3428 ▶ if (@$t) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$t) 12 3416 3428
70 27 my $now = now();
71 27 ▶ 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
72 20 ▶ return 1 if @{$l->{q}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$l->{"q"};} 6 6 12
73 21 ▶ $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
74 3407 ▶ die "Proc::Spawner: nothing left to wait for\n" unless %{$l->{watch}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$l->{"watch"};} 1 3421 3422
75 3406 my $n = 0;
76 3406 for my $e ($l->{backend}->wait($timeout)) { # [ID, ARGS...] per event, the oldest first
77 3116 ▶ my ($id, @a) = @$e; my $w = delete $l->{watch}{$id} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $w = delete $l->{"watch"}{$id} or next 3124 0 3124
78 3118 $l->defer($w->[1], @a); $n++ }
79 3408 $n }
80  
81 package Proc::Spawner::Loop::Watch; # [LOOP, ID, KIND]
82 9 ▶sub id { $_[0][1] }
Subroutine id: called CC=1 SCAR=0.0
83 2 ▶sub kind { $_[0][2] }
Subroutine kind: called CC=1 SCAR=0.0
84 5 ▶sub cancel { my $w = shift; $w->[0]->cancel($w->[1]) }
Subroutine cancel: called CC=1 SCAR=0.0
85 3 ▶sub active { my $w = shift; exists $w->[0]{watch}{$w->[1]} }
Subroutine active: called CC=1 SCAR=0.0
86  
87 554 1;
88  
89 __END__
« lib/Proc/ShellSpawn/Builtins.pm ↑ Summary t/try.pl »