blib/lib/Devel/PtrTable.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 57 CC 5 SCAR 15.0
CC
5
Cov
0%
CLONE_SKIP
6.0
CLONE
6.0
PtrTable_get
6.0
CRAP 4.5
1 package Devel::PtrTable;
2 use strict;
3 use warnings;
4 use Task::Weaken;
5 use base qw(Exporter);
6  
7 our $VERSION = 0.03;
8 require XSLoader;
9 XSLoader::load(__PACKAGE__, $VERSION);
10  
11  
12 our @EXPORT = qw(PtrTable_get PtrTable_freecopied);
13  
14 my $obj;
15  
16 sub CLONE_SKIP {
17 my $pkg = shift;
18 return if $pkg ne __PACKAGE__;
19 $obj = \"OPAQUE";
20 _PtrTable_init($obj);
21 return 0;
22 }
23  
24 sub CLONE {
25 my $pkg = shift;
26 return if $pkg ne __PACKAGE__;
27 _PtrTable_make_our_table($obj);
28 }
29  
30 sub PtrTable_get($) {
31 my $memaddr = shift;
32 if(!$obj) {
33 warn("This is not a child thread or ptr_table has been deleted");
34 return;
35 }
36 _PtrTable_get($obj, $memaddr);
37 }
38  
39 sub PtrTable_freecopied() {
40 if(!$obj) {
41 return;
42 }
43 _PtrTable_freecopied($obj);
44 undef $obj;
45 }
46  
47 1;
48  
49 =head1 NAME
50  
51 Devel::PtrTable - Interface to perl's old-new pointer mapping for cloning
52  
53 =head2 DESCRIPTION
54  
55 This provides an interface to map B<memory addresses> of duplicated/cloned perl
56 variables between threads.
57  
58 Perl duplicates each variable when a new thread is spawned, and thus the new
59 underlying SV object has a new memory address.
60  
61 Internally, perl maintains a C<struct ptr_tbl> during cloning, which allows the
62 interpreter to properly copy objects, as well as keep track of which objects were
63 copied.
64  
65 =head2 SYNOPSIS
66  
67 use threads;
68 use Devel::PtrTable;
69
70 my $someref = \"somestring";
71 my $refhash = {
72 $someref + 0 => $someref
73 };
74
75 my $thr = $thr->create(
76 sub {
77 while (my ($addr,$v) = each %$refhash) {
78 my $newval = PtrTable_get($addr);
79 die("oops!") unless $newval == $addr;
80 }
81 }
82 );
83  
84  
85 =head2 FUNCTIONS
86  
87 =head3 PtrTable_get($memaddr)
88  
89 Returns a reference to whatever happened to have been stored under C<$memaddr>
90 in the parent thread, or undef if there is no such entry.
91  
92 =head3 PtrTable_freecopied()
93  
94 Free the copied pointer table
95  
96 =head2 GUTS
97  
98 For those who might be interested, this module does the following
99  
100 =over
101  
102 =item 1
103  
104 Using C<CLONE_SKIP> to initialize an opaque SV and magic structure with a C<MGVTBL>
105 including C<svt_dup>
106  
107 =item 2
108  
109 Inside the C<svt_dup> hook, the stash for this module is unshifted to the beginning
110 of the C<stashes> fields of the C<CLONE_PARAMS> argument. This ensures that no
111 other module will destroy the global C<PL_ptr_table> before us. This also means
112 that we won't be able to see any new pointers possibly added by other packages
113 in their C<CLONE> methods - though this is unlikely
114  
115 =item 3
116  
117 After the interpreter has been cloned, our C<CLONE> method is the first thing called.
118 In this method, we make a duplicate C<struct ptr_tbl> object, which is available
119 until the perl-level C<PtrTable_freecopied> is called
120  
121 =back
122  
123 =head2 CAVEATS
124  
125 This module cannot be too smart about which entries in the pointer table are valid
126 SVs, which are PerlIO objects, and which are random junk. Users of this module
127 are expected to have a list of valid addresses to use.
128  
129 Additionally, modules which may insert other entries into the pointer table during
130 their own C<CLONE> methods will not have those entries available to us. See L</GUTS>
131 for the reason
132  
133 =head1 SEE ALSO
134  
135 L<perlapi> and I<perl.h>
136  
137 =head1 AUTHOR AND COPYRIGHT
138  
139 Copyright (C) 2011 by M. Nunberg.
140  
141 You may use and distribute this module under the same terms and license as Perl
142 itself
143  
144 =cut