| 1 | package File::Lock::Multi::Base; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use 5.006000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings (FATAL => 'all'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Class::Accessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use base q(Class::Accessor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Time::HiRes qw(sleep); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Params::Validate; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Params::Classify qw(is_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 8 | __PACKAGE__->mk_accessors(qw(max name timeout polling_interval)); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | return 1; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # rename file -> name to make more sense with virtual resources | ||||||||||||||||||||||||||||
| 18 | 367 | ▶ | sub file { &name } | ||||||||||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub __Validators { | ||||||||||||||||||||||||||||
| 21 | 26 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
__Validators: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $float_spec = { optional => 1, callbacks => { number => sub { | ||||||||||||||||||||||||||||
| 24 | 20 | ▶ | is_number(shift) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | } } | ||||||||||||||||||||||||||||
| 26 | 26 | };; | |||||||||||||||||||||||||||
| 27 | 26 | my $integer_spec = { optional => 1, regex => qr/^\d+$/ }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | return( | ||||||||||||||||||||||||||||
| 30 | 26 | name => 1, | |||||||||||||||||||||||||||
| 31 | polling_interval => $float_spec, | ||||||||||||||||||||||||||||
| 32 | timeout => $float_spec, | ||||||||||||||||||||||||||||
| 33 | max => $integer_spec, | ||||||||||||||||||||||||||||
| 34 | @_ | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub new { | ||||||||||||||||||||||||||||
| 40 | 28 | ▶ | my($class, %args_in) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 28 | (my $subclass = __PACKAGE__) =~ s{::Base$}{}; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 28 | ▶ | $args_in{name} = delete $args_in{file} if exists $args_in{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | # silliness to accomodate Params::Validate | ||||||||||||||||||||||||||||
| 45 | 28 | my @args_in = %args_in; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 28 | ▶ | croak "$class is a base class; please find a suitable subclass to use" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$class eq __PACKAGE__ or $class eq $subclass
MC/DC50%1/2
$class eq __PACKAGE__ or $class eq $subclass
$class eq __PACKAGE__
$class eq $subclass
Truth table66%2/3
A or B
A= $class eq __PACKAGE__B= $class eq $subclass
| |||||||||||||||||||||||||||||
| 48 | if $class eq __PACKAGE__ || $class eq $subclass; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 26 | my %validate_spec = $class->__Validators; | |||||||||||||||||||||||||||
| 51 | 26 | my %args = validate(@args_in, \%validate_spec); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 24 | ▶ | $args{polling_interval} ||= 0.2; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"polling_interval"} ||= "0.2"
MC/DC100%1/1
$args{"polling_interval"} ||= "0.2"
$args{"polling_interval"}
Truth table100%2/2
A
A= $args{"polling_interval"}
| |||||||||||||||||||||||||||||
| 54 | 24 | ▶ | $args{timeout} = -1 unless defined $args{timeout}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 24 | ▶ | $args{max} ||= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"max"} ||= 1
MC/DC100%1/1
$args{"max"} ||= 1
$args{"max"}
Truth table100%2/2
A
A= $args{"max"}
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 24 | return $class->SUPER::new(\%args); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub lockable { | ||||||||||||||||||||||||||||
| 61 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
lockable: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | if($self->lock(0)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 4 | return $self->release; | |||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 6 | return; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub _lock_non_block { | ||||||||||||||||||||||||||||
| 70 | 114 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_lock_non_block: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 114 | ▶ | croak("i already have a lock on ", $self->file) if $self->locked; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 112 | ▶ | if(my $id = $self->_lock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 71 | ▶ | if($self->lockers > $self->max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 39 | $self->release; | |||||||||||||||||||||||||||
| 77 | 39 | return; | |||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 32 | return $id; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 41 | return; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub lock { | ||||||||||||||||||||||||||||
| 87 | 50 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 50 | ▶ | my $timeout = scalar(@_) ? shift : $self->timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 50 | ▶ | return $self->_lock_non_block unless $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 20 | my $polling_interval = $self->polling_interval; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 20 | ▶ | if($timeout < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 8 | while(1) { | |||||||||||||||||||||||||||
| 96 | 38 | ▶ | if(my $id = $self->_lock_non_block) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 8 | return $id; | |||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||
| 99 | 30 | sleep($polling_interval); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||
| 103 | 12 | my $cycles = $timeout / $polling_interval; | |||||||||||||||||||||||||||
| 104 | 12 | ▶ | if($cycles < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 2 | $cycles = 1; | |||||||||||||||||||||||||||
| 106 | 2 | $polling_interval = $timeout; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 12 | ▶ | while($cycles) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 46 | ▶ | if(my $id = $self->_lock_non_block) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 4 | return $id; | |||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||
| 113 | 42 | sleep($polling_interval); | |||||||||||||||||||||||||||
| 114 | 42 | $cycles --; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | sub release { | ||||||||||||||||||||||||||||
| 121 | 75 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
release: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | 75 | ▶ | croak("i do not have a lock on ", $self->file) unless $self->locked; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 71 | return $self->_release; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub DESTROY { | ||||||||||||||||||||||||||||
| 127 | 24 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 128 | 24 | ▶ | $self->release if $self->locked; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||