| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 336 | ▶ | sub tr_ { $TRACE->(join '', @_) if $TRACE } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tr_: called CC=2 SCAR=7.6
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | $w->[0] eq 'timer' ? (@{$l->{timers}} = grep $_->[1] != $id, @{$l->{timers}}) : $l->{backend}->cancel($id); 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 595 | ▶ | $l->{backend}->begin unless $DEPTH; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 609 | ▶ | my $end = defined $timeout ? now() + $timeout : undef; my $met; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 639 | ▶ | $l->{backend}->end unless $DEPTH; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 624 | ▶ | die $@ unless $ok; $met } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$Proc::Spawner::Loop::REACTING or $Proc::Spawner::Loop::DEPTH
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$l->pending and $l->turn(0)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 3413 | my $t = $l->{timers}; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 3428 | ▶ | if (@$t) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@$t and $t->[0][0] <= $now
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
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 20 | ▶ | return 1 if @{$l->{q}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 21 | ▶ | $timeout = $t->[0][0] - $now if @$t && (!defined $timeout || $t->[0][0] - $now < $timeout) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@$t and !defined($timeout) || $t->[0][0] - $now < $timeout
not defined $timeout or $t->[0][0] - $now < $timeout
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
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 3407 | ▶ | die "Proc::Spawner: nothing left to wait for\n" unless %{$l->{watch}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||