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});
66 delete($shared{$$self});
69 package threads::shared::sv;
70 use base 'threads::shared';
74 package threads::shared::av;
75 use base 'threads::shared';
76 use Scalar::Util qw(weaken);
80 my $self = bless \threads::shared::av->new($value),'threads::shared::av';
81 $shared{$self->ptr} = $value;
82 weaken($shared{$self->ptr});
86 package threads::shared::hv;
87 use base 'threads::shared';
88 use Scalar::Util qw(weaken);
92 my $self = bless \threads::shared::hv->new($value),'threads::shared::hv';
93 $shared{$self->ptr} = $value;
94 weaken($shared{$self->ptr});
98 package threads::shared;
99 bootstrap threads::shared $VERSION;
105 threads::shared - Perl extension for sharing data structures between threads
111 my($foo, @foo, %foo);
116 $hash{bar} = share({});
121 cond_broadcast(\@array);
122 cond_signal($scalar);
126 This modules allows you to share() variables. These variables will
127 then be shared across different threads (and pseudoforks on
128 win32). They are used together with the threads module.
132 share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
137 Does not support splice on arrays!
141 Arthur Bergman E<lt>arthur at contiller.seE<gt>
143 threads::shared is released under the same license as Perl