1 package threads::shared;
6 use Scalar::Util qw(weaken);
7 use attributes qw(reftype);
10 if($Config{'useithreads'} && $Config::threads) {
11 *share = \&share_enabled;
12 *cond_wait = \&cond_wait_disabled;
13 *cond_signal = \&cond_signal_disabled;
14 *cond_broadcast = \&cond_broadcast_disabled;
15 *unlock = \&unlock_disabled;
16 *lock = \&lock_disabled;
18 *share = \&share_enabled;
24 our @ISA = qw(Exporter DynaLoader);
26 our @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock lock);
27 our $VERSION = '0.01';
31 sub cond_wait_disabled { return @_ };
32 sub cond_signal_disabled { return @_};
33 sub cond_broadcast_disabled { return @_};
34 sub unlock_disabled { 1 };
35 sub lock_disabled { 1 }
36 sub share_disabled { return @_}
38 sub share_enabled (\[$@%]) { # \]
40 my $ref = reftype($value);
41 if($ref eq 'SCALAR') {
42 my $obj = \threads::shared::sv->new($$value);
43 bless $obj, 'threads::shared::sv';
44 $shared{$$obj} = $value;
45 weaken($shared{$$obj});
46 } elsif($ref eq "ARRAY") {
47 tie @$value, 'threads::shared::av', $value;
48 } elsif($ref eq "HASH") {
49 tie %$value, "threads::shared::hv", $value;
51 die "You cannot share ref of type $_[0]\n";
56 return unless($_[0] eq "threads::shared");
57 foreach my $ptr (keys %shared) {
59 thrcnt_inc($shared{$ptr});
67 delete($shared{$$self});
70 package threads::shared::sv;
71 use base 'threads::shared';
75 package threads::shared::av;
76 use base 'threads::shared';
77 use Scalar::Util qw(weaken);
81 my $self = bless \threads::shared::av->new($value),'threads::shared::av';
82 $shared{$self->ptr} = $value;
83 weaken($shared{$self->ptr});
87 package threads::shared::hv;
88 use base 'threads::shared';
89 use Scalar::Util qw(weaken);
93 my $self = bless \threads::shared::hv->new($value),'threads::shared::hv';
94 $shared{$self->ptr} = $value;
95 weaken($shared{$self->ptr});
99 package threads::shared;
100 bootstrap threads::shared $VERSION;
106 threads::shared - Perl extension for sharing data structures between threads
112 my($foo, @foo, %foo);
117 $hash{bar} = share({});
122 cond_broadcast(\@array);
123 cond_signal($scalar);
127 This modules allows you to share() variables. These variables will
128 then be shared across different threads (and pseudoforks on
129 win32). They are used together with the threads module.
133 share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
138 Does not support splice on arrays!
142 Arthur Bergman E<lt>arthur at contiller.seE<gt>
144 threads::shared is released under the same license as Perl