blib/lib/IPC/ShareLite.pm

filter: Statementstmt 86.1% 106 / 123 Branchbran 54.0% 27 / 50 Conditioncond 50.0% 9 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 71.4% 20 / 28 Podpod 87.5% 14 / 16 totaltotal 72.6% 178 / 245 CC 34 SCAR 20.7
CC
34
Cov
74%
_initialize
22.0
_rearrange_args
7.3
AUTOLOAD
5.4
CRAP 7.9
↑ Summary ShareLite.xs »
1 package IPC::ShareLite;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 IPC::ShareLite - Lightweight interface to shared memory
10  
11 =head1 VERSION
12  
13 This document describes IPC::ShareLite version 0.17
14  
15 =cut
16  
17 4 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
18 $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD
19 4 );
20  
21 4 ▶use subs qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
22 IPC_CREAT IPC_EXCL IPC_RMID IPC_STAT IPC_PRIVATE GETVAL SETVAL GETALL
23 SEM_UNDO LOCK_EX LOCK_SH LOCK_UN LOCK_NB
24 4 );
25  
26 2 require Exporter;
27 2 require DynaLoader;
28 2 require AutoLoader;
29  
30 2 @ISA = qw( Exporter DynaLoader );
31  
32 2 @EXPORT = qw( );
33  
34 2 @EXPORT_OK = qw(
35 IPC_CREAT IPC_EXCL IPC_RMID IPC_STATE IPC_PRIVATE GETVAL SETVAL GETALL
36 SEM_UNDO LOCK_EX LOCK_SH LOCK_UN LOCK_NB
37 );
38  
39 2 %EXPORT_TAGS = (
40 all => [
41 qw(
42 IPC_CREAT IPC_EXCL IPC_RMID IPC_PRIVATE LOCK_EX LOCK_SH LOCK_UN
43 LOCK_NB
44 )
45 ],
46 lock => [qw( LOCK_EX LOCK_SH LOCK_UN LOCK_NB )],
47 flock => [qw( LOCK_EX LOCK_SH LOCK_UN LOCK_NB )],
48 );
49  
50 2 Exporter::export_ok_tags( 'all', 'lock', 'flock' );
51  
52 2 $VERSION = '0.17';
53  
54 =head1 SYNOPSIS
55  
56 use IPC::ShareLite;
57  
58 my $share = IPC::ShareLite->new(
59 -key => 1971,
60 -create => 'yes',
61 -destroy => 'no'
62 ) or die $!;
63  
64 $share->store( "This is stored in shared memory" );
65 my $str = $share->fetch;
66  
67 =head1 DESCRIPTION
68  
69 IPC::ShareLite provides a simple interface to shared memory, allowing
70 data to be efficiently communicated between processes. Your operating
71 system must support SysV IPC (shared memory and semaphores) in order to
72 use this module.
73  
74 IPC::ShareLite provides an abstraction of the shared memory and
75 semaphore facilities of SysV IPC, allowing the storage of arbitrarily
76 large data; the module automatically acquires and removes shared memory
77 segments as needed. Storage and retrieval of data is atomic, and locking
78 functions are provided for higher-level synchronization.
79  
80 In many respects, this module is similar to IPC::Shareable. However,
81 IPC::ShareLite does not provide a tied interface, does not
82 (automatically) allow the storage of variables, and is written in C
83 for additional speed.
84  
85 Construct an IPC::ShareLite object by calling its constructor:
86  
87 my $share = IPC::ShareLite->new(
88 -key => 1971,
89 -create => 'yes',
90 -destroy => 'no'
91 ) or die $!;
92  
93 Once an instance has been created, data can be written to shared memory
94 by calling the store() method:
95  
96 $share->store("This is going in shared memory");
97  
98 Retrieve the data by calling the fetch() method:
99  
100 my $str = $share->fetch();
101  
102 The store() and fetch() methods are atomic; any processes attempting
103 to read or write to the memory are blocked until these calls finish.
104 However, in certain situations, you'll want to perform multiple
105 operations atomically. Advisory locking methods are available for
106 this purpose.
107  
108 An exclusive lock is obtained by calling the lock() method:
109  
110 $share->lock();
111  
112 Happily, the lock() method also accepts all of the flags recognized
113 by the flock() system call. So, for example, you can obtain a
114 shared lock like this:
115  
116 $share->lock( LOCK_SH );
117  
118 Or, you can make either type of lock non-blocking:
119  
120 $share->lock( LOCK_EX|LOCK_NB );
121  
122 Release the lock by calling the unlock() method:
123  
124 $share->unlock;
125  
126 =head1 METHODS
127  
128 =head2 C<< new($key, $create, $destroy, $exclusive, $mode, $flags, $size) >>
129  
130 This is the constructor for IPC::ShareLite. It accepts both
131 the positional and named parameter calling styles.
132  
133 C<$key> is an integer value used to associate data between processes.
134 All processes wishing to communicate should use the same $key value.
135 $key may also be specified as a four character string, in which case
136 it will be converted to an integer value automatically. If $key
137 is undefined, the shared memory will not be accessible from other
138 processes.
139  
140 C<$create> specifies whether the shared memory segment should be
141 created if it does not already exist. Acceptable values are
142 1, 'yes', 0, or 'no'.
143  
144 C<$destroy> indicates whether the shared memory segments and semaphores
145 should be removed from the system once the object is destroyed.
146 Acceptable values are 1, 'yes', 0, or 'no'.
147  
148 If C<$exclusive> is true, instantiation will fail if the shared memory
149 segment already exists. Acceptable values are 1, 'yes', 0, or 'no'.
150  
151 C<$mode> specifies the permissions for the shared memory and semaphores.
152 The default value is 0666.
153  
154 C<$flags> specifies the exact shared memory and semaphore flags to
155 use. The constants IPC_CREAT, IPC_EXCL, and IPC_PRIVATE are available
156 for import.
157  
158 C<$size> specifies the shared memory segment size, in bytes. The default
159 size is 65,536 bytes, which is fairly portable. Linux, as an example,
160 supports segment sizes of 4 megabytes.
161  
162 The constructor croaks on error.
163  
164 =cut
165  
166 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
167 4 my $class = shift;
168 4 ▶ my $self = bless {}, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
169  
170 4 my $args = $class->_rearrange_args(
171 [
172 qw( key create destroy exclusive mode
173 flags size glue )
174 ],
175 \@_
176 );
177  
178 4 $self->_initialize( $args );
179  
180 2 return $self;
181 }
182  
183 2 ▶sub _8bit_clean {
Subroutine _8bit_clean: called CC=2 SCAR=7.1
184 2 my ( $self, $str ) = @_;
185 croak "$str is not 8-bit clean"
186 2 ▶ if grep { $_ > 255 } map ord, split //, $str;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 255;} map(ord($_), split(//, $str, 0)) 2 0 2
187 }
188  
189 4 ▶sub _initialize {
Subroutine _initialize: called CC=16 SCAR=30.9
190 4 my $self = shift;
191 4 my $args = shift;
192  
193 4 for ( qw( create exclusive destroy ) ) {
194 $args->{$_} = 0
195 12 ▶ if defined $args->{$_} and lc $args->{$_} eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$_} and lc $args->{$_} eq "no" 2 10 12
Condition100%3/3
defined $args->{$_} and lc $args->{$_} eq "no"
!ll&&!rl&&r
462
MC/DC100%2/2
defined $args->{$_} and lc $args->{$_} eq "no"
defined $args->{$_} lc $args->{$_} eq "no"
Truth table100%3/3
A and B
A= defined $args->{$_}B= lc $args->{$_} eq "no"
ABresult
0X0
100
111
196 }
197  
198 # Allow glue as a synonym for key
199 4 ▶ $self->{key} = $args->{key} || $args->{glue} || IPC_PRIVATE;
Condition33%2/6
$args->{'key'} || $args->{'glue'} || IPC_PRIVATE()
l!l&&r!l&&!r
400
$args->{"key"} or $args->{"glue"}
l!l&&r!l&&!r
400
MC/DC0%0/3
$args->{'key'} || $args->{'glue'} || IPC_PRIVATE()
$args->{"key"} $args->{"glue"} IPC_PRIVATE()
Truth table25%1/4
A or B || C
A= $args->{"key"}B= $args->{"glue"}C= IPC_PRIVATE()
ABCresult
1XX1
01X1
0011
0000
200  
201 # Allow a four character string as the key
202 4 ▶ unless ( $self->{key} =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"key"} =~ /^\d+$/) 2 2 4
203 croak "Key must be a number or four character string"
204 2 ▶ if length $self->{key} > 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"key"} > 4 0 2 2
205 2 $self->_8bit_clean( $self->{key} );
206 0 $self->{key} = unpack( 'i', pack( 'A4', $self->{key} ) );
207 }
208  
209 2 ▶ $self->{create} = ( $args->{create} ? IPC_CREAT : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$args->{'create'} ? : 2 0 2
210  
211 $self->{exclusive} = (
212 $args->{exclusive}
213 2 ▶ ? IPC_EXCL | IPC_CREAT
Branch: 1 branch
BranchTrueFalseTotal
$args->{'exclusive'} ? : 0 2 2
214 : 0
215 );
216  
217 2 ▶ $self->{destroy} = ( $args->{destroy} ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$args->{'destroy'} ? : 2 0 2
218  
219 2 ▶ $self->{flags} = $args->{flags} || 0;
Condition50%1/2
$args->{'flags'} || 0
l!l
02
MC/DC0%0/1
$args->{'flags'} || 0
$args->{'flags'}
Truth table50%1/2
A
A= $args->{'flags'}
Aresult
11
00
220 2 ▶ $self->{mode} = $args->{mode} || 0666 unless $args->{flags};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"flags"} 2 0 2
Condition50%1/2
$args->{'mode'} || 438
l!l
02
MC/DC0%0/1
$args->{'mode'} || 438
$args->{'mode'}
Truth table50%1/2
A
A= $args->{'mode'}
Aresult
11
00
221 2 ▶ $self->{size} = $args->{size} || 0;
Condition50%1/2
$args->{'size'} || 0
l!l
20
MC/DC0%0/1
$args->{'size'} || 0
$args->{'size'}
Truth table50%1/2
A
A= $args->{'size'}
Aresult
11
00
222  
223 $self->{flags} = $self->{flags} | $self->{exclusive} | $self->{create}
224 2 | $self->{mode};
225  
226 $self->{share}
227 = new_share( $self->{key}, $self->{size}, $self->{flags} )
228 2 ▶ or croak "Failed to create share";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"share"} = new_share($self->{"key"}, $self->{"size"}, $self->{"flags"}) or croak("Failed to create share") 2 0 2
229  
230 2 return 1;
231 }
232  
233 4 ▶sub _rearrange_args {
Subroutine _rearrange_args: called CC=6 SCAR=19.9
234 4 my ( $self, $names, $params ) = @_;
235 4 my ( %hash, %names );
236  
237 4 ▶ return \%hash unless ( @$params );
Branch: 1 branch
BranchTrueFalseTotal
unless @$params 0 4 4
238  
239 4 ▶ unless ( $params->[0] =~ /^-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->[0] =~ /^-/) 0 4 4
240 0 ▶ croak "unexpected number of parameters"
Branch: 1 branch
BranchTrueFalseTotal
unless @$names == @$params 0 0 0
241 unless ( @$names == @$params );
242 0 $hash{@$names} = @$params;
243 0 return \%hash;
244 }
245  
246 4 %names = map { $_ => 1 } @$names;
247  
248 4 ▶ while ( @$params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$params) 14 4 18
249 14 my $param = lc substr( shift @$params, 1 );
250 14 ▶ exists $names{$param} or croak "unexpected parameter '-$param'";
Branch: 1 branch
BranchTrueFalseTotal
exists $names{$param} or croak("unexpected parameter '-${param}'") 14 0 14
251 14 $hash{$param} = shift @$params;
252 }
253  
254 4 return \%hash;
255 }
256  
257 =head2 C<< store( $scalar ) >>
258  
259 This method stores C<$scalar> into shared memory. C<$scalar> may be
260 arbitrarily long. Shared memory segments are acquired and
261 released automatically as the data length changes.
262 The only limits on the amount of data are the system-wide
263 limits on shared memory pages (SHMALL) and segments (SHMMNI)
264 as compiled into the kernel.
265  
266 The method raises an exception on error.
267  
268 Note that unlike L<IPC::Shareable>, this module does not automatically
269 allow references to be stored. Serializing all data is expensive, and is
270 not always necessary. If you need to store a reference, you should employ
271 the L<Storable> module yourself. For example:
272  
273 use Storable qw( freeze thaw );
274 ...
275 $hash = { red => 1, white => 1, blue => 1 };
276 $share->store( freeze( $hash ) );
277 ...
278 $hash = thaw( $share->fetch );
279  
280 =cut
281  
282 2006 ▶sub store {
Subroutine store: called CC=2 SCAR=7.6
Pod documented
283 2006 my $self = shift;
284  
285 2006 ▶ if ( write_share( $self->{share}, $_[0], length $_[0] ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_share($self->{"share"}, $_[0], length $_[0]) < 0) 0 2006 2006
286 0 croak "IPC::ShareLite store() error: $!";
287 }
288 2006 return 1;
289 }
290  
291 =head2 C<< fetch >>
292  
293 This method returns the data that was previously stored in
294 shared memory. The empty string is returned if no data was
295 previously stored.
296  
297 The method raises an exception on error.
298  
299 =cut
300  
301 2005 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=7.0
Pod documented
302 2005 my $self = shift;
303  
304 2005 my $str = read_share( $self->{share} );
305 2005 ▶ defined $str or croak "IPC::ShareLite fetch() error: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $str or croak("IPC::ShareLite fetch() error: $!") 2005 0 2005
306 2005 return $str;
307 }
308  
309 =head2 C<< lock( $type ) >>
310  
311 Obtains a lock on the shared memory. $type specifies the type
312 of lock to acquire. If $type is not specified, an exclusive
313 read/write lock is obtained. Acceptable values for $type are
314 the same as for the flock() system call. The method returns
315 true on success, and undef on error. For non-blocking calls
316 (see below), the method returns 0 if it would have blocked.
317  
318 Obtain an exclusive lock like this:
319
320 $share->lock( LOCK_EX ); # same as default
321  
322 Only one process can hold an exclusive lock on the shared memory at
323 a given time.
324  
325 Obtain a shared lock this this:
326  
327 $share->lock( LOCK_SH );
328  
329 Multiple processes can hold a shared lock at a given time. If a process
330 attempts to obtain an exclusive lock while one or more processes hold
331 shared locks, it will be blocked until they have all finished.
332  
333 Either of the locks may be specified as non-blocking:
334  
335 $share->lock( LOCK_EX|LOCK_NB );
336 $share->lock( LOCK_SH|LOCK_NB );
337
338 A non-blocking lock request will return 0 if it would have had to
339 wait to obtain the lock.
340  
341 Note that these locks are advisory (just like flock), meaning that
342 all cooperating processes must coordinate their accesses to shared memory
343 using these calls in order for locking to work. See the flock() call for
344 details.
345  
346 Locks are inherited through forks, which means that two processes actually
347 can possess an exclusive lock at the same time. Don't do that.
348  
349 The constants LOCK_EX, LOCK_SH, LOCK_NB, and LOCK_UN are available
350 for import:
351  
352 use IPC::ShareLite qw( :lock );
353  
354 Or, just use the flock constants available in the Fcntl module.
355  
356 =cut
357  
358 2000 ▶sub lock {
Subroutine lock: called CC=3 SCAR=11.3
Pod documented
359 2000 my $self = shift;
360  
361 2000 my $response = sharelite_lock( $self->{share}, shift() );
362 2000 ▶ return undef if ( $response == -1 );
Branch: 1 branch
BranchTrueFalseTotal
if $response == -1 0 2000 2000
363 2000 ▶ return 0 if ( $response == 1 ); # operation failed due to LOCK_NB
Branch: 1 branch
BranchTrueFalseTotal
if $response == 1 0 2000 2000
364 2000 return 1;
365 }
366  
367 =head2 C<< unlock >>
368  
369 Releases any locks. This is actually equivalent to:
370  
371 $share->lock( LOCK_UN );
372  
373 The method returns true on success and undef on error.
374  
375 =cut
376  
377 2000 ▶sub unlock {
Subroutine unlock: called CC=2 SCAR=7.1
Pod documented
378 2000 my $self = shift;
379  
380 2000 ▶ return undef if ( sharelite_unlock( $self->{share} ) < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if sharelite_unlock($self->{"share"}) < 0 0 2000 2000
381 2000 return 1;
382 }
383  
384 # DEPRECATED -- Use lock() and unlock() instead.
385 0 ▶sub shlock { shift->lock( @_ ) }
Subroutine shlock: not called CC=1 SCAR=6.9
Pod undocumented
386 0 ▶sub shunlock { shift->unlock( @_ ) }
Subroutine shunlock: not called CC=1 SCAR=6.9
Pod undocumented
387  
388 =head2 C<< version >>
389  
390 Each share has a version number that incrementents monotonically for
391 each write to the share. When the share is initally created its version
392 number will be 1.
393  
394 my $num_writes = $share->version;
395  
396 =cut
397  
398 9 ▶sub version { sharelite_version( shift->{share} ) }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
399  
400 =head2 C<< key >>
401  
402 Get a share's key.
403  
404 my $key = $share->key;
405  
406 =cut
407  
408 0 ▶sub key { shift->{key} }
Subroutine key: not called CC=1 SCAR=6.9
Pod documented
409  
410 =head2 C<< create >>
411  
412 Get a share's create flag.
413  
414 =cut
415  
416 0 ▶sub create { shift->{create} }
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
417  
418 =head2 C<< exclusive >>
419  
420 Get a share's exclusive flag.
421  
422 =cut
423  
424 0 ▶sub exclusive { shift->{exclusive} }
Subroutine exclusive: not called CC=1 SCAR=6.9
Pod documented
425  
426 =head2 C<< flags >>
427  
428 Get a share's flag.
429  
430 =cut
431  
432 0 ▶sub flags { shift->{flags} }
Subroutine flags: not called CC=1 SCAR=6.9
Pod documented
433  
434 =head2 C<< mode >>
435  
436 Get a share's mode.
437  
438 =cut
439  
440 0 ▶sub mode { shift->{mode} }
Subroutine mode: not called CC=1 SCAR=6.9
Pod documented
441  
442 =head2 C<< size >>
443  
444 Get a share's segment size.
445  
446 =cut
447  
448 0 ▶sub size { shift->{size} }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
449  
450 =head2 C<< num_segments >>
451  
452 Get the number of segments in a share. The memory usage of a share can
453 be approximated like this:
454  
455 my $usage = $share->size * $share->num_segments;
456  
457 C<$usage> will be the memory usage rounded up to the next segment
458 boundary.
459  
460 =cut
461  
462 2 ▶sub num_segments {
Subroutine num_segments: called CC=2 SCAR=7.0
Pod documented
463 2 my $self = shift;
464  
465 2 my $count = sharelite_num_segments( $self->{share} );
466 2 ▶ return undef if $count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 0 0 2 2
467 2 return $count;
468 }
469  
470 =head2 C<< destroy >>
471  
472 Get or set the share's destroy flag.
473  
474 =cut
475  
476 1 ▶sub destroy {
Subroutine destroy: called CC=2 SCAR=7.1
Pod documented
477 1 my $self = shift;
478 1 ▶ $self->{destroy} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
479 1 return $self->{destroy};
480 }
481  
482 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
483 4 my $self = shift;
484  
485 destroy_share( $self->{share}, $self->{destroy} )
486 4 ▶ if $self->{share};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"share"} 2 2 4
487 }
488  
489 4 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=16.9
490 # This AUTOLOAD is used to 'autoload' constants from the constant()
491 # XS function. If a constant is not found then control is passed
492 # to the AUTOLOAD in AutoLoader.
493  
494 4 my $constname;
495 4 ( $constname = $AUTOLOAD ) =~ s/.*:://;
496 4 ▶ my $val = constant( $constname, @_ ? $_[0] : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 4 4
497 4 ▶ if ( $! != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 0) 0 4 4
498 0 ▶ if ( $! =~ /Invalid/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! =~ /Invalid/) { } 0 0 0
499 0 $AutoLoader::AUTOLOAD = $AUTOLOAD;
500 0 goto &AutoLoader::AUTOLOAD;
501 }
502 else {
503 0 croak "Your vendor has not defined ShareLite macro $constname";
504 }
505 }
506 4 ▶ eval "sub $AUTOLOAD { $val }";
Subroutine IPC_CREAT: called CC=1 SCAR=0.0
Subroutine LOCK_EX: called CC=1 SCAR=0.0
507 4 goto &$AUTOLOAD;
508 }
509  
510 2 bootstrap IPC::ShareLite $VERSION;
511  
512 2 1;
513  
514 __END__
↑ Summary ShareLite.xs »