<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Lock::_static;

################################################################################
#                                                                              #
# File:        shared/Lock/_static.pm                                          #
#                                                                              #
# Authors:     André Malo &lt;nd@o3media.de&gt;                                      #
#                                                                              #
# Description: belongs to Locking and Filehandle class                         #
#              NO PUBLIC USE                                                   #
#              save the lock object static information                         #
#              (because the lock object is a blessed file handle)              #
#                                                                              #
################################################################################

use strict;
use Carp;

################################################################################
#
# Version check
#
# last modified:
#    $Date$ (GMT)
# by $Author$
#
sub VERSION {(q$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}

################################################################################
#
# Variables
#
my (%static, %access);

# define standard timeouts
# (seconds)
#
my %timeout = (
  shared     =&gt; 10,       # for shared and exclusive shared locks
  exclusive  =&gt; 10,       # for exclusive locks
  violent    =&gt; 600,      # for violent unlocks (10 minutes should justify a process abort)
  master     =&gt; 20        # for master locks
);

### timeout ####################################################################
#
# set and read out the timeout
#
# Params: $type - timeout type (defined in %timeout, see above)
#                 in this case, the specified timeout will be returned
#         OR
#         %hash - (type =&gt; time) pairs
#                  the specified timouts will be set
#
# Return: specified timeout or nothing
#
sub timeout {
  my ($self, @ary) = @_;

  return if (@ary == 0);

  if (@ary == 1) {
    my $type = shift @ary;
    my $hash = $self -&gt; get_static('timeout') || {};

    return defined $hash-&gt;{$type}
    ? $hash -&gt; {$type}
    : $timeout {$type};
  }

  my %hash = @ary;
  $self-&gt;set_static(timeout =&gt; {%{$self -&gt; get_static('timeout') || {}},%hash});

  return;
}

### set_static #################################################################
#
# set an object property
#
# Params: $key   - property and method name
#         $value - property
#
# Return: $value or nothing
#
sub set_static {
  my ($self, $key, $value) = @_;

  $static{$self}={} unless exists($static{$self});
  $static{$self}-&gt;{$key} = $value;

  defined wantarray and return $value;
  return;
}

### get_static #################################################################
#
# read out an object property
#
# Params: $key - property name
#
# Return: value or false
#
sub get_static {
  my ($self, $key) = @_;

  return unless exists($static{$self});
  $static{$self}-&gt;{$key};
}

################################################################################
#
# define the lock file names
#
sub reffile    {shift -&gt; filename . '.lock.ref'}
sub lockfile   {shift -&gt; filename . '.lock'}
sub reflock    {shift -&gt; filename . '.lock.ref.lock'}
sub exshlock   {shift -&gt; filename . '.exshlock'}
sub masterlock {shift -&gt; filename . '.masterlock'}

################################################################################
#
# autoload the general access methods
#
BEGIN {
  %access = map {$_=&gt;1} qw(
    filename
    locked_shared
    locked_exclusive
    locked_exsh
    es_announced
    announced
  );
}
AUTOLOAD {
  my $self = shift;
  (my $attr = $Lock::_static::AUTOLOAD) =~ s/.*:://;
  return if ($attr eq 'DESTROY');

  if ($access{$attr}) {
    return $self -&gt; get_static($attr);
  }
  else {
    eval {
      local $SIG{__DIE__};
      my $sup = "SUPER::$attr";
      return $self -&gt; $sup(@_);
    };
    croak $@;
  }
}

################################################################################
#
# destrcutor - try to unlock, if neccessary and possible
#
DESTROY {
  my $self = shift;

  $self -&gt; unlock if ($self =~ /^Lock=/);
  delete $static{$self};
}

################################################################################
#
# terminator, catch sigTERM and (try to) destroy all objects
#
sub destroy_all {
  $SIG{TERM} = \&amp;destroy_all;

  $_ -&gt; unlock for (grep ((ref $_ and /^Lock=/) =&gt; keys %static));

  exit (0);
}
BEGIN {
  $SIG{TERM} = \&amp;destroy_all;
}

# keep 'require' happy
1;

#
#
### end of Lock::_static #######################################################</pre></body></html>