| 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
MC/DC0%0/1
$size || 1
$size
Truth table0%0/2
A
A= $size
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if (defined (my $w = shift @$watchers)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$holes > 100 and $holes * 2 > @$watchers
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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||