1 package threads::shared;
6 use Scalar::Util qw(weaken);
7 use attributes qw(reftype);
10 if($Config{'useithreads'} && $threads::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;
19 *cond_wait = \&cond_wait_enabled;
20 *cond_signal = \&cond_signal_enabled;
21 *cond_broadcast = \&cond_broadcast_enabled;
22 *unlock = \&unlock_enabled;
23 *lock = \&lock_enabled;
29 our @ISA = qw(Exporter DynaLoader);
31 our @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock lock);
32 our $VERSION = '0.01';
36 sub cond_wait_disabled { return @_ };
37 sub cond_signal_disabled { return @_};
38 sub cond_broadcast_disabled { return @_};
39 sub unlock_disabled { 1 };
40 sub lock_disabled { 1 }
41 sub share_disabled { return @_}
43 sub share_enabled (\[$@%]) { # \]
45 my $ref = reftype($value);
46 if($ref eq 'SCALAR') {
47 my $obj = \threads::shared::sv->new($$value);
48 bless $obj, 'threads::shared::sv';
49 $shared{$$obj} = $value;
50 weaken($shared{$$obj});
51 } elsif($ref eq "ARRAY") {
52 tie @$value, 'threads::shared::av', $value;
53 } elsif($ref eq "HASH") {
54 tie %$value, "threads::shared::hv", $value;
56 die "You cannot share ref of type $_[0]\n";
61 return unless($_[0] eq "threads::shared");
62 foreach my $ptr (keys %shared) {
64 thrcnt_inc($shared{$ptr});
72 delete($shared{$$self});
75 package threads::shared::sv;
76 use base 'threads::shared';
80 package threads::shared::av;
81 use base 'threads::shared';
82 use Scalar::Util qw(weaken);
86 my $self = bless \threads::shared::av->new($value),'threads::shared::av';
87 $shared{$self->ptr} = $value;
88 weaken($shared{$self->ptr});
92 package threads::shared::hv;
93 use base 'threads::shared';
94 use Scalar::Util qw(weaken);
98 my $self = bless \threads::shared::hv->new($value),'threads::shared::hv';
99 $shared{$self->ptr} = $value;
100 weaken($shared{$self->ptr});
104 package threads::shared;
105 bootstrap threads::shared $VERSION;
111 threads::shared - Perl extension for sharing data structures between threads
117 my($foo, @foo, %foo);
122 $hash{bar} = share({});
127 cond_broadcast(\@array);
128 cond_signal($scalar);
132 This modules allows you to share() variables. These variables will
133 then be shared across different threads (and pseudoforks on
134 win32). They are used together with the threads module.
138 share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
143 Does not support splice on arrays!
147 Arthur Bergman E<lt>arthur at contiller.seE<gt>
149 threads::shared is released under the same license as Perl