| 1 | package AnyEvent::FileLock; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use AE; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Fcntl (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Method::WeakCallback qw(weak_method_callback); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | sub flock { | ||||||||||||||||||||||||||||||||
|
Subroutine
flock: called CC=19 SCAR=34.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 15 | 3 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | my $type = delete $opts{type} // 'flock'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts{"type"} // "flock"
MC/DC0%0/1
delete $opts{"type"} // "flock"
delete $opts{"type"}
Truth table50%1/2
A
A= delete $opts{"type"}
| |||||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | $type =~ /^(?:flock|fcntl)$/ or croak "invalid lock type '$type'"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | my $mode = delete $opts{mode} // '+<'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts{"mode"} // "+<"
MC/DC0%0/1
delete $opts{"mode"} // "+<"
delete $opts{"mode"}
Truth table50%1/2
A
A= delete $opts{"mode"}
| |||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | my $lock_mode = delete $opts{lock_mode} // $mode; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $opts{"lock_mode"} // $mode
MC/DC0%0/2
delete $opts{"lock_mode"} // $mode
delete $opts{"lock_mode"}
$mode
Truth table33%1/3
A // B
A= delete $opts{"lock_mode"}B= $mode
| |||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | my $delay = delete $opts{delay} || 0.1; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts{'delay'} || 0.1
MC/DC0%0/1
delete $opts{'delay'} || 0.1
delete $opts{'delay'}
Truth table50%1/2
A
A= delete $opts{'delay'}
| |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | my $user_cb = delete $opts{cb} // croak "cb argument is missing"; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts{"cb"} // croak("cb argument is missing")
MC/DC0%0/1
delete $opts{"cb"} // croak("cb argument is missing")
delete $opts{"cb"}
Truth table50%1/2
A
A= delete $opts{"cb"}
| |||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | 3 | my $max_time; | |||||||||||||||||||||||||||||||||
| 27 | 3 | ▶ | if (defined(my $timeout = delete $opts{timeout})) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 28 | 1 | $max_time = AE::now() + $timeout; | |||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 3 | my $fh; | |||||||||||||||||||||||||||||||||
| 32 | 3 | my $file = delete $opts{file}; | |||||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | if (defined $file) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | my $open_mode = delete $opts{open_mode} // $mode; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $opts{"open_mode"} // $mode
MC/DC0%0/2
delete $opts{"open_mode"} // $mode
delete $opts{"open_mode"}
$mode
Truth table33%1/3
A // B
A= delete $opts{"open_mode"}B= $mode
| |||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | $open_mode =~ /^\+?(?:<|>>?)/ or croak "bad mode specification"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | open $fh, $mode, $file or return | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | $fh = delete $opts{fh} // croak "file or fh argument is required"; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts{"fh"} // croak("file or fh argument is required")
MC/DC0%0/1
delete $opts{"fh"} // croak("file or fh argument is required")
delete $opts{"fh"}
Truth table50%1/2
A
A= delete $opts{"fh"}
| |||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 3 | my $self = { file => $file, | |||||||||||||||||||||||||||||||||
| 43 | fh => $fh, | ||||||||||||||||||||||||||||||||||
| 44 | type => $type, | ||||||||||||||||||||||||||||||||||
| 45 | max_time => $max_time, | ||||||||||||||||||||||||||||||||||
| 46 | user_cb => $user_cb, | ||||||||||||||||||||||||||||||||||
| 47 | delay => $delay }; | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 3 | bless $self, $class; | |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if ($type eq 'fcntl') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 52 | $self->{$_} = delete($opts{lock_start}) // 0 | ||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | for qw(lock_start lock_whence lock_len); | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
delete $opts{"lock_start"} // 0
MC/DC0%0/1
delete $opts{"lock_start"} // 0
delete $opts{"lock_start"}
Truth table0%0/2
A
A= delete $opts{"lock_start"}
| |||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $self->{operation} = ($lock_mode eq '<' ? Fcntl::F_RDLCK() : Fcntl::F_WRLCK()); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | $self->{operation} = ($lock_mode eq '<' ? Fcntl::LOCK_SH() : Fcntl::LOCK_EX()); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | %opts and croak "unkwnown arguments found (".join(', ', sort keys %opts).")"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | 3 | my $alcb = $self->{acquire_lock_cb} = weak_method_callback($self, '_acquire_lock'); | |||||||||||||||||||||||||||||||||
| 64 | 3 | &AE::postpone($alcb); | |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 3 | $self; | |||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | 16 | ▶ | sub _acquire_lock { | ||||||||||||||||||||||||||||||||
|
Subroutine
_acquire_lock: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||||||||
| 70 | 16 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 71 | 16 | my $operation = $self->{opertation}; | |||||||||||||||||||||||||||||||||
| 72 | 16 | my $now = AE::now; | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 16 | my $ok; | |||||||||||||||||||||||||||||||||
| 75 | 16 | ▶ | if ($self->{type} eq 'flock') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 76 | 16 | $ok = CORE::flock($self->{fh}, $self->{operation}|Fcntl::LOCK_NB()); | |||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||||||||
| 79 | 0 | require Fcntl::Packer; | |||||||||||||||||||||||||||||||||
| 80 | 0 | my %flock = (type => $self->{operation}); | |||||||||||||||||||||||||||||||||
| 81 | 0 | $flock{$_} = $self->{"lock_$_"} for qw(whence start len); | |||||||||||||||||||||||||||||||||
| 82 | 0 | $ok = fcntl($self->{fh}, Fcntl::F_SETLK, Fcntl::Packer::pack_fcntl_flock(\%flock)); | |||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||
| 84 | 16 | ▶ | if ($ok) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 85 | 2 | $self->{user_cb}->($self->{fh}); | |||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||
| 87 | ▶ | elsif ($! == Errno::EAGAIN() and | |||||||||||||||||||||||||||||||||
|
Condition83%5/6
$! == &Errno::EAGAIN() and !defined($self->{"max_time"}) || $self->{"max_time"} >= $now
not defined $self->{"max_time"} or $self->{"max_time"} >= $now
MC/DC66%2/3
$! == &Errno::EAGAIN() and !defined($self->{"max_time"}) || $self->{"max_time"} >= $now
$! == &Errno::EAGAIN()
not defined $self->{"max_time"}
$self->{"max_time"} >= $now
Truth table75%3/4
A and not(B) or C
A= $! == &Errno::EAGAIN()B= not defined $self->{"max_time"}C= $self->{"max_time"} >= $now
| |||||||||||||||||||||||||||||||||||
| 88 | (!defined($self->{max_time}) or $self->{max_time} >= $now)) { | ||||||||||||||||||||||||||||||||||
| 89 | # we add some randomness into the delay to avoid the case | ||||||||||||||||||||||||||||||||||
| 90 | # where all the contenders follow exactly the same pattern so | ||||||||||||||||||||||||||||||||||
| 91 | # that they end looking for the pattern all at once every time | ||||||||||||||||||||||||||||||||||
| 92 | # (and obviosly all but one failing). | ||||||||||||||||||||||||||||||||||
| 93 | 13 | $self->{timer} = &AE::timer($self->{delay} * (0.8 + rand 0.40), 0, $self->{acquire_lock_cb}); | |||||||||||||||||||||||||||||||||
| 94 | 13 | return; | |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||||||||
| 97 | 1 | $self->{user_cb}->(); | |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | # release all the references, the object is useless from this | ||||||||||||||||||||||||||||||||||
| 100 | # point on time. | ||||||||||||||||||||||||||||||||||
| 101 | 3 | %$self = (); | |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||||||||