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.90';
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},$threads::origthread);
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);
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 C<share>, C<lock>, C<unlock>, C<cond_wait>, C<cond_signal>, C<cond_broadcast>
144 C<share> takes a value and marks it as shared, you can share a scalar, array, hash
145 scalar ref, array ref and hash ref, C<share> will return the shared value.
147 C<share> will traverse up references exactly I<one> level.
148 C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not.
152 C<lock> places a lock on a variable until the lock goes out of scope. If
153 the variable is locked by another thread, the C<lock> call will block until
154 it's available. C<lock> is recursive, so multiple calls to C<lock> are
155 safe--the variable will remain locked until the outermost lock on the
156 variable goes out of scope or C<unlock> is called enough times to match
157 the number of calls to <lock>.
159 If a container object, such as a hash or array, is locked, all the elements
160 of that container are not locked. For example, if a thread does a C<lock
161 @a>, any other thread doing a C<lock($a[12])> won't block.
163 C<lock> will traverse up references exactly I<one> level.
164 C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
167 =item unlock VARIABLE
169 C<unlock> takes a locked shared value and decrements the lock count.
170 If the lock count is zero the variable is unlocked. It is not necessary
171 to call C<unlock> but it can be usefull to reduce lock contention.
173 C<unlock> will traverse up references exactly I<one> level.
174 C<unlock(\$a)> is equivalent to C<unlock($a)>, while C<unlock(\\$a)> is not.
176 =item cond_wait VARIABLE
178 The C<cond_wait> function takes a B<locked> variable as a parameter,
179 unlocks the variable, and blocks until another thread does a C<cond_signal>
180 or C<cond_broadcast> for that same locked variable. The variable that
181 C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
182 If there are multiple threads C<cond_wait>ing on the same variable, all but
183 one will reblock waiting to reaquire the lock on the variable. (So if
184 you're only using C<cond_wait> for synchronization, give up the lock as
187 It is important to note that the variable can be notified even if no
188 thread C<cond_signal> or C<cond_broadcast> on the variable. It is therefore
189 important to check the value of the variable and go back to waiting if the
190 requirment is not fullfilled.
192 =item cond_signal VARIABLE
194 The C<cond_signal> function takes a B<locked> variable as a parameter and
195 unblocks one thread that's C<cond_wait>ing on that variable. If more than
196 one thread is blocked in a C<cond_wait> on that variable, only one (and
197 which one is indeterminate) will be unblocked.
199 If there are no threads blocked in a C<cond_wait> on the variable, the
202 =item cond_broadcast VARIABLE
204 The C<cond_broadcast> function works similarly to C<cond_signal>.
205 C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
206 in a C<cond_wait> on the locked variable, rather than only one.
210 C<bless> is not supported on shared references, in the current version
211 C<bless> will only bless the thread local reference and the blessing
212 will not propagate to the other threads, this is expected to be implmented
215 Does not support splice on arrays!
219 Arthur Bergman E<lt>arthur at contiller.seE<gt>
221 threads::shared is released under the same license as Perl
223 Documentation borrowed from Thread.pm