lib/Narada/Lock.pm

filter: Statementstmt 96.6% 87 / 90 Branchbran 57.5% 23 / 40 Conditioncond 40.0% 2 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 23 / 23 Podpod 100% 5 / 5 totaltotal 84.9% 141 / 166 CC 23 SCAR 17.4
CC
23
Cov
83%
shared_lock
17.0
exclusive_lock
7.3
child_inherit_lock
5.3
CRAP 5.7
« blib/script/narada-install ↑ Summary blib/script/narada-setup-cron »
1 package Narada::Lock;
2  
3 227 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 227 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 227 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 227 our $VERSION = 'v2.3.8';
8  
9 227 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 227 ▶use Narada;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 227 ▶use Fcntl qw( :DEFAULT :flock F_SETFD FD_CLOEXEC );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 227 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 227 ▶use Time::HiRes qw( sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 227 ▶use constant IS_NARADA1 => eval { local $SIG{__DIE__}; Narada::detect('narada-1') } || undef;
Subroutine BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
eval { do { local $SIG{'__DIE__'}; Narada::detect('narada-1') } } || undef
l!l
75152
MC/DC100%1/1
eval { do { local $SIG{'__DIE__'}; Narada::detect('narada-1') } } || undef
eval { do { local $SIG{'__DIE__'}; Narada::detect('narada-1') } }
Truth table100%2/2
A
A= eval { do { local $SIG{'__DIE__'}; Narada::detect('narada-1') } }
Aresult
11
00
17 227 ▶use constant DIR => IS_NARADA1 ? 'var/' : q{};
Subroutine BEGIN: called CC=1 SCAR=0.0
18 227 ▶use constant LOCKNEW => DIR.'.lock.new';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 227 ▶use constant LOCKFILE => DIR.'.lock';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 227 ▶use constant TICK => 0.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 227 my $F_lock;
23  
24  
25 79 ▶sub shared_lock :Export {
Subroutine shared_lock: called CC=8 SCAR=28.3
Pod documented
26 79 my $timeout = shift;
27 79 ▶ return 1 if $ENV{NARADA_SKIP_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 2 77 79
28 77 ▶ sysopen $F_lock, LOCKFILE, O_RDONLY|O_CREAT or croak "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen $F_lock, ".lock", 64 or croak("open: $!") 77 0 77
29 77 while (1) {
30 77 ▶ next if -e LOCKNEW;
Branch: 1 branch
BranchTrueFalseTotal
if -e ".lock.new" 0 77 77
31 77 ▶ last if flock $F_lock, LOCK_SH|LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
if flock $F_lock, 5 77 0 77
32 0 ▶ $!{EWOULDBLOCK} or croak "flock: $!";
Branch: 1 branch
BranchTrueFalseTotal
$!{"EWOULDBLOCK"} or croak("flock: $!") 0 0 0
33 } continue {
34 0 ▶ return if defined $timeout and (($timeout-=TICK) < TICK);
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout and ($timeout -= "0.1") < "0.1" 0 0 0
Condition0%0/3
defined $timeout and ($timeout -= "0.1") < "0.1"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $timeout and ($timeout -= "0.1") < "0.1"
defined $timeout ($timeout -= "0.1") < "0.1"
Truth table0%0/3
A and B
A= defined $timeoutB= ($timeout -= "0.1") < "0.1"
ABresult
0X0
100
111
35 0 sleep TICK;
36 }
37 77 return 1;
38 227 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 127 ▶sub exclusive_lock :Export {
Subroutine exclusive_lock: called CC=7 SCAR=19.9
Pod documented
41 127 ▶ return if $ENV{NARADA_SKIP_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 2 125 127
42 125 ▶ sysopen $F_lock, LOCKFILE, O_WRONLY|O_CREAT or croak "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen $F_lock, ".lock", 65 or croak("open: $!") 125 0 125
43 125 while (1) {
44 149 ▶ last if flock $F_lock, LOCK_EX|LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
if flock $F_lock, 6 125 24 149
45 24 ▶ $!{EWOULDBLOCK} or croak "flock: $!";
Branch: 1 branch
BranchTrueFalseTotal
$!{"EWOULDBLOCK"} or croak("flock: $!") 24 0 24
46 24 ▶ system('touch', LOCKNEW) == 0 or croak "touch: $!/$?";
Branch: 1 branch
BranchTrueFalseTotal
system("touch", ".lock.new") == 0 or croak("touch: $!/$?") 24 0 24
47 24 sleep TICK;
48 }
49 125 ▶ system('touch', LOCKNEW) == 0 or croak "touch: $!/$?";
Branch: 1 branch
BranchTrueFalseTotal
system("touch", ".lock.new") == 0 or croak("touch: $!/$?") 125 0 125
50 125 return;
51 227 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 121 ▶sub unlock_new :Export {
Subroutine unlock_new: called CC=2 SCAR=6.9
Pod documented
54 121 ▶ return if $ENV{NARADA_SKIP_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 2 119 121
55 119 unlink LOCKNEW;
56 119 return;
57 227 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 67 ▶sub unlock :Export {
Subroutine unlock: called CC=4 SCAR=14.9
Pod documented
60 67 ▶ return if $ENV{NARADA_SKIP_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 0 67 67
61 67 ▶ if ($F_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_lock) 67 0 67
62 67 ▶ flock $F_lock, LOCK_UN or croak "flock: $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $F_lock, 8 or croak("flock: $!") 67 0 67
63 }
64 67 return;
65 227 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 12 ▶sub child_inherit_lock :Export {
Subroutine child_inherit_lock: called CC=5 SCAR=16.7
Pod documented
68 12 my ($is_inherit) = @_;
69 12 ▶ return if $ENV{NARADA_SKIP_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 2 10 12
70 10 ▶ if ($F_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_lock) 10 0 10
71 10 ▶ fcntl $F_lock, F_SETFD, $is_inherit ? 0 : FD_CLOEXEC or croak "fcntl: $!";
Branch: 2 branches
BranchTrueFalseTotal
fcntl $F_lock, 2, $is_inherit ? 0 : 1 or croak("fcntl: $!") 10 0 10
$is_inherit ? : 10 0 10
72 }
73 10 return;
74 227 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76  
77 227 1; # Magic true value required at end of module
78 __END__
« blib/script/narada-install ↑ Summary blib/script/narada-setup-cron »