Parallel::Workers::Shared - [One line description of module's purpose here]


Parallel-Workers documentation Contained in the Parallel-Workers distribution.

Index


Code Index:

NAME

Top

Parallel::Workers::Shared - [One line description of module's purpose here]

VERSION

Top

This document describes Parallel::Workers::Shared version 0.0.1

SYNOPSIS

Top

    use Parallel::Workers::Shared;

  


DESCRIPTION

Top

INTERFACE

Top

DIAGNOSTICS

Top

Error message here, perhaps with %s placeholders

[Description of error here]

Another error message here

[Description of error here]

[Et cetera, et cetera]

CONFIGURATION AND ENVIRONMENT

Top

Parallel::Workers::Shared requires no configuration files or environment variables.

DEPENDENCIES

Top

None.

INCOMPATIBILITIES

Top

None reported.

BUGS AND LIMITATIONS

Top

No bugs have been reported.

Please report any bugs or feature requests to bug-parallel-jobs@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Top

Olivier Evalet <evaleto@gelux.ch>

LICENCE AND COPYRIGHT

Top

DISCLAIMER OF WARRANTY

Top

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


Parallel-Workers documentation Contained in the Parallel-Workers distribution.

package Parallel::Workers::Shared;

use warnings;
use strict;
use Carp;
use Scalar::Util qw(blessed dualvar isweak readonly refaddr reftype tainted
                        weaken isvstring looks_like_number set_prototype);
use threads 1.39;
use threads::shared;
use Thread::Queue;
use Data::Dumper;


our (@ISA, @EXPORT, @EXPORT_OK);
@ISA = qw(Exporter);

@EXPORT = qw(shared_hash_set shared_share);
@EXPORT_OK = ();

# Adds fields to a shared object
sub shared_hash_set{
  my ($this, $tag, $value) = @_;
  share ($this) unless is_shared($this);
  lock($this);
  $this->{$tag} = shared_share($value);
}

# Make a thread-shared version of a complex data structure or object
sub shared_share{
  my $in = shift;
# If already thread-shared, then just return the input
  return ($in) if (is_shared($in));
#  print "shared_share( ".ref($in).")\n";

# Make copies of array, hash and scalar refs
  my $out;
  if (my $ref_type = reftype($in)) {
# Copy an array ref
    if ($ref_type eq 'ARRAY') {
# Make empty shared array ref
      $out = &share([]);
# Recursively copy and add contents
      foreach my $val (@$in) {
        push(@$out, shared_share($val));
      }
    }

# Copy a hash ref
    elsif ($ref_type eq 'HASH') {
# Make empty shared hash ref
      $out = &share({});
# Recursively copy and add contents
      foreach my $key (keys(%{$in})) {
        $out->{$key} = shared_share($in->{$key});
      }
    }

# Copy a scalar ref
    elsif ($ref_type eq 'SCALAR') {
      $out = \do{ my $scalar = $$in; };
        share($out);
    }
  }

# If copy created above ...
  if ($out) {
# Clone READONLY flag
    if (Internals::SvREADONLY($in)) {
      Internals::SvREADONLY($out, 1);
    }
# Make blessed copy, if applicable
    if (my $class = blessed($in)) {
      bless($out, $class);
    }
# Return copy
    return ($out);
  }

# Just return anything else
# NOTE: This will generate an error if we're thread-sharing,
#       and $in is not an ordinary scalar.
  return ($in);
}


1; # Magic true value required at end of module
__END__