Lock.pm

filter: Statementstmt 22.8% 13 / 57 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 27.2% 3 / 11 Podpod 0.0% 0 / 6 totaltotal 13.2% 16 / 121 CC 18 SCAR 32.0
CC
18
Cov
14%
new
42.0
lock
42.0
unlock
20.0
CRAP 24.5
↑ Summary Lock/Memcached.pm »
1 package IPC::Lock;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.20';
9 2 our @CATCH_SIGS = qw(TERM INT);
10  
11 ### from File::NFSLock
12 0 ▶my $graceful_sig = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
13 0 ▶ print STDERR "Received SIG$_[0]\n" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
14 # Perl's exit should safely DESTROY any objects
15 # still "alive" before calling the real _exit().
16 0 exit;
17 2 };
18  
19 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
20 0 my $type = shift;
21 0 ▶ my @PASSED_ARGS = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
22 0 my @DEFAULT_ARGS = (
23 locked => {},
24 ttl => 60,
25 patience => 2,
26 increment => '0.05',
27 );
28  
29 0 my %ARGS = (@DEFAULT_ARGS, @PASSED_ARGS);
30 0 ▶ unless($ARGS{hostname}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ARGS{"hostname"}) 0 0 0
31 0 require Sys::Hostname;
32 0 $ARGS{hostname} = &Sys::Hostname::hostname();
33 }
34  
35 0 foreach my $signal (@CATCH_SIGS) {
36 0 ▶ if (!$SIG{$signal} || $SIG{$signal} eq "DEFAULT") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $SIG{$signal} or $SIG{$signal} eq "DEFAULT") 0 0 0
Condition0%0/3
not $SIG{$signal} or $SIG{$signal} eq "DEFAULT"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $SIG{$signal} or $SIG{$signal} eq "DEFAULT"
not $SIG{$signal} $SIG{$signal} eq "DEFAULT"
Truth table0%0/3
not(A) or B
A= not $SIG{$signal}B= $SIG{$signal} eq "DEFAULT"
ABresult
1X1
011
000
37 0 $SIG{$signal} = $graceful_sig;
38 }
39 }
40 0 return bless \%ARGS, $type;
41 }
42  
43 0 ▶sub lock {
Subroutine lock: not called CC=6 SCAR=37.4
Pod undocumented
44 0 my $self = shift;
45 0 ▶ my $key = shift || die "need a key";
Condition0%0/2
shift() || die('need a key')
l!l
00
MC/DC0%0/1
shift() || die('need a key')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
46 0 $self->{key} = $key;
47  
48 0 my $ttl = shift;
49 0 ▶ $ttl = $self->{ttl} unless( (defined $ttl) && length $ttl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ttl and length $ttl 0 0 0
Condition0%0/3
defined $ttl and length $ttl
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ttl and length $ttl
defined $ttl length $ttl
Truth table0%0/3
A and B
A= defined $ttlB= length $ttl
ABresult
0X0
100
111
50  
51 0 my $patience = $self->{patience};
52 0 my $increment = $self->{increment};
53  
54 0 my $start = gettimeofday;
55  
56 0 my $got_lock = 0;
57  
58 0 while(1) {
59 0 ▶ if($self->atomic($key, $ttl)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->atomic($key, $ttl)) 0 0 0
60 0 $self->{locked}{$key} = 1;
61 0 $got_lock = 1;
62 0 last;
63 }
64 0 ▶ last if(gettimeofday - $start > $patience);
Branch: 1 branch
BranchTrueFalseTotal
if &gettimeofday() - $start > $patience 0 0 0
65 0 select(undef, undef, undef, $increment);
66 }
67  
68 0 return $got_lock;
69 }
70  
71 0 ▶sub unlock {
Subroutine unlock: not called CC=4 SCAR=30.0
Pod undocumented
72 0 my $self = shift;
73 0 ▶ my $key = shift || $self->{key} || die "need a key";
Condition0%0/5
shift() || $self->{'key'} || die('need a key')
l!l
00
shift() or $self->{"key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'key'} || die('need a key')
shift() $self->{"key"}
Truth table0%0/3
A or B
A= shift()B= $self->{"key"}
ABresult
1X1
011
000
74 0 my $unlock = $self->unatomic($key);
75 0 ▶ if($unlock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unlock) 0 0 0
76 0 delete $self->{locked}{$key};
77 }
78 0 return $unlock;
79 }
80  
81 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=30.0
82 0 my $self = shift;
83  
84 0 ▶ if($self->{locked} && $self->{key} && $self->{locked}{$self->{key}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"locked"} and $self->{"key"} and $self->{"locked"}{$self->{"key"}}) 0 0 0
Condition0%0/6
$self->{"locked"} and $self->{"key"} and $self->{"locked"}{$self->{"key"}}
!ll&&!rl&&r
000
$self->{"locked"} and $self->{"key"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"locked"} and $self->{"key"} and $self->{"locked"}{$self->{"key"}}
$self->{"locked"} $self->{"key"} $self->{"locked"}{$self->{"key"}}
Truth table0%0/4
A and B and C
A= $self->{"locked"}B= $self->{"key"}C= $self->{"locked"}{$self->{"key"}}
ABCresult
0XX0
10X0
1100
1111
85 0 $self->unlock($self->{key});
86 }
87 }
88  
89 0 ▶sub atomic_value {
Subroutine atomic_value: not called CC=1 SCAR=6.9
Pod undocumented
90 0 my $self = shift;
91 0 return "$self->{hostname}:$$:" . scalar gettimeofday;
92 }
93  
94 0 ▶sub atomic {
Subroutine atomic: not called CC=1 SCAR=6.9
Pod undocumented
95 0 die "please write your own atomic method";
96 }
97  
98 0 ▶sub unatomic {
Subroutine unatomic: not called CC=1 SCAR=6.9
Pod undocumented
99 0 die "please write your own unatomic method";
100 }
101  
102 2 1;
103  
104 __END__
↑ Summary Lock/Memcached.pm »