blib/lib/Thread/Subs.pm

filter: Statementstmt 56.6% 281 / 496 Branchbran 49.1% 121 / 246 Conditioncond 44.2% 46 / 104 MC/DCmcdc 23.0% 15 / 65 Subroutinesub 66.2% 59 / 89 Podpod 100% 16 / 16 totaltotal 52.9% 538 / 1016 CC 176 SCAR 27.2
CC
176
Cov
53%
start_workers
76.3
is_idle
72.0
_be_worker
50.3
CRAP 15.2
↑ Summary
1 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use if $ENV{DEBUG_THREAD_SUBS} => 'Debug::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 my $DEFAULT = 'DEFAULT'; # default pool name
6 2 my $SIG = 'CONT';
7 2 ▶my $THREADS = threads::posix->can('create') ? 'threads::posix' : 'threads';
Branch: 1 branch
BranchTrueFalseTotal
'threads::posix'->can('create') ? : 0 2 2
8 2 ▶my $MAIN = $THREADS->can('self') && $THREADS->self;
Condition33%1/3
$THREADS->can('self') && $THREADS->self
!ll&&!rl&&r
200
MC/DC0%0/2
$THREADS->can('self') && $THREADS->self
$THREADS->can('self') $THREADS->self
Truth table33%1/3
A && B
A= $THREADS->can('self')B= $THREADS->self
ABresult
0X0
100
111
9  
10 # "CRITICAL" indicates an operation which must be signal-safe
11 # (i.e. uninterruptible), not just thread-safe.
12  
13 package Thread::Subs;
14 2 our $VERSION = '1.000';
15  
16 2 ▶use threads::shared;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use POSIX (); # sigprocmask-related
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Sub::Util qw(set_subname subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 our @CARP_NOT = qw(attributes);
23  
24 # If a _die message starts with "BUG", it's meant to be unreachable.
25 8 ▶sub _die { exists(&Carp::croak) ? goto &Carp::croak : die "@_\n" }
Subroutine _die: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
exists &Carp::croak ? : 8 0 8
26 1 ▶sub _bad { _die("Invalid Thread attribute: @_") }
Subroutine _bad: called CC=1 SCAR=0.0
27 0 ▶sub _nap { select(undef, undef, undef, 0.05) } # microsleep
Subroutine _nap: not called CC=1 SCAR=6.9
28  
29 2 my %POOL; # per-pool worker count
30 my %SHIM; # per-package auto-shim setting
31 2 my %CLIM :shared; # per-sub concurrency limit (shared scalar ref)
32 2 my %DEFER :shared; # per-sub array for concurrency limits
33 2 my %QLIM :shared; # per-sub queue limit Thread::Subs::qlim
34 2 my %REQ :shared; # per-pool request arrays
35 2 my %SUB; # all subs Thread::Subs::attr
36 2 my %TASK :shared; # per-thread current sub
37  
38 2 my $ENDWAIT = 0;
39 2 my $STAGE :shared = 0; # 0: defs, 1: pools, 2: workers, 3: shims, 4: stop
40  
41 2 our @AllowSig;
42 2 $AllowSig[$_] = 1 for
43 POSIX::SIGFPE(),
44 POSIX::SIGILL(),
45 POSIX::SIGPIPE(),
46 POSIX::SIGSEGV(),
47 ;
48  
49 2 our $Caller; # for _name() qualification
50  
51 # start_workers redefines this with signal details
52 0 ▶sub _send_callback_signal { _die("Signal not settled yet") }
Subroutine _send_callback_signal: not called CC=1 SCAR=6.9
53  
54 3 ▶sub import {
Subroutine import: called CC=5 SCAR=16.2
55 3 my $class = shift;
56 3 my $caller = caller;
57 3 ▶ unless ($STAGE > 0 or exists $SHIM{$caller}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($STAGE > 0 or exists $SHIM{$caller}) 2 1 3
Condition66%2/3
$STAGE > 0 or exists $SHIM{$caller}
l!l&&r!l&&!r
012
MC/DC50%1/2
$STAGE > 0 or exists $SHIM{$caller}
$STAGE > 0 exists $SHIM{$caller}
Truth table66%2/3
A or B
A= $STAGE > 0B= exists $SHIM{$caller}
ABresult
1X1
011
000
58 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 push @{"${caller}::ISA"}, 'Thread::Subs::attributes';
60 }
61 3 $SHIM{$caller} = 1;
62 3 for (@_) {
63 1 ▶ if ($_ eq 'noshim') { $SHIM{$caller} = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'noshim') { } 1 0 1
64 0 else { _die("Invalid $class import option '$_'") }
65 }
66 3 return;
67 }
68  
69 26 ▶sub _name {
Subroutine _name: called CC=6 SCAR=18.0
70 26 my ($sub) = @_;
71 26 ▶ if (ref $sub) { $sub = subname($sub) // _die("BUG: sub has no name") }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $sub) { } 17 9 26
elsif ($Thread::Subs::Caller and not $sub =~ /:/u) { } 1 12 13
Condition33%1/3
subname($sub) // _die("BUG: sub has no name")
l!l&&r!l&&!r
1700
MC/DC0%0/2
subname($sub) // _die("BUG: sub has no name")
subname($sub) _die("BUG: sub has no name")
Truth table33%1/3
A // B
A= subname($sub)B= _die("BUG: sub has no name")
ABresult
1X1
011
000
72 1 ▶ elsif ($Caller and $sub !~ /:/) { $sub = "${Caller}::$sub" }
Condition100%3/3
$Thread::Subs::Caller and not $sub =~ /:/u
!ll&&!rl&&r
441
MC/DC100%2/2
$Thread::Subs::Caller and not $sub =~ /:/u
$Thread::Subs::Caller not $sub =~ /:/u
Truth table100%3/3
A and not(B)
A= $Thread::Subs::CallerB= not $sub =~ /:/u
ABresult
0X0
100
111
73 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 26 ▶ _die("Sub '$sub' does not exist")
Branch: 1 branch
BranchTrueFalseTotal
unless exists &$sub 1 25 26
75 unless exists &$sub;
76 25 return $sub;
77 }
78  
79 7 ▶sub _attr { local $Caller = caller; $SUB{&_name} } # used by t/01-nothreads.t
Subroutine _attr: called CC=1 SCAR=0.0
80  
81 12 ▶sub _define_one {
Subroutine _define_one: called CC=10 SCAR=23.4
82 12 ▶ _die("Too late to define sub attributes")
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE > 0 0 12 12
83 if $STAGE > 0;
84 12 my ($sub, $prop) = @_;
85 12 $sub = _name($sub);
86 11 ▶ my $attr = $SUB{$sub} //= Thread::Subs::attr->new;
Condition66%2/3
$SUB{$sub} //= "Thread::Subs::attr"->new
l!l&&r!l&&!r
470
MC/DC0%0/2
$SUB{$sub} //= "Thread::Subs::attr"->new
$SUB{$sub} "Thread::Subs::attr"->new
Truth table66%2/3
A //= B
A= $SUB{$sub}B= "Thread::Subs::attr"->new
ABresult
1X1
011
000
87 11 for (keys %$prop) {
88 16 ▶ _die("Invalid attribute '$_' in definition of $sub")
Branch: 1 branch
BranchTrueFalseTotal
unless /^(?:pool|clim|qlim|shim)$/u 0 16 16
89 unless m/^(?:pool|clim|qlim|shim)$/;
90 16 my $val = $prop->{$_};
91 16 ▶ _die("Attribute '$_' must be numeric in definition of $sub")
Branch: 1 branch
BranchTrueFalseTotal
if /^[cq]lim$/u and $val and $val =~ /\D/u 0 16 16
Condition66%4/6
/^[cq]lim$/u and $val and $val =~ /\D/u
!ll&&!rl&&r
790
/^[cq]lim$/u and $val
!ll&&!rl&&r
709
MC/DC0%0/3
/^[cq]lim$/u and $val and $val =~ /\D/u
/^[cq]lim$/u $val $val =~ /\D/u
Truth table50%2/4
A and B and C
A= /^[cq]lim$/uB= $valC= $val =~ /\D/u
ABCresult
0XX0
10X0
1100
1111
92 if /^[cq]lim$/ && $val && $val =~ /\D/;
93 16 $attr->$_($val);
94 }
95 11 ▶ if (my $clim = $attr->clim) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $clim = $attr->clim) { } 5 6 11
96 5 $CLIM{$sub} = shared_clone(\$clim);
97 5 $DEFER{$sub} = shared_clone([]);
98 }
99 else {
100 6 delete $CLIM{$sub};
101 6 delete $DEFER{$sub};
102 }
103 11 ▶ if ($attr->qlim) { $QLIM{$sub} = Thread::Subs::qlim->new($attr->qlim) }
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->qlim) { } 8 3 11
104 3 else { delete $QLIM{$sub} }
105 11 return;
106 }
107  
108 4 ▶sub define {
Subroutine define: called CC=7 SCAR=19.5
Pod documented
109 4 local $Caller = caller;
110 4 ▶ if (@_ == 1 and ref($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 1 3 4
elsif (@_ > 1 and ref $_[1] eq 'HASH') { } 1 2 3
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
211
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
111 1 my ($hash) = @_;
112 _define_one($_, $hash->{$_})
113 1 for keys %$hash;
114 }
115 ▶ elsif (@_ > 1 and ref($_[1]) eq 'HASH') {
Condition100%3/3
@_ > 1 and ref $_[1] eq "HASH"
!ll&&!rl&&r
111
MC/DC100%2/2
@_ > 1 and ref $_[1] eq "HASH"
@_ > 1 ref $_[1] eq "HASH"
Truth table100%3/3
A and B
A= @_ > 1B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
116 1 ▶ _define_one(shift, shift)
Branch: 1 branch
BranchTrueFalseTotal
@_ and do { _define_one(shift(), shift()) } 3 1 4
117 while @_;
118 }
119 else {
120 2 my ($sub, %opts) = @_;
121 2 _define_one($sub, \%opts);
122 }
123 3 return;
124 }
125  
126 my %_ATTR = (
127 1 ▶ clim => sub { m/^=(\d+)$/ ? $1 : _bad("clim$_") },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
/^=(\d+)$/u ? : 1 0 1
128 3 ▶ pool => sub { m/^=(\w+)$/ ? $1 : _bad("pool$_") },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
/^=(\w+)$/u ? : 3 0 3
129 1 ▶ qlim => sub { m/^=(\d+)$/ ? $1 : _bad("qlim$_") },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
/^=(\d+)$/u ? : 1 0 1
130 2 );
131 5 ▶sub _attribute {
Subroutine _attribute: called CC=13 SCAR=25.8
132 5 my ($class, $sub) = @_;
133 5 ▶ return 1 unless /^Thread(?:\(|$)/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^Thread(?:\(|$)/u 0 5 5
134 5 $sub = _name($sub);
135 5 ▶ my @attr = /^Thread\((.+)\)$/ ? ($1 =~ m/[^, ]+/g) : ();
Branch: 1 branch
BranchTrueFalseTotal
/^Thread\((.+)\)$/u ? : 4 1 5
136 #@! Handling attributes for $sub
137 5 ▶ my %opt = (shim => $SHIM{$class} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$SHIM{$class} ? : 1 4 5
138 5 for (@attr) {
139 6 my ($name, $val) = /^(\w+)(=.+)?$/;
140 _bad("'$_' is unrecognised")
141 6 ▶ unless $name && exists($_ATTR{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless $name and exists $_ATTR{$name} 1 5 6
Condition66%2/3
$name and exists $_ATTR{$name}
!ll&&!rl&&r
015
MC/DC50%1/2
$name and exists $_ATTR{$name}
$name exists $_ATTR{$name}
Truth table66%2/3
A and B
A= $nameB= exists $_ATTR{$name}
ABresult
0X0
100
111
142 _bad("multiple '$name' definitions")
143 5 ▶ if exists $opt{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{$name} 0 5 5
144 5 ▶ $opt{$name} = $_ATTR{$name}->() for $val//'';
Condition50%1/2
$val // ""
l!l
50
MC/DC0%0/1
$val // ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
145 }
146 4 ▶ if ($opt{pool}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"pool"}) 3 1 4
147 3 ▶ if ($opt{pool} eq 'SUB') { $opt{pool} = $sub }
Branch: 2 branches
BranchTrueFalseTotal
if ($opt{'pool'} eq 'SUB') { } 1 2 3
elsif ($opt{'pool'} eq 'PKG') { } 1 1 2
148 1 elsif ($opt{pool} eq 'PKG') { $opt{pool} = $class }
149 }
150 4 _define_one($sub, \%opt);
151 4 return;
152 }
153  
154 5 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=1 SCAR=0.0
155 5 my ($class, $code, @attr) = @_;
156 5 return grep { _attribute($class, $code) } @attr;
157 }
158 2 *Thread::Subs::attributes::MODIFY_CODE_ATTRIBUTES = \&MODIFY_CODE_ATTRIBUTES;
159  
160 3 ▶sub end_definitions {
Subroutine end_definitions: called CC=6 SCAR=17.9
Pod documented
161 3 ▶ if ($STAGE == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($STAGE == 0) 1 2 3
162 1 $STAGE = 0.5; # intermediate
163 1 for (values %SUB) {
164 7 my $p = $_->pool;
165 7 my $c = $_->clim;
166 7 ▶ $POOL{$p} //= 1;
Condition100%2/2
$POOL{$p} //= 1
l!l
34
MC/DC100%1/1
$POOL{$p} //= 1
$POOL{$p}
Truth table100%2/2
A
A= $POOL{$p}
Aresult
11
00
167 7 ▶ $POOL{$p} = $c if $c > $POOL{$p};
Branch: 1 branch
BranchTrueFalseTotal
if $c > $POOL{$p} 1 6 7
168 }
169 1 $STAGE = 1; # %POOL now valid
170 }
171 3 ▶ return wantarray ? %POOL : scalar(keys %POOL);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
172 }
173  
174 2 ▶sub set_pool {
Subroutine set_pool: called CC=9 SCAR=23.9
Pod documented
175 2 ▶ _die("set_pool called in stage $STAGE (stage 0 or 1 required)")
Branch: 1 branch
BranchTrueFalseTotal
unless $STAGE == 0 or $STAGE == 1 0 2 2
Condition33%1/3
$STAGE == 0 or $STAGE == 1
l!l&&r!l&&!r
020
MC/DC0%0/2
$STAGE == 0 or $STAGE == 1
$STAGE == 0 $STAGE == 1
Truth table33%1/3
A or B
A= $STAGE == 0B= $STAGE == 1
ABresult
1X1
011
000
176 unless $STAGE == 0 or $STAGE == 1;
177 2 ▶ end_definitions() if $STAGE == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE == 0 0 2 2
178 2 ▶ unshift @_, $DEFAULT
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 2 2
179 if @_ == 1;
180 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
181 3 my $pool = shift;
182 _die("No subs use '$pool' worker pool")
183 3 ▶ unless exists $POOL{$pool};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $POOL{$pool} 1 2 3
184 2 my $count = shift;
185 2 ▶ _die("Invalid worker count '$count' for '$pool' pool")
Branch: 1 branch
BranchTrueFalseTotal
if $count =~ /\D/u or $count < 1 0 2 2
Condition33%1/3
$count =~ /\D/u or $count < 1
l!l&&r!l&&!r
002
MC/DC0%0/2
$count =~ /\D/u or $count < 1
$count =~ /\D/u $count < 1
Truth table33%1/3
A or B
A= $count =~ /\D/uB= $count < 1
ABresult
1X1
011
000
186 if $count =~ /\D/ or $count < 1;
187 2 $POOL{$pool} = $count;
188 }
189 1 return end_definitions();
190 }
191  
192 4 ▶sub signal {
Subroutine signal: called CC=6 SCAR=17.9
Pod documented
193 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
194 3 my ($sig) = @_;
195 _die("Invalid signal '$sig'")
196 3 ▶ if $sig and not exists $SIG{$sig};
Branch: 1 branch
BranchTrueFalseTotal
if $sig and not exists $SIG{$sig} 1 2 3
Condition100%3/3
$sig and not exists $SIG{$sig}
!ll&&!rl&&r
111
MC/DC100%2/2
$sig and not exists $SIG{$sig}
$sig not exists $SIG{$sig}
Truth table100%3/3
A and not(B)
A= $sigB= not exists $SIG{$sig}
ABresult
0X0
100
111
197 2 ▶ _die("Too late to change signal (stage $STAGE)")
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE > 1 0 2 2
198 if $STAGE > 1;
199 2 ▶ $SIG = $sig || '';
Condition100%2/2
$sig || ''
l!l
11
MC/DC100%1/1
$sig || ''
$sig
Truth table100%2/2
A
A= $sig
Aresult
11
00
200 }
201 3 return $SIG;
202 }
203  
204 0 ▶sub safe_create_thread {
Subroutine safe_create_thread: not called CC=5 SCAR=34.0
Pod documented
205 0 my $orig = POSIX::SigSet->new;
206 0 my $safe = POSIX::SigSet->new;
207 0 $safe->fillset;
208 $safe->delset($_)
209 0 ▶ for grep { $_ && $AllowSig[$_] } keys @AllowSig;
Condition0%0/3
$_ && $Thread::Subs::AllowSig[$_]
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && $Thread::Subs::AllowSig[$_]
$_ $Thread::Subs::AllowSig[$_]
Truth table0%0/3
A && B
A= $_B= $Thread::Subs::AllowSig[$_]
ABresult
0X0
100
111
210 0 ▶ POSIX::sigprocmask(POSIX::SIG_BLOCK(), $safe, $orig)
Condition0%0/3
POSIX::sigprocmask(0, $safe, $orig) // _die("Can't set sigprocmask: $!")
l!l&&r!l&&!r
000
MC/DC0%0/2
POSIX::sigprocmask(0, $safe, $orig) // _die("Can't set sigprocmask: $!")
POSIX::sigprocmask(0, $safe, $orig) _die("Can't set sigprocmask: $!")
Truth table0%0/3
A // B
A= POSIX::sigprocmask(0, $safe, $orig)B= _die("Can't set sigprocmask: $!")
ABresult
1X1
011
000
211 // _die("Can't set sigprocmask: $!");
212 # SigSet objects segfault perl v5.12 on threads->create
213 0 my $thread = $THREADS->create(@_);
214 0 ▶ POSIX::sigprocmask(POSIX::SIG_SETMASK(), $orig)
Condition0%0/3
POSIX::sigprocmask(2, $orig) // _die("Can't restore sigprocmask: $!")
l!l&&r!l&&!r
000
MC/DC0%0/2
POSIX::sigprocmask(2, $orig) // _die("Can't restore sigprocmask: $!")
POSIX::sigprocmask(2, $orig) _die("Can't restore sigprocmask: $!")
Truth table0%0/3
A // B
A= POSIX::sigprocmask(2, $orig)B= _die("Can't restore sigprocmask: $!")
ABresult
1X1
011
000
215 // _die("Can't restore sigprocmask: $!");
216 0 return $thread;
217 }
218  
219 0 ▶sub _be_worker {
Subroutine _be_worker: not called CC=7 SCAR=39.2
220 0 my ($pool) = @_;
221 0 my $id = join('-', $THREADS->tid, $pool);
222 #@! Worker $id spawned
223 0 do { lock(%TASK); $TASK{$id} = ''; cond_signal(%TASK) };
224 0 my $queue = $REQ{$pool};
225 0 my ($work, $result, $sub, @arg, $clim, $qlim);
226 0 ▶ my $obtain_sub = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
227 0 lock(@$queue);
228 { # redo point
229 0 ▶ unless (@$queue) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$queue) 0 0 0
230 0 ▶ if ($STAGE < 4) { cond_wait(@$queue); redo }
Branch: 1 branch
BranchTrueFalseTotal
if ($STAGE < 4) { } 0 0 0
231 0 else { cond_signal(@$queue); return 0 }
232 }
233 0 $work = shift @$queue;
234 0 $sub = $work->[0];
235 0 $clim = $CLIM{$sub};
236 0 ▶ if ($clim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clim) 0 0 0
237 0 lock($$clim); # exclusive on $clim and $DEFER{$sub}
238 0 ▶ unless ($$clim > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$clim > 0) 0 0 0
239 #@! Request for $sub deferred due to concurrency limit
240 0 push @{$DEFER{$sub}}, $work;
241 0 undef $work;
242 0 redo;
243 }
244 0 --$$clim;
245 }
246 }
247 #@! Worker $id assigned to $sub
248 0 $TASK{$id} = $sub;
249 0 ▶ cond_signal(@$queue) if @$queue;
Branch: 1 branch
BranchTrueFalseTotal
if @$queue 0 0 0
250 0 return 1;
251 0 };
252 0 ▶ while (&$obtain_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$obtain_sub) 0 0 0
253 0 $qlim = $QLIM{$sub};
254 0 ▶ while ($work) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work) 0 0 0
255 0 (undef, $result, @arg) = @$work;
256 0 undef $work;
257 0 ▶ $qlim->out if $qlim;
Branch: 1 branch
BranchTrueFalseTotal
if $qlim 0 0 0
258 0 ▶ my @res = eval { no strict 'refs'; $sub->(@arg) };
Subroutine BEGIN: called CC=1 SCAR=0.6
259 0 ▶ if (my $ex = $@) { $result->croak($ex) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ex = $@) { } 0 0 0
260 0 else { $result->send(@res) }
261 #@! Worker $id executed $sub
262 0 ▶ if ($clim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clim) 0 0 0
263 0 lock($$clim); # exclusive on $clim and $DEFER{$sub}
264 0 $work = shift @{$DEFER{$sub}};
265 0 ▶ ++$$clim unless $work;
Branch: 1 branch
BranchTrueFalseTotal
unless $work 0 0 0
266 }
267 }
268 #@! Worker $id finished handling $sub
269 0 $TASK{$id} = '';
270 }
271 #@! Worker $id exits
272 0 return;
273 }
274  
275 2 ▶sub start_workers {
Subroutine start_workers: called CC=10 SCAR=43.3
Pod documented
276 2 ▶ _die("Can't start workers: threads not available")
Branch: 1 branch
BranchTrueFalseTotal
unless $MAIN 2 0 2
277 unless $MAIN;
278 0 ▶ end_definitions() if $STAGE == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE == 0 0 0 0
279 0 ▶ _die("start_workers only available in stage 1 (now stage $STAGE)")
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE != 1 0 0 0
280 if $STAGE != 1;
281 0 $STAGE = 1.5; # intermediate
282 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
283 0 ▶ if (!$SIG) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $SIG) { } 0 0 0
elsif ($THREADS ne 'threads') { } 0 0 0
284 #@! No signal or handler for callbacks
285 0 ▶ *_send_callback_signal = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
286 }
287 elsif ($THREADS ne 'threads') {
288 #@! Using $THREADS->kill($SIG) for callbacks
289 0 ▶ *_send_callback_signal = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
290 #@! Sending $SIG via $THREADS->kill
291 0 $MAIN->kill($SIG);
292 0 return;
293 0 };
294 }
295 else {
296 #@! Using kill $SIG for callbacks
297 0 ▶ *_send_callback_signal = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 #@! Sending $SIG to PID=$$
299 0 kill $SIG, $$;
300 0 return;
301 0 };
302 }
303 0 lock(%TASK);
304 0 for my $pool (keys %POOL) {
305 0 my $count = $POOL{$pool};
306 #@! Starting '$pool' worker pool ($count)
307 0 $REQ{$pool} = shared_clone([]);
308 0 for (1..$count) {
309 0 my $tid = safe_create_thread(\&_be_worker, $pool)->tid;
310 0 ▶ cond_wait(%TASK) until exists($TASK{"$tid-$pool"});
Branch: 1 branch
BranchTrueFalseTotal
exists $TASK{"$tid-$pool"} or do { &cond_wait(\%TASK) } 0 0 0
311 }
312 }
313 0 ▶ $SIG{$SIG} = \&Thread::Subs::result::run_callback_queue
Branch: 1 branch
BranchTrueFalseTotal
if $SIG 0 0 0
314 if $SIG;
315 0 $STAGE = 2;
316 0 return end_definitions();
317 }
318  
319 0 ▶sub shim {
Subroutine shim: not called CC=3 SCAR=24.8
Pod documented
320 0 ▶ _die("Shim requested before workers started")
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE < 2 0 0 0
321 if $STAGE < 2;
322 0 my ($sub) = @_;
323 0 local $Caller = caller;
324 0 $sub = _name($sub);
325 0 ▶ my $attr = $SUB{$sub} or _die("Can't shim non-thread sub '$sub'");
Branch: 1 branch
BranchTrueFalseTotal
my $attr = $SUB{$sub} or _die("Can't shim non-thread sub '${sub}'") 0 0 0
326 0 my $pool = $attr->pool;
327 0 my $qlim = $QLIM{$sub};
328 0 my $queue = $REQ{$pool};
329 0 ▶ return set_subname "$sub<shim>" => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
330 0 ▶ _die("Shim for '$sub' called after workers stopped")
Branch: 1 branch
BranchTrueFalseTotal
unless $STAGE < 4 0 0 0
331 unless $STAGE < 4;
332 #@! Requesting $sub pool=$pool @{[$qlim ? 'qlim='.$qlim->slack : '']}
333 0 my $res = Thread::Subs::result->new;
334 0 ▶ $res->fatal("Exception in sub '$sub'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray or $THREADS->tid 0 0 0
Condition0%0/3
defined wantarray or $THREADS->tid
l!l&&r!l&&!r
000
MC/DC0%0/2
defined wantarray or $THREADS->tid
defined wantarray $THREADS->tid
Truth table0%0/3
A or B
A= defined wantarrayB= $THREADS->tid
ABresult
1X1
011
000
335 unless defined(wantarray) or $THREADS->tid;
336 0 my $req = shared_clone([$sub, $res, @_]);
337 0 ▶ $qlim->in if $qlim; # can block
Branch: 1 branch
BranchTrueFalseTotal
if $qlim 0 0 0
338 0 lock(@$queue);
339 0 push @$queue, $req; # CRITICAL
340 0 cond_signal(@$queue);
341 0 return $res;
342 0 };
343 }
344  
345 1 ▶sub deploy_shims {
Subroutine deploy_shims: called CC=4 SCAR=19.6
Pod documented
346 1 ▶ _die("deploy_shims only available in stage 2 (now stage $STAGE)")
Branch: 1 branch
BranchTrueFalseTotal
unless $STAGE == 2 1 0 1
347 unless $STAGE == 2;
348 0 ▶ _die("Attempt to deploy shims in a thread")
Branch: 1 branch
BranchTrueFalseTotal
if $THREADS->tid 0 0 0
349 if $THREADS->tid;
350 0 $STAGE = 2.5; # intermediate
351 0 for (grep { $SUB{$_}->shim } keys %SUB) {
352 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
353 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
354 0 *{$_} = shim($_);
355 #@! Deployed shim for $_
356 }
357 0 $STAGE = 3;
358 0 return;
359 }
360  
361 1 ▶sub startup {
Subroutine startup: called CC=3 SCAR=13.3
Pod documented
362 1 ▶ _die("Workers already started")
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE > 1 0 1 1
363 if $STAGE > 1;
364 1 ▶ &set_pool if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
365 1 start_workers();
366 0 deploy_shims();
367 0 return end_definitions();
368 }
369  
370 4 ▶sub endwait {
Subroutine endwait: called CC=6 SCAR=18.6
Pod documented
371 4 ▶ _die("Attempt to use endwait in a thread")
Branch: 1 branch
BranchTrueFalseTotal
if $MAIN and $THREADS->tid 0 4 4
Condition33%1/3
$MAIN and $THREADS->tid
!ll&&!rl&&r
400
MC/DC0%0/2
$MAIN and $THREADS->tid
$MAIN $THREADS->tid
Truth table33%1/3
A and B
A= $MAINB= $THREADS->tid
ABresult
0X0
100
111
372 if $MAIN and $THREADS->tid;
373 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
374 3 my ($t) = @_;
375 3 ▶ _die("Invalid endwait '$t'")
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($t) and $t >= 0 1 2 3
Condition66%2/3
&looks_like_number($t) and $t >= 0
!ll&&!rl&&r
012
MC/DC50%1/2
&looks_like_number($t) and $t >= 0
&looks_like_number($t) $t >= 0
Truth table66%2/3
A and B
A= &looks_like_number($t)B= $t >= 0
ABresult
0X0
100
111
376 unless looks_like_number($t) and $t >= 0;
377 2 $ENDWAIT = $t;
378 }
379 3 return $ENDWAIT;
380 }
381  
382 3 ▶sub stop_workers {
Subroutine stop_workers: called CC=3 SCAR=11.4
Pod documented
383 3 ▶ if ($STAGE < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($STAGE < 4) 2 1 3
384 #@! Stopping workers
385 2 $STAGE = 4;
386 2 for my $queue (values %REQ) {
387 0 lock(@$queue);
388 0 cond_signal(@$queue);
389 }
390 }
391 3 return;
392 }
393  
394 4 ▶sub running_workers {
Subroutine running_workers: called CC=8 SCAR=28.9
Pod documented
395 4 ▶ return () if $STAGE < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE < 2 2 2 4
396 2 my ($sig, @thr);
397 2 do {
398 0 ▶ local $SIG{$SIG} = sub { $sig = 1 } if $SIG;
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $SIG 1 1 2
399 2 for (keys %TASK) {
400 0 ▶ if (my $t = $THREADS->object(/^(\d+)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $THREADS->object(/^(\d+)/u)) { } 0 0 0
401 0 ▶ if ($t->is_joinable) { $t->join; delete $TASK{$_} }
Branch: 2 branches
BranchTrueFalseTotal
if ($t->is_joinable) { } 0 0 0
elsif ($t->is_running) { } 0 0 0
402 0 elsif ($t->is_running) { push @thr, $t }
403 }
404 0 else { delete $TASK{$_} } # detached thread terminated?
405 }
406 };
407 2 ▶ _send_callback_signal() if $sig;
Branch: 1 branch
BranchTrueFalseTotal
if $sig 0 2 2
408 2 return @thr;
409 }
410  
411 0 ▶sub stop_and_wait {
Subroutine stop_and_wait: not called CC=4 SCAR=30.0
Pod documented
412 0 ▶ _die("Attempt to stop_and_wait in a thread")
Branch: 1 branch
BranchTrueFalseTotal
if $MAIN and $THREADS->tid 0 0 0
Condition0%0/3
$MAIN and $THREADS->tid
!ll&&!rl&&r
000
MC/DC0%0/2
$MAIN and $THREADS->tid
$MAIN $THREADS->tid
Truth table0%0/3
A and B
A= $MAINB= $THREADS->tid
ABresult
0X0
100
111
413 if $MAIN and $THREADS->tid;
414 0 stop_workers();
415 #@! Waiting for workers
416 0 ▶ _nap() while running_workers();
Branch: 1 branch
BranchTrueFalseTotal
running_workers() and do { _nap() } 0 0 0
417 #@! All worker threads joined
418 0 Thread::Subs::result::run_callback_queue();
419 0 return;
420 }
421  
422 1 ▶sub current_tasks { running_workers(); return %TASK }
Subroutine current_tasks: called CC=1 SCAR=0.0
Pod documented
423  
424 0 ▶sub queue_slack {
Subroutine queue_slack: not called CC=5 SCAR=34.0
Pod documented
425 0 ▶ _die("queue_slack not available until stage 1 (now stage $STAGE)")
Branch: 1 branch
BranchTrueFalseTotal
unless $STAGE >= 1 0 0 0
426 unless $STAGE >= 1;
427 0 local $Caller = caller;
428 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
429 0 my $sub = &_name;
430 0 ▶ return exists($QLIM{$sub}) ? $QLIM{$sub}->slack : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $QLIM{$sub} ? : 0 0 0
431 }
432 0 my %q;
433 $q{$_} = $QLIM{$_}->slack
434 0 for keys %QLIM;
435 0 return %q;
436 }
437  
438 0 ▶sub is_idle {
Subroutine is_idle: not called CC=8 SCAR=42.8
Pod documented
439 0 ▶ return if $STAGE < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $STAGE < 2 0 0 0
440 _die("No such worker pool '$_'")
441 0 for grep { !defined($REQ{$_}) } @_;
442 0 ▶ my @pool = @_ ? @_ : keys(%REQ);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
443 0 lock(@$_) for map { $REQ{$_} } @pool;
444 0 ▶ return 0 if grep { @{$REQ{$_}} > 0 } @pool;
Branch: 1 branch
BranchTrueFalseTotal
if grep {@{$REQ{$_};} > 0;} @pool 0 0 0
445 0 my $pools = join('|', map { quotemeta($_) } @pool);
446 return 0
447 0 ▶ for grep { $TASK{$_} and /^\d+-(?:$pools)$/ } keys %TASK;
Condition0%0/3
$TASK{$_} && /^\d+-(?:$pools)$/u
!ll&&!rl&&r
000
MC/DC0%0/2
$TASK{$_} && /^\d+-(?:$pools)$/u
$TASK{$_} /^\d+-(?:$pools)$/u
Truth table0%0/3
A && B
A= $TASK{$_}B= /^\d+-(?:$pools)$/u
ABresult
0X0
100
111
448 0 return 1;
449 }
450  
451 2 ▶END {
Subroutine END: called CC=4 SCAR=20.9
452 #@! Thread::Subs END (ENDWAIT=$ENDWAIT)
453 2 &stop_workers;
454 2 my $lim = time + $ENDWAIT;
455 2 ▶ while (&running_workers) {
Branch: 1 branch
BranchTrueFalseTotal
if (&running_workers) 0 2 2
456 0 ▶ if (time < $lim) { _nap() }
Branch: 1 branch
BranchTrueFalseTotal
if (&Thread::Subs::time() < $lim) { } 0 0 0
457 else {
458 #@! Detaching remaining workers
459 0 $_->detach for &running_workers;
460 0 return;
461 }
462 }
463 #@! Clean exit
464 }
465  
466  
467 package Thread::Subs::attr;
468 7 ▶sub new { bless [] }
Subroutine new: called CC=1 SCAR=0.0
469 14 ▶sub pool { @_ == 1 ? $_[0][0] || $DEFAULT : do { $_[0][0] = $_[1]; $_[0] } }
Subroutine pool: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 11 3 14
Condition66%2/3
$_[0][0] or $DEFAULT
l!l&&r!l&&!r
650
MC/DC0%0/2
$_[0][0] or $DEFAULT
$_[0][0] $DEFAULT
Truth table66%2/3
A or B
A= $_[0][0]B= $DEFAULT
ABresult
1X1
011
000
470 27 ▶sub clim { @_ == 1 ? $_[0][1] || 0 : do { $_[0][1] = $_[1]; $_[0] } }
Subroutine clim: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 22 5 27
Condition100%2/2
$_[0][1] or 0
l!l
1210
MC/DC100%1/1
$_[0][1] or 0
$_[0][1]
Truth table100%2/2
A
A= $_[0][1]
Aresult
11
00
471 26 ▶sub qlim { @_ == 1 ? $_[0][2] || 0 : do { $_[0][2] = $_[1]; $_[0] } }
Subroutine qlim: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 22 4 26
Condition100%2/2
$_[0][2] or 0
l!l
184
MC/DC100%1/1
$_[0][2] or 0
$_[0][2]
Truth table100%2/2
A
A= $_[0][2]
Aresult
11
00
472 6 ▶sub shim { @_ == 1 ? !!$_[0][3] : do { $_[0][3] = $_[1]; $_[0] } }
Subroutine shim: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2 4 6
473  
474  
475 package Thread::Subs::qlim;
476  
477 2 ▶use threads::shared;
Subroutine BEGIN: called CC=1 SCAR=0.0
478  
479 # Queue limits can't be handled with a simple semaphore because we
480 # need to maintain an order of arrival across threads. This object
481 # limits the queue using a ticket dispenser: calls to in() block when
482 # the limit is reached, and unblock on a FIFO basis per call to out().
483  
484 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
485 8 my ($class, $lim) = @_;
486 8 return bless shared_clone([0, $lim]);
487 }
488  
489 0 ▶sub slack {
Subroutine slack: not called CC=1 SCAR=6.9
490 0 my ($self) = @_;
491 0 lock($self);
492 0 return $self->[1] - $self->[0];
493 }
494  
495 0 ▶sub in {
Subroutine in: not called CC=2 SCAR=17.9
496 0 my ($self) = @_;
497 0 lock($self);
498 0 my $ticket = $self->[0]++; # CRITICAL
499 0 ▶ cond_wait($self) until $ticket < $self->[1];
Branch: 1 branch
BranchTrueFalseTotal
$ticket < $self->[1] or do { &cond_wait(\$self) } 0 0 0
500 0 return;
501 }
502  
503 0 ▶sub out {
Subroutine out: not called CC=1 SCAR=6.9
504 0 my ($self) = @_;
505 0 lock($self);
506 0 $self->[1]++; # CRITICAL
507 0 cond_broadcast($self);
508 0 return;
509 }
510  
511  
512 package Thread::Subs::result;
513  
514 2 ▶use threads::shared;
Subroutine BEGIN: called CC=1 SCAR=0.0
515 2 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
516  
517 # %CB requires careful management. Callback CODE refs can't be
518 # shared, so they have to be stored in this hash in the main thread.
519 # It's important that callbacks be executed so as to clear out the
520 # hash entry: you can't simply catch object expiry with DESTROY for
521 # shared objects.
522  
523 2 my %CB;
524 2 my @CBQ :shared; # callback queue (ready result objects)
525 2 my $CBF :shared = 0; # callbacks running flag (do not signal when true)
526  
527 1 ▶sub _die { exists(&Carp::croak) ? goto &Carp::croak : die "@_\n" }
Subroutine _die: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
exists &Carp::croak ? : 1 0 1
528  
529 2 ▶END {
Subroutine END: called CC=1 SCAR=0.0
530 2 $CBF = -1; # disable run_callback_queue and inhibit signals
531 #@! Thread::Subs::result END: Cancel callbacks (@{[scalar keys %CB]})
532 2 %CB = ();
533 }
534  
535 2 ▶sub new { bless shared_clone([0, 0]) }
Subroutine new: called CC=1 SCAR=0.0
536  
537 8 ▶sub _id { $MAIN ? is_shared($_[0]) // _die("BUG: result object not shared") : refaddr($_[0]) }
Subroutine _id: called CC=3 SCAR=17.3
Branch: 1 branch
BranchTrueFalseTotal
$MAIN ? : 0 8 8
Condition0%0/3
&is_shared(\$_[0]) // _die("BUG: result object not shared")
l!l&&r!l&&!r
000
MC/DC0%0/2
&is_shared(\$_[0]) // _die("BUG: result object not shared")
&is_shared(\$_[0]) _die("BUG: result object not shared")
Truth table0%0/3
A // B
A= &is_shared(\$_[0])B= _die("BUG: result object not shared")
ABresult
1X1
011
000
538  
539 # NB: this can be executed directly or by a signal handler, so it can
540 # interrupt itself! $CBF handles self-exclusion.
541 2 ▶sub run_callback_queue {
Subroutine run_callback_queue: called CC=12 SCAR=26.8
542 2 ▶ return 0 if $CBF; # at END or already executing
Branch: 1 branch
BranchTrueFalseTotal
if $CBF 0 2 2
543 2 ▶ _die("Callbacks must be executed in the main thread")
Branch: 1 branch
BranchTrueFalseTotal
if $MAIN and $THREADS->tid 0 2 2
Condition33%1/3
$MAIN and $THREADS->tid
!ll&&!rl&&r
200
MC/DC0%0/2
$MAIN and $THREADS->tid
$MAIN $THREADS->tid
Truth table33%1/3
A and B
A= $MAINB= $THREADS->tid
ABresult
0X0
100
111
544 if $MAIN and $THREADS->tid;
545 2 $CBF = 1; # exclude self and inhibit signals
546 #@! Invoking callbacks
547 2 my $n = 0;
548 2 ▶ while ($CBF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CBF) 4 2 6
549 4 my $result = do {
550 4 lock(@CBQ);
551 4 ▶ if (@CBQ) { shift @CBQ }
Branch: 1 branch
BranchTrueFalseTotal
if (@CBQ) { } 2 2 4
552 2 ▶ else { $CBF = 0; $CBF = 1 if @CBQ } # signal race possile
Branch: 1 branch
BranchTrueFalseTotal
if @CBQ 0 2 2
553 };
554 4 ▶ if (ref $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result) 2 2 4
555 2 $n++;
556 eval {
557 2 my $id = $result->_id;
558 2 ▶ if ($id && $CB{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and $CB{$id}) 2 0 2
Condition33%1/3
$id and $CB{$id}
!ll&&!rl&&r
002
MC/DC0%0/2
$id and $CB{$id}
$id $CB{$id}
Truth table33%1/3
A and B
A= $idB= $CB{$id}
ABresult
0X0
100
111
559 2 ▶ _die("BUG: attempt to invoke callback on unready result")
Branch: 1 branch
BranchTrueFalseTotal
unless $result->[0] 0 2 2
560 unless $result->[0];
561 2 (delete $CB{$id})->($result);
562 #@! Callback executed successfully
563 }
564 2 return 1;
565 0 ▶ } or do { $CBF = 0; die $@ };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $id = $result->_id; if ($id and $CB{$id}) { _die("BUG: attempt to invoke callback on unready result") unless $result->[0]; delete $CB{$id}->($result); } ; return 1 } } or do { $CBF = 0; die $@ } 2 0 2
566 }
567 }
568 #@! Callback processing complete ($n)
569 2 return $n;
570 }
571  
572 6 ▶sub cb {
Subroutine cb: called CC=11 SCAR=25.9
573 6 ▶ _die("Thread::Subs::result cb method only available in the main thread")
Branch: 1 branch
BranchTrueFalseTotal
if $MAIN and $THREADS->tid 0 6 6
Condition33%1/3
$MAIN and $THREADS->tid
!ll&&!rl&&r
600
MC/DC0%0/2
$MAIN and $THREADS->tid
$MAIN $THREADS->tid
Truth table33%1/3
A and B
A= $MAINB= $THREADS->tid
ABresult
0X0
100
111
574 if $MAIN and $THREADS->tid;
575 6 my ($self, $cb) = @_;
576 6 my $id = $self->_id;
577 6 ▶ return $CB{$id} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 3 3 6
578 3 ▶ return $self if $CBF == -1; # no-op if END reached
Branch: 1 branch
BranchTrueFalseTotal
if $CBF == -1 0 3 3
579 3 my $sig;
580 3 do {
581 0 ▶ local $SIG{$SIG} = sub { $sig = 1 } if $SIG; # defer callbacks
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $SIG 0 3 3
582 3 lock($self);
583 3 ▶ if ($cb) { $CB{$id} = $cb }
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 3 0 3
584 0 else { delete $CB{$id} }
585 3 ▶ if ($self->[0] == 0) { $self->[1] = $cb ? 1 : 0 }
Branch: 3 branches
BranchTrueFalseTotal
if ($self->[0] == 0) { } 2 1 3
elsif ($cb) { } 1 0 1
$cb ? : 2 0 2
586 elsif ($cb) {
587 1 lock(@CBQ);
588 1 push @CBQ, $self; # CRITICAL
589 1 $sig = $SIG;
590 }
591 };
592 3 ▶ Thread::Subs::_send_callback_signal()
Branch: 1 branch
BranchTrueFalseTotal
if $sig 0 3 3
593 if $sig;
594 3 return $self;
595 }
596  
597 2 ▶sub ready { $_[0][0] }
Subroutine ready: called CC=1 SCAR=0.0
598 3 ▶sub failed { $_[0][0] < 0 }
Subroutine failed: called CC=1 SCAR=0.0
599  
600 5 ▶sub _set {
Subroutine _set: called CC=9 SCAR=25.3
601 5 my $self = shift;
602 5 my $args = shared_clone([@_]);
603 5 my $sig;
604 5 do {
605 0 ▶ local $SIG{$SIG} = sub { $sig = 1 } # defer callbacks
Subroutine __ANON__: not called CC=1 SCAR=6.9
606 5 ▶ if $SIG && !($MAIN && $THREADS->tid); # if main thread
Branch: 1 branch
BranchTrueFalseTotal
if $SIG and not $MAIN && $THREADS->tid 0 5 5
Condition16%1/6
$SIG and not $MAIN && $THREADS->tid
!ll&&!rl&&r
500
$MAIN && $THREADS->tid
!ll&&!rl&&r
000
MC/DC0%0/3
$SIG and not $MAIN && $THREADS->tid
$SIG $MAIN $THREADS->tid
Truth table25%1/4
A and not(B && C)
A= $SIGB= $MAINC= $THREADS->tid
ABCresult
0XX0
1110
10X1
1101
607 5 lock($self);
608 5 ▶ if ($self->[0] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] == 0) 2 3 5
609 2 my $cb = $self->[1];
610 2 @$self = @$args;
611 2 ▶ if ($cb && $CBF != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb and $CBF != -1) 2 0 2
Condition33%1/3
$cb and $CBF != -1
!ll&&!rl&&r
002
MC/DC0%0/2
$cb and $CBF != -1
$cb $CBF != -1
Truth table33%1/3
A and B
A= $cbB= $CBF != -1
ABresult
0X0
100
111
612 2 lock(@CBQ);
613 2 push @CBQ, $self; # CRITICAL
614 2 $sig = $SIG;
615 }
616 2 cond_broadcast($self);
617 }
618 };
619 5 ▶ Thread::Subs::_send_callback_signal()
Branch: 1 branch
BranchTrueFalseTotal
if $sig and not $CBF 0 5 5
Condition33%1/3
$sig and not $CBF
!ll&&!rl&&r
500
MC/DC0%0/2
$sig and not $CBF
$sig not $CBF
Truth table33%1/3
A and not(B)
A= $sigB= not $CBF
ABresult
0X0
100
111
620 if $sig && !$CBF;
621 5 return $self;
622 }
623  
624 3 ▶sub send { shift()->_set( 1, @_) }
Subroutine send: called CC=1 SCAR=0.0
625 2 ▶sub croak { shift()->_set(-1, @_) }
Subroutine croak: called CC=1 SCAR=0.0
626  
627 5 ▶sub data {
Subroutine data: called CC=3 SCAR=11.0
628 5 my ($self) = @_;
629 5 lock($self);
630 5 ▶ cond_wait($self) until $self->[0];
Branch: 1 branch
BranchTrueFalseTotal
$self->[0] or do { &cond_wait(\$self) } 5 0 5
631 5 my (undef, @data) = @$self;
632 5 ▶ return wantarray ? @data : $data[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 3 5
633 }
634  
635 2 ▶sub recv {
Subroutine recv: called CC=3 SCAR=11.0
636 2 my ($self) = @_;
637 2 my @data = $self->data;
638 2 ▶ _die(@data) if $self->failed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->failed 1 1 2
639 1 ▶ return wantarray ? @data : $data[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
640 }
641  
642 0 ▶sub warn {
Subroutine warn: not called CC=2 SCAR=17.9
643 0 my ($self, $msg) = @_;
644 0 ▶ $msg ||= "Exception in threaded sub";
Condition0%0/2
$msg ||= "Exception in threaded sub"
l!l
00
MC/DC0%0/1
$msg ||= "Exception in threaded sub"
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
645 0 ▶ my $cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
646 0 ▶ warn "$msg: @{[$_[0]->data]}"
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->failed 0 0 0
647 if $_[0]->failed;
648 0 };
649 0 return $self->cb($cb);
650 }
651  
652 0 ▶sub fatal {
Subroutine fatal: not called CC=2 SCAR=17.9
653 0 my ($self, $msg) = @_;
654 0 ▶ $msg ||= "Exception in threaded sub";
Condition0%0/2
$msg ||= "Exception in threaded sub"
l!l
00
MC/DC0%0/1
$msg ||= "Exception in threaded sub"
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
655 0 ▶ my $cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
656 0 ▶ die "$msg: @{[$_[0]->data]}"
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->failed 0 0 0
657 if $_[0]->failed;
658 0 };
659 0 return $self->cb($cb);
660 }
661  
662 0 ▶sub ae_cv {
Subroutine ae_cv: not called CC=1 SCAR=6.9
663 0 my ($self) = @_;
664 0 my $cv = AE::cv();
665 0 ▶ my $cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
666 0 my @data = $_[0]->data;
667 0 ▶ if ($_[0]->failed) { $cv->croak(@data) }
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->failed) { } 0 0 0
668 0 else { $cv->send(@data) }
669 0 return;
670 0 };
671 0 $self->cb($cb);
672 0 return $cv;
673 }
674  
675 0 ▶sub mojo_promise {
Subroutine mojo_promise: not called CC=1 SCAR=6.9
676 0 my ($self) = @_;
677 0 my $p = Mojo::Promise->new;
678 0 ▶ my $cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
679 0 my @data = $_[0]->data;
680 0 ▶ if ($_[0]->failed) { $p->reject(@data) }
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->failed) { } 0 0 0
681 0 else { $p->resolve(@data) }
682 0 return;
683 0 };
684 0 $self->cb($cb);
685 0 return $p;
686 }
687  
688 0 ▶sub future {
Subroutine future: not called CC=1 SCAR=6.9
689 0 my ($self) = @_;
690 0 my $f = Future->new;
691 0 ▶ my $cancel = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
692 0 $self->cb(undef);
693 0 $self->croak("cancelled");
694 0 return;
695 0 };
696 0 $f->on_cancel($cancel);
697 0 ▶ my $cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
698 0 my @data = $_[0]->data;
699 0 ▶ if ($_[0]->failed) { $f->fail(@data) }
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->failed) { } 0 0 0
700 0 else { $f->done(@data) }
701 0 return;
702 0 };
703 0 $self->cb($cb);
704 0 return $f;
705 }
706  
707 2 1;
708 __END__
↑ Summary