| 1 |
package Thread::Serialize; |
| 2 |
|
| 3 |
$VERSION= '0.14'; |
| 4 |
|
| 5 |
# be as strict as possble |
| 6 |
use strict; |
| 7 |
|
| 8 |
# modules that we need |
| 9 |
use load; |
| 10 |
use Storable (); |
| 11 |
|
| 12 |
# use ourselves to determine signature |
| 13 |
our $iced; |
| 14 |
if ( $Thread::Serialize::no_external_perl ) { |
| 15 |
$iced= unpack( 'l',Storable::freeze( [] )); |
| 16 |
$Thread::Serialize::no_external_perl= 'Signature obtained locally'; |
| 17 |
} |
| 18 |
|
| 19 |
# use external perl to create signature |
| 20 |
else { |
| 21 |
open( my $handle, |
| 22 |
qq($^X -MStorable -e "print unpack('l',Storable::freeze( [] ))" | ) |
| 23 |
) or die "Cannot determine Storable signature\n"; |
| 24 |
$iced= readline $handle; |
| 25 |
} |
| 26 |
|
| 27 |
# satisfy -require- |
| 28 |
1; |
| 29 |
|
| 30 |
# The following subroutines are loaded on demand only |
| 31 |
|
| 32 |
__END__ |
| 33 |
|
| 34 |
#------------------------------------------------------------------------------- |
| 35 |
# freeze |
| 36 |
# |
| 37 |
# Freeze given parameters into a single scalar value |
| 38 |
# |
| 39 |
# IN: 1..N parameters to freeze |
| 40 |
# OUT: 1 frozen scalar |
| 41 |
|
| 42 |
sub freeze { |
| 43 |
|
| 44 |
# something to be frozen |
| 45 |
if (@_) { |
| 46 |
|
| 47 |
# any of the parameters is special, so really freeze |
| 48 |
foreach (@_) { |
| 49 |
return Storable::freeze( \@_ ) if !defined() or ref() or m#\0#; |
| 50 |
} |
| 51 |
|
| 52 |
# just concatenate with null bytes (WAY faster) |
| 53 |
return join( "\0", @_ ); |
| 54 |
} |
| 55 |
|
| 56 |
# no parameters, so just undef |
| 57 |
return; |
| 58 |
} #freeze |
| 59 |
|
| 60 |
#------------------------------------------------------------------------------- |
| 61 |
# IN: 1 frozen scalar to defrost |
| 62 |
# OUT: 1..N thawed data structure |
| 63 |
|
| 64 |
sub thaw { |
| 65 |
|
| 66 |
# nothing here or not interested |
| 67 |
return if !defined( $_[0] ) or !defined(wantarray); |
| 68 |
|
| 69 |
# return as list |
| 70 |
if (wantarray) { |
| 71 |
return ( unpack( 'l', $_[0] ) || 0 ) == $iced |
| 72 |
? @{ Storable::thaw( $_[0] ) } |
| 73 |
: split "\0", $_[0]; |
| 74 |
} |
| 75 |
|
| 76 |
# scalar, really frozen, return first value |
| 77 |
elsif ( ( unpack( 'l',$_[0] ) || 0 ) == $iced ) { |
| 78 |
return Storable::thaw( $_[0] )->[0]; |
| 79 |
} |
| 80 |
|
| 81 |
# return first part of scalar |
| 82 |
return $_[0] =~ m#^([^\0]*)# ? $1 : $_[0]; |
| 83 |
} #thaw |
| 84 |
|
| 85 |
#------------------------------------------------------------------------------- |
| 86 |
# |
| 87 |
# Standard Perl Features |
| 88 |
# |
| 89 |
#------------------------------------------------------------------------------- |
| 90 |
|
| 91 |
sub import { |
| 92 |
shift; |
| 93 |
|
| 94 |
# determine namespace and subs |
| 95 |
my $namespace= caller().'::'; |
| 96 |
@_= qw( freeze thaw ) if !@_; |
| 97 |
|
| 98 |
# do our exports |
| 99 |
no strict 'refs'; |
| 100 |
*{$namespace.$_}= \&$_ foreach @_; |
| 101 |
|
| 102 |
return; |
| 103 |
} #import |
| 104 |
|
| 105 |
#------------------------------------------------------------------------------- |
| 106 |
|
| 107 |
__END__ |
| 108 |
|
| 109 |
=head1 NAME |
| 110 |
|
| 111 |
Thread::Serialize - serialize data-structures between threads |
| 112 |
|
| 113 |
=head1 SYNOPSIS |
| 114 |
|
| 115 |
use Thread::Serialize; # export freeze() and thaw() |
| 116 |
|
| 117 |
use Thread::Serialize (); # must call fully qualified subs |
| 118 |
|
| 119 |
my $frozen = freeze( any data structure ); |
| 120 |
any data structure = thaw( $frozen ); |
| 121 |
|
| 122 |
=head1 VERSION |
| 123 |
|
| 124 |
This documentation describes version 0.14. |
| 125 |
|
| 126 |
=head1 DESCRIPTION |
| 127 |
|
| 128 |
*** A note of CAUTION *** |
| 129 |
|
| 130 |
This module only functions on threaded perl or an unthreaded perl |
| 131 |
with the "forks" module installed. |
| 132 |
|
| 133 |
Please also note that this documentation describes the "maint" version |
| 134 |
of this code. This version is essentially frozen. Please use a 5.14 |
| 135 |
or higher version of perl for the "blead" version of this code. |
| 136 |
|
| 137 |
************************* |
| 138 |
|
| 139 |
The Thread::Serialize module is a library for centralizing the routines |
| 140 |
used to serialize data-structures between threads. Because of this central |
| 141 |
location, other modules such as L<Thread::Conveyor>, L<Thread::Pool> or |
| 142 |
L<Thread::Tie> can benefit from the same optimilizations that may take |
| 143 |
place here in the future. |
| 144 |
|
| 145 |
=head1 SUBROUTINES |
| 146 |
|
| 147 |
There are only two subroutines. |
| 148 |
|
| 149 |
=head2 freeze |
| 150 |
|
| 151 |
my $frozen = freeze( $scalar ); |
| 152 |
|
| 153 |
my $frozen = freeze( @array ); |
| 154 |
|
| 155 |
The "freeze" subroutine takes all the parameters passed to it, freezes them |
| 156 |
and returns a frozen representation of what was given. The parameters can |
| 157 |
be scalar values or references to arrays or hashes. Use the L<thaw> |
| 158 |
subroutine to obtain the original data-structure back. |
| 159 |
|
| 160 |
=head2 thaw |
| 161 |
|
| 162 |
my $scalar = thaw( $frozen ); |
| 163 |
|
| 164 |
my @array = thaw( $frozen ); |
| 165 |
|
| 166 |
The "thaw" subroutine returns the data-structure that was frozen with a call |
| 167 |
to L<freeze>. If called in a scalar context, only the first element of the |
| 168 |
data-structure that was passed, will be returned. Otherwise the entire |
| 169 |
data-structure will be returned. |
| 170 |
|
| 171 |
It is up to the developer to make sure that single argument calls to L<freeze> |
| 172 |
are always matched by scalar context calls to L<thaw>. |
| 173 |
|
| 174 |
=head1 REQUIRED MODULES |
| 175 |
|
| 176 |
load (0.10) |
| 177 |
Storable (any) |
| 178 |
|
| 179 |
=head1 OPTIMIZATIONS |
| 180 |
|
| 181 |
To reduce memory and CPU usage, this module uses L<load>. This causes |
| 182 |
subroutines only to be compiled in a thread when they are actually needed at |
| 183 |
the expense of more CPU when they need to be compiled. Simple benchmarks |
| 184 |
however revealed that the overhead of the compiling single routines is not |
| 185 |
much more (and sometimes a lot less) than the overhead of cloning a Perl |
| 186 |
interpreter with a lot of subroutines pre-loaded. |
| 187 |
|
| 188 |
To reduce the number of modules and subroutines loaded, an external Perl |
| 189 |
interpreter is started to determine the Storable signature at compile time. |
| 190 |
In some situations this may cause a problem: please set the |
| 191 |
C<$Thread::Serialize::no_external_perl> variable to a true value at compile |
| 192 |
time B<before> loading Thread::Serialize if this causes a problem. |
| 193 |
|
| 194 |
BEGIN { $Thread::Serialize::no_external_perl= 1 } |
| 195 |
use Thread::Serialize; |
| 196 |
|
| 197 |
=head1 KNOWN ISSUES |
| 198 |
|
| 199 |
=head2 Embedded Perls |
| 200 |
|
| 201 |
Philip Monsen reported that in the case of an embedded Perl interpreter (e.g. |
| 202 |
in a C program), the use of an external executor to determine the Storable |
| 203 |
signature, causes problems. This has been fixed by introducing the global |
| 204 |
variable C<$Thread::Serialize::no_external_perl> (see L<OPTIMIZATIONS>). |
| 205 |
|
| 206 |
=head1 AUTHOR |
| 207 |
|
| 208 |
Elizabeth Mattijsen, <[email protected]>. |
| 209 |
|
| 210 |
Please report bugs to <[email protected]>. |
| 211 |
|
| 212 |
=head1 COPYRIGHT |
| 213 |
|
| 214 |
Copyright (c) 2002, 2003, 2004, 2010, 2012 Elizabeth Mattijsen <[email protected]>. |
| 215 |
All rights reserved. This program is free software; you can redistribute it |
| 216 |
and/or modify it under the same terms as Perl itself. |
| 217 |
|
| 218 |
=head1 SEE ALSO |
| 219 |
|
| 220 |
L<load>, L<Thread::Conveyor>, L<Thread::Pool>, L<Thread::Tie>. |
| 221 |
|
| 222 |
=cut |