ConcurrencyLimit/Lock/Flock.pm

filter: Statementstmt 95.5% 64 / 67 Branchbran 80.7% 21 / 26 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 85.7% 12 / 14 Podpod 100% 3 / 3 totaltotal 91.1% 103 / 113 CC 15 SCAR 15.9
CC
15
Cov
92%
new
9.4
_get_lock
6.0
lock_file
2.0
CRAP 4.9
« ConcurrencyLimit/WithStandby.pm ↑ Summary ConcurrencyLimit.pm »
1 package IPC::ConcurrencyLimit::Lock::Flock;
2 9 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use File::Path qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 our $VERSION = '0.17';
12 9 ▶use IPC::ConcurrencyLimit::Lock;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 our @ISA = qw(IPC::ConcurrencyLimit::Lock);
14  
15 399 ▶sub new {
Subroutine new: called CC=9 SCAR=22.4
Pod documented
16 399 my $class = shift;
17 399 my $opt = shift;
18  
19 my $max_procs = $opt->{max_procs}
20 399 ▶ or croak("Need a 'max_procs' parameter");
Branch: 1 branch
BranchTrueFalseTotal
my $max_procs = $opt->{"max_procs"} or croak("Need a 'max_procs' parameter") 399 0 399
21 my $path = $opt->{path}
22 399 ▶ or croak("Need a 'path' parameter");
Branch: 1 branch
BranchTrueFalseTotal
my $path = $opt->{"path"} or croak("Need a 'path' parameter") 399 0 399
23 399 ▶ my $lock_mode = lc($opt->{lock_mode} || 'exclusive');
Condition100%2/2
$opt->{'lock_mode'} || 'exclusive'
l!l
32277
MC/DC100%1/1
$opt->{'lock_mode'} || 'exclusive'
$opt->{'lock_mode'}
Truth table100%2/2
A
A= $opt->{'lock_mode'}
Aresult
11
00
24 399 ▶ if ($lock_mode !~ /^(?:exclusive|shared)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lock_mode =~ /^(?:exclusive|shared)$/) 0 399 399
25 0 croak("Invalid lock mode '$lock_mode'");
26 }
27 399 ▶ my $file_prefix= defined $opt->{file_prefix} ? $opt->{file_prefix} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'file_prefix'} ? : 46 353 399
28 399 ▶ $file_prefix .= "." if length $file_prefix;
Branch: 1 branch
BranchTrueFalseTotal
if length $file_prefix 46 353 399
29 399 ▶ my $file_ext= defined $opt->{file_ext} ? $opt->{file_ext} : "lock";
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'file_ext'} ? : 0 399 399
30 399 $file_ext=~s/^\.?/./;
31  
32 399 my $self = bless {
33 max_procs => $max_procs,
34 path => $path,
35 lock_fh => undef,
36 lock_file => undef,
37 id => undef,
38 file_prefix => $file_prefix,
39 file_ext => $file_ext,
40 lock_mode => $lock_mode,
41 } => $class;
42  
43 399 ▶ $self->_get_lock() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_get_lock or return undef 72 327 399
44  
45 72 return $self;
46 }
47  
48 399 ▶sub _get_lock {
Subroutine _get_lock: called CC=6 SCAR=17.9
49 399 my $self = shift;
50  
51 399 File::Path::mkpath($self->{path});
52 399 ▶ my $lock_mode_flag = $self->{lock_mode} eq 'shared' ? LOCK_SH : LOCK_EX;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'lock_mode'} eq 'shared' ? : 4 395 399
53  
54 # We try in reverse order, so that if a processor is started with
55 # a higher number of allowed locks there is less chance that it starves
56 # a processor with a lower number of allowed locks.
57 399 my $worker_id = $self->{max_procs};
58 399 ▶ while ($worker_id > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($worker_id > 0) 612 327 939
59 612 my $lock_file = File::Spec->catfile($self->{path}, join("", $self->{file_prefix}, $worker_id, $self->{file_ext}));
60  
61 612 ▶ sysopen(my $fh, $lock_file, O_RDWR|O_CREAT)
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $lock_file, 66 or die "can't open '${lock_file}': $!" 612 0 612
62 or die "can't open '$lock_file': $!";
63  
64 612 ▶ if (flock($fh, $lock_mode_flag|LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $fh, $lock_mode_flag | 4) 72 540 612
65 72 $self->{lock_fh} = $fh;
66 72 seek($fh, 0, 0);
67 72 truncate($fh, 0);
68 72 print $fh $$;
69 72 $fh->flush;
70 72 $self->{id} = $worker_id;
71 72 $self->{lock_file} = $lock_file;
72 72 last;
73 }
74  
75 540 close $fh;
76 540 $worker_id--;
77 }
78  
79 399 ▶ return undef if not $self->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"id"} 327 72 399
80 72 return 1;
81 }
82  
83 0 ▶sub lock_file { $_[0]->{lock_file} }
Subroutine lock_file: not called CC=1 SCAR=6.9
Pod documented
84 0 ▶sub path { $_[0]->{path} }
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
85  
86 399 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
87 399 my $self = shift;
88 # should be superfluous
89 399 ▶ close($self->{lock_fh}) if $self->{lock_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lock_fh"} 72 327 399
90 }
91  
92 9 1;
93  
94 __END__
« ConcurrencyLimit/WithStandby.pm ↑ Summary ConcurrencyLimit.pm »