| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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
|
|
|
2
|
|
22480
|
use Data::Dumper; |
|
|
2
|
|
|
|
|
14674
|
|
|
|
2
|
|
|
|
|
132
|
|
|
11
|
2
|
|
|
2
|
|
1144
|
use EMDIS::ECS qw($VERSION); |
|
|
2
|
|
|
|
|
8
|
|
|
|
2
|
|
|
|
|
280
|
|
|
12
|
2
|
|
|
2
|
|
14
|
use Fcntl qw(:DEFAULT :flock); |
|
|
2
|
|
|
|
|
30
|
|
|
|
2
|
|
|
|
|
682
|
|
|
13
|
|
|
|
|
|
|
#use File::lockf; # potential alternate locking method |
|
14
|
2
|
|
|
2
|
|
1246
|
use SDBM_File; |
|
|
2
|
|
|
|
|
1058
|
|
|
|
2
|
|
|
|
|
82
|
|
|
15
|
2
|
|
|
2
|
|
14
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
46
|
|
|
16
|
2
|
|
|
2
|
|
10
|
use vars qw($VERSION); |
|
|
2
|
|
|
|
|
16
|
|
|
|
2
|
|
|
|
|
3652
|
|
|
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
|
|
|
|
|
|
|
sub new { |
|
22
|
7
|
|
|
7
|
0
|
2561
|
my $class = shift; |
|
23
|
7
|
|
|
|
|
79
|
my $dbfile = shift; |
|
24
|
7
|
|
|
|
|
29
|
my $lockfile = shift; |
|
25
|
7
|
|
|
|
|
17
|
my $lock_timeout = shift; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# validate aaarghs |
|
28
|
7
|
100
|
|
|
|
46
|
if(!defined $dbfile) { |
|
29
|
2
|
|
|
|
|
30
|
warn "EMDIS::ECS::LockedHash::new() failed: missing database file name."; |
|
30
|
2
|
|
|
|
|
14
|
return undef; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
5
|
100
|
|
|
|
23
|
if(!defined $lockfile) { |
|
33
|
2
|
|
|
|
|
22
|
warn "EMDIS::ECS::LockedHash::new() failed: missing lock file name."; |
|
34
|
2
|
|
|
|
|
10
|
return undef; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
3
|
100
|
|
|
|
16
|
$lock_timeout = 10 unless defined $lock_timeout; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# define this object |
|
39
|
3
|
|
|
|
|
50
|
my $this = {}; |
|
40
|
3
|
|
|
|
|
23
|
bless $this, $class; |
|
41
|
3
|
|
|
|
|
40
|
$this->{dbfile} = $dbfile; |
|
42
|
3
|
|
|
|
|
40
|
$this->{lockfile} = $lockfile; |
|
43
|
3
|
|
|
|
|
24
|
$this->{lock_timeout} = $lock_timeout; |
|
44
|
3
|
|
|
|
|
35
|
$this->{ERROR} = ''; |
|
45
|
3
|
|
|
|
|
23
|
$this->{LOCK} = 0; |
|
46
|
3
|
|
|
|
|
13
|
$this->{TIED} = ''; |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# open lock file and retain file handle |
|
49
|
3
|
50
|
|
|
|
490
|
if(!sysopen($this->{FH_LOCK}, $this->{lockfile}, O_RDWR|O_CREAT)) { |
|
50
|
0
|
|
|
|
|
0
|
warn "EMDIS::ECS::LockedHash::new() failed: unable to access lock file " . |
|
51
|
|
|
|
|
|
|
"'$this->{lockfile}': $!"; |
|
52
|
0
|
|
|
|
|
0
|
return undef; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# tie/untie db file, to test whether it's accessible |
|
56
|
3
|
50
|
|
|
|
57
|
if(!$this->_tie()) { |
|
57
|
0
|
|
|
|
|
0
|
warn "EMDIS::ECS::LockedHash::new() failed: " . $this->ERROR; |
|
58
|
0
|
|
|
|
|
0
|
return undef; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
3
|
|
|
|
|
41
|
$this->_untie; |
|
61
|
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
18
|
return $this; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
66
|
|
|
|
|
|
|
# set/get error description |
|
67
|
|
|
|
|
|
|
sub ERROR { |
|
68
|
40
|
|
|
40
|
0
|
539
|
my $this = shift; |
|
69
|
40
|
|
|
|
|
90
|
my $err = shift; |
|
70
|
40
|
100
|
|
|
|
98
|
if(defined $err) { |
|
71
|
33
|
|
|
|
|
113
|
$this->{ERROR} = $err; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
40
|
|
|
|
|
116
|
return $this->{ERROR}; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
77
|
|
|
|
|
|
|
# set/get locked status indicator |
|
78
|
|
|
|
|
|
|
sub LOCK { |
|
79
|
59
|
|
|
59
|
0
|
101
|
my $this = shift; |
|
80
|
59
|
|
|
|
|
76
|
my $status = shift; |
|
81
|
59
|
100
|
|
|
|
122
|
if(defined $status) { |
|
82
|
18
|
|
|
|
|
43
|
$this->{LOCK} = $status; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
59
|
|
|
|
|
189
|
return $this->{LOCK}; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
88
|
|
|
|
|
|
|
# set/get tied status indicator |
|
89
|
|
|
|
|
|
|
sub TIED { |
|
90
|
43
|
|
|
43
|
0
|
79
|
my $this = shift; |
|
91
|
43
|
|
|
|
|
92
|
my $status = shift; |
|
92
|
43
|
100
|
|
|
|
103
|
if(defined $status) { |
|
93
|
21
|
|
|
|
|
52
|
$this->{TIED} = $status; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
43
|
|
|
|
|
153
|
return $this->{TIED}; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
99
|
|
|
|
|
|
|
# Read one key-value from the database under a shared lock |
|
100
|
|
|
|
|
|
|
sub read { |
|
101
|
5
|
|
|
5
|
0
|
304
|
my $this = shift; |
|
102
|
5
|
|
|
|
|
12
|
my $key = shift; |
|
103
|
5
|
|
|
|
|
10
|
my $value = undef; |
|
104
|
|
|
|
|
|
|
|
|
105
|
5
|
|
|
|
|
13
|
$this->ERROR(''); # reset error status |
|
106
|
|
|
|
|
|
|
# check lock status |
|
107
|
5
|
100
|
66
|
|
|
11
|
if($this->LOCK != LOCK_SH and $this->LOCK != LOCK_EX) { |
|
108
|
1
|
|
|
|
|
4
|
$this->ERROR( |
|
109
|
|
|
|
|
|
|
"EMDIS::ECS::LockedHash::read() requires shared or exclusive lock."); |
|
110
|
1
|
|
|
|
|
5
|
return undef; |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
# read value from hash |
|
113
|
4
|
|
|
|
|
8
|
$value = undef; |
|
114
|
4
|
100
|
|
|
|
56
|
$value = $this->{hash}->{$key} if exists $this->{hash}->{$key}; |
|
115
|
4
|
100
|
100
|
|
|
36
|
if(defined($value) and ($value =~ /^\$\w+\s*=\s*\{.*\}\s*\;\s*$/s)) { |
|
116
|
|
|
|
|
|
|
# convert Dumper() string to hash ref |
|
117
|
1
|
|
|
|
|
7
|
$value =~ s/^\$\w+/\$value/; # convert "$VAR1 = ..." to "$value = ..." |
|
118
|
1
|
|
|
|
|
160
|
eval($value); # eval "$value = ..." string |
|
119
|
|
|
|
|
|
|
} |
|
120
|
4
|
|
|
|
|
17
|
return $value; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
124
|
|
|
|
|
|
|
# Write one key-value to the database under an exclusive lock |
|
125
|
|
|
|
|
|
|
sub write { |
|
126
|
3
|
|
|
3
|
0
|
8
|
my $this = shift; |
|
127
|
3
|
|
|
|
|
9
|
my $key = shift; |
|
128
|
3
|
|
|
|
|
12
|
my $value = shift; |
|
129
|
|
|
|
|
|
|
|
|
130
|
3
|
|
|
|
|
10
|
$this->ERROR(''); # reset error status |
|
131
|
|
|
|
|
|
|
# check lock status |
|
132
|
3
|
100
|
|
|
|
9
|
if($this->LOCK != LOCK_EX) { |
|
133
|
1
|
|
|
|
|
3
|
$this->ERROR("EMDIS::ECS::LockedHash::write() requires exclusive lock."); |
|
134
|
1
|
|
|
|
|
11
|
return ''; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
# write value to hash |
|
137
|
2
|
100
|
|
|
|
8
|
if(ref $value) { |
|
138
|
1
|
|
|
|
|
16
|
local $Data::Dumper::Indent = 0; |
|
139
|
1
|
|
|
|
|
19
|
$value = Dumper($value); # convert ref to Dumper() string |
|
140
|
|
|
|
|
|
|
} |
|
141
|
2
|
|
|
|
|
295
|
$this->{hash}->{$key} = $value; |
|
142
|
2
|
|
|
|
|
18
|
return 1; # successful |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
146
|
|
|
|
|
|
|
# Delete a key-value under an exclusive lock |
|
147
|
|
|
|
|
|
|
sub delete { |
|
148
|
2
|
|
|
2
|
0
|
9
|
my $this = shift; |
|
149
|
2
|
|
|
|
|
4
|
my $key = shift; |
|
150
|
2
|
|
|
|
|
4
|
my $value = undef; |
|
151
|
|
|
|
|
|
|
|
|
152
|
2
|
|
|
|
|
6
|
$this->ERROR(''); # reset error status |
|
153
|
|
|
|
|
|
|
# check lock status |
|
154
|
2
|
100
|
|
|
|
6
|
if($this->LOCK != LOCK_EX) { |
|
155
|
1
|
|
|
|
|
3
|
$this->ERROR("EMDIS::ECS::LockedHash::delete() requires exclusive lock."); |
|
156
|
1
|
|
|
|
|
6
|
return ''; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
# delete value from hash |
|
159
|
1
|
|
|
|
|
37
|
$value = delete $this->{hash}->{$key}; |
|
160
|
1
|
|
|
|
|
7
|
return 1; # successful |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
164
|
|
|
|
|
|
|
# Return a list of key values under a shared lock |
|
165
|
|
|
|
|
|
|
sub keys { |
|
166
|
3
|
|
|
3
|
0
|
6
|
my $this = shift; |
|
167
|
3
|
|
|
|
|
6
|
my @ks = (); |
|
168
|
|
|
|
|
|
|
|
|
169
|
3
|
|
|
|
|
9
|
$this->ERROR(''); # reset error status |
|
170
|
|
|
|
|
|
|
# check lock status |
|
171
|
3
|
100
|
66
|
|
|
6
|
if($this->LOCK != LOCK_SH and $this->LOCK != LOCK_EX) { |
|
172
|
1
|
|
|
|
|
4
|
$this->ERROR( |
|
173
|
|
|
|
|
|
|
"EMDIS::ECS::LockedHash::keys() requires shared or exclusive lock."); |
|
174
|
1
|
|
|
|
|
7
|
return ''; |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
# get keys from hash |
|
177
|
2
|
|
|
|
|
4
|
@ks = keys %{$this->{hash}}; |
|
|
2
|
|
|
|
|
54
|
|
|
178
|
2
|
|
|
|
|
10
|
return @ks; |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
182
|
|
|
|
|
|
|
# Obtain (advisory) lock and tie internal hash to db file. |
|
183
|
|
|
|
|
|
|
sub lock { |
|
184
|
6
|
|
|
6
|
0
|
12
|
my $this = shift; |
|
185
|
6
|
|
|
|
|
9
|
my $lock_type = shift; |
|
186
|
6
|
|
|
|
|
14
|
my $oldlock = $this->LOCK; |
|
187
|
6
|
50
|
|
|
|
17
|
$lock_type = LOCK_EX unless $lock_type; # default = LOCK_EX |
|
188
|
6
|
|
|
|
|
20
|
$this->ERROR(''); # reset error status |
|
189
|
6
|
50
|
|
|
|
13
|
return 1 if $oldlock == $lock_type; # already locked |
|
190
|
6
|
|
|
|
|
9
|
my $locked = 0; |
|
191
|
6
|
|
|
|
|
7
|
my $attempt = 0; |
|
192
|
6
|
|
66
|
|
|
37
|
while(!$locked and $attempt++ < 5) { |
|
193
|
6
|
50
|
|
|
|
14
|
sleep 2 if $attempt > 1; |
|
194
|
6
|
|
|
|
|
14
|
$this->ERROR(''); # reset error status |
|
195
|
6
|
|
|
|
|
13
|
$locked = $this->_lock($lock_type); |
|
196
|
|
|
|
|
|
|
} |
|
197
|
6
|
50
|
|
|
|
15
|
if(!$locked) { |
|
198
|
0
|
|
|
|
|
0
|
$this->ERROR("EMDIS::ECS::LockedHash::lock() failed: " . $this->ERROR); |
|
199
|
0
|
|
|
|
|
0
|
return ''; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
6
|
50
|
33
|
|
|
11
|
if(!$this->TIED and !$this->_tie()) { |
|
202
|
0
|
|
|
|
|
0
|
$this->ERROR("EMDIS::ECS::LockedHash::lock() failed: " . $this->ERROR); |
|
203
|
0
|
|
|
|
|
0
|
return ''; |
|
204
|
|
|
|
|
|
|
} |
|
205
|
6
|
|
|
|
|
31
|
return 1; # successful |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
209
|
|
|
|
|
|
|
# Release (advisory) lock and untie internal hash. |
|
210
|
|
|
|
|
|
|
sub unlock { |
|
211
|
6
|
|
|
6
|
0
|
222
|
my $this = shift; |
|
212
|
6
|
|
|
|
|
17
|
$this->_untie(); |
|
213
|
6
|
|
|
|
|
14
|
$this->_unlock(); |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
217
|
|
|
|
|
|
|
# Quickly delete all key-values under an exclusive lock |
|
218
|
|
|
|
|
|
|
sub undef { |
|
219
|
2
|
|
|
2
|
0
|
15
|
my $this = shift; |
|
220
|
|
|
|
|
|
|
|
|
221
|
2
|
|
|
|
|
7
|
$this->ERROR(''); # reset error status |
|
222
|
|
|
|
|
|
|
# check lock status |
|
223
|
2
|
100
|
|
|
|
6
|
if($this->LOCK != LOCK_EX) { |
|
224
|
1
|
|
|
|
|
3
|
$this->ERROR("EMDIS::ECS::LockedHash::undef() requires exclusive lock."); |
|
225
|
1
|
|
|
|
|
6
|
return ''; |
|
226
|
|
|
|
|
|
|
} |
|
227
|
|
|
|
|
|
|
# delete everything from hash |
|
228
|
1
|
|
|
|
|
3
|
undef %{$this->{hash}}; |
|
|
1
|
|
|
|
|
32
|
|
|
229
|
1
|
|
|
|
|
62
|
return 1; # successful |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
234
|
|
|
|
|
|
|
# untie hash and close lock file when perl object passes out of scope |
|
235
|
|
|
|
|
|
|
sub DESTROY { |
|
236
|
1
|
|
|
1
|
|
52
|
my $this = shift; |
|
237
|
1
|
|
|
|
|
22
|
$this->_untie(); |
|
238
|
|
|
|
|
|
|
close($this->{FH_LOCK}) |
|
239
|
1
|
50
|
|
|
|
510
|
if defined $this->{FH_LOCK}; |
|
240
|
|
|
|
|
|
|
} |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
243
|
|
|
|
|
|
|
# Select UNIX or Win32 version of _lock |
|
244
|
|
|
|
|
|
|
sub _lock |
|
245
|
|
|
|
|
|
|
{ |
|
246
|
10
|
50
|
|
10
|
|
126
|
$^O =~ /MSWin32/ ? _lock_win32(@_) : _lock_unix(@_); |
|
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
|
|
|
|
|
|
|
sub _lock_unix { |
|
254
|
10
|
|
|
10
|
|
22
|
my $this = shift; |
|
255
|
10
|
|
|
|
|
14
|
my $lock_type = shift; |
|
256
|
10
|
100
|
|
|
|
29
|
$lock_type = LOCK_EX unless defined $lock_type; |
|
257
|
10
|
|
|
|
|
17
|
my $result = 1; |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
# set up "local" SIG_ALRM handler |
|
260
|
|
|
|
|
|
|
# (Note: not using "local $SIG{PIPE}" because it ignores die()) |
|
261
|
10
|
|
|
|
|
45
|
my $oldsigalrm = $SIG{ALRM}; |
|
262
|
|
|
|
|
|
|
$SIG{ALRM} = sub { |
|
263
|
1
|
|
|
1
|
|
59
|
die "timeout - $this->{lock_timeout} second time limit exceeded\n"; |
|
264
|
10
|
|
|
|
|
324
|
}; |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
# attempt to obtain lock, with time limit |
|
267
|
10
|
|
|
|
|
68
|
eval { |
|
268
|
10
|
|
|
|
|
86
|
alarm($this->{lock_timeout}); # set alarm |
|
269
|
|
|
|
|
|
|
die "flock() failed: $!\n" |
|
270
|
10
|
100
|
|
|
|
2000497
|
unless flock($this->{FH_LOCK}, $lock_type); |
|
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
|
|
|
|
|
56
|
alarm(0); # turn off alarm |
|
276
|
|
|
|
|
|
|
}; |
|
277
|
10
|
100
|
|
|
|
52
|
if($@) { |
|
278
|
1
|
|
|
|
|
42
|
alarm(0); # turn off alarm |
|
279
|
1
|
|
|
|
|
50
|
$this->ERROR("EMDIS::ECS::LockedHash::_lock_unix() failed: $@"); |
|
280
|
1
|
|
|
|
|
142
|
$this->LOCK(0); # reset status indicator |
|
281
|
1
|
|
|
|
|
5
|
$result = ''; |
|
282
|
|
|
|
|
|
|
} |
|
283
|
|
|
|
|
|
|
# restore previous SIG_ALRM handler |
|
284
|
10
|
50
|
|
|
|
28
|
if(defined $oldsigalrm) { $SIG{ALRM} = $oldsigalrm; } |
|
|
0
|
|
|
|
|
0
|
|
|
285
|
10
|
|
|
|
|
203
|
else { delete $SIG{ALRM}; } |
|
286
|
10
|
100
|
|
|
|
70
|
$this->LOCK($lock_type) # set status indicator |
|
287
|
|
|
|
|
|
|
if $result; |
|
288
|
10
|
|
|
|
|
44
|
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
|
|
|
|
|
|
|
sub _lock_win32 { |
|
296
|
0
|
|
|
0
|
|
0
|
my $this = shift; |
|
297
|
0
|
|
|
|
|
0
|
my $lock_type = shift; |
|
298
|
0
|
0
|
|
|
|
0
|
$lock_type = LOCK_EX unless defined $lock_type; |
|
299
|
0
|
|
|
|
|
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
|
|
|
|
|
0
|
my $timeoutCount = 0; |
|
305
|
0
|
|
|
|
|
0
|
my $locked; |
|
306
|
0
|
|
0
|
|
|
0
|
while (!($locked = flock($this->{FH_LOCK}, $lock_type | LOCK_NB)) and |
|
307
|
|
|
|
|
|
|
($timeoutCount++ <= $this->{lock_timeout})) { |
|
308
|
0
|
|
|
|
|
0
|
sleep 1; |
|
309
|
|
|
|
|
|
|
} |
|
310
|
|
|
|
|
|
|
|
|
311
|
0
|
0
|
|
|
|
0
|
if(!$locked) { |
|
312
|
0
|
|
|
|
|
0
|
$this->ERROR("EMDIS::ECS::LockedHash::_lock_win32() failed: $@"); |
|
313
|
0
|
|
|
|
|
0
|
$this->LOCK(0); # reset status indicator |
|
314
|
0
|
|
|
|
|
0
|
$result = ''; |
|
315
|
|
|
|
|
|
|
} |
|
316
|
0
|
0
|
|
|
|
0
|
$this->LOCK($lock_type) # set status indicator |
|
317
|
|
|
|
|
|
|
if $result; |
|
318
|
0
|
|
|
|
|
0
|
return $result; # successful |
|
319
|
|
|
|
|
|
|
} |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
322
|
|
|
|
|
|
|
# Internal subroutine: tie hash to db file |
|
323
|
|
|
|
|
|
|
sub _tie { |
|
324
|
10
|
|
|
10
|
|
25
|
my $this = shift; |
|
325
|
10
|
|
|
|
|
801
|
$this->{db_obj} = tie(%{$this->{hash}}, 'SDBM_File', $this->{dbfile}, |
|
326
|
10
|
50
|
|
|
|
15
|
O_CREAT|O_RDWR, (defined $EMDIS::ECS::FILEMODE ? $EMDIS::ECS::FILEMODE : 0664)) |
|
|
|
50
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
or $this->ERROR( |
|
328
|
|
|
|
|
|
|
"EMDIS::ECS::LockedHash::_tie() failed ($this->{dbfile}): $!"); |
|
329
|
10
|
50
|
|
|
|
46
|
if($this->{db_obj}) { |
|
330
|
10
|
|
|
|
|
35
|
$this->TIED(1); # set status indicator |
|
331
|
|
|
|
|
|
|
} else { |
|
332
|
0
|
|
|
|
|
0
|
$this->TIED(''); # reset status indicator |
|
333
|
|
|
|
|
|
|
} |
|
334
|
10
|
|
|
|
|
23
|
return $this->TIED; |
|
335
|
|
|
|
|
|
|
} |
|
336
|
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
338
|
|
|
|
|
|
|
# Internal subroutine: release (advisory) lock |
|
339
|
|
|
|
|
|
|
sub _unlock { |
|
340
|
8
|
|
|
8
|
|
21
|
my $this = shift; |
|
341
|
8
|
|
|
|
|
79
|
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
|
|
|
|
|
90
|
$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
|
|
|
|
|
|
|
sub _untie { |
|
351
|
11
|
|
|
11
|
|
26
|
my $this = shift; |
|
352
|
|
|
|
|
|
|
untie $this->{hash} |
|
353
|
11
|
100
|
|
|
|
53
|
if exists $this->{hash}; |
|
354
|
11
|
|
|
|
|
37
|
delete $this->{hash}; |
|
355
|
11
|
|
|
|
|
181
|
delete $this->{db_obj}; |
|
356
|
11
|
|
|
|
|
43
|
$this->TIED(''); # reset status indicator |
|
357
|
|
|
|
|
|
|
} |
|
358
|
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
1; |
|
360
|
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
__DATA__ |