]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Lock/_simple.pm
3 ################################################################################
5 # File: shared/Lock/_simple.pm #
7 # Authors: André Malo <nd@o3media.de> #
9 # Description: belongs to Locking and Filehandle class #
10 # NOT FOR PUBLIC USE #
12 ################################################################################
17 ################################################################################
25 sub VERSION
{(q
$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}
27 ### sub _simple_esna ###########################################################
30 # excl shared lock announced, no locking possible
32 # Params: $filename - filename
35 # Return: success code (boolean)
38 my ($self, $filename, $timeout) = @_;
39 my $fh = new Lock
::Handle
($filename);
42 unless ($self -> exsh_announced
) {
43 $self -> _simple_lock
($fh) and return 1;
52 ### sub _simple_ana ############################################################
55 # while excl lock announced, no locking possible
57 # Params: $filename - filename
60 # Return: success code (boolean)
63 my ($self, $filename, $timeout) = @_;
64 my $fh = new Lock
::Handle
($filename);
67 unless ($self -> excl_announced
) {
68 $self -> _simple_lock
($fh) and return 1;
77 ### sub _simple_aa #############################################################
80 # while excl lock announced, locking is possible
82 # Params: $filename - filename
85 # Return: success code (boolean)
88 my ($self, $filename, $timeout) = @_;
89 my $fh = new Lock
::Handle
($filename);
92 $self -> _simple_lock
($fh) and return 1;
100 ### sub es_add_ref #############################################################
102 # increase shared lock reference counter
103 # (for excl shared lock)
105 # Params: $timeout - timeout
107 # Return: success code (boolean)
110 my ($self, $timeout) = @_;
112 # lock reference counter file
113 # increase reference counter
114 # set excl shared lock
115 # release ref. counter file
117 return unless($self -> _simple_esna
($self->reflock, $timeout));
118 $self -> set_ref
($self -> get_ref
+ 1) or return;
119 $self -> set_exsh_announce
or return;
120 $self -> _simple_unlock
($self -> reflock
) or return;
126 ### sub es_sub_ref #############################################################
128 # decrease shared lock reference counter
129 # (of an excl shared locked file)
131 # Params: $timeout - timeout
133 # Return: success code (boolean)
136 my ($self, $timeout) = @_;
138 # lock reference counter file
139 # increase reference counter
140 # release ref. counter file
142 return unless($self -> _simple_aa
($self->reflock, $timeout));
143 $self -> set_ref
($self -> get_ref
- 1) or return;
144 $self -> remove_exsh_announce
;
145 $self -> _simple_unlock
($self -> reflock
) or return;
151 ### sub add_ref ################################################################
153 # increase shared lock reference counter
155 # Params: $timeout - timeout
157 # Return: success code (boolean)
160 my ($self, $timeout) = @_;
162 # lock reference counter file
163 # increase reference counter
164 # release ref. counter file
166 return unless($self -> _simple_ana
($self->reflock, $timeout));
167 $self -> set_ref
($self -> get_ref
+ 1) or return;
168 $self -> _simple_unlock
($self -> reflock
) or return;
174 ### sub sub_ref ################################################################
176 # decrease shared lock reference counter
178 # Params: $timeout - timeout
180 # Return: success code (boolean)
183 my ($self, $timeout) = @_;
185 # lock reference counter file
186 # increase reference counter
187 # release ref. counter file
189 return unless($self -> _simple_aa
($self->reflock, $timeout));
190 $self -> set_ref
($self -> get_ref
- 1) or return;
191 $self -> _simple_unlock
($self -> reflock
) or return;
197 ### sub get_ref ################################################################
199 # read out the reference counter
204 # Return: counter value
208 my ($fh, $val) = new Lock
::Handle
($self -> reffile
);
212 sysopen ($fh, $fh->filename, O_RDONLY
) or return 0;
222 ### sub set_ref ################################################################
224 # write reference counter into file
227 # Params: counter value
229 # Return: success code (boolean)
232 my ($self, $val) = @_;
233 my $fh = new Lock
::Handle
($self -> reffile
);
236 if (-f
$fh->filename) {
237 unlink $fh->filename or return;
242 sysopen ($fh, $fh->filename, O_WRONLY
| O_TRUNC
| O_CREAT
) or return;
243 print $fh $val or do {
245 unlink $fh->filename;
250 unlink $fh->filename;
260 ### sub set_excl_announce ######################################################
262 # try to announce an exclusive lock
266 # Return: status (boolean)
268 sub set_excl_announce
{
271 if ($self -> excl_announced
) {
272 return ($self -> announced
) ?
1 : return;
275 if ($self -> _simple_lock
(new Lock
::Handle
($self -> lockfile
))) {
276 $self -> set_static
(announced
=> 1);
283 ### sub remove_excl_announce ###################################################
285 # remove announce of an exclusive lock, if it's set by ourself
291 sub remove_excl_announce
{
294 if ($self -> excl_announced
and $self -> announced
) {
295 $self -> _simple_unlock
($self -> lockfile
);
298 $self -> set_static
(announced
=> 0);
303 ### sub set_exsh_announce ######################################################
305 # try to announce an exclusive shared lock
309 # Return: status (boolean)
311 sub set_exsh_announce
{
314 if ($self -> exsh_announced
) {
315 return ($self -> es_announced
) ?
1 : return;
318 if ($self -> _simple_lock
(new Lock
::Handle
($self -> exshlock
))) {
319 $self -> set_static
(es_announced
=> 1);
326 ### sub remove_exsh_announce ###################################################
328 # remove an exclusive shared lock, if it's set by ourself
334 sub remove_exsh_announce
{
337 if ($self -> exsh_announced
and $self -> es_announced
) {
338 $self -> _simple_unlock
($self -> exshlock
);
341 $self -> set_static
(es_announced
=> 0);
346 # keep 'require' happy
351 ### end of Lock::_simple #######################################################
patrick-canterino.de