| 1 | package Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore; | ||||||||||||||||||||||||||||||||
| 2 | 39 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 39 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 39 | ▶ | use POSIX qw(O_WRONLY O_CREAT O_NONBLOCK O_NOCTTY); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | use IPC::SysV | ||||||||||||||||||||||||||||||||
| 7 | 39 | ▶ | qw(ftok IPC_NOWAIT IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR S_IRGRP S_IWGRP S_IROTH S_IWOTH SEM_UNDO); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 39 | ▶ | use IPC::Semaphore; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 39 | our @EXPORT_OK = qw(semaphore); | |||||||||||||||||||||||||||||||
| 10 | 39 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 39 | ▶ | use constant DEBUG => $ENV{MOJO_PROCESS_DEBUG}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | has key => sub { shift->_genkey }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | has _sem => sub { $_[0]->_create(shift->key) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 15 | 39 | has count => 1; | |||||||||||||||||||||||||||||||
| 16 | 39 | has _value => 1; | |||||||||||||||||||||||||||||||
| 17 | 39 | has flags => IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | |||||||||||||||||||||||||||||||
| 18 | | S_IROTH | S_IWOTH; | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub semaphore { __PACKAGE__->new(@_) } | ||||||||||||||||||||||||||||||
|
Subroutine
semaphore: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub _genkey { ftok($0, 0) } | ||||||||||||||||||||||||||||||
|
Subroutine
_genkey: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | # The following is an adaptation over IPC::Semaphore::Concurrency | ||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub _create { | ||||||||||||||||||||||||||||||
|
Subroutine
_create: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 26 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | # Try acquiring already existing semaphore | ||||||||||||||||||||||||||||||||
| 29 | 0 | my $sem = IPC::Semaphore->new($key, $self->count, 0); | |||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | unless (defined $sem) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 0 | warn "[debug:$$] Create semaphore $key" if DEBUG; | |||||||||||||||||||||||||||||||
| 32 | 0 | $sem = IPC::Semaphore->new($key, $self->count, $self->flags); | |||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | confess 'Semaphore creation failed! ' unless defined($sem); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 0 | $sem->setall($self->_value); | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | 0 | return $sem; | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub acquire { | ||||||||||||||||||||||||||||||
|
Subroutine
acquire: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my %args = @_ % 2 == 0 ? @_ : @_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : (); | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | # Defaults | ||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $args{'sem'} = 0 unless defined($args{'sem'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $args{'wait'} = 0 unless defined($args{'wait'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $args{'max'} = -1 unless defined($args{'max'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $args{'undo'} = 0 unless defined($args{'undo'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 0 | warn "[debug:$$] Acquire semaphore " . $self->key if DEBUG; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 0 | my $sem = $self->_sem; | |||||||||||||||||||||||||||||||
| 51 | 0 | my $flags = IPC_NOWAIT; | |||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $flags |= SEM_UNDO if ($args{'undo'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($args{'wait'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 0 | my $ncnt = $self->getncnt($args{'sem'}); | |||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return if ($args{'max'} >= 0 && $ncnt >= $args{'max'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"max"} >= 0 and $ncnt >= $args{"max"}
MC/DC0%0/2
$args{"max"} >= 0 and $ncnt >= $args{"max"}
$args{"max"} >= 0
$ncnt >= $args{"max"}
Truth table0%0/3
A and B
A= $args{"max"} >= 0B= $ncnt >= $args{"max"}
| |||||||||||||||||||||||||||||||||
| 57 | 0 | warn "[debug:$$] Semaphore wait" if DEBUG; | |||||||||||||||||||||||||||||||
| 58 | 0 | warn "[debug:$$] Semaphore val " . $self->getval($args{sem}) if DEBUG; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | # Remove NOWAIT and block | ||||||||||||||||||||||||||||||||
| 61 | 0 | $flags ^= IPC_NOWAIT; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 0 | return $sem->op($args{'sem'}, -1, $flags); | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub getall { shift->_sem->getall() } | ||||||||||||||||||||||||||||||
|
Subroutine
getall: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub getval { shift->_sem->getval(shift // 0) } | ||||||||||||||||||||||||||||||
|
Subroutine
getval: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub getncnt { shift->_sem->getncnt(shift // 0) } | ||||||||||||||||||||||||||||||
|
Subroutine
getncnt: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub setall { shift->_sem->setall(@_) } | ||||||||||||||||||||||||||||||
|
Subroutine
setall: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub setval { shift->_sem->setval(@_) } | ||||||||||||||||||||||||||||||
|
Subroutine
setval: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub stat { shift->_sem->stat() } | ||||||||||||||||||||||||||||||
|
Subroutine
stat: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub id { shift->_sem->id() } | ||||||||||||||||||||||||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub release { shift->_sem->op(shift || 0, 1, 0) } | ||||||||||||||||||||||||||||||
|
Subroutine
release: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub remove { shift->_sem->remove() } | ||||||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 39 | !!42; | |||||||||||||||||||||||||||||||