File Coverage

blib/lib/threads/variables/reap.pm
Criterion Covered Total %
statement 12 25 48.0
branch 0 6 0.0
condition 0 3 0.0
subroutine 4 7 57.1
pod 2 2 100.0
total 18 43 41.8


line stmt bran cond sub pod time code
1             package threads::variables::reap;
2              
3 1     1   23582 use strict;
  1         2  
  1         34  
4 1     1   6 use warnings;
  1         2  
  1         26  
5              
6 1     1   5 use Exporter;
  1         5  
  1         98  
7             our @ISA = qw(Exporter);
8             our @EXPORT = qw(reap);
9             our @EXPORT_OK = qw(reap reapref);
10              
11             our $VERSION = '0.06';
12              
13 1     1   6 use Scalar::Util qw(weaken reftype);
  1         2  
  1         355  
14             my @reapem;
15              
16 0     0 1   sub reap(\[$@%]) { my $ref = $_[0]; weaken($ref); push( @reapem, $ref ); }
  0            
  0            
17 0     0 1   sub reapref { my $ref = $_[0]; weaken($ref); push( @reapem, $ref ); }
  0            
  0            
18              
19             sub CLONE
20             {
21 0     0     foreach my $ref (@reapem)
22             {
23 0 0 0       if ( reftype($ref) eq "SCALAR" || reftype($ref) eq "REF" ) { $$ref = undef; }
  0            
24 0 0         if ( reftype($ref) eq "ARRAY" ) { @$ref = (); }
  0            
25 0 0         if ( reftype($ref) eq "HASH" ) { %$ref = (); }
  0            
26             }
27             }
28              
29             =pod
30              
31             =head1 NAME
32              
33             threads::variables::reap - reap variables in new threads
34              
35             =head1 SYNOPSIS
36              
37             use threads::variables::reap;
38              
39             my $bigObj = SomeBigObj->new(); # create some real big object
40             reap($bigObj); # force $bigObj being reaped in each other thread
41             # created after this line is passed
42              
43             =head1 DESCRIPTION
44              
45             This module provides a helper to ensure threads can/must have own instances
46             of some variables. It ensures that all variables marked to get Ced are
47             C in a new thread (instead being a clone like default behaviour).
48              
49             =head1 MOTIVATION
50              
51             I became inspired to create C when I was trying
52             to switch a logging framework in a multi-threaded application, which logged
53             to a database table, to L. I read often, L wasn't made
54             for threaded environments and here I was going to learn what it means in
55             real life. So I found myseld in the situation, Joshua described for me:
56             I
57             it when that happen.>
58              
59             Another reason was to read about optimization effort in large applications,
60             when they're going to be split in several threads. In those cases, all
61             unnecessary objects for worker threads (or attributes of some objects which
62             doesn't need to be available in other threads) could be marked with C<:reap>
63             and so new threads start with a small memory overhead.
64              
65             =head1 EXPORT
66              
67             This module exports only one function: C.
68              
69             =head1 SUBROUTINES/METHODS
70              
71             =head2 reap(\[$@%])
72              
73             Marks a given variable to get reaped in each other thread except the current.
74             This could either help saving memory or increase safety when using modules
75             which are known being not thread-safe.
76              
77             Be careful about the calling convention of this function: it's taking the
78             reference of the variable you'll give to it. So using C
79             will abort with compilation errors and is not intended to work. Use I
80             in those cases.
81              
82             =head2 reapref
83              
84             Marks the variable to get reaped in each new thread where the reference
85             points to. This function isn't exported by default and shouldn't be used
86             unless you're familiar with references and how cloning for new threads
87             work.
88              
89             =head1 AUTHOR
90              
91             Jens Rehsack, C<< >>
92              
93             =head1 BUGS
94              
95             Please report any bugs or feature requests to C,
96             or through the web interface at L.
97             I will be notified, and then you'll automatically be notified of progress
98             on your bug as I make changes.
99              
100             =head1 LIMITATIONS
101              
102             I wonder if variables could be marked as shared (using L)
103             and for reaping seamless. This makes it impossible to give parameter objects
104             for threads attributes that will be reaped for new threads, especially when
105             used in common with L.
106              
107             =head1 SUPPORT
108              
109             You can find documentation for this module with the perldoc command.
110              
111             perldoc threads::variables::reap
112              
113             You can also look for information at:
114              
115             =over 4
116              
117             =item * RT: CPAN's request tracker
118              
119             L
120              
121             =item * AnnoCPAN: Annotated CPAN documentation
122              
123             L
124              
125             =item * CPAN Ratings
126              
127             L
128              
129             =item * Search CPAN
130              
131             L
132              
133             =back
134              
135             =head1 ACKNOWLEDGEMENTS
136              
137             Larry Wall for giving us Perl - all our modules provide on his work.
138             David Golden for his great contribution about Perl and threads on PerlMonks
139             (see http://www.perlmonks.org/?node_id=483162).
140             Steffen Mueller for Attribute::Handlers and the helpful explanantion there.
141             Andrew Main, Adam Kennedy and Joshua ben Jore helping me pointing my problem
142             I'm going to solve with this module.
143              
144             =head1 LICENSE AND COPYRIGHT
145              
146             Copyright 2010 Jens Rehsack.
147              
148             This program is free software; you can redistribute it and/or modify it
149             under the terms of either: the GNU General Public License as published
150             by the Free Software Foundation; or the Artistic License.
151              
152             See http://dev.perl.org/licenses/ for more information.
153              
154             =cut
155              
156             1; # End of threads::variables::reap