blib/lib/AnyEvent/Semaphore.pm

filter: Statementstmt 27.8% 17 / 61 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 41.6% 5 / 12 Podpod 100% 4 / 4 totaltotal 26.8% 26 / 97 CC 10 SCAR 22.8
CC
10
Cov
22%
_schedule
20.0
DESTROY
20.0
new
6.0
CRAP 9.8
↑ Summary
1 package AnyEvent::Semaphore;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use AE;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Method::WeakCallback qw(weak_method_callback);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # internal representation of watcher is an array [$semaphore, $cb]
13  
14 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
15 0 my ($class, $size) = @_;
16 0 ▶ my $sem = { size => $size || 1,
Condition0%0/2
$size || 1
l!l
00
MC/DC0%0/1
$size || 1
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
17 holes => 0,
18 running => 0,
19 watchers => [] };
20 0 $sem->{schedule_cb} = weak_method_callback($sem, '_schedule'),
21 bless $sem, $class;
22 }
23  
24 0 ▶sub size {
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
25 0 my $sem = shift;
26 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
27 0 $sem->{size} = shift;
28 0 &AE::postpone($sem->{schedule_cb});
29 }
30 0 $sem->{size};
31 }
32  
33 0 ▶sub running { shift->{running} }
Subroutine running: not called CC=1 SCAR=6.9
Pod documented
34  
35 0 ▶sub down {
Subroutine down: not called CC=2 SCAR=17.9
Pod documented
36 0 ▶ return unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 0 0
37 0 my ($sem) = @_;
38 0 my $watchers = $sem->{watchers};
39 0 my $w = [@_];
40 0 bless $w, 'AnyEvent::Semaphore::Watcher';
41 0 push @{$watchers}, $w;
42 0 Scalar::Util::weaken($watchers->[-1]);
43 0 &AE::postpone($sem->{schedule_cb});
44 0 $w;
45 }
46  
47 0 ▶sub _schedule {
Subroutine _schedule: not called CC=4 SCAR=30.0
48 0 my $sem = shift;
49 0 my $watchers = $sem->{watchers};
50 0 ▶ while ($sem->{size} > $sem->{running}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sem->{"size"} > $sem->{"running"}) 0 0 0
51 0 ▶ if (defined (my $w = shift @$watchers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $w = shift @$watchers)) { } 0 0 0
52 0 $sem->{running}++;
53 0 my ($cb, @args) = splice @$w, 1;
54 0 bless $w, 'AnyEvent::Semaphore::Down';
55 0 $cb->(@args);
56 }
57 else {
58 0 ▶ @$watchers or return;
Branch: 1 branch
BranchTrueFalseTotal
@$watchers or return 0 0 0
59 }
60 }
61 }
62  
63 0 ▶sub AnyEvent::Semaphore::Watcher::DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=30.0
64 0 local ($!, $@, $SIG{__DIE__});
65 0 eval {
66 0 my $watcher = shift;
67 0 my $sem = $watcher->[0];
68 0 my $holes = ++$sem->{holes};
69 0 my $watchers = $sem->{watchers};
70 0 ▶ if ($holes > 100 and $holes * 2 > @$watchers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($holes > 100 and $holes * 2 > @$watchers) 0 0 0
Condition0%0/3
$holes > 100 and $holes * 2 > @$watchers
!ll&&!rl&&r
000
MC/DC0%0/2
$holes > 100 and $holes * 2 > @$watchers
$holes > 100 $holes * 2 > @$watchers
Truth table0%0/3
A and B
A= $holes > 100B= $holes * 2 > @$watchers
ABresult
0X0
100
111
71 0 @{$sem->{watchers}} = grep defined, @$watchers;
72 0 Scalar::Util::weaken $_ for @$watchers;
73 0 $sem->{holes} = 0;
74 }
75 }
76 }
77  
78 0 ▶sub AnyEvent::Semaphore::Down::DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
79 0 local ($!, $@, $SIG{__DIE__});
80 0 eval {
81 0 my $sem = shift->[0];
82 0 $sem->{running}--;
83 &AE::postpone($sem->{schedule_cb})
84 0 }
85 }
86  
87 1 1;
88  
89 __END__
↑ Summary