blib/lib/IPC/Shm/Simple.pm

filter: Statementstmt 11.0% 27 / 244 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 21.9% 9 / 41 Podpod n/a totaltotal 8.2% 36 / 434 CC 63 SCAR 41.7
CC
63
Cov
7%
fetch
182.0
attach
156.0
store
90.0
CRAP 64.4
↑ Summary Simple.xs »
1 package IPC::Shm::Simple;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #
7 # Copyright (C) 2005,2014 by Kevin Cody-Little <kcody@cpan.org>
8 #
9 # Although this package as a whole is derived from
10 # IPC::ShareLite, this particular file is a new work.
11 #
12 # This code may be modified or redistributed under the terms
13 # of either the Artistic or GNU General Public licenses, at
14 # the modifier or redistributor's discretion.
15 #
16  
17 =head1 NAME
18  
19 IPC::Shm::Simple - Simple data in SysV shared memory segments.
20  
21 =head1 SYNOPSIS
22  
23 Provides the ability to create a shared segment with or without first
24 knowing what ipckey it will use. Caches shared memory reads in process
25 memory, and defeatably verifies writes by reading the value back
26 and comparing it stringwise.
27  
28 Can only store string or numeric data.
29  
30 =head1 OBJECT CACHING
31  
32 This module caches the underlying C object, such that the process only
33 has one attachment to the shared segment. However, the Perl objects are
34 not cached, to facilitate timely destruction. There can be many distinct
35 blessed references to the same shared segment. This is made transparent
36 by storing all state information in package lexicals, not upon the object.
37  
38 =cut
39  
40  
41 3 ▶use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use IPC::SysV qw( IPC_PRIVATE );
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 3 ▶use Class::Attrib;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 3 ▶use vars qw( $VERSION @ISA %Attrib );
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 $VERSION = '1.10';
51 @ISA = qw( Class::Attrib DynaLoader );
52 %Attrib = (
53 Mode => oct( 660 ),
54 Size => 4096,
55 dwell => 0,
56 verify => 1
57 );
58  
59  
60 ###
61 ### Constructors
62 ###
63  
64 =head1 CONSTRUCTORS
65  
66 =head2 $this->bind( ipckey, [size], [mode] );
67  
68 Attach to the shared memory segment identified by ipckey, whether it
69 exists already or not.
70  
71 If a segment must be created, size and permissions may be specified as
72 for the C<< $this->create() >> call. Otherwise, the class defaults will apply.
73  
74 Returns blessed reference on success, undef on failure.
75  
76 Throws an exception on invalid parameters.
77  
78 The segment will be unlocked even if it was just created.
79  
80 =cut
81  
82 0 ▶sub bind {
Subroutine bind: not called CC=3 SCAR=24.8
83 0 my ( $this, $ipckey, $size, $mode ) = @_;
84 0 my ( $self );
85  
86 0 ▶ unless ( $self = $this->attach( $ipckey ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self = $this->attach($ipckey)) 0 0 0
87  
88 0 ▶ $self = $this->create( $ipckey, $size, $mode )
Branch: 1 branch
BranchTrueFalseTotal
$self = $this->create($ipckey, $size, $mode) or return 0 0 0
89 or return;
90  
91 0 $self->unlock;
92  
93 }
94  
95 0 return $self;
96 }
97  
98 { # BEGIN lexical scope
99 my %ShmIndex = (); # cache key=ipckey value=shmid
100 my %ShmShare = (); # cache key=shmid value=sharelite
101 my %ShmCount = (); # cache key=shmid value=integer
102  
103 =head2 $this->attach( ipckey );
104  
105 Attach to the shared memory segment identified by ipckey if it exists.
106  
107 Returns blessed reference on success, undef on failure.
108  
109 Throws an exception on invalid parameters.
110  
111 =cut
112  
113 0 ▶sub attach {
Subroutine attach: not called CC=12 SCAR=50.5
114 0 my ( $this, $ipckey ) = @_;
115  
116 0 ▶ confess( __PACKAGE__ . "->attach: Called without ipckey." )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ipckey 0 0 0
117 unless defined $ipckey;
118  
119 0 ▶ confess( __PACKAGE__ . "->attach: Called with empty ipckey." )
Branch: 1 branch
BranchTrueFalseTotal
unless $ipckey 0 0 0
120 unless $ipckey;
121  
122 0 ▶ confess( __PACKAGE__ . "->attach: Called with string ipckey." )
Branch: 1 branch
BranchTrueFalseTotal
unless $ipckey > 0 0 0 0
123 unless $ipckey > 0;
124  
125 0 ▶ confess( __PACKAGE__ . "->attach: Called with IPC_PRIVATE." )
Branch: 1 branch
BranchTrueFalseTotal
if $ipckey == IPC_PRIVATE() 0 0 0
126 if $ipckey == IPC_PRIVATE;
127  
128 0 ▶ my $class = ref( $this ) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
129  
130 0 ▶ if ( my $shmid = $ShmIndex{$ipckey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $shmid = $ShmIndex{$ipckey}) 0 0 0
131 0 my $share = $ShmShare{$shmid};
132 0 ▶ unless ( $share ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share) 0 0 0
133 0 carp __PACKAGE__ . "->attach: dangling ShmIndex";
134 0 delete $ShmIndex{$ipckey};
135 0 return;
136 }
137  
138 0 bless my $self = {}, $class;
139 0 $self->{share} = $share;
140 0 $self->{shmid} = $shmid;
141  
142 0 ▶ if ( $self->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid) 0 0 0
143 0 $ShmCount{$shmid}++;
144 0 return $self;
145 }
146  
147 0 carp __PACKAGE__ . "->attach: got invalid cached object";
148  
149 0 delete $ShmCount{$shmid};
150 0 delete $ShmShare{$shmid};
151 0 delete $ShmIndex{$ipckey};
152  
153 0 return;
154 }
155  
156 0 my $share = sharelite_attach( $ipckey );
157  
158 0 ▶ return unless $share; # no carp, $! is set
Branch: 1 branch
BranchTrueFalseTotal
unless $share 0 0 0
159  
160 0 my $shmid = sharelite_shmid( $share );
161  
162 0 ▶ unless ( defined $shmid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shmid) 0 0 0
163 0 carp "sharelite_shmid returned undef";
164 0 sharelite_shmdt( $share );
165 0 return;
166 }
167  
168 0 bless my $self = {}, $class;
169 0 $self->{share} = $share;
170 0 $self->{shmid} = $shmid;
171  
172 # inform subclasses that an uncached attachment has occurred
173 0 ▶ $self->ATTACH()
Branch: 1 branch
BranchTrueFalseTotal
$self->ATTACH or return 0 0 0
174 or return;
175  
176 # save the attached object in the cache
177 0 $ShmIndex{$ipckey} = $shmid;
178 0 $ShmShare{$shmid} = $share;
179 0 $ShmCount{$shmid} = 1;
180  
181 0 return $self;
182 }
183  
184 =head2 $self->ATTACH();
185  
186 Called by C< $this->attach() > and C< $this->shmat() > when an uncached
187 attachment occurs.
188  
189 Must return true, otherwise the attachment is aborted.
190  
191 Does nothing on its own; this is meant for subclasses to override.
192  
193 =cut
194  
195 0 ▶sub ATTACH {
Subroutine ATTACH: not called CC=1 SCAR=6.9
196 0 my ( $self ) = @_;
197  
198 0 return 1;
199 }
200  
201 =head2 $this->create( [ipckey], [segsize], [permissions] )
202  
203 Create a new shared memory segment, with the given ipckey, unless it exists.
204 Can be given C<IPC_PRIVATE> as an ipckey to create an unkeyed segment, which
205 is assumed if no argument is provided.
206  
207 The optional parameters segsize and permissions default to C<< $this->Size() >>
208 and C<< $this->Mode() >>, respectively.
209  
210 Returns blessed reference on success, undef on failure.
211  
212 The segment will automatically have a writelock in effect.
213  
214 =cut
215  
216 0 ▶sub create {
Subroutine create: not called CC=8 SCAR=42.8
217 0 my ( $this, $ipckey, $size, $mode ) = @_;
218  
219 0 ▶ $ipckey ||= IPC_PRIVATE;
Condition0%0/3
$ipckey ||= IPC_PRIVATE()
l!l&&r!l&&!r
000
MC/DC0%0/2
$ipckey ||= IPC_PRIVATE()
$ipckey IPC_PRIVATE()
Truth table0%0/3
A ||= B
A= $ipckeyB= IPC_PRIVATE()
ABresult
1X1
011
000
220 0 ▶ $size ||= $this->Size();
Condition0%0/3
$size ||= $this->Size
l!l&&r!l&&!r
000
MC/DC0%0/2
$size ||= $this->Size
$size $this->Size
Truth table0%0/3
A ||= B
A= $sizeB= $this->Size
ABresult
1X1
011
000
221 0 ▶ $mode ||= $this->Mode();
Condition0%0/3
$mode ||= $this->Mode
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode ||= $this->Mode
$mode $this->Mode
Truth table0%0/3
A ||= B
A= $modeB= $this->Mode
ABresult
1X1
011
000
222  
223 0 ▶ my $class = ref( $this ) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
224  
225 0 my $share = sharelite_create( $ipckey, $size, $mode );
226  
227 0 ▶ return unless $share; # no carp, $! is set
Branch: 1 branch
BranchTrueFalseTotal
unless $share 0 0 0
228  
229 0 my $shmid = sharelite_shmid( $share );
230  
231 0 ▶ unless ( defined $shmid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shmid) 0 0 0
232 0 carp "sharelite_shmid returned undef";
233 0 sharelite_remove( $share );
234 0 sharelite_shmdt( $share );
235 0 return;
236 }
237  
238 0 bless my $self = {}, $class;
239 0 $self->{share} = $share;
240 0 $self->{shmid} = $shmid;
241  
242 0 ▶ $ShmIndex{$ipckey} = $shmid unless $ipckey == IPC_PRIVATE;
Branch: 1 branch
BranchTrueFalseTotal
unless $ipckey == IPC_PRIVATE() 0 0 0
243 0 $ShmShare{$shmid} = $share;
244 0 $ShmCount{$shmid} = 1;
245  
246 0 return $self;
247 }
248  
249 =head2 $this->shmat( shmid );
250  
251 Attach to an existing shared memory segment by its shmid.
252  
253 =cut
254  
255 0 ▶sub shmat {
Subroutine shmat: not called CC=8 SCAR=42.8
256 0 my ( $this, $shmid ) = @_;
257  
258 0 ▶ confess( __PACKAGE__ . "->shmat: Called without shmid." )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shmid 0 0 0
259 unless defined $shmid;
260  
261 0 ▶ confess( __PACKAGE__ . "->shmat: Called with invalid shmid." )
Branch: 1 branch
BranchTrueFalseTotal
if $shmid == -1 0 0 0
262 if $shmid == -1;
263  
264 0 ▶ my $class = ref( $this ) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
265  
266 0 ▶ if ( my $share = $ShmShare{$shmid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $share = $ShmShare{$shmid}) 0 0 0
267  
268 0 bless my $self = {}, $class;
269 0 $self->{share} = $share;
270 0 $self->{shmid} = $shmid;
271  
272 0 ▶ if ( $self->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid) 0 0 0
273 0 $ShmCount{$shmid}++;
274 0 return $self;
275 }
276  
277 0 carp __PACKAGE__ . "->shmat: got invalid cached object";
278  
279 0 delete $ShmCount{$shmid};
280 0 delete $ShmShare{$shmid};
281  
282 0 return;
283 }
284  
285 0 my $share = sharelite_shmat( $shmid );
286  
287 0 ▶ return unless $share; # no carp, $! is set
Branch: 1 branch
BranchTrueFalseTotal
unless $share 0 0 0
288  
289 0 bless my $self = {}, $class;
290 0 $self->{share} = $share;
291 0 $self->{shmid} = $shmid;
292  
293 # inform subclasses that an uncached attachment has occurred
294 0 ▶ $self->ATTACH()
Branch: 1 branch
BranchTrueFalseTotal
$self->ATTACH or return 0 0 0
295 or return;
296  
297 # save the attached object in the cache
298 0 $ShmShare{$shmid} = $share;
299 0 $ShmCount{$shmid} = 1;
300  
301 0 return $self;
302 }
303  
304 =head1 CLEANUP METHOD
305  
306 =head2 $self->remove();
307  
308 Uncaches the referenced instance, and causes the underlying shared
309 memory segments to be removed from the operating system when DESTROYed.
310  
311 Returns 1 on success, undef on failure.
312  
313 =cut
314  
315 0 ▶sub remove {
Subroutine remove: not called CC=3 SCAR=24.8
316 0 my ( $self ) = @_;
317 0 my ( $share, $shmid, $ipckey );
318  
319 0 $share = $self->{share};
320  
321 0 ▶ unless ( $share ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share) 0 0 0
322 0 carp "undefined share during remove";
323 0 return;
324 }
325  
326 0 ▶ return ( sharelite_remove( $share ) == -1 ) ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
sharelite_remove($share) == -1 ? : 0 0 0
327 }
328  
329 # when the object is destroyed, the sharelite object must be too
330 # otherwise segment removal (and even removal marking) would never occur
331 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
332 0 my ( $self ) = @_;
333  
334 0 my $shmid = $self->{shmid};
335  
336 0 ▶ unless ( defined $shmid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shmid) 0 0 0
337 0 carp "undefined shmid during DESTROY";
338 0 return;
339 }
340  
341 0 $ShmCount{$shmid}--;
342  
343 0 ▶ return if $ShmCount{$shmid};
Branch: 1 branch
BranchTrueFalseTotal
if $ShmCount{$shmid} 0 0 0
344  
345 0 $self->DETACH;
346  
347 0 return;
348 }
349  
350 =head1 DESTRUCTOR
351  
352 =head2 $self->DETACH();
353  
354 Called by C< $self->DESTROY() > on the last copy of the object.
355  
356 Uncaches the referenced instance, and causes the underlying shared
357 memory segments to be detached by the operating system.
358  
359 If subclasees override this, they must call C< $self->SUPER::DESTROY() >.
360  
361 =cut
362 0 ▶sub DETACH {
Subroutine DETACH: not called CC=4 SCAR=30.0
363 0 my ( $self ) = @_;
364  
365 0 $self->scache_clean;
366  
367 0 my $shmid = $self->{shmid};
368  
369 0 ▶ unless ( defined $shmid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shmid) 0 0 0
370 0 carp "undefined shmid during DETACH";
371 0 return;
372 }
373  
374 0 my $share = $self->{share};
375  
376 0 ▶ unless ( $share ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share) 0 0 0
377 0 carp "undefined share during DETACH";
378 0 return;
379 }
380  
381 0 my $ipckey = sharelite_key( $share );
382  
383 0 delete $ShmCount{$shmid};
384 0 delete $ShmShare{$shmid};
385 0 ▶ delete $ShmIndex{$ipckey} unless $ipckey == IPC_PRIVATE;
Branch: 1 branch
BranchTrueFalseTotal
unless $ipckey == IPC_PRIVATE() 0 0 0
386  
387 0 sharelite_shmdt( $share );
388  
389 0 return;
390 }
391  
392 } # END lexical scope
393  
394  
395 =head1 ACCESSOR METHODS
396  
397 =head2 $self->key();
398  
399 Returns the ipckey assigned by the program at instantiation.
400  
401 =head2 $self->shmid();
402  
403 Returns the shmid assigned by the operating system at instantiation.
404  
405 =head2 $self->flags();
406  
407 Returns the permissions flags assigned at instantiation.
408  
409 =head2 $self->length();
410  
411 Returns the number of bytes currently stored in the share.
412  
413 =head2 $self->serial();
414  
415 Returns the serial number of the current shared memory value.
416  
417 =head2 $self->top_seg_size();
418  
419 Returns the total size of the top share segment, in bytes.
420  
421 =head2 $self->chunk_seg_size();
422  
423 Returns the size of data chunk segments, in bytes.
424  
425 =head2 $self->chunk_seg_size( chunk_segment_size );
426  
427 Changes the size of chunk data segments. The share must have only one
428 allocated segment (the top segment) for this call to succeed.
429  
430 =head2 $self->nconns();
431  
432 Reports the number of processes connected to the share.
433  
434 =head2 $self->nrefs();
435  
436 Returns the current shared reference count.
437  
438 =head2 $self->incref();
439  
440 Increments the shared reference counter.
441  
442 =head2 $self->decref();
443  
444 Decrements the shared reference counter.
445  
446 =cut
447  
448 0 ▶sub key {
Subroutine key: not called CC=1 SCAR=6.9
449 0 return sharelite_key( shift->{share} );
450 }
451  
452 0 ▶sub shmid {
Subroutine shmid: not called CC=1 SCAR=6.9
453 0 return sharelite_shmid( shift->{share} );
454 }
455  
456 0 ▶sub flags {
Subroutine flags: not called CC=1 SCAR=6.9
457 0 return sharelite_flags( shift->{share} );
458 }
459  
460 0 ▶sub length {
Subroutine length: not called CC=1 SCAR=6.9
461 0 return sharelite_length( shift->{share} );
462 }
463  
464 0 ▶sub serial {
Subroutine serial: not called CC=1 SCAR=6.9
465 0 return sharelite_serial( shift->{share} );
466 }
467  
468 0 ▶sub is_valid {
Subroutine is_valid: not called CC=1 SCAR=6.9
469 0 return sharelite_is_valid( shift->{share} );
470 }
471  
472 0 ▶sub nsegments {
Subroutine nsegments: not called CC=1 SCAR=6.9
473 0 return sharelite_nsegments( shift->{share} );
474 }
475  
476 0 ▶sub top_seg_size {
Subroutine top_seg_size: not called CC=1 SCAR=6.9
477 0 return sharelite_top_seg_size( shift->{share} );
478 }
479  
480 0 ▶sub chunk_seg_size {
Subroutine chunk_seg_size: not called CC=1 SCAR=6.9
481 0 return sharelite_chunk_seg_size( shift->{share}, @_ );
482 }
483  
484 0 ▶sub nconns {
Subroutine nconns: not called CC=1 SCAR=6.9
485 0 return sharelite_nconns( shift->{share} );
486 }
487  
488 0 ▶sub nrefs {
Subroutine nrefs: not called CC=1 SCAR=6.9
489 0 return sharelite_nrefs( shift->{share} );
490 }
491  
492 0 ▶sub incref {
Subroutine incref: not called CC=1 SCAR=6.9
493 0 return sharelite_incref( shift->{share}, @_ );
494 }
495  
496 0 ▶sub decref {
Subroutine decref: not called CC=1 SCAR=6.9
497 0 return sharelite_decref( shift->{share}, @_ );
498 }
499  
500  
501 =head1 DATA METHODS
502  
503 =head2 $self->scache();
504  
505 Returns a scalar reference to the segment cache. Does not guarantee
506 freshness, and the reference can become invalid after the next I/O
507 operation.
508  
509 =head2 $self->scache_clean();
510  
511 Entirely removes the cache entry for the object.
512  
513 =head2 $self->fetch();
514  
515 Fetch a previously stored value.
516  
517 If nothing has been stored yet, C<''> (the empty string) is returned.
518  
519 =head2 $self->FRESH();
520  
521 Invoked by C< fetch() > when the data has been changed by another process.
522  
523 =cut
524  
525  
526 { # BEGIN private lexical scope
527 my %ShmCache = (); # cache key=shmid value={}
528 # scache = string
529 # serial = integer
530 # sstamp = timestamp
531  
532 0 ▶sub scache {
Subroutine scache: not called CC=4 SCAR=30.0
533 0 my $self = shift;
534  
535 0 my $shmid = $self->{shmid};
536  
537 0 ▶ unless ( defined $shmid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shmid) 0 0 0
538 0 carp "undefined shmid during scache retrieval";
539 0 return;
540 }
541  
542 0 ▶ my $cache = $ShmCache{$shmid} ||= {};
Condition0%0/2
$ShmCache{$shmid} ||= {}
l!l
00
MC/DC0%0/1
$ShmCache{$shmid} ||= {}
$ShmCache{$shmid}
Truth table0%0/2
A
A= $ShmCache{$shmid}
Aresult
11
00
543  
544 0 ▶ $cache->{scache} ||= '';
Condition0%0/2
$cache->{"scache"} ||= ""
l!l
00
MC/DC0%0/1
$cache->{"scache"} ||= ""
$cache->{"scache"}
Truth table0%0/2
A
A= $cache->{"scache"}
Aresult
11
00
545  
546 0 return \($cache->{scache});
547 }
548  
549 0 ▶sub scache_clean {
Subroutine scache_clean: not called CC=1 SCAR=6.9
550 0 my $self = shift;
551  
552 0 delete $ShmCache{$self->{shmid}};
553  
554 }
555  
556 0 ▶sub fetch {
Subroutine fetch: not called CC=13 SCAR=52.0
557 0 my $self = shift;
558  
559 0 ▶ carp( __PACKAGE__ . "->fetch: Called without at least shared lock!" )
Branch: 1 branch
BranchTrueFalseTotal
if $self->_locked(8) 0 0 0
560 if $self->_locked( LOCK_UN );
561  
562 0 my $share = $self->{share};
563  
564 0 ▶ unless ( $share ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share) 0 0 0
565 0 carp "undefined share during fetch";
566 0 return;
567 }
568  
569 0 my $shmid = $self->{shmid};
570  
571 0 ▶ unless ( defined $shmid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shmid) 0 0 0
572 0 carp "undefined shmid during fetch";
573 0 return;
574 }
575  
576 0 ▶ my $cache = $ShmCache{$shmid} ||= {};
Condition0%0/2
$ShmCache{$shmid} ||= {}
l!l
00
MC/DC0%0/1
$ShmCache{$shmid} ||= {}
$ShmCache{$shmid}
Truth table0%0/2
A
A= $ShmCache{$shmid}
Aresult
11
00
577  
578 # determine current shared memory value serial number
579 0 my $serial = sharelite_serial( $share );
580  
581 # short circuit remaining tests if cache is found invalid
582 0 my $dofetch = 0;
583  
584 # definitely fetch if we don't have a matching serial number
585 $dofetch = 1
586 0 ▶ unless $cache->{serial} && ( $cache->{serial} == $serial );
Branch: 1 branch
BranchTrueFalseTotal
unless $cache->{"serial"} and $cache->{"serial"} == $serial 0 0 0
Condition0%0/3
$cache->{"serial"} and $cache->{"serial"} == $serial
!ll&&!rl&&r
000
MC/DC0%0/2
$cache->{"serial"} and $cache->{"serial"} == $serial
$cache->{"serial"} $cache->{"serial"} == $serial
Truth table0%0/3
A and B
A= $cache->{"serial"}B= $cache->{"serial"} == $serial
ABresult
0X0
100
111
587  
588 # same serial; believe the cached value if it isn't too old
589 # a zero ttl means trust the cached value until the serial changes
590 0 ▶ unless ( $dofetch ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dofetch) 0 0 0
591 0 ▶ if ( my $ttl = $self->dwell() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ttl = $self->dwell) 0 0 0
592 0 ▶ $dofetch = 1 if $cache->{sstamp} + $ttl < time();
Branch: 1 branch
BranchTrueFalseTotal
if $cache->{"sstamp"} + $ttl < time 0 0 0
593 }
594 }
595  
596 0 ▶ if ( $dofetch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dofetch) 0 0 0
597  
598 0 $cache->{scache} = sharelite_fetch( $share );
599  
600 croak( __PACKAGE__ . "->fetch: failed: $!" )
601 0 ▶ unless defined $cache->{scache};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache->{"scache"} 0 0 0
602  
603 0 $cache->{sstamp} = time();
604 0 $cache->{serial} = $serial;
605  
606 0 ▶ if ( my $cref = UNIVERSAL::can( $self, 'FRESH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cref = UNIVERSAL::can($self, "FRESH")) 0 0 0
607 0 &$cref( $self );
608 }
609  
610 }
611  
612 0 return $cache->{scache};
613 }
614  
615 =head2 $self->store( value );
616  
617 Stores a string or numeric value in the shared memory segment.
618  
619 =cut
620  
621 0 ▶sub store {
Subroutine store: not called CC=9 SCAR=45.0
622 0 my $self = shift;
623  
624 0 ▶ carp( __PACKAGE__ . "->store: Called without exclusive lock!" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_locked(2) 0 0 0
625 unless $self->_locked( LOCK_EX );
626  
627 0 my $share = $self->{share};
628  
629 0 ▶ unless ( $share ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share) 0 0 0
630 0 carp "undefined share during store";
631 0 return;
632 }
633  
634 0 my $shmid = $self->{shmid};
635  
636 0 ▶ unless ( $shmid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($shmid) 0 0 0
637 0 carp "undefined shmid during store";
638 0 return;
639 }
640  
641 0 ▶ my $cache = $ShmCache{$shmid} ||= {};
Condition0%0/2
$ShmCache{$shmid} ||= {}
l!l
00
MC/DC0%0/1
$ShmCache{$shmid} ||= {}
$ShmCache{$shmid}
Truth table0%0/2
A
A= $ShmCache{$shmid}
Aresult
11
00
642  
643 0 my $rc = sharelite_store( $share, $_[0], CORE::length( $_[0] ) );
644  
645 0 ▶ croak( __PACKAGE__ . "->store: failed: $!" )
Branch: 1 branch
BranchTrueFalseTotal
if $rc == -1 0 0 0
646 if $rc == -1;
647  
648 0 ▶ if ( $self->verify() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify) 0 0 0
649 0 my $data = sharelite_fetch( $share );
650  
651 0 ▶ croak( __PACKAGE__ . "->store: fetch failed: $!" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
652 unless defined $data;
653  
654 0 ▶ croak( __PACKAGE__ . "->store: Write verify failed!" )
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] eq $data 0 0 0
655 unless $_[0] eq $data;
656  
657 }
658  
659 # simulate a fetch because storing also serves to confirm the value
660 0 $cache->{scache} = $_[0];
661 0 $cache->{sstamp} = time();
662 0 $cache->{serial} = sharelite_serial( $share );
663  
664 # return true so test harnesses pass
665 0 return 1;
666 }
667  
668 } # END scope
669  
670  
671 ###
672 ### Object Lock Methods - Class::Lockable friendly
673 ###
674  
675 0 ▶sub lock {
Subroutine lock: not called CC=1 SCAR=6.9
676 0 return shift->_lock( @_ );
677 }
678  
679 0 ▶sub _lock {
Subroutine _lock: not called CC=4 SCAR=30.0
680 0 my ( $self, $flag ) = @_;
681  
682 0 my $share = $self->{share};
683  
684 0 ▶ unless ( $share ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share) 0 0 0
685 0 carp "undefined share during _lock";
686 0 return;
687 }
688  
689 # short circuit if already locked as requested
690 0 ▶ return 0 if sharelite_locked( $share, $flag );
Branch: 1 branch
BranchTrueFalseTotal
if sharelite_locked($share, $flag) 0 0 0
691  
692 0 my $rc = sharelite_lock( $share, $flag );
693  
694 0 ▶ if ( $rc == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == -1) 0 0 0
695 0 carp( __PACKAGE__ . "->_lock: $!" );
696 0 return;
697 }
698  
699 0 return $rc == 0;
700 }
701  
702 0 ▶sub locked {
Subroutine locked: not called CC=1 SCAR=6.9
703 0 return shift->_locked( @_ );
704 }
705  
706 0 ▶sub _locked {
Subroutine _locked: not called CC=3 SCAR=24.8
707 0 my ( $self, $flag ) = @_;
708  
709 0 my $share = $self->{share};
710  
711 0 ▶ unless ( $share ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share) 0 0 0
712 0 carp "undefined share during _locked";
713 0 return;
714 }
715  
716 0 my $rc = sharelite_locked( $share, $flag );
717  
718 0 ▶ if ( $rc == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == -1) 0 0 0
719 0 carp( __PACKAGE__ . "->_locked: $!" );
720 0 return;
721 }
722  
723 0 return $rc != 0;
724 }
725  
726  
727 ###
728 ### Higher Level Lock Methods
729 ###
730  
731 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
732 0 return shift->_lock( LOCK_UN );
733 }
734  
735 0 ▶sub readlock {
Subroutine readlock: not called CC=1 SCAR=6.9
736 0 return shift->_lock( LOCK_SH );
737 }
738  
739 0 ▶sub writelock {
Subroutine writelock: not called CC=1 SCAR=6.9
740 0 return shift->_lock( LOCK_EX );
741 }
742  
743  
744 bootstrap IPC::Shm::Simple $VERSION;
745  
746 1;
747  
748  
749 =head1 INSTANCE ATTRIBUTES - I/O BEHAVIOR
750  
751 =head2 $this->dwell( [seconds] );
752  
753 Specifies the time-to-live of cached shared memory reads, in seconds.
754 This only affects the case where the serial number has -not- changed.
755  
756 Default: 0.
757  
758 =head2 $this->verify( [boolean] );
759  
760 Specifies whether to read-back and compare shared memory writes.
761  
762 Expensive.
763  
764 Default: 1.
765  
766 =head1 PACKAGE ATTRIBUTES - SEGMENT PARAMETERS
767  
768 These methods carry the default values used during instantiation.
769  
770 =head2 $this->Mode( [value] );
771  
772 Specifies or fetches the permissions for new segments. Default: 0660.
773  
774 =head2 $this->Size( [value] );
775  
776 Specifies or fetches the initial size of new shared memory segments.
777 Default: 4096
778  
779 =head1 CAVEATS
780  
781 To do.
782  
783 =cut
784  
↑ Summary Simple.xs »