| 1 | ;# $Id | ||||||||||||||||||||
| 2 | ;# | ||||||||||||||||||||
| 3 | ;# @COPYRIGHT@ | ||||||||||||||||||||
| 4 | ;# | ||||||||||||||||||||
| 5 | ;# $Log: Manager.pm,v $ | ||||||||||||||||||||
| 6 | ;# Revision 0.2 1999/12/07 20:51:05 ram | ||||||||||||||||||||
| 7 | ;# Baseline for 0.2 release. | ||||||||||||||||||||
| 8 | ;# | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | ######################################################################## | ||||||||||||||||||||
| 13 | package LockFile::Manager; | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # | ||||||||||||||||||||
| 16 | # A pool of all created locks. | ||||||||||||||||||||
| 17 | # | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 2 | my $MANAGER = undef; # The main manager | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | # ->make | ||||||||||||||||||||
| 23 | # | ||||||||||||||||||||
| 24 | # Creates a new LockFile::Manager to hold the locks. | ||||||||||||||||||||
| 25 | # | ||||||||||||||||||||
| 26 | # Attributes: | ||||||||||||||||||||
| 27 | # | ||||||||||||||||||||
| 28 | # pool hash of LockFile::Lock objects | ||||||||||||||||||||
| 29 | # wfunc warning function to be called | ||||||||||||||||||||
| 30 | # efunc error function to be called | ||||||||||||||||||||
| 31 | # | ||||||||||||||||||||
| 32 | 2 | ▶ | sub make { | ||||||||||||||||||
|
Subroutine
make: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 33 | 2 | my $self = bless {}, shift; | |||||||||||||||||||
| 34 | 2 | my ($wfunc, $efunc) = @_; | |||||||||||||||||||
| 35 | 2 | $self->{'pool'} = {}; | |||||||||||||||||||
| 36 | 2 | $self->{'wfunc'} = $wfunc; | |||||||||||||||||||
| 37 | 2 | $self->{'efunc'} = $efunc; | |||||||||||||||||||
| 38 | 2 | return $self; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | # | ||||||||||||||||||||
| 42 | # Attribute access | ||||||||||||||||||||
| 43 | # | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 4 | ▶ | sub pool { $_[0]->{'pool'} } | ||||||||||||||||||
|
Subroutine
pool: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 46 | 1 | ▶ | sub wfunc { $_[0]->{'wfunc'} } | ||||||||||||||||||
|
Subroutine
wfunc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 47 | 0 | ▶ | sub efunc { $_[0]->{'efunc'} } | ||||||||||||||||||
|
Subroutine
efunc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | # | ||||||||||||||||||||
| 50 | # ->manager -- "once" function | ||||||||||||||||||||
| 51 | # | ||||||||||||||||||||
| 52 | # Returns the main manager. | ||||||||||||||||||||
| 53 | # | ||||||||||||||||||||
| 54 | 2 | ▶ | sub manager { | ||||||||||||||||||
|
Subroutine
manager: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||
| 55 | 2 | my ($class, $wfunc, $efunc) = @_; | |||||||||||||||||||
| 56 | 2 | ▶ | return $MANAGER || ($MANAGER = $class->make($wfunc, $efunc)); | ||||||||||||||||||
|
Condition33%1/3
$MANAGER || ($MANAGER = $class->make($wfunc, $efunc))
MC/DC0%0/2
$MANAGER || ($MANAGER = $class->make($wfunc, $efunc))
$MANAGER
($MANAGER = $class->make($wfunc, $efunc))
Truth table33%1/3
A || B
A= $MANAGERB= ($MANAGER = $class->make($wfunc, $efunc))
| |||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # | ||||||||||||||||||||
| 60 | # ->remember | ||||||||||||||||||||
| 61 | # | ||||||||||||||||||||
| 62 | # Remember created locks. | ||||||||||||||||||||
| 63 | # | ||||||||||||||||||||
| 64 | 1 | ▶ | sub remember { | ||||||||||||||||||
|
Subroutine
remember: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||
| 65 | 1 | my $self = shift; | |||||||||||||||||||
| 66 | 1 | my ($lock) = @_; # A LockFile::Lock object | |||||||||||||||||||
| 67 | 1 | my $pool = $self->pool; | |||||||||||||||||||
| 68 | 1 | ▶ | if (exists $pool->{$lock}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | &{$self->efunc}("lock $lock already remembered"); | |||||||||||||||||||
| 70 | 0 | return; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | 1 | $pool->{$lock} = $lock; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # | ||||||||||||||||||||
| 76 | # ->forget | ||||||||||||||||||||
| 77 | # | ||||||||||||||||||||
| 78 | # Forget about a lock. | ||||||||||||||||||||
| 79 | # | ||||||||||||||||||||
| 80 | 1 | ▶ | sub forget { | ||||||||||||||||||
|
Subroutine
forget: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||
| 81 | 1 | my $self = shift; | |||||||||||||||||||
| 82 | 1 | my ($lock) = @_; # A LockFile::Lock object | |||||||||||||||||||
| 83 | 1 | my $pool = $self->pool; | |||||||||||||||||||
| 84 | 1 | ▶ | unless (exists $pool->{$lock}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | &{$self->efunc}("lock $lock not remembered yet"); | |||||||||||||||||||
| 86 | 0 | return; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | 1 | delete $pool->{$lock}; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # | ||||||||||||||||||||
| 92 | # ->release_all | ||||||||||||||||||||
| 93 | # | ||||||||||||||||||||
| 94 | # Release all the locks. | ||||||||||||||||||||
| 95 | # | ||||||||||||||||||||
| 96 | 2 | ▶ | sub release_all { | ||||||||||||||||||
|
Subroutine
release_all: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 97 | 2 | my $self = shift; | |||||||||||||||||||
| 98 | 2 | my $pool = $self->pool; | |||||||||||||||||||
| 99 | 2 | my $locks = scalar keys %$pool; | |||||||||||||||||||
| 100 | 2 | ▶ | return unless $locks; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 1 | ▶ | my $s = $locks == 1 ? '' : 's'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 1 | &{$self->wfunc}("releasing $locks pending lock$s..."); | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 1 | foreach my $lock (values %$pool) { | |||||||||||||||||||
| 106 | 1 | $lock->release; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | sub END { $MANAGER->release_all if defined $MANAGER } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 2 | 1; | |||||||||||||||||||
| 113 | |||||||||||||||||||||