lib/Genezzo/Contrib/Clustered/GLock/GLock.pm

filter: Statementstmt 14.6% 19 / 130 Branchbran 4.1% 3 / 72 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 33.3% 4 / 12 Podpod 75.0% 6 / 8 totaltotal 13.5% 32 / 237 CC 37 SCAR 39.1
CC
37
Cov
10%
lock
72.0
promote
72.0
demote
72.0
CRAP 49.8
« lib/Genezzo/Contrib/Clustered.pm ↑ Summary lib/Genezzo/Contrib/Clustered/GLock/GTXLock.pm »
1 #!/usr/bin/perl
2 #
3 # copyright (c) 2005, Eric Rollins, all rights reserved, worldwide
4 #
5 #
6 #
7  
8 package Genezzo::Contrib::Clustered::GLock::GLock;
9  
10 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use Genezzo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 our $LOCKER = 1; # IPC::Locker
17 10 our $DLM = 2; # opendlm
18 10 our $NONE = 3; # no locking (still error check)
19 10 our $UR = 4; # UNIX fcntl record locking
20 10 our $IMPL = $NONE; # this should be $NONE in distribution
21  
22 10 ▶if($IMPL == $LOCKER){
Branch: 3 branches
BranchTrueFalseTotal
if ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::LOCKER) { } 0 10 10
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::DLM) { } 0 10 10
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::UR) { } 0 10 10
23 0 require IPC::Locker;
24 }elsif($IMPL == $DLM){
25 0 require Genezzo::Contrib::Clustered::GLock::GLockDLM;
26 }elsif($IMPL == $UR){
27 0 require Genezzo::Contrib::Clustered::GLock::GLockUR;
28 }
29  
30 # options lock: lockName
31 # block: 1 for blocking (default)
32 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
33 0 ▶ @_ >= 1 or croak 'usage: GLock->new({options})';
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 or croak("usage: GLock->new({options})") 0 0 0
34 0 my $proto = shift;
35 0 my $tmp = { @_,};
36 0 my $lockName = $tmp->{lock};
37 0 ▶ $lockName = "lock" if !defined($lockName);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lockName 0 0 0
38 0 my $block = $tmp->{block};
39 0 ▶ $block = 1 if !defined($block);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block 0 0 0
40 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
41 0 my $self = {};
42 0 $self->{block} = $block;
43 0 $self->{lock} = $lockName;
44  
45 0 ▶ if($IMPL == $LOCKER){
Branch: 2 branches
BranchTrueFalseTotal
if ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::LOCKER) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::DLM) { } 0 0 0
46 0 my $l = IPC::Locker->new(lock => $lockName, block => $block,
47 timeout => 0);
48 0 $self->{impl} = $l;
49 }elsif($IMPL == $DLM){
50 }
51  
52 0 bless $self, $class;
53 0 return $self;
54 }
55  
56 # options: shared: 1 for shared lock
57 # 0 for exclusive (default)
58 # returns undef for failure
59 0 ▶sub lock{
Subroutine lock: not called CC=8 SCAR=42.8
Pod documented
60 0 my $self = shift;
61  
62 0 my $tmp = { @_,};
63 0 my $shared = $tmp->{shared};
64 0 ▶ $shared = 0 if !defined($shared);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shared 0 0 0
65  
66 0 ▶ if(defined($self->{"shared"})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"shared"}) 0 0 0
67 0 croak "GLock::lock(): lock $self->{lock} already locked in mode $self->{shared}";
68 }
69  
70 0 whisper "GLock::lock($self->{lock} shared=$shared block=$self->{block})\n";
71  
72 0 ▶ if($IMPL == $LOCKER){
Branch: 3 branches
BranchTrueFalseTotal
if ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::LOCKER) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::DLM) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::UR) { } 0 0 0
73 0 my $l = $self->{impl};
74 0 $self->{shared} = $shared;
75 0 return $l->lock();
76 }elsif($IMPL == $DLM){
77 my $l = Genezzo::Contrib::Clustered::GLock::GLockDLM::dlm_lock(
78 0 $self->{lock},$shared,$self->{block});
79  
80 0 ▶ if($l == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) 0 0 0
81 0 $l = undef;
82 }
83  
84 0 $self->{impl} = $l;
85 0 $self->{shared} = $shared;
86 0 return $l;
87 }elsif($IMPL == $UR){
88 my $l = Genezzo::Contrib::Clustered::GLock::GLockUR::ur_lock(
89 0 $self->{lock},$shared,$self->{block});
90  
91 0 ▶ if($l == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) 0 0 0
92 0 $l = undef;
93 }
94  
95 0 $self->{impl} = $l;
96 0 $self->{shared} = $shared;
97 0 return $l;
98 }else{
99 0 $self->{shared} = $shared;
100 0 return 1;
101 }
102  
103 0 return undef;
104 }
105  
106 0 ▶sub unlock {
Subroutine unlock: not called CC=5 SCAR=34.0
Pod documented
107 0 my $self = shift;
108  
109 0 ▶ if(!defined($self->{shared})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"shared"}) 0 0 0
110 0 croak "GLock::unlock(): lock $self->{lock} not locked";
111 }
112  
113 0 whisper "GLock::unlock($self->{lock})\n";
114  
115 0 $self->{shared} = undef;
116  
117 0 ▶ if($IMPL == $LOCKER){
Branch: 3 branches
BranchTrueFalseTotal
if ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::LOCKER) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::DLM) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::UR) { } 0 0 0
118 0 my $l = $self->{impl};
119 0 $l->unlock();
120 }elsif($IMPL == $DLM){
121 my $r = Genezzo::Contrib::Clustered::GLock::GLockDLM::dlm_unlock(
122 0 $self->{impl});
123 }elsif($IMPL == $UR){
124 my $r = Genezzo::Contrib::Clustered::GLock::GLockUR::ur_unlock(
125 0 $self->{impl});
126 }else{
127 }
128  
129 0 return 1;
130 }
131  
132 # promote shared to exclusive
133 # returns undef for failure
134 0 ▶sub promote {
Subroutine promote: not called CC=8 SCAR=42.8
Pod documented
135 0 my $self = shift;
136 0 my $tmp = { @_,};
137  
138 0 ▶ if(!defined($self->{shared}) || ($self->{shared} != 1)){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"shared"} or $self->{"shared"} != 1) 0 0 0
Condition0%0/3
not defined $self->{"shared"} or $self->{"shared"} != 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"shared"} or $self->{"shared"} != 1
not defined $self->{"shared"} $self->{"shared"} != 1
Truth table0%0/3
not(A) or B
A= not defined $self->{"shared"}B= $self->{"shared"} != 1
ABresult
1X1
011
000
139 0 croak "GLock::promote(): lock $self->{lock} not locked in shared mode";
140 }
141  
142 0 whisper "GLock::promote($self->{lock} block=$self->{block})\n";
143  
144 0 ▶ if($IMPL == $LOCKER){
Branch: 3 branches
BranchTrueFalseTotal
if ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::LOCKER) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::DLM) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::UR) { } 0 0 0
145 0 my $l = $self->{impl};
146 0 $self->{"shared"} = 0;
147 # nothing to do
148 0 return $l;
149 }elsif ($IMPL == $DLM){
150 my $l = Genezzo::Contrib::Clustered::GLock::GLockDLM::dlm_promote(
151 0 $self->{lock},$self->{impl},$self->{block});
152 0 ▶ if($l == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) 0 0 0
153 0 $l = undef;
154 }
155  
156 0 $self->{"shared"} = 0;
157 0 return $l;
158 }elsif ($IMPL == $UR){
159 my $l = Genezzo::Contrib::Clustered::GLock::GLockUR::ur_promote(
160 0 $self->{lock},$self->{impl},$self->{block});
161 0 ▶ if($l == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) 0 0 0
162 0 $l = undef;
163 }
164  
165 0 $self->{"shared"} = 0;
166 0 return $l;
167 }else{
168 0 $self->{"shared"} = 0;
169 0 return 1;
170 }
171  
172 0 return undef;
173 }
174  
175 # demote exclusive to shared
176 # returns undef for failure
177 0 ▶sub demote {
Subroutine demote: not called CC=8 SCAR=42.8
Pod documented
178 0 my $self = shift;
179 0 my $tmp = { @_,};
180  
181 0 ▶ if(!defined($self->{shared}) || ($self->{shared} != 0)){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"shared"} or $self->{"shared"} != 0) 0 0 0
Condition0%0/3
not defined $self->{"shared"} or $self->{"shared"} != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"shared"} or $self->{"shared"} != 0
not defined $self->{"shared"} $self->{"shared"} != 0
Truth table0%0/3
not(A) or B
A= not defined $self->{"shared"}B= $self->{"shared"} != 0
ABresult
1X1
011
000
182 0 croak "GLock::demote(): lock $self->{lock} not locked in exclusive mode";
183 }
184  
185 0 whisper "GLock::demote($self->{lock} block=$self->{block})\n";
186  
187 0 ▶ if($IMPL == $LOCKER){
Branch: 3 branches
BranchTrueFalseTotal
if ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::LOCKER) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::DLM) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::UR) { } 0 0 0
188 0 my $l = $self->{impl};
189 # nothing to do
190 0 $self->{"shared"} = 1;
191 0 return $l;
192 }elsif ($IMPL == $DLM){
193 my $l = Genezzo::Contrib::Clustered::GLock::GLockDLM::dlm_demote(
194 0 $self->{lock},$self->{impl},$self->{block});
195 0 ▶ if($l == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) 0 0 0
196 0 $l = undef;
197 }
198  
199 0 $self->{"shared"} = 1;
200 0 return $l;
201 }elsif ($IMPL == $UR){
202 my $l = Genezzo::Contrib::Clustered::GLock::GLockUR::ur_demote(
203 0 $self->{lock},$self->{impl},$self->{block});
204 0 ▶ if($l == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) 0 0 0
205 0 $l = undef;
206 }
207  
208 0 $self->{"shared"} = 1;
209 0 return $l;
210 }else{
211 0 $self->{"shared"} = 1;
212 0 return 1;
213 }
214  
215 0 return undef;
216 }
217  
218 0 ▶sub isShared {
Subroutine isShared: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my $self = shift;
220 0 return $self->{shared};
221 }
222  
223 0 ▶sub ast_poll {
Subroutine ast_poll: not called CC=4 SCAR=30.0
Pod documented
224 0 ▶ if($IMPL == $LOCKER){
Branch: 3 branches
BranchTrueFalseTotal
if ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::LOCKER) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::DLM) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::UR) { } 0 0 0
225 0 return 0;
226 }elsif ($IMPL == $DLM){
227 0 return Genezzo::Contrib::Clustered::GLock::GLockDLM::dlm_ast_poll();
228 }elsif ($IMPL == $UR){
229 0 return 1; # assume notification only sent when restart is needed
230 }else{
231 0 return 0;
232 }
233 }
234  
235 0 ▶sub set_notify {
Subroutine set_notify: not called CC=3 SCAR=24.8
Pod undocumented
236 0 ▶ if($IMPL == $LOCKER){
Branch: 2 branches
BranchTrueFalseTotal
if ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::LOCKER) { } 0 0 0
elsif ($Genezzo::Contrib::Clustered::GLock::GLock::IMPL == $Genezzo::Contrib::Clustered::GLock::GLock::DLM) { } 0 0 0
237 0 return 0;
238 }elsif ($IMPL == $DLM){
239 0 return Genezzo::Contrib::Clustered::GLock::GLockDLM::dlm_set_notify();
240 }else{
241 0 return 0;
242 }
243 }
244  
245 10 1;
246  
247 __DATA__
« lib/Genezzo/Contrib/Clustered.pm ↑ Summary lib/Genezzo/Contrib/Clustered/GLock/GTXLock.pm »