Box/Locker/Multi.pm

filter: Statementstmt 46.8% 30 / 64 Branchbran 22.7% 5 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 80.0% 4 / 5 totaltotal 44.1% 45 / 102 CC 16 SCAR 25.4
CC
16
Cov
41%
lock
30.0
_try_lock
20.0
unlock
12.0
CRAP 12.7
« Box/Message/Destructed.pm ↑ Summary Box/Locker/DotLock.pm »
1 # Copyrights 2001-2020 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution Mail-Box. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Box::Locker::Multi;
10 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $VERSION = '3.009';
12  
13 2 ▶use base 'Mail::Box::Locker';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 sub init($)
22 1 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=6 SCAR=19.9
Pod undocumented
23 1 $self->SUPER::init($args);
24  
25 my @use
26 1 ▶ = exists $args->{use} ? @{delete $args->{use}}
Branch: 2 branches
BranchTrueFalseTotal
exists $args->{'use'} ? : 0 1 1
$^O eq 'MSWin32' ? : 0 1 1
27 : $^O eq 'MSWin32' ? qw/Flock/
28 : qw/NFS FcntlLock Flock/;
29  
30 1 my (@lockers, @used);
31  
32 1 foreach my $method (@use)
33 3 ▶ { if(UNIVERSAL::isa($method, 'Mail::Box::Locker'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($method, "Mail::Box::Locker")) 0 3 3
34 0 { push @lockers, $method;
35 0 (my $used = ref $method) =~ s/.*\:\://;
36 0 push @used, $used;
37 0 next;
38 }
39  
40 my $locker = eval
41 3 { Mail::Box::Locker->new
42 ( %$args
43 , method => $method
44 , timeout => 1
45 )
46 };
47 3 ▶ next unless defined $locker;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $locker 1 2 3
48  
49 2 push @lockers, $locker;
50 2 push @used, $method;
51 }
52  
53 1 $self->{MBLM_lockers} = \@lockers;
54 1 $self->log(PROGRESS => "Multi-locking via @used.");
55 0 $self;
56 }
57  
58 #-------------------------------------------
59  
60 sub name() {'MULTI'}
61  
62 sub _try_lock($)
63 0 ▶{ my $self = shift;
Subroutine _try_lock: not called CC=4 SCAR=30.0
64 0 my @successes;
65  
66 0 foreach my $locker ($self->lockers)
67 {
68 0 ▶ unless($locker->lock)
Branch: 1 branch
BranchTrueFalseTotal
unless ($locker->lock) 0 0 0
69 0 { $_->unlock foreach @successes;
70 0 return 0;
71 }
72 0 push @successes, $locker;
73 }
74  
75 0 1;
76 }
77  
78 sub unlock()
79 0 ▶{ my $self = shift;
Subroutine unlock: not called CC=3 SCAR=24.8
Pod documented
80 0 ▶ $self->hasLock
Branch: 1 branch
BranchTrueFalseTotal
$self->hasLock or return $self 0 0 0
81 or return $self;
82  
83 0 $_->unlock foreach $self->lockers;
84 0 $self->SUPER::unlock;
85  
86 0 $self;
87 }
88  
89 sub lock()
90 0 ▶{ my $self = shift;
Subroutine lock: not called CC=5 SCAR=34.0
Pod documented
91 0 ▶ return 1 if $self->hasLock;
Branch: 1 branch
BranchTrueFalseTotal
if $self->hasLock 0 0 0
92  
93 0 my $timeout = $self->timeout;
94 0 ▶ my $end = $timeout eq 'NOTIMEOUT' ? -1 : $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$timeout eq 'NOTIMEOUT' ? : 0 0 0
95  
96 0 while(1)
97 0 ▶ { return $self->SUPER::lock
Branch: 1 branch
BranchTrueFalseTotal
if $self->_try_lock 0 0 0
98 if $self->_try_lock;
99  
100 0 ▶ last unless --$end;
Branch: 1 branch
BranchTrueFalseTotal
unless --$end 0 0 0
101 0 sleep 1;
102 }
103  
104 0 return 0;
105 }
106  
107 sub isLocked()
108 0 ▶{ my $self = shift;
Subroutine isLocked: not called CC=2 SCAR=17.9
Pod documented
109  
110 # Try get a lock
111 0 ▶ $self->_try_lock($self->filename) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_try_lock($self->filename) or return 0 0 0 0
112  
113 # and release it immediately
114 0 $self->unlock;
115 0 1;
116 }
117  
118 #-------------------------------------------
119  
120  
121 0 ▶sub lockers() { @{shift->{MBLM_lockers}} }
Subroutine lockers: not called CC=1 SCAR=6.9
Pod documented
122  
123 2 1;
« Box/Message/Destructed.pm ↑ Summary Box/Locker/DotLock.pm »