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_disabled;
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;
104 $threads::shared::threads_shared = 1;
106 bootstrap threads::shared $VERSION;
112 threads::shared - Perl extension for sharing data structures between threads
118 my($foo, @foo, %foo);
123 $hash{bar} = share({});
128 cond_broadcast(@array);
133 This modules allows you to share() variables. These variables will
134 then be shared across different threads (and pseudoforks on
135 win32). They are used together with the threads module.
139 C<share>, C<lock>, C<unlock>, C<cond_wait>, C<cond_signal>, C<cond_broadcast>
147 C<share> takes a value and marks it as shared, you can share a scalar, array, hash
148 scalar ref, array ref and hash ref, C<share> will return the shared value.
150 C<share> will traverse up references exactly I<one> level.
151 C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not.
155 C<lock> places a lock on a variable until the lock goes out of scope. If
156 the variable is locked by another thread, the C<lock> call will block until
157 it's available. C<lock> is recursive, so multiple calls to C<lock> are
158 safe--the variable will remain locked until the outermost lock on the
159 variable goes out of scope or C<unlock> is called enough times to match
160 the number of calls to <lock>.
162 If a container object, such as a hash or array, is locked, all the elements
163 of that container are not locked. For example, if a thread does a C<lock
164 @a>, any other thread doing a C<lock($a[12])> won't block.
166 C<lock> will traverse up references exactly I<one> level.
167 C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
170 =item unlock VARIABLE
172 C<unlock> takes a locked shared value and decrements the lock count.
173 If the lock count is zero the variable is unlocked. It is not necessary
174 to call C<unlock> but it can be usefull to reduce lock contention.
176 C<unlock> will traverse up references exactly I<one> level.
177 C<unlock(\$a)> is equivalent to C<unlock($a)>, while C<unlock(\\$a)> is not.
179 =item cond_wait VARIABLE
181 The C<cond_wait> function takes a B<locked> variable as a parameter,
182 unlocks the variable, and blocks until another thread does a C<cond_signal>
183 or C<cond_broadcast> for that same locked variable. The variable that
184 C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
185 If there are multiple threads C<cond_wait>ing on the same variable, all but
186 one will reblock waiting to reaquire the lock on the variable. (So if
187 you're only using C<cond_wait> for synchronization, give up the lock as
190 It is important to note that the variable can be notified even if no
191 thread C<cond_signal> or C<cond_broadcast> on the variable. It is therefore
192 important to check the value of the variable and go back to waiting if the
193 requirment is not fullfilled.
195 =item cond_signal VARIABLE
197 The C<cond_signal> function takes a B<locked> variable as a parameter and
198 unblocks one thread that's C<cond_wait>ing on that variable. If more than
199 one thread is blocked in a C<cond_wait> on that variable, only one (and
200 which one is indeterminate) will be unblocked.
202 If there are no threads blocked in a C<cond_wait> on the variable, the
205 =item cond_broadcast VARIABLE
207 The C<cond_broadcast> function works similarly to C<cond_signal>.
208 C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
209 in a C<cond_wait> on the locked variable, rather than only one.
214 threads::shared is designed to disable itself silently if threads are
215 not available. If you want access to threads, you must C<use threads>
216 before you C<use threads::shared>. threads will emit a warning if you
217 use it after threads::shared.
221 C<bless> is not supported on shared references, in the current version
222 C<bless> will only bless the thread local reference and the blessing
223 will not propagate to the other threads, this is expected to be implmented
226 Does not support splice on arrays!
230 Arthur Bergman E<lt>arthur at contiller.seE<gt>
232 threads::shared is released under the same license as Perl
234 Documentation borrowed from Thread.pm