| 1 | 3 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | package Data::Conveyor::Lock; | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | $Data::Conveyor::Lock::VERSION = '1.103130'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | } | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Stage-based conveyor-belt-like ticket handling system | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Fcntl ':flock'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use constant PREFIX => '.lock.'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use constant GREPPX => '^%s\.lock\.'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use constant SAFETY => 25; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | # This class doesn't use Class::Accessor::*, because it is | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # used mainly within the "launcher" applications that go off | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # up to twice a minute, just to exit immediately afterwards. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | $self->init(@_); | |||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | $self->$_($args{$_}) for keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub numlocks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
numlocks: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $self->{numlocks} = $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_[0] and $_[0] + 0 eq $_[0]
MC/DC0%0/2
defined $_[0] and $_[0] + 0 eq $_[0]
defined $_[0]
$_[0] + 0 eq $_[0]
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] + 0 eq $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | if defined $_[0] | ||||||||||||||||||||||||||||||||||||||||||
| 37 | && $_[0] + 0 eq $_[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | $self->{numlocks}; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub maxlocks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
maxlocks: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $self->{maxlocks} = $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_[0] and $_[0] + 0 eq $_[0]
MC/DC0%0/2
defined $_[0] and $_[0] + 0 eq $_[0]
defined $_[0]
$_[0] + 0 eq $_[0]
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] + 0 eq $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | if defined $_[0] | ||||||||||||||||||||||||||||||||||||||||||
| 45 | && $_[0] + 0 eq $_[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $self->{maxlocks}; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub lockfile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lockfile: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $self->{lockfile} = $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | if $_[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $self->{lockfile}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub lockname { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lockname: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $self->{lockname} = $_[0] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | if $_[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $self->{lockname}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub lockpath { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lockpath: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (defined(my $path = shift)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $path =~ s!/$!!go; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | die sprintf "Can't access lockdirectory '%s'", $path | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$path and -d $path and -w _
$path and -d $path
MC/DC0%0/3
$path and -d $path and -w _
$path
-d $path
-w _
Truth table0%0/4
A and B and C
A= $pathB= -d $pathC= -w _
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | unless $path && -d $path && -w _; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $self->{lockpath} = $path; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $self->{lockpath}; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # call this one from launcher. it will create/remove locks | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # according to the value of the numlocks accessor. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub administrate_locks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
administrate_locks: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | die sprintf "invalid numlocks value: '%s'", $self->numlocks | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $self->numlocks and defined $self->numlocks + 0 and $self->numlocks < $self->maxlocks
defined $self->numlocks and defined $self->numlocks + 0
MC/DC0%0/3
defined $self->numlocks and defined $self->numlocks + 0 and $self->numlocks < $self->maxlocks
defined $self->numlocks
defined $self->numlocks + 0
$self->numlocks < $self->maxlocks
Truth table0%0/4
A and B and C
A= defined $self->numlocksB= defined $self->numlocks + 0C= $self->numlocks < $self->maxlocks
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | unless defined $self->numlocks | ||||||||||||||||||||||||||||||||||||||||||
| 80 | && defined $self->numlocks + 0 | ||||||||||||||||||||||||||||||||||||||||||
| 81 | && $self->numlocks < $self->maxlocks; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | die "invalid lockpath" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | unless defined $self->lockpath; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | die "invalid lockname" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | unless $self->lockname; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my %have; | |||||||||||||||||||||||||||||||||||||||||
| 87 | my %want; | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $expr = sprintf GREPPX, $self->lockname; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $pref = sprintf "%s%s", $self->lockname, PREFIX; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | opendir D, $self->lockpath; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my @locks = grep { $_ =~ $expr } readdir D; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | closedir D; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # be a little more restrictive here. quickly get rid | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # of invalid or superfluous files with our prefix. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $have{ sprintf("%s/%s", $self->lockpath, $_) }++ for (@locks); | |||||||||||||||||||||||||||||||||||||||||
| 97 | $want{ sprintf("%s/%s%02d", $self->lockpath, $pref, $_) }++ | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | for (1 .. $self->numlocks); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | for my $illegal (grep { !exists $want{$_} } keys %have) { | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | unlink $illegal | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | or die sprintf "Can't remove '%s'", $illegal; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | for my $lockfile (sort keys %want) { | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | next if -f $lockfile; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | open my $lock_fh, '>', $lockfile | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | or die sprintf "Can't create lockfile '%s'", | ||||||||||||||||||||||||||||||||||||||||||
| 107 | $lockfile; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | close $lock_fh; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub release_lock { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
release_lock: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | return unless $self->lockfile; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | flock $self->lockfile->[1], LOCK_UN; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | close $self->lockfile->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->{lockfile} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub get_lock { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_lock: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | die "invalid lockpath" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | unless defined $self->lockpath; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | die "invalid lockname" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | unless $self->lockname; | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my $pref = sprintf "%s%s", $self->lockname, PREFIX; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | my $handle; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my $counter = 1; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $lockfile = sprintf "%s/%s%02d", $self->lockpath, $pref, $counter; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | last unless -f $lockfile; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | open $handle, '>', $lockfile; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if (flock $handle, LOCK_EX | LOCK_NB) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $self->lockfile([ $lockfile, $handle ]); | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | close $handle; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | die sprintf "Severe lockcount problem! N=%s", $counter | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | if ++$counter > $self->maxlocks; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | 0; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub lockstate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lockstate: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # the amazing thing here is that the stream doesn't go down | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # if the file is removed. flock and fcntl F_GETFL just keep | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # returning true; and so does a print with autoflush turned | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # on. that's a bit awkward. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($self->lockfile) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | return -f $self->lockfile->[0] | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
-f $self->lockfile->[0] && flock($self->lockfile->[1], 6)
MC/DC0%0/2
-f $self->lockfile->[0] && flock($self->lockfile->[1], 6)
-f $self->lockfile->[0]
flock($self->lockfile->[1], 6)
Truth table0%0/3
A && B
A= -f $self->lockfile->[0]B= flock($self->lockfile->[1], 6)
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | && flock $self->lockfile->[1], LOCK_EX | LOCK_NB; | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | return $self->get_lock; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||||||||||||||||