lib/Mojo/IOLoop/ReadWriteProcess/Shared/Semaphore.pm

filter: Statementstmt 41.7% 28 / 67 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 31.8% 7 / 22 Podpod 0.0% 0 / 11 totaltotal 24.8% 35 / 141 CC 17 SCAR 39.5
CC
17
Cov
28%
acquire
156.0
_create
12.0
getval
6.0
CRAP 51.8
↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Shared/Memory.pm »
1 package Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore;
2 39 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 39 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use POSIX qw(O_WRONLY O_CREAT O_NONBLOCK O_NOCTTY);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use IPC::SysV
7 39 ▶ qw(ftok IPC_NOWAIT IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR S_IRGRP S_IWGRP S_IROTH S_IWOTH SEM_UNDO);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use IPC::Semaphore;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 our @EXPORT_OK = qw(semaphore);
10 39 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 39 ▶use constant DEBUG => $ENV{MOJO_PROCESS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 0 ▶has key => sub { shift->_genkey };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 0 ▶has _sem => sub { $_[0]->_create(shift->key) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 39 has count => 1;
16 39 has _value => 1;
17 39 has flags => IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
18 | S_IROTH | S_IWOTH;
19  
20 0 ▶sub semaphore { __PACKAGE__->new(@_) }
Subroutine semaphore: not called CC=1 SCAR=6.9
Pod undocumented
21  
22 0 ▶sub _genkey { ftok($0, 0) }
Subroutine _genkey: not called CC=1 SCAR=6.9
23  
24 # The following is an adaptation over IPC::Semaphore::Concurrency
25 0 ▶sub _create {
Subroutine _create: not called CC=3 SCAR=24.8
26 0 my ($self, $key) = @_;
27  
28 # Try acquiring already existing semaphore
29 0 my $sem = IPC::Semaphore->new($key, $self->count, 0);
30 0 ▶ unless (defined $sem) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sem) 0 0 0
31 0 warn "[debug:$$] Create semaphore $key" if DEBUG;
32 0 $sem = IPC::Semaphore->new($key, $self->count, $self->flags);
33 0 ▶ confess 'Semaphore creation failed! ' unless defined($sem);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sem 0 0 0
34 0 $sem->setall($self->_value);
35 }
36 0 return $sem;
37 }
38  
39 0 ▶sub acquire {
Subroutine acquire: not called CC=12 SCAR=50.5
Pod undocumented
40 0 my $self = shift;
41 0 ▶ my %args = @_ % 2 == 0 ? @_ : @_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : ();
Branch: 2 branches
BranchTrueFalseTotal
@_ % 2 == 0 ? : 0 0 0
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
42  
43 # Defaults
44 0 ▶ $args{'sem'} = 0 unless defined($args{'sem'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"sem"} 0 0 0
45 0 ▶ $args{'wait'} = 0 unless defined($args{'wait'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"wait"} 0 0 0
46 0 ▶ $args{'max'} = -1 unless defined($args{'max'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"max"} 0 0 0
47 0 ▶ $args{'undo'} = 0 unless defined($args{'undo'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"undo"} 0 0 0
48 0 warn "[debug:$$] Acquire semaphore " . $self->key if DEBUG;
49  
50 0 my $sem = $self->_sem;
51 0 my $flags = IPC_NOWAIT;
52 0 ▶ $flags |= SEM_UNDO if ($args{'undo'});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"undo"} 0 0 0
53  
54 0 ▶ if ($args{'wait'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"wait"}) 0 0 0
55 0 my $ncnt = $self->getncnt($args{'sem'});
56 0 ▶ return if ($args{'max'} >= 0 && $ncnt >= $args{'max'});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"max"} >= 0 and $ncnt >= $args{"max"} 0 0 0
Condition0%0/3
$args{"max"} >= 0 and $ncnt >= $args{"max"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"max"} >= 0 and $ncnt >= $args{"max"}
$args{"max"} >= 0 $ncnt >= $args{"max"}
Truth table0%0/3
A and B
A= $args{"max"} >= 0B= $ncnt >= $args{"max"}
ABresult
0X0
100
111
57 0 warn "[debug:$$] Semaphore wait" if DEBUG;
58 0 warn "[debug:$$] Semaphore val " . $self->getval($args{sem}) if DEBUG;
59  
60 # Remove NOWAIT and block
61 0 $flags ^= IPC_NOWAIT;
62 }
63  
64 0 return $sem->op($args{'sem'}, -1, $flags);
65 }
66  
67  
68 0 ▶sub getall { shift->_sem->getall() }
Subroutine getall: not called CC=1 SCAR=6.9
Pod undocumented
69  
70 0 ▶sub getval { shift->_sem->getval(shift // 0) }
Subroutine getval: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
71  
72 0 ▶sub getncnt { shift->_sem->getncnt(shift // 0) }
Subroutine getncnt: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
73  
74 0 ▶sub setall { shift->_sem->setall(@_) }
Subroutine setall: not called CC=1 SCAR=6.9
Pod undocumented
75  
76 0 ▶sub setval { shift->_sem->setval(@_) }
Subroutine setval: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 0 ▶sub stat { shift->_sem->stat() }
Subroutine stat: not called CC=1 SCAR=6.9
Pod undocumented
79  
80 0 ▶sub id { shift->_sem->id() }
Subroutine id: not called CC=1 SCAR=6.9
Pod undocumented
81  
82 0 ▶sub release { shift->_sem->op(shift || 0, 1, 0) }
Subroutine release: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
83  
84 0 ▶sub remove { shift->_sem->remove() }
Subroutine remove: not called CC=1 SCAR=6.9
Pod undocumented
85  
86 39 !!42;
↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Shared/Memory.pm »