| 1 | package IPC::Semaphore::Concurrency; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008008; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use POSIX qw(O_WRONLY O_CREAT O_NONBLOCK O_NOCTTY); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use IPC::SysV 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
| |||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use IPC::Semaphore; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | require Exporter; | |||||||||||||||||||||||||||||||
| 13 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||
| 14 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | |||||||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||||||
| 16 | 1 | our @EXPORT = qw(); | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=12 SCAR=26.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 21 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 2 | my %args; | |||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | if (@_ == 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | # Only one required argument | ||||||||||||||||||||||||||||||||
| 26 | 1 | $args{'path'} = shift; | |||||||||||||||||||||||||||||||
| 27 | } else { | ||||||||||||||||||||||||||||||||
| 28 | 1 | %args = @_; | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | if (!exists($args{'path'})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | 0 | carp "Must supply a path!"; #TODO: Allow private semaphores | |||||||||||||||||||||||||||||||
| 33 | 0 | return undef; | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | # Set defaults | ||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | $args{'project'} = 0 if (!exists($args{'project'})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | $args{'count'} = 1 if (!exists($args{'count'})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | $args{'value'} = 1 if (!exists($args{'value'})); # TODO: allow array (one value per semaphore) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | $args{'touch'} = 1 if (!exists($args{'touch'})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 2 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||
| 42 | 2 | $self->{'_args'} = { %args }; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | $self->_touch($self->{'_args'}->{'path'}) if (!-e $self->{'_args'}->{'path'} || $self->{'_args'}->{'touch'}) or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
not -e $self->{"_args"}{"path"} or $self->{"_args"}{"touch"} or return undef
not -e $self->{"_args"}{"path"} or $self->{"_args"}{"touch"}
MC/DC0%0/2
not -e $self->{"_args"}{"path"} or $self->{"_args"}{"touch"} or return undef
not -e $self->{"_args"}{"path"}
$self->{"_args"}{"touch"}
Truth table33%1/3
not(A) or B
A= not -e $self->{"_args"}{"path"}B= $self->{"_args"}{"touch"}
| |||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | $self->{'_args'}->{'key'} = $self->_ftok() or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | $self->{'_args'}->{'sem'} = $self->_create($self->key()) or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | # Internal functions | ||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub _touch { | ||||||||||||||||||||||||||||||
|
Subroutine
_touch: called CC=6 SCAR=22.3
| |||||||||||||||||||||||||||||||||
| 54 | # Create and/or touch the path, returns false if there's an error | ||||||||||||||||||||||||||||||||
| 55 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 56 | 2 | my $path = shift; | |||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | sysopen(my $fh, $path, O_WRONLY|O_CREAT|O_NONBLOCK|O_NOCTTY) or carp "Can't create $path: $!" and return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
sysopen(my $fh, $path, 2369) || carp("Can't create ${path}: $!") && (return 0)
carp("Can't create ${path}: $!") and return 0
MC/DC0%0/2
sysopen(my $fh, $path, 2369) || carp("Can't create ${path}: $!") && (return 0)
sysopen(my $fh, $path, 2369)
carp("Can't create ${path}: $!")
Truth table33%1/3
A || B
A= sysopen(my $fh, $path, 2369)B= carp("Can't create ${path}: $!")
| |||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | utime(undef, undef, $path) if ($self->{'_args'}->{'touch'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | close $fh or carp "Can't close $path: $!" and return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
close $fh || carp("Can't close ${path}: $!") && (return 0)
carp("Can't close ${path}: $!") and return 0
MC/DC0%0/2
close $fh || carp("Can't close ${path}: $!") && (return 0)
close $fh
carp("Can't close ${path}: $!")
Truth table33%1/3
A || B
A= close $fhB= carp("Can't close ${path}: $!")
| |||||||||||||||||||||||||||||||||
| 60 | 2 | return 1; | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | sub _ftok { | ||||||||||||||||||||||||||||||
|
Subroutine
_ftok: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 64 | # Create an IPC key, returns result of ftok() | ||||||||||||||||||||||||||||||||
| 65 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 66 | 2 | return ftok($self->{'_args'}->{'path'}, $self->{'_args'}->{'project'}) or carp "Can't create semaphore key: $!" and return undef; | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | sub _create { | ||||||||||||||||||||||||||||||
|
Subroutine
_create: called CC=5 SCAR=17.1
| |||||||||||||||||||||||||||||||||
| 70 | # Create the semaphore and assign it its initial value | ||||||||||||||||||||||||||||||||
| 71 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 72 | 2 | my $key = shift; | |||||||||||||||||||||||||||||||
| 73 | # Presubably the semaphore exists already, so try using it right away | ||||||||||||||||||||||||||||||||
| 74 | 2 | my $sem = IPC::Semaphore->new($key, 0, 0); | |||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | if (!defined($sem)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | # Creatie a new semaphore... | ||||||||||||||||||||||||||||||||
| 77 | 2 | $sem = IPC::Semaphore->new($key, $self->{'_args'}->{'count'}, IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); | |||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | if (!defined($sem)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | # Make sure another process did not create it in our back | ||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $sem = IPC::Semaphore->new($key, 0, 0) or carp "Semaphore creation failed!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||||||
| 82 | # If we created the semaphore now we assign its initial value | ||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | for (my $i=0; $i<$self->{'_args'}->{'count'}; $i++) { # TODO: Support array - see above | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 21 | $sem->op($i, $self->{'_args'}->{'value'}, 0); | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | # Return whatever last semget call got us | ||||||||||||||||||||||||||||||||
| 89 | 2 | return $sem; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | # External API | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub getall { | ||||||||||||||||||||||||||||||
|
Subroutine
getall: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 96 | 0 | return $self->{'_args'}->{'sem'}->getall(); | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub getval { | ||||||||||||||||||||||||||||||
|
Subroutine
getval: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 100 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $nsem = shift or 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | 0 | return $self->{'_args'}->{'sem'}->getval($nsem); | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub getncnt { | ||||||||||||||||||||||||||||||
|
Subroutine
getncnt: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $nsem = shift or 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | 0 | return $self->{'_args'}->{'sem'}->getncnt($nsem); | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub setall { | ||||||||||||||||||||||||||||||
|
Subroutine
setall: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 113 | 0 | return $self->{'_args'}->{'sem'}->setall(@_); | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub setval { | ||||||||||||||||||||||||||||||
|
Subroutine
setval: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 117 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 118 | 0 | my ($nsem, $val) = @_; | |||||||||||||||||||||||||||||||
| 119 | 0 | return $self->{'_args'}->{'sem'}->setval($nsem, $val); | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub stat { | ||||||||||||||||||||||||||||||
|
Subroutine
stat: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 124 | 0 | return $self->{'_args'}->{'sem'}->stat(); | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub id { | ||||||||||||||||||||||||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 128 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 129 | 0 | return $self->{'_args'}->{'sem'}->id(); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | sub key { | ||||||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 133 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 134 | 2 | return $self->{'_args'}->{'key'}; | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub acquire { | ||||||||||||||||||||||||||||||
|
Subroutine
acquire: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 138 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 0 | my %args; | |||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (@_ >= 1 && $_[0] =~ /^\d+$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ >= 1 and $_[0] =~ /^\d+$/
MC/DC0%0/2
@_ >= 1 and $_[0] =~ /^\d+$/
@_ >= 1
$_[0] =~ /^\d+$/
Truth table0%0/3
A and B
A= @_ >= 1B= $_[0] =~ /^\d+$/
| |||||||||||||||||||||||||||||||||
| 142 | # Positional arguments | ||||||||||||||||||||||||||||||||
| 143 | 0 | ($args{'sem'}, $args{'wait'}, $args{'max'}, $args{'undo'}) = @_; | |||||||||||||||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||||||||||||||
| 145 | 0 | %args = @_; | |||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | # Defaults | ||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | $args{'sem'} = 0 if (!defined($args{'sem'})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | $args{'wait'} = 0 if (!defined($args{'wait'})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $args{'max'} = -1 if (!defined($args{'max'})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $args{'undo'} = 1 if (!defined($args{'undo'})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 0 | my $sem = $self->{'_args'}->{'sem'}; | |||||||||||||||||||||||||||||||
| 154 | 0 | my $flags = IPC_NOWAIT; | |||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | $flags |= SEM_UNDO if ($args{'undo'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 0 | my ($ret, $ncnt); | |||||||||||||||||||||||||||||||
| 158 | # Get blocked process count here to retain Errno (thus $!) after the first semop call. | ||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | $ncnt = $self->getncnt($args{'sem'}) if ($args{'wait'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if (($ret = $sem->op($args{'sem'}, -1, $flags))) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 162 | 0 | return $ret; | |||||||||||||||||||||||||||||||
| 163 | } elsif ($args{'wait'}) { | ||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | return $ret 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"}
| |||||||||||||||||||||||||||||||||
| 165 | # Remove NOWAIT and block | ||||||||||||||||||||||||||||||||
| 166 | 0 | $flags ^= IPC_NOWAIT; | |||||||||||||||||||||||||||||||
| 167 | 0 | return $sem->op($args{'sem'}, -1, $flags); | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | 0 | return $ret; | |||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub release { | ||||||||||||||||||||||||||||||
|
Subroutine
release: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 173 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | my $number = shift || 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 175 | 0 | return $self->{'_args'}->{'sem'}->op($number, 1, 0); | |||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | sub remove { | ||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 179 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 180 | 2 | return $self->{'_args'}->{'sem'}->remove(); | |||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 1 | 1; | |||||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||||||