Mutex/Flock.pm

filter: Statementstmt 48.5% 50 / 103 Branchbran 17.8% 15 / 84 Conditioncond 29.6% 8 / 27 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 47.3% 9 / 19 Podpod 100% 7 / 7 totaltotal 35.1% 90 / 256 CC 49 SCAR 35.9
CC
49
Cov
34%
synchronize
72.0
timedwait
72.0
new
54.9
CRAP 36.3
« Mutex/Channel.pm ↑ Summary Mutex.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Mutex::Flock - Mutex locking via Fcntl.
4 ##
5 ###############################################################################
6  
7 package Mutex::Flock;
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶no warnings qw( threads recursion uninitialized once );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '1.011';
15  
16 2 ▶use base 'Mutex';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Time::HiRes 'alarm';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶my $tid = $INC{'threads.pm'} ? threads->tid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 2 2
22  
23 0 ▶sub CLONE {
Subroutine CLONE: not called CC=2 SCAR=17.9
24 0 ▶ $tid = threads->tid() if $INC{'threads.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 0 0
25 }
26  
27 0 ▶sub Mutex::Flock::_guard::DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
28 0 my ($pid, $obj) = @{ $_[0] };
29 0 ▶ CORE::flock ($obj->{_fh}, LOCK_UN), $obj->{ $pid } = 0 if $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$pid} 0 0 0
30  
31 0 return;
32 }
33  
34 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=16.7
35 2 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, @_);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 2 2
36 2 ▶ $obj->unlock(), close(delete $obj->{_fh}) if $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$pid} 0 2 2
37 2 ▶ unlink $obj->{path} if ($obj->{_init} && $obj->{_init} eq $pid);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"_init"} and $obj->{"_init"} eq $pid 1 1 2
Condition66%2/3
$obj->{"_init"} and $obj->{"_init"} eq $pid
!ll&&!rl&&r
101
MC/DC50%1/2
$obj->{"_init"} and $obj->{"_init"} eq $pid
$obj->{"_init"} $obj->{"_init"} eq $pid
Truth table66%2/3
A and B
A= $obj->{"_init"}B= $obj->{"_init"} eq $pid
ABresult
0X0
100
111
38  
39 2 return;
40 }
41  
42 0 ▶sub _open {
Subroutine _open: not called CC=4 SCAR=30.0
43 0 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, @_);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
44 0 ▶ return if exists $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
if exists $obj->{$pid} 0 0 0
45  
46 open $obj->{_fh}, '+>>:raw:stdio', $obj->{path}
47 0 ▶ or Carp::croak("Could not create temp file $obj->{path}: $!");
Branch: 1 branch
BranchTrueFalseTotal
open $obj->{"_fh"}, "+>>:raw:stdio", $obj->{"path"} or Carp::croak(qq[Could not create temp file $obj->{"path"}: $!]) 0 0 0
48  
49 0 return;
50 }
51  
52 ###############################################################################
53 ## ----------------------------------------------------------------------------
54 ## Public methods.
55 ##
56 ###############################################################################
57  
58 2 my ($id, $prog_name) = (0);
59  
60 2 $prog_name = $0;
61 2 $prog_name =~ s{^.*[\\/]}{}g;
62 2 ▶$prog_name = 'perl' if ($prog_name eq '-e' || $prog_name eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $prog_name eq "-e" or $prog_name eq "-" 0 2 2
Condition33%1/3
$prog_name eq "-e" or $prog_name eq "-"
l!l&&r!l&&!r
002
MC/DC0%0/2
$prog_name eq "-e" or $prog_name eq "-"
$prog_name eq "-e" $prog_name eq "-"
Truth table33%1/3
A or B
A= $prog_name eq "-e"B= $prog_name eq "-"
ABresult
1X1
011
000
63  
64 2 ▶sub new {
Subroutine new: called CC=17 SCAR=40.1
Pod documented
65 2 my ($class, %obj) = (@_, impl => 'Flock');
66  
67 2 ▶ if (! defined $obj{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $obj{'path'}) { } 1 1 2
68 1 my ($pid, $tmp_dir, $tmp_file) = ( abs($$) );
69  
70 1 ▶ if ($ENV{TEMP} && -d $ENV{TEMP} && -w _) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ENV{'TEMP'} and -d $ENV{'TEMP'} and -w _) { } 0 1 1
elsif ($ENV{'TMPDIR'} and -d $ENV{'TMPDIR'} and -w _) { } 0 1 1
elsif (-d '/tmp' and -w _) { } 1 0 1
Condition33%2/6
$ENV{"TEMP"} and -d $ENV{"TEMP"} and -w _
!ll&&!rl&&r
100
$ENV{"TEMP"} and -d $ENV{"TEMP"}
!ll&&!rl&&r
100
MC/DC0%0/3
$ENV{"TEMP"} and -d $ENV{"TEMP"} and -w _
$ENV{"TEMP"} -d $ENV{"TEMP"} -w _
Truth table25%1/4
A and B and C
A= $ENV{"TEMP"}B= -d $ENV{"TEMP"}C= -w _
ABCresult
0XX0
10X0
1100
1111
71 0 ▶ if ($^O =~ /mswin|mingw|msys|cygwin/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /mswin|mingw|msys|cygwin/i) { } 0 0 0
72 0 $tmp_dir = $ENV{TEMP};
73 0 ▶ $tmp_dir .= ($^O eq 'MSWin32') ? "\\Perl-MCE" : "/Perl-MCE";
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
74 0 ▶ mkdir $tmp_dir unless (-d $tmp_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $tmp_dir 0 0 0
75 }
76 else {
77 0 $tmp_dir = $ENV{TEMP};
78 }
79 }
80 ▶ elsif ($ENV{TMPDIR} && -d $ENV{TMPDIR} && -w _) {
Condition33%2/6
$ENV{"TMPDIR"} and -d $ENV{"TMPDIR"} and -w _
!ll&&!rl&&r
100
$ENV{"TMPDIR"} and -d $ENV{"TMPDIR"}
!ll&&!rl&&r
100
MC/DC0%0/3
$ENV{"TMPDIR"} and -d $ENV{"TMPDIR"} and -w _
$ENV{"TMPDIR"} -d $ENV{"TMPDIR"} -w _
Truth table25%1/4
A and B and C
A= $ENV{"TMPDIR"}B= -d $ENV{"TMPDIR"}C= -w _
ABCresult
0XX0
10X0
1100
1111
81 0 $tmp_dir = $ENV{TMPDIR};
82 }
83 ▶ elsif (-d '/tmp' && -w _) {
Condition33%1/3
-d "/tmp" and -w _
!ll&&!rl&&r
001
MC/DC0%0/2
-d "/tmp" and -w _
-d "/tmp" -w _
Truth table33%1/3
A and B
A= -d "/tmp"B= -w _
ABresult
0X0
100
111
84 1 $tmp_dir = '/tmp';
85 }
86 else {
87 0 Carp::croak("No writable dir found for a temp file");
88 }
89  
90 1 $id++, $tmp_dir =~ s{[\\/]$}{};
91  
92 # remove tainted'ness from $tmp_dir
93 1 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 1 1
94 0 ($tmp_file) = "$tmp_dir\\$prog_name.$pid.$tid.$id" =~ /(.*)/;
95 } else {
96 1 ($tmp_file) = "$tmp_dir/$prog_name.$pid.$tid.$id" =~ /(.*)/;
97 }
98  
99 1 ▶ $obj{_init} = $tid ? $$ .'.'. $tid : $$;
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 1 1
100 1 $obj{ path} = $tmp_file.'.lock';
101  
102 # test open
103 open my $fh, '+>>:raw:stdio', $obj{path}
104 1 ▶ or Carp::croak("Could not create temp file $obj{path}: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+>>:raw:stdio", $obj{"path"} or Carp::croak(qq[Could not create temp file $obj{"path"}: $!]) 1 0 1
105  
106 1 close $fh;
107  
108 # set permission
109 1 chmod 0600, $obj{path};
110 }
111 else {
112 # test open
113 open my $fh, '+>>:raw:stdio', $obj{path}
114 1 ▶ or Carp::croak("Could not obtain flock on file $obj{path}: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+>>:raw:stdio", $obj{"path"} or Carp::croak(qq[Could not obtain flock on file $obj{"path"}: $!]) 1 0 1
115  
116 1 close $fh;
117 }
118  
119 2 return bless(\%obj, $class);
120 }
121  
122 0 ▶sub lock {
Subroutine lock: not called CC=4 SCAR=30.0
Pod documented
123 0 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
124 0 ▶ $obj->_open() unless exists $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{$pid} 0 0 0
125  
126 CORE::flock ($obj->{_fh}, LOCK_EX), $obj->{ $pid } = 1
127 0 ▶ unless $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{$pid} 0 0 0
128  
129 0 return;
130 }
131  
132 0 ▶sub guard_lock {
Subroutine guard_lock: not called CC=2 SCAR=17.9
Pod documented
133 0 &lock(@_);
134 0 ▶ bless([ $tid ? $$ .'.'. $tid : $$, $_[0] ], Mutex::Flock::_guard::);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
135 }
136  
137 2 *lock_exclusive = \&lock;
138  
139 0 ▶sub lock_shared {
Subroutine lock_shared: not called CC=4 SCAR=30.0
Pod documented
140 0 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
141 0 ▶ $obj->_open() unless exists $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{$pid} 0 0 0
142  
143 CORE::flock ($obj->{_fh}, LOCK_SH), $obj->{ $pid } = 1
144 0 ▶ unless $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{$pid} 0 0 0
145  
146 0 return;
147 }
148  
149 0 ▶sub unlock {
Subroutine unlock: not called CC=3 SCAR=24.8
Pod documented
150 0 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
151  
152 CORE::flock ($obj->{_fh}, LOCK_UN), $obj->{ $pid } = 0
153 0 ▶ if $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$pid} 0 0 0
154  
155 0 return;
156 }
157  
158 0 ▶sub synchronize {
Subroutine synchronize: not called CC=8 SCAR=42.8
Pod documented
159 0 ▶ my ($pid, $obj, $code) = ($tid ? $$ .'.'. $tid : $$, shift, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
160 0 my (@ret);
161  
162 0 ▶ return unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
163  
164 0 ▶ $obj->_open() unless exists $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{$pid} 0 0 0
165  
166 # lock, run, unlock - inlined for performance
167 0 my $guard = bless([ $pid, $obj ], Mutex::Flock::_guard::);
168 0 ▶ unless ($obj->{ $pid }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{$pid}) 0 0 0
169 0 CORE::flock ($obj->{_fh}, LOCK_EX), $obj->{ $pid } = 1;
170 }
171 (defined wantarray)
172 0 ▶ ? @ret = wantarray ? $code->(@_) : scalar $code->(@_)
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
wantarray ? : 0 0 0
173 : $code->(@_);
174  
175 0 ▶ return wantarray ? @ret : $ret[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
176 }
177  
178 2 *enter = \&synchronize;
179  
180 0 ▶sub timedwait {
Subroutine timedwait: not called CC=8 SCAR=42.8
Pod documented
181 0 my ($obj, $timeout) = @_;
182 0 ▶ die 'Mutex::Flock::timedwait() unimplemented in this platform'
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
183 if ($^O eq 'MSWin32');
184  
185 0 ▶ $timeout = 1 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
186 0 ▶ Carp::croak('Mutex::Flock: timedwait (timeout) is not valid')
Branch: 1 branch
BranchTrueFalseTotal
if not &looks_like_number($timeout) or $timeout < 0 0 0 0
Condition0%0/3
not &looks_like_number($timeout) or $timeout < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not &looks_like_number($timeout) or $timeout < 0
not &looks_like_number($timeout) $timeout < 0
Truth table0%0/3
not(A) or B
A= not &looks_like_number($timeout)B= $timeout < 0
ABresult
1X1
011
000
187 if (!looks_like_number($timeout) || $timeout < 0);
188  
189 0 ▶ $timeout = 0.0003 if $timeout < 0.0003;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < "0.0003" 0 0 0
190  
191 0 ▶ local $@; local $SIG{ALRM} = sub { alarm 0; die "timed out\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 eval { alarm $timeout; $obj->lock_exclusive };
193 0 alarm 0;
194  
195 0 ▶ ( $@ && $@ eq "timed out\n" ) ? '' : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ && $@ eq "timed out\n" ? : 0 0 0
Condition0%0/3
$@ and $@ eq "timed out\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $@ eq "timed out\n"
$@ $@ eq "timed out\n"
Truth table0%0/3
A and B
A= $@B= $@ eq "timed out\n"
ABresult
0X0
100
111
196 }
197  
198 2 1;
199  
200 __END__
« Mutex/Channel.pm ↑ Summary Mutex.pm »