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_enabled;
13 *cond_signal = \&cond_signal_enabled;
14 *cond_broadcast = \&cond_broadcast_enabled;
15 *unlock = \&unlock_enabled;
17 *share = \&share_disabled;
18 *cond_wait = \&cond_wait_disabled;
19 *cond_signal = \&cond_signal_disabled;
20 *cond_broadcast = \&cond_broadcast_dosabled;
21 *unlock = \&unlock_disabled;
27 our @ISA = qw(Exporter DynaLoader);
29 our @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock);
30 our $VERSION = '0.01';
34 sub cond_wait_disabled { return @_ };
35 sub cond_signal_disabled { return @_};
36 sub cond_broadcast_disabled { return @_};
37 sub unlock_disabled { 1 };
38 sub lock_disabled { 1 }
39 sub share_disabled { return @_}
41 sub share_enabled (\[$@%]) { # \]
43 my $ref = reftype($value);
44 if($ref eq 'SCALAR') {
45 my $obj = \threads::shared::sv->new($$value);
46 bless $obj, 'threads::shared::sv';
47 $shared{$$obj} = $value;
48 weaken($shared{$$obj});
49 } elsif($ref eq "ARRAY") {
50 tie @$value, 'threads::shared::av', $value;
51 } elsif($ref eq "HASH") {
52 tie %$value, "threads::shared::hv", $value;
54 die "You cannot share ref of type $_[0]\n";
59 return unless($_[0] eq "threads::shared");
60 foreach my $ptr (keys %shared) {
62 thrcnt_inc($shared{$ptr});
70 delete($shared{$$self});
73 package threads::shared::sv;
74 use base 'threads::shared';
78 package threads::shared::av;
79 use base 'threads::shared';
80 use Scalar::Util qw(weaken);
84 my $self = bless \threads::shared::av->new($value),'threads::shared::av';
85 $shared{$self->ptr} = $value;
86 weaken($shared{$self->ptr});
90 package threads::shared::hv;
91 use base 'threads::shared';
92 use Scalar::Util qw(weaken);
96 my $self = bless \threads::shared::hv->new($value),'threads::shared::hv';
97 $shared{$self->ptr} = $value;
98 weaken($shared{$self->ptr});
102 package threads::shared;
103 bootstrap threads::shared $VERSION;
109 threads::shared - Perl extension for sharing data structures between threads
115 my($foo, @foo, %foo);
120 $hash{bar} = share({});
125 cond_broadcast(\@array);
126 cond_signal($scalar);
130 This modules allows you to share() variables. These variables will
131 then be shared across different threads (and pseudoforks on
132 win32). They are used together with the threads module.
136 share(), lock(), unlock(), cond_wait, cond_signal, cond_broadcast
141 Does not support splice on arrays!
145 Arthur Bergman E<lt>arthur at contiller.seE<gt>
147 threads::shared is released under the same license as Perl