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 package threads::shared::sv;
60 use base 'threads::shared';
64 package threads::shared::av;
65 use base 'threads::shared';
66 use Scalar::Util qw(weaken);
70 my $self = bless \threads::shared::av->new($value),'threads::shared::av';
71 $shared{$self->ptr} = $value;
72 weaken($shared{$self->ptr});
76 package threads::shared::hv;
77 use base 'threads::shared';
78 use Scalar::Util qw(weaken);
82 my $self = bless \threads::shared::hv->new($value),'threads::shared::hv';
83 $shared{$self->ptr} = $value;
84 weaken($shared{$self->ptr});
88 package threads::shared;
90 $threads::shared::threads_shared = 1;
92 bootstrap threads::shared $VERSION;
98 threads::shared - Perl extension for sharing data structures between threads
104 my($foo, @foo, %foo);
109 $hash{bar} = share({});
114 cond_broadcast(@array);
119 This modules allows you to share() variables. These variables will
120 then be shared across different threads (and pseudoforks on
121 win32). They are used together with the threads module.
125 C<share>, C<lock>, C<unlock>, C<cond_wait>, C<cond_signal>, C<cond_broadcast>
133 C<share> takes a value and marks it as shared, you can share a scalar, array, hash
134 scalar ref, array ref and hash ref, C<share> will return the shared value.
136 C<share> will traverse up references exactly I<one> level.
137 C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not.
141 C<lock> places a lock on a variable until the lock goes out of scope. If
142 the variable is locked by another thread, the C<lock> call will block until
143 it's available. C<lock> is recursive, so multiple calls to C<lock> are
144 safe--the variable will remain locked until the outermost lock on the
145 variable goes out of scope or C<unlock> is called enough times to match
146 the number of calls to <lock>.
148 If a container object, such as a hash or array, is locked, all the elements
149 of that container are not locked. For example, if a thread does a C<lock
150 @a>, any other thread doing a C<lock($a[12])> won't block.
152 C<lock> will traverse up references exactly I<one> level.
153 C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
156 =item unlock VARIABLE
158 C<unlock> takes a locked shared value and decrements the lock count.
159 If the lock count is zero the variable is unlocked. It is not necessary
160 to call C<unlock> but it can be usefull to reduce lock contention.
162 C<unlock> will traverse up references exactly I<one> level.
163 C<unlock(\$a)> is equivalent to C<unlock($a)>, while C<unlock(\\$a)> is not.
165 =item cond_wait VARIABLE
167 The C<cond_wait> function takes a B<locked> variable as a parameter,
168 unlocks the variable, and blocks until another thread does a C<cond_signal>
169 or C<cond_broadcast> for that same locked variable. The variable that
170 C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
171 If there are multiple threads C<cond_wait>ing on the same variable, all but
172 one will reblock waiting to reaquire the lock on the variable. (So if
173 you're only using C<cond_wait> for synchronization, give up the lock as
176 It is important to note that the variable can be notified even if no
177 thread C<cond_signal> or C<cond_broadcast> on the variable. It is therefore
178 important to check the value of the variable and go back to waiting if the
179 requirment is not fullfilled.
181 =item cond_signal VARIABLE
183 The C<cond_signal> function takes a B<locked> variable as a parameter and
184 unblocks one thread that's C<cond_wait>ing on that variable. If more than
185 one thread is blocked in a C<cond_wait> on that variable, only one (and
186 which one is indeterminate) will be unblocked.
188 If there are no threads blocked in a C<cond_wait> on the variable, the
191 =item cond_broadcast VARIABLE
193 The C<cond_broadcast> function works similarly to C<cond_signal>.
194 C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
195 in a C<cond_wait> on the locked variable, rather than only one.
200 threads::shared is designed to disable itself silently if threads are
201 not available. If you want access to threads, you must C<use threads>
202 before you C<use threads::shared>. threads will emit a warning if you
203 use it after threads::shared.
207 C<bless> is not supported on shared references, in the current version
208 C<bless> will only bless the thread local reference and the blessing
209 will not propagate to the other threads, this is expected to be implmented
212 Does not support splice on arrays!
216 Arthur Bergman E<lt>arthur at contiller.seE<gt>
218 threads::shared is released under the same license as Perl
220 Documentation borrowed from Thread.pm