]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Lock/_static.pm
improved (?) the master lock. Now, if first trial fails, the file will be locked...
[selfforum.git] / selfforum-cgi / shared / Lock / _static.pm
1 package Lock::_static;
2
3 ################################################################################
4 # #
5 # File: shared/Lock/_static.pm #
6 # #
7 # Authors: Andre Malo <nd@o3media.de>, 2001-05-25 #
8 # #
9 # Description: belongs to Locking and Filehandle class #
10 # NO PUBLIC USE #
11 # save the lock object static information #
12 # (because the lock object is a blessed file handle) #
13 # #
14 ################################################################################
15
16 use strict;
17 use vars qw(
18 $VERSION
19 );
20
21 use Carp;
22
23 ################################################################################
24 #
25 # Version check
26 #
27 $VERSION = do { my @r =(q$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
28
29 ################################################################################
30 #
31 # Variables
32 #
33 my (%static, %access);
34
35 # define standard timeouts
36 # (seconds)
37 #
38 my %timeout = (
39 shared => 10, # for shared and exclusive shared locks
40 exclusive => 10, # for exclusive locks
41 violent => 600, # for violent unlocks (10 minutes should justify a process abort)
42 master => 20 # for master locks
43 );
44
45 ### timeout ####################################################################
46 #
47 # set and read out the timeout
48 #
49 # Params: $type - timeout type (defined in %timeout, see above)
50 # in this case, the specified timeout will be returned
51 # OR
52 # %hash - (type => time) pairs
53 # the specified timouts will be set
54 #
55 # Return: specified timeout or nothing
56 #
57 sub timeout {
58 my ($self, @ary) = @_;
59
60 return if (@ary == 0);
61
62 if (@ary == 1) {
63 my $type = shift @ary;
64 my $hash = $self -> get_static('timeout') || {};
65
66 return defined $hash->{$type}
67 ? $hash -> {$type}
68 : $timeout {$type};
69 }
70
71 my %hash = @ary;
72 $self->set_static(timeout => {%{$self -> get_static('timeout') || {}},%hash});
73
74 return;
75 }
76
77 ### set_static #################################################################
78 #
79 # set an object property
80 #
81 # Params: $key - property and method name
82 # $value - property
83 #
84 # Return: $value or nothing
85 #
86 sub set_static {
87 my ($self, $key, $value) = @_;
88
89 $static{$self}={} unless exists($static{$self});
90 $static{$self}->{$key} = $value;
91
92 defined wantarray and return $value;
93 return;
94 }
95
96 ### get_static #################################################################
97 #
98 # read out an object property
99 #
100 # Params: $key - property name
101 #
102 # Return: value or false
103 #
104 sub get_static {
105 my ($self, $key) = @_;
106
107 return unless exists($static{$self});
108 $static{$self}->{$key};
109 }
110
111 ################################################################################
112 #
113 # define the lock file names
114 #
115 sub reffile {shift -> filename . '.lock.ref'}
116 sub lockfile {shift -> filename . '.lock'}
117 sub reflock {shift -> filename . '.lock.ref.lock'}
118 sub exshlock {shift -> filename . '.exshlock'}
119 sub masterlock {shift -> filename . '.masterlock'}
120
121 ################################################################################
122 #
123 # autoload the general access methods
124 #
125 BEGIN {
126 %access = map {$_=>1} qw(
127 filename
128 locked_shared
129 locked_exclusive
130 locked_exsh
131 es_announced
132 announced
133 );
134 }
135 AUTOLOAD {
136 my $self = shift;
137 (my $attr = $Lock::_static::AUTOLOAD) =~ s/.*:://;
138 return if ($attr eq 'DESTROY');
139
140 if ($access{$attr}) {
141 return $self -> get_static($attr);
142 }
143 else {
144 eval {
145 local $SIG{__DIE__};
146 my $sup = "SUPER::$attr";
147 return $self -> $sup(@_);
148 };
149 croak $@;
150 }
151 }
152
153 ################################################################################
154 #
155 # destrcutor - try to unlock, if neccessary and possible
156 #
157 DESTROY {
158 my $self = shift;
159
160 $self -> unlock if ($self =~ /^Lock=/);
161 delete $static{$self};
162 }
163
164 ################################################################################
165 #
166 # terminator, catch sigTERM and (try to) destroy all objects
167 #
168 sub destroy_all {
169 $SIG{TERM} = \&destroy_all;
170
171 $_ -> unlock for (grep ((ref $_ and /^Lock=/) => keys %static));
172
173 exit (0);
174 }
175 BEGIN {
176 $SIG{TERM} = \&destroy_all;
177 }
178
179 # keep 'require' happy
180 1;
181
182 #
183 #
184 ### end of Lock::_static #######################################################

patrick-canterino.de