| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 38 | 0 | my $block = $tmp->{block}; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | $block = 1 if !defined($block); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 41 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 42 | 0 | $self->{block} = $block; | |||||||||||||||||||||||||||
| 43 | 0 | $self->{lock} = $lockName; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if($IMPL == $LOCKER){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if(defined($self->{"shared"})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not defined $self->{"shared"} or $self->{"shared"} != 1
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not defined $self->{"shared"} or $self->{"shared"} != 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||