1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
# Copyrights 2001-2014 by [Mark Overmeer].
# For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.01.
use strict;
package Mail::Box::Locker::Multi;
use vars '$VERSION';
$VERSION = '2.117';
use base 'Mail::Box::Locker';
use Carp;
sub init($)
{ my ($self, $args) = @_;
$self->SUPER::init($args);
my @use
= exists $args->{use} ? @{delete $args->{use}}
: $^O eq 'MSWin32' ? qw/Flock/
: qw/NFS POSIX Flock/;
my (@lockers, @used);
foreach my $method (@use)
{ if(UNIVERSAL::isa($method, 'Mail::Box::Locker'))
{ push @lockers, $method;
(my $used = ref $method) =~ s/.*\:\://;
push @used, $used;
next;
}
my $locker = eval
{ Mail::Box::Locker->new
( %$args
, method => $method
, timeout => 1
)
};
next unless defined $locker;
push @lockers, $locker;
push @used, $method;
}
$self->{MBLM_lockers} = \@lockers;
$self->log(PROGRESS => "Multi-locking via @used.");
$self;
}
#-------------------------------------------
sub name() {'MULTI'}
sub _try_lock($)
{ my $self = shift;
my @successes;
foreach my $locker ($self->lockers)
{
unless($locker->lock)
{ $_->unlock foreach @successes;
return 0;
}
push @successes, $locker;
}
1;
}
sub unlock()
{ my $self = shift;
return $self unless $self->{MBL_has_lock};
$_->unlock foreach $self->lockers;
delete $self->{MBL_has_lock};
$self;
}
sub lock()
{ my $self = shift;
return 1 if $self->hasLock;
my $end = $self->{MBL_timeout} eq 'NOTIMEOUT' ? -1 : $self->{MBL_timeout};
while(1)
{ return $self->{MBL_has_lock} = 1
if $self->_try_lock;
last unless --$end;
sleep 1;
}
return 0;
}
sub isLocked()
{ my $self = shift;
$self->_try_lock($self->filename) or return 0;
$self->unlock;
1;
}
#-------------------------------------------
sub lockers() { @{shift->{MBLM_lockers}} }
1;
|