| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (C) 2002-2016 National Marrow Donor Program. All rights reserved. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # For a description of this module, please refer to the POD documentation | ||||||||||||||||||||||||||||
| 6 | # embedded at the bottom of the file (e.g. perldoc EMDIS::ECS::LockedHash). | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package EMDIS::ECS::LockedHash; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use EMDIS::ECS qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Fcntl qw(:DEFAULT :flock); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | #use File::lockf; # potential alternate locking method | ||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use SDBM_File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | # Constructor. Requires name of the database file and name of lock file | ||||||||||||||||||||||||||||
| 20 | # as parameters. Also accepts optional lock timeout parameter. | ||||||||||||||||||||||||||||
| 21 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | 7 | my $dbfile = shift; | |||||||||||||||||||||||||||
| 24 | 7 | my $lockfile = shift; | |||||||||||||||||||||||||||
| 25 | 7 | my $lock_timeout = shift; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # validate aaarghs | ||||||||||||||||||||||||||||
| 28 | 7 | ▶ | if(!defined $dbfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 2 | warn "EMDIS::ECS::LockedHash::new() failed: missing database file name."; | |||||||||||||||||||||||||||
| 30 | 2 | return undef; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 5 | ▶ | if(!defined $lockfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 2 | warn "EMDIS::ECS::LockedHash::new() failed: missing lock file name."; | |||||||||||||||||||||||||||
| 34 | 2 | return undef; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 3 | ▶ | $lock_timeout = 10 unless defined $lock_timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # define this object | ||||||||||||||||||||||||||||
| 39 | 3 | my $this = {}; | |||||||||||||||||||||||||||
| 40 | 3 | bless $this, $class; | |||||||||||||||||||||||||||
| 41 | 3 | $this->{dbfile} = $dbfile; | |||||||||||||||||||||||||||
| 42 | 3 | $this->{lockfile} = $lockfile; | |||||||||||||||||||||||||||
| 43 | 3 | $this->{lock_timeout} = $lock_timeout; | |||||||||||||||||||||||||||
| 44 | 3 | $this->{ERROR} = ''; | |||||||||||||||||||||||||||
| 45 | 3 | $this->{LOCK} = 0; | |||||||||||||||||||||||||||
| 46 | 3 | $this->{TIED} = ''; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # open lock file and retain file handle | ||||||||||||||||||||||||||||
| 49 | 3 | ▶ | if(!sysopen($this->{FH_LOCK}, $this->{lockfile}, O_RDWR|O_CREAT)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | warn "EMDIS::ECS::LockedHash::new() failed: unable to access lock file " . | |||||||||||||||||||||||||||
| 51 | "'$this->{lockfile}': $!"; | ||||||||||||||||||||||||||||
| 52 | 0 | return undef; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # tie/untie db file, to test whether it's accessible | ||||||||||||||||||||||||||||
| 56 | 3 | ▶ | if(!$this->_tie()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | warn "EMDIS::ECS::LockedHash::new() failed: " . $this->ERROR; | |||||||||||||||||||||||||||
| 58 | 0 | return undef; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 3 | $this->_untie; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | return $this; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 66 | # set/get error description | ||||||||||||||||||||||||||||
| 67 | 40 | ▶ | sub ERROR { | ||||||||||||||||||||||||||
|
Subroutine
ERROR: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 40 | my $this = shift; | |||||||||||||||||||||||||||
| 69 | 40 | my $err = shift; | |||||||||||||||||||||||||||
| 70 | 40 | ▶ | if(defined $err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 33 | $this->{ERROR} = $err; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 40 | return $this->{ERROR}; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 77 | # set/get locked status indicator | ||||||||||||||||||||||||||||
| 78 | 59 | ▶ | sub LOCK { | ||||||||||||||||||||||||||
|
Subroutine
LOCK: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 59 | my $this = shift; | |||||||||||||||||||||||||||
| 80 | 59 | my $status = shift; | |||||||||||||||||||||||||||
| 81 | 59 | ▶ | if(defined $status) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 18 | $this->{LOCK} = $status; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 59 | return $this->{LOCK}; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 88 | # set/get tied status indicator | ||||||||||||||||||||||||||||
| 89 | 43 | ▶ | sub TIED { | ||||||||||||||||||||||||||
|
Subroutine
TIED: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 43 | my $this = shift; | |||||||||||||||||||||||||||
| 91 | 43 | my $status = shift; | |||||||||||||||||||||||||||
| 92 | 43 | ▶ | if(defined $status) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 21 | $this->{TIED} = $status; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 43 | return $this->{TIED}; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 99 | # Read one key-value from the database under a shared lock | ||||||||||||||||||||||||||||
| 100 | 5 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 5 | my $this = shift; | |||||||||||||||||||||||||||
| 102 | 5 | my $key = shift; | |||||||||||||||||||||||||||
| 103 | 5 | my $value = undef; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 5 | $this->ERROR(''); # reset error status | |||||||||||||||||||||||||||
| 106 | # check lock status | ||||||||||||||||||||||||||||
| 107 | 5 | ▶ | if($this->LOCK != LOCK_SH and $this->LOCK != LOCK_EX) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$this->LOCK != 1 and $this->LOCK != 2
MC/DC50%1/2
$this->LOCK != 1 and $this->LOCK != 2
$this->LOCK != 1
$this->LOCK != 2
Truth table66%2/3
A and B
A= $this->LOCK != 1B= $this->LOCK != 2
| |||||||||||||||||||||||||||||
| 108 | 1 | $this->ERROR( | |||||||||||||||||||||||||||
| 109 | "EMDIS::ECS::LockedHash::read() requires shared or exclusive lock."); | ||||||||||||||||||||||||||||
| 110 | 1 | return undef; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | # read value from hash | ||||||||||||||||||||||||||||
| 113 | 4 | $value = undef; | |||||||||||||||||||||||||||
| 114 | 4 | ▶ | $value = $this->{hash}->{$key} if exists $this->{hash}->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | if(defined($value) and ($value =~ /^\$\w+\s*=\s*\{.*\}\s*\;\s*$/s)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value =~ /^\$\w+\s*=\s*\{.*\}\s*\;\s*$/s
MC/DC100%2/2
defined $value and $value =~ /^\$\w+\s*=\s*\{.*\}\s*\;\s*$/s
defined $value
$value =~ /^\$\w+\s*=\s*\{.*\}\s*\;\s*$/s
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /^\$\w+\s*=\s*\{.*\}\s*\;\s*$/s
| |||||||||||||||||||||||||||||
| 116 | # convert Dumper() string to hash ref | ||||||||||||||||||||||||||||
| 117 | 1 | $value =~ s/^\$\w+/\$value/; # convert "$VAR1 = ..." to "$value = ..." | |||||||||||||||||||||||||||
| 118 | 1 | eval($value); # eval "$value = ..." string | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 4 | return $value; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 124 | # Write one key-value to the database under an exclusive lock | ||||||||||||||||||||||||||||
| 125 | 3 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 3 | my $this = shift; | |||||||||||||||||||||||||||
| 127 | 3 | my $key = shift; | |||||||||||||||||||||||||||
| 128 | 3 | my $value = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | $this->ERROR(''); # reset error status | |||||||||||||||||||||||||||
| 131 | # check lock status | ||||||||||||||||||||||||||||
| 132 | 3 | ▶ | if($this->LOCK != LOCK_EX) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 1 | $this->ERROR("EMDIS::ECS::LockedHash::write() requires exclusive lock."); | |||||||||||||||||||||||||||
| 134 | 1 | return ''; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | # write value to hash | ||||||||||||||||||||||||||||
| 137 | 2 | ▶ | if(ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 1 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||
| 139 | 1 | $value = Dumper($value); # convert ref to Dumper() string | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 2 | $this->{hash}->{$key} = $value; | |||||||||||||||||||||||||||
| 142 | 2 | return 1; # successful | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 146 | # Delete a key-value under an exclusive lock | ||||||||||||||||||||||||||||
| 147 | 2 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 2 | my $this = shift; | |||||||||||||||||||||||||||
| 149 | 2 | my $key = shift; | |||||||||||||||||||||||||||
| 150 | 2 | my $value = undef; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 2 | $this->ERROR(''); # reset error status | |||||||||||||||||||||||||||
| 153 | # check lock status | ||||||||||||||||||||||||||||
| 154 | 2 | ▶ | if($this->LOCK != LOCK_EX) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1 | $this->ERROR("EMDIS::ECS::LockedHash::delete() requires exclusive lock."); | |||||||||||||||||||||||||||
| 156 | 1 | return ''; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | # delete value from hash | ||||||||||||||||||||||||||||
| 159 | 1 | $value = delete $this->{hash}->{$key}; | |||||||||||||||||||||||||||
| 160 | 1 | return 1; # successful | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 164 | # Return a list of key values under a shared lock | ||||||||||||||||||||||||||||
| 165 | 3 | ▶ | sub keys { | ||||||||||||||||||||||||||
|
Subroutine
keys: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | 3 | my $this = shift; | |||||||||||||||||||||||||||
| 167 | 3 | my @ks = (); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 3 | $this->ERROR(''); # reset error status | |||||||||||||||||||||||||||
| 170 | # check lock status | ||||||||||||||||||||||||||||
| 171 | 3 | ▶ | if($this->LOCK != LOCK_SH and $this->LOCK != LOCK_EX) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$this->LOCK != 1 and $this->LOCK != 2
MC/DC50%1/2
$this->LOCK != 1 and $this->LOCK != 2
$this->LOCK != 1
$this->LOCK != 2
Truth table66%2/3
A and B
A= $this->LOCK != 1B= $this->LOCK != 2
| |||||||||||||||||||||||||||||
| 172 | 1 | $this->ERROR( | |||||||||||||||||||||||||||
| 173 | "EMDIS::ECS::LockedHash::keys() requires shared or exclusive lock."); | ||||||||||||||||||||||||||||
| 174 | 1 | return ''; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | # get keys from hash | ||||||||||||||||||||||||||||
| 177 | 2 | @ks = keys %{$this->{hash}}; | |||||||||||||||||||||||||||
| 178 | 2 | return @ks; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 182 | # Obtain (advisory) lock and tie internal hash to db file. | ||||||||||||||||||||||||||||
| 183 | 6 | ▶ | sub lock { | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=9 SCAR=24.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 184 | 6 | my $this = shift; | |||||||||||||||||||||||||||
| 185 | 6 | my $lock_type = shift; | |||||||||||||||||||||||||||
| 186 | 6 | my $oldlock = $this->LOCK; | |||||||||||||||||||||||||||
| 187 | 6 | ▶ | $lock_type = LOCK_EX unless $lock_type; # default = LOCK_EX | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 6 | $this->ERROR(''); # reset error status | |||||||||||||||||||||||||||
| 189 | 6 | ▶ | return 1 if $oldlock == $lock_type; # already locked | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 6 | my $locked = 0; | |||||||||||||||||||||||||||
| 191 | 6 | my $attempt = 0; | |||||||||||||||||||||||||||
| 192 | 6 | ▶ | while(!$locked and $attempt++ < 5) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $locked and $attempt++ < 5
MC/DC50%1/2
not $locked and $attempt++ < 5
not $locked
$attempt++ < 5
Truth table66%2/3
not(A) and B
A= not $lockedB= $attempt++ < 5
| |||||||||||||||||||||||||||||
| 193 | 6 | ▶ | sleep 2 if $attempt > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 6 | $this->ERROR(''); # reset error status | |||||||||||||||||||||||||||
| 195 | 6 | $locked = $this->_lock($lock_type); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 6 | ▶ | if(!$locked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | $this->ERROR("EMDIS::ECS::LockedHash::lock() failed: " . $this->ERROR); | |||||||||||||||||||||||||||
| 199 | 0 | return ''; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 6 | ▶ | if(!$this->TIED and !$this->_tie()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$this->TIED or $this->_tie
MC/DC0%0/2
$this->TIED or $this->_tie
$this->TIED
$this->_tie
Truth table33%1/3
A or B
A= $this->TIEDB= $this->_tie
| |||||||||||||||||||||||||||||
| 202 | 0 | $this->ERROR("EMDIS::ECS::LockedHash::lock() failed: " . $this->ERROR); | |||||||||||||||||||||||||||
| 203 | 0 | return ''; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 6 | return 1; # successful | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 209 | # Release (advisory) lock and untie internal hash. | ||||||||||||||||||||||||||||
| 210 | 6 | ▶ | sub unlock { | ||||||||||||||||||||||||||
|
Subroutine
unlock: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | 6 | my $this = shift; | |||||||||||||||||||||||||||
| 212 | 6 | $this->_untie(); | |||||||||||||||||||||||||||
| 213 | 6 | $this->_unlock(); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 217 | # Quickly delete all key-values under an exclusive lock | ||||||||||||||||||||||||||||
| 218 | 2 | ▶ | sub undef { | ||||||||||||||||||||||||||
|
Subroutine
undef: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 219 | 2 | my $this = shift; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 2 | $this->ERROR(''); # reset error status | |||||||||||||||||||||||||||
| 222 | # check lock status | ||||||||||||||||||||||||||||
| 223 | 2 | ▶ | if($this->LOCK != LOCK_EX) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 1 | $this->ERROR("EMDIS::ECS::LockedHash::undef() requires exclusive lock."); | |||||||||||||||||||||||||||
| 225 | 1 | return ''; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | # delete everything from hash | ||||||||||||||||||||||||||||
| 228 | 1 | undef %{$this->{hash}}; | |||||||||||||||||||||||||||
| 229 | 1 | return 1; # successful | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 234 | # untie hash and close lock file when perl object passes out of scope | ||||||||||||||||||||||||||||
| 235 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 236 | 1 | my $this = shift; | |||||||||||||||||||||||||||
| 237 | 1 | $this->_untie(); | |||||||||||||||||||||||||||
| 238 | close($this->{FH_LOCK}) | ||||||||||||||||||||||||||||
| 239 | 1 | ▶ | if defined $this->{FH_LOCK}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 243 | # Select UNIX or Win32 version of _lock | ||||||||||||||||||||||||||||
| 244 | sub _lock | ||||||||||||||||||||||||||||
| 245 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_lock: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 246 | 10 | ▶ | $^O =~ /MSWin32/ ? _lock_win32(@_) : _lock_unix(@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 250 | # Internal subroutine: obtain (advisory) lock, using time limit to | ||||||||||||||||||||||||||||
| 251 | # avoid indefinite blocking. Returns true if able to obtain lock within | ||||||||||||||||||||||||||||
| 252 | # time limit; otherwise returns false. | ||||||||||||||||||||||||||||
| 253 | 10 | ▶ | sub _lock_unix { | ||||||||||||||||||||||||||
|
Subroutine
_lock_unix: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 254 | 10 | my $this = shift; | |||||||||||||||||||||||||||
| 255 | 10 | my $lock_type = shift; | |||||||||||||||||||||||||||
| 256 | 10 | ▶ | $lock_type = LOCK_EX unless defined $lock_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 10 | my $result = 1; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # set up "local" SIG_ALRM handler | ||||||||||||||||||||||||||||
| 260 | # (Note: not using "local $SIG{PIPE}" because it ignores die()) | ||||||||||||||||||||||||||||
| 261 | 10 | my $oldsigalrm = $SIG{ALRM}; | |||||||||||||||||||||||||||
| 262 | 1 | ▶ | $SIG{ALRM} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 263 | 1 | die "timeout - $this->{lock_timeout} second time limit exceeded\n"; | |||||||||||||||||||||||||||
| 264 | 10 | }; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # attempt to obtain lock, with time limit | ||||||||||||||||||||||||||||
| 267 | 10 | eval { | |||||||||||||||||||||||||||
| 268 | 10 | alarm($this->{lock_timeout}); # set alarm | |||||||||||||||||||||||||||
| 269 | die "flock() failed: $!\n" | ||||||||||||||||||||||||||||
| 270 | 10 | ▶ | unless flock($this->{FH_LOCK}, $lock_type); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | # File::lockf -- potential alternate locking method: | ||||||||||||||||||||||||||||
| 272 | # my $status = File::lockf::lock($this->{FH_LOCK}, 0); | ||||||||||||||||||||||||||||
| 273 | # die "lockf failed: $status\n" | ||||||||||||||||||||||||||||
| 274 | # if $status != 0; | ||||||||||||||||||||||||||||
| 275 | 9 | alarm(0); # turn off alarm | |||||||||||||||||||||||||||
| 276 | }; | ||||||||||||||||||||||||||||
| 277 | 10 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 1 | alarm(0); # turn off alarm | |||||||||||||||||||||||||||
| 279 | 1 | $this->ERROR("EMDIS::ECS::LockedHash::_lock_unix() failed: $@"); | |||||||||||||||||||||||||||
| 280 | 1 | $this->LOCK(0); # reset status indicator | |||||||||||||||||||||||||||
| 281 | 1 | $result = ''; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | # restore previous SIG_ALRM handler | ||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if(defined $oldsigalrm) { $SIG{ALRM} = $oldsigalrm; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 10 | else { delete $SIG{ALRM}; } | |||||||||||||||||||||||||||
| 286 | 10 | ▶ | $this->LOCK($lock_type) # set status indicator | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | if $result; | ||||||||||||||||||||||||||||
| 288 | 10 | return $result; # successful | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 292 | # Internal subroutine: obtain (advisory) lock, using time limit to | ||||||||||||||||||||||||||||
| 293 | # avoid indefinite blocking. Returns true if able to obtain lock within | ||||||||||||||||||||||||||||
| 294 | # time limit; otherwise returns false. | ||||||||||||||||||||||||||||
| 295 | 0 | ▶ | sub _lock_win32 { | ||||||||||||||||||||||||||
|
Subroutine
_lock_win32: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 296 | 0 | my $this = shift; | |||||||||||||||||||||||||||
| 297 | 0 | my $lock_type = shift; | |||||||||||||||||||||||||||
| 298 | 0 | ▶ | $lock_type = LOCK_EX unless defined $lock_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 0 | my $result = 1; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | # attempt to obtain lock, with time limit | ||||||||||||||||||||||||||||
| 302 | # (uses polling method to obtain lock -- somewhat more crude than | ||||||||||||||||||||||||||||
| 303 | # the unix method, which uses blocking with SIGALRM to enforce timeout) | ||||||||||||||||||||||||||||
| 304 | 0 | my $timeoutCount = 0; | |||||||||||||||||||||||||||
| 305 | 0 | my $locked; | |||||||||||||||||||||||||||
| 306 | 0 | ▶ | while (!($locked = flock($this->{FH_LOCK}, $lock_type | LOCK_NB)) and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $locked = flock($this->{"FH_LOCK"}, $lock_type | 4) and $timeoutCount++ <= $this->{"lock_timeout"}
MC/DC0%0/2
not $locked = flock($this->{"FH_LOCK"}, $lock_type | 4) and $timeoutCount++ <= $this->{"lock_timeout"}
not $locked = flock($this->{"FH_LOCK"}, $lock_type | 4)
$timeoutCount++ <= $this->{"lock_timeout"}
Truth table0%0/3
not(A) and B
A= not $locked = flock($this->{"FH_LOCK"}, $lock_type | 4)B= $timeoutCount++ <= $this->{"lock_timeout"}
| |||||||||||||||||||||||||||||
| 307 | ($timeoutCount++ <= $this->{lock_timeout})) { | ||||||||||||||||||||||||||||
| 308 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | if(!$locked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 0 | $this->ERROR("EMDIS::ECS::LockedHash::_lock_win32() failed: $@"); | |||||||||||||||||||||||||||
| 313 | 0 | $this->LOCK(0); # reset status indicator | |||||||||||||||||||||||||||
| 314 | 0 | $result = ''; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | $this->LOCK($lock_type) # set status indicator | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | if $result; | ||||||||||||||||||||||||||||
| 318 | 0 | return $result; # successful | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 322 | # Internal subroutine: tie hash to db file | ||||||||||||||||||||||||||||
| 323 | 10 | ▶ | sub _tie { | ||||||||||||||||||||||||||
|
Subroutine
_tie: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||
| 324 | 10 | my $this = shift; | |||||||||||||||||||||||||||
| 325 | 10 | ▶ | $this->{db_obj} = tie(%{$this->{hash}}, 'SDBM_File', $this->{dbfile}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 10 | ▶ | O_CREAT|O_RDWR, (defined $EMDIS::ECS::FILEMODE ? $EMDIS::ECS::FILEMODE : 0664)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | or $this->ERROR( | ||||||||||||||||||||||||||||
| 328 | "EMDIS::ECS::LockedHash::_tie() failed ($this->{dbfile}): $!"); | ||||||||||||||||||||||||||||
| 329 | 10 | ▶ | if($this->{db_obj}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 10 | $this->TIED(1); # set status indicator | |||||||||||||||||||||||||||
| 331 | } else { | ||||||||||||||||||||||||||||
| 332 | 0 | $this->TIED(''); # reset status indicator | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | 10 | return $this->TIED; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 338 | # Internal subroutine: release (advisory) lock | ||||||||||||||||||||||||||||
| 339 | 8 | ▶ | sub _unlock { | ||||||||||||||||||||||||||
|
Subroutine
_unlock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 340 | 8 | my $this = shift; | |||||||||||||||||||||||||||
| 341 | 8 | flock($this->{FH_LOCK}, LOCK_UN); | |||||||||||||||||||||||||||
| 342 | # File::lockf -- potential alternate locking method: | ||||||||||||||||||||||||||||
| 343 | # my $status = File::lockf::ulock($this->{FH_LOCK}, 0); | ||||||||||||||||||||||||||||
| 344 | 8 | $this->LOCK(0); # reset status indicator | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 348 | # Internal subroutine: untie hash from db file | ||||||||||||||||||||||||||||
| 349 | # (mainly, insure that output is flushed to disk) | ||||||||||||||||||||||||||||
| 350 | 11 | ▶ | sub _untie { | ||||||||||||||||||||||||||
|
Subroutine
_untie: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 351 | 11 | my $this = shift; | |||||||||||||||||||||||||||
| 352 | untie $this->{hash} | ||||||||||||||||||||||||||||
| 353 | 11 | ▶ | if exists $this->{hash}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 11 | delete $this->{hash}; | |||||||||||||||||||||||||||
| 355 | 11 | delete $this->{db_obj}; | |||||||||||||||||||||||||||
| 356 | 11 | $this->TIED(''); # reset status indicator | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 2 | 1; | |||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | __DATA__ | ||||||||||||||||||||||||||||