Shm/Tied.pm

filter: Statementstmt 79.0% 83 / 105 Branchbran 40.6% 13 / 32 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 91.3% 21 / 23 Podpod 100% 11 / 11 totaltotal 69.6% 133 / 191 CC 21 SCAR 16.5
CC
21
Cov
68%
retie
20.0
reftype
9.1
tiedref
7.3
CRAP 5.2
« Shm/Segment.pm ↑ Summary Shm.pm »
1 package IPC::Shm::Tied;
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #
6 # Copyright (c) 2014 by Kevin Cody-Little <kcody@cpan.org>
7 #
8 # This code may be modified or redistributed under the terms
9 # of either the Artistic or GNU General Public licenses, at
10 # the modifier or redistributor's discretion.
11 #
12  
13 =head1 NAME
14  
15 IPC::Shm::Tied
16  
17 =head1 SYNOPSIS
18  
19 This class is part of the IPC::Shm implementation.
20  
21 use IPC::Shm;
22 my $obj = tie my %foo, 'IPC::Shm::Tied';
23 $obj->tiedref( \%foo );
24  
25 You may use this module to tie lexicals as above, but if used on
26 a package variable, it will behave as a lexical and be destroyed
27 when all connections are closed.
28  
29 If the call to $obj->tiedref is omitted, another tied reference
30 will be created when another shared variable's reference to this
31 one is dereferenced. This is not desirable behavior.
32  
33 Optionally takes an IPC::Shm::Segment object as an argument. If
34 none is supplied, C<IPC::Shm::Segment->anonymous> is called.
35  
36 It is simpler to use the C<: shm> interface. See C<perldoc IPC::Shm>.
37  
38 =head1 SUPERCLASS
39  
40 This class is a derivative of IPC::Shm::Segment, which in turn
41 is a derivative of IPC::Shm::Simple.
42  
43 =head1 CONSTRUCTORS
44  
45 =head2 TIEHASH, TIEARRAY, TIESCALAR
46  
47 This package supports the tie() call.
48  
49 =head2 $this->retie
50  
51 When an anonymous variable is dereferenced, and in some other
52 circumstances, it has to be tied to a variable so it can be
53 accessed normally.
54  
55 =head1 DESTRUCTOR
56  
57 =head2 $this->DETACH
58  
59 Called from IPC::Shm::Simple when the last in-process instance
60 of this same segment is being DESTROYed.
61  
62 =head1 TIED REFERENCE RETRIEVAL
63  
64 =head2 $this->tiedref
65  
66 Retrieves a reference to the object's associated
67 tied variable. Calls retie() when necessary.
68  
69 =head2 $this->tiedref( $reference )
70  
71 Stores a reference to the object's associated tied variable.
72 This allows retie() to be avoided most of the time.
73  
74 =head2 $this->tiedref_clean
75  
76 Removes the object's tied reference from the cache.
77  
78 =head2 $this->standin_tiedref( $standin )
79  
80 Returns a reference to the tied variable, given a standin hash.
81 See IPC::Shm::Segment for more about standins.
82  
83 =head2 $this->reftype( $reftype )
84  
85 Stores the type of object the associated reference
86 points to. This makes the retie() method possible
87 for anonymous segments.
88  
89 Valid values are 'HASH', 'ARRAY', and 'SCALAR'.
90  
91 =head2 $this->reftype
92  
93 Retrieves the reference type stored above.
94  
95 =head1 VALUE CACHE METHODS
96  
97 =head2 $this->vcache
98  
99 Retrieves the cached copy of the deserializer's last run.
100  
101 =head2 $this->vcache( $newvalue )
102  
103 Stores a new cached value, discarding the old. The Storable
104 module expects this to be a reference (no raw strings).
105  
106 =head2 $this->vcache_clean
107  
108 Removes the object's value cache from in-process memory.
109  
110 =head2 $class->EMPTY
111  
112 Returns a reference to an empty object, compatible with
113 the vcache method above. This is an abstract method and
114 must be implemented by inheriting classes.
115  
116 =head1 SERIALIZE/DESERIALIZE
117  
118 =head2 $this->FRESH
119  
120 Called by IPC::Shm::Simple->fetch when a new value is
121 actually read in from shared memory. The deserializing
122 step happens here.
123  
124 =head2 $this->flush
125  
126 Serializes and writes the contents of the value cache to shared memory.
127  
128  
129 =cut
130  
131 ###############################################################################
132 # dependencies
133  
134 6 ▶use base 'IPC::Shm::Segment';
Subroutine BEGIN: called CC=1 SCAR=0.0
135  
136 6 ▶use IPC::Shm::Tied::HASH;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 6 ▶use IPC::Shm::Tied::ARRAY;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 6 ▶use IPC::Shm::Tied::SCALAR;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 6 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
141 6 ▶use Storable qw( freeze thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143  
144 ###############################################################################
145 # tie constructors
146  
147 24 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=2 SCAR=7.0
148 24 shift; # discard class we were called as
149 24 ▶ $_[0] ||= IPC::Shm::Segment->anonymous;
Condition66%2/3
$_[0] ||= "IPC::Shm::Segment"->anonymous
l!l&&r!l&&!r
1950
MC/DC0%0/2
$_[0] ||= "IPC::Shm::Segment"->anonymous
$_[0] "IPC::Shm::Segment"->anonymous
Truth table66%2/3
A ||= B
A= $_[0]B= "IPC::Shm::Segment"->anonymous
ABresult
1X1
011
000
150 24 return IPC::Shm::Tied::HASH->TIEHASH( @_ );
151 }
152  
153 1 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=2 SCAR=7.6
154 1 shift; # discard class we were called as
155 1 ▶ $_[0] ||= IPC::Shm::Segment->anonymous;
Condition33%1/3
$_[0] ||= "IPC::Shm::Segment"->anonymous
l!l&&r!l&&!r
100
MC/DC0%0/2
$_[0] ||= "IPC::Shm::Segment"->anonymous
$_[0] "IPC::Shm::Segment"->anonymous
Truth table33%1/3
A ||= B
A= $_[0]B= "IPC::Shm::Segment"->anonymous
ABresult
1X1
011
000
156 1 return IPC::Shm::Tied::ARRAY->TIEARRAY( @_ );
157 }
158  
159 7 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=2 SCAR=7.6
160 7 shift; # discard class we were called as
161 7 ▶ $_[0] ||= IPC::Shm::Segment->anonymous;
Condition33%1/3
$_[0] ||= "IPC::Shm::Segment"->anonymous
l!l&&r!l&&!r
700
MC/DC0%0/2
$_[0] ||= "IPC::Shm::Segment"->anonymous
$_[0] "IPC::Shm::Segment"->anonymous
Truth table33%1/3
A ||= B
A= $_[0]B= "IPC::Shm::Segment"->anonymous
ABresult
1X1
011
000
162 7 return IPC::Shm::Tied::SCALAR->TIESCALAR( @_ );
163 }
164  
165  
166 ###############################################################################
167 # reconstructor - dynamically create a tied reference
168  
169 0 ▶sub retie {
Subroutine retie: not called CC=4 SCAR=30.0
Pod documented
170 0 my ( $this ) = @_;
171 0 my ( $rv );
172  
173 0 my $type = $this->vartype;
174  
175 0 ▶ if ( $type eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'SCALAR') { } 0 0 0
176 0 tie my %tmp, 'IPC::Shm::Tied', $this;
177 0 $this->tiedref( $rv = \%tmp );
178 }
179  
180 elsif ( $type eq 'ARRAY' ) {
181 0 tie my @tmp, 'IPC::Shm::Tied', $this;
182 0 $this->tiedref( $rv = \@tmp );
183 }
184  
185 elsif ( $type eq 'SCALAR' ) {
186 0 tie my $tmp, 'IPC::Shm::Tied', $this;
187 0 $this->tiedref( $rv = \$tmp );
188 }
189  
190 else {
191 0 confess "unknown reference type";
192 }
193  
194 0 return $rv;
195 }
196  
197  
198 ###############################################################################
199 # destructor - called when the last in-process instance is DESTROYed
200  
201 7 ▶sub DETACH {
Subroutine DETACH: called CC=1 SCAR=0.0
Pod documented
202 7 my ( $this ) = @_;
203  
204 7 $this->SUPER::DETACH;
205 7 $this->vcache_clean;
206 7 $this->tiedref_clean;
207  
208 7 return;
209 }
210  
211  
212 ###############################################################################
213 # store the tied reference so we can get it back from the object later
214  
215 { # BEGIN private lexicals
216 6 my %TiedRef = ();
217  
218 32 ▶sub tiedref {
Subroutine tiedref: called CC=5 SCAR=19.9
Pod documented
219 32 my $this = shift;
220  
221 32 my $shmid = $this->{shmid};
222  
223 32 ▶ if ( my $newval = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $newval = shift()) 32 0 32
224  
225 32 ▶ unless ( defined $newval ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $newval) 0 32 32
226 0 delete $TiedRef{$shmid};
227 0 return;
228 }
229  
230 32 ▶ confess __PACKAGE__ . "->tiedref() expects a reference"
Branch: 1 branch
BranchTrueFalseTotal
unless my $reftype = ref $newval 0 32 32
231 unless my $reftype = ref( $newval );
232  
233 32 $this->reftype( $reftype );
234  
235 32 $TiedRef{$shmid} = $newval;
236 32 weaken $TiedRef{$shmid};
237  
238 32 return $newval;
239 }
240  
241 0 my $tv; # silence perlcritic by declaring before conditional
242  
243 # keep a temporary reference to the end of this sub
244 0 ▶ $tv = $this->retie unless defined $TiedRef{$shmid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $TiedRef{$shmid} 0 0 0
245  
246 0 return $TiedRef{$shmid};
247 }
248  
249 7 ▶sub tiedref_clean {
Subroutine tiedref_clean: called CC=1 SCAR=0.0
Pod documented
250 7 delete $TiedRef{shift->{shmid}};
251 7 return;
252 }
253  
254 12 ▶sub standin_tiedref {
Subroutine standin_tiedref: called CC=2 SCAR=8.4
Pod documented
255 12 my ( $callclass, $standin ) = @_;
256  
257 12 my $shmid = $callclass->standin_shmid( $standin );
258  
259 12 ▶ return $TiedRef{$shmid} if defined $TiedRef{$shmid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $TiedRef{$shmid} 12 0 12
260  
261 0 my $this = $callclass->standin_restand( $standin );
262  
263 0 return $this->tiedref;
264 }
265  
266 } # END private lexicals
267  
268 6 ▶sub reftype {
Subroutine reftype: called CC=7 SCAR=22.0
Pod documented
269 32 my $this = shift;
270  
271 32 ▶ return $this->{reftype} unless my $newval = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless my $newval = shift() 0 32 32
272  
273 # avoid unnecessary shared memory access
274 32 ▶ if ( $this->{reftype} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"reftype"}) 0 32 32
275 0 ▶ return $newval if $newval eq $this->{reftype};
Branch: 1 branch
BranchTrueFalseTotal
if $newval eq $this->{"reftype"} 0 0 0
276 }
277  
278 # we only care about anonymous segments
279 32 ▶ return $this->{reftype} unless my $vanon = $this->varanon;
Branch: 1 branch
BranchTrueFalseTotal
unless my $vanon = $this->varanon 25 7 32
280  
281 7 my $value = $IPC::Shm::ANONTYPE{$vanon};
282  
283 # and we want to avoid unnecessary shared memory writes
284 7 ▶ unless ( $value and $value eq $newval ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value and $value eq $newval) 7 0 7
Condition33%1/3
$value and $value eq $newval
!ll&&!rl&&r
700
MC/DC0%0/2
$value and $value eq $newval
$value $value eq $newval
Truth table33%1/3
A and B
A= $valueB= $value eq $newval
ABresult
0X0
100
111
285 7 $IPC::Shm::ANONTYPE{$vanon} = $newval;
286 }
287  
288 7 return $this->{reftype} = $newval;
289 }
290  
291  
292 ###############################################################################
293 # value cache, for the unserialized in-memory state
294  
295 { # BEGIN private lexicals
296 6 my %ValCache = ();
297  
298 327 ▶sub vcache {
Subroutine vcache: called CC=3 SCAR=11.2
Pod documented
299 327 my $this = shift;
300  
301 327 my $shmid = $this->{shmid};
302  
303 327 ▶ if ( my $newval = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $newval = shift()) 54 273 327
304 54 return $ValCache{$shmid} = $newval;
305 }
306  
307 273 ▶ unless ( defined $ValCache{$shmid} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ValCache{$shmid}) 0 273 273
308 0 $ValCache{$shmid} = $this->EMPTY;
309 }
310  
311 273 return $ValCache{$shmid};
312 }
313  
314 7 ▶sub vcache_clean {
Subroutine vcache_clean: called CC=1 SCAR=0.0
Pod documented
315 7 delete $ValCache{shift->{shmid}};
316 7 return;
317 }
318  
319 } # END private lexicals
320  
321  
322 ###############################################################################
323 # abstract empty value representation
324  
325 6 ▶sub EMPTY {
Subroutine EMPTY: not called CC=1 SCAR=1.2
Pod documented
326 0 croak "Abstract EMPTY() invocation";
327 }
328  
329  
330 ###############################################################################
331 # serialize and deserialize routines
332  
333 # reads from scache, writes to vcache
334 # called by IPC::Shm::Simple::fetch
335 27 ▶sub FRESH {
Subroutine FRESH: called CC=2 SCAR=7.0
Pod documented
336 27 my ( $this ) = @_;
337  
338 27 my $thawed = eval { thaw( ${$this->scache} ) };
339 27 ▶ $this->vcache( $thawed ? $thawed : $this->EMPTY );
Branch: 1 branch
BranchTrueFalseTotal
$thawed ? : 0 27 27
340  
341 }
342  
343 # reads from vcache, calls store
344 84 ▶sub flush {
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
345 84 my ( $this ) = @_;
346  
347 84 $this->store( freeze( $this->vcache ) );
348
349 }
350  
351  
352 ###############################################################################
353 ###############################################################################
354  
355 =head1 AUTHOR
356  
357 Kevin Cody-Little <kcody@cpan.org>
358  
359 =cut
360  
361 6 1;
« Shm/Segment.pm ↑ Summary Shm.pm »