This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump $VERSION in many modules that have changed.
[perl5.git] / ext / threads / shared / shared.pm
CommitLineData
b050c948 1package threads::shared;
73e09c8f 2
c46325ea 3use 5.008;
b050c948
AB
4use strict;
5use warnings;
5c360ac5
AB
6BEGIN {
7 require Exporter;
8 our @ISA = qw(Exporter);
a0e036c1 9 our @EXPORT = qw(share cond_wait cond_timedwait cond_broadcast cond_signal);
98225a64 10 our $VERSION = '0.94';
73e09c8f 11
5c360ac5 12 if ($threads::threads) {
6f942b98 13 *cond_wait = \&cond_wait_enabled;
a0e036c1 14 *cond_timedwait = \&cond_timedwait_enabled;
6f942b98
AB
15 *cond_signal = \&cond_signal_enabled;
16 *cond_broadcast = \&cond_broadcast_enabled;
9c4972d9
NIS
17 require XSLoader;
18 XSLoader::load('threads::shared',$VERSION);
5c360ac5
AB
19 push @EXPORT,'bless';
20 }
21 else {
b050c948 22
df5c998e
EM
23# String eval is generally evil, but we don't want these subs to exist at all
24# if threads are loaded successfully. Vivifying them conditionally this way
25# saves on average about 4K of memory per thread.
b050c948 26
df5c998e 27 eval <<'EOD';
a0e036c1
MP
28sub cond_wait (\[$@%];\[$@%]) { undef }
29sub cond_timedwait (\[$@%]$;\[$@%]) { undef }
30sub cond_signal (\[$@%]) { undef }
31sub cond_broadcast (\[$@%]) { undef }
32sub share (\[$@%]) { return $_[0] }
df5c998e
EM
33EOD
34 }
35}
b050c948 36
dab065ea
AB
37$threads::shared::threads_shared = 1;
38
6b85e4fe
NIS
39sub threads::shared::tie::SPLICE
40{
41 die "Splice not implemented for shared arrays";
42}
43
b050c948
AB
44__END__
45
46=head1 NAME
47
48threads::shared - Perl extension for sharing data structures between threads
49
50=head1 SYNOPSIS
51
73e09c8f 52 use threads;
b050c948
AB
53 use threads::shared;
54
38875929 55 my $var : shared;
ca5ff8b2
DM
56 $var = $scalar_value;
57 $var = $shared_ref_value;
58 $var = &share($simple_unshared_ref_value);
59 $var = &share(new Foo);
38875929 60
4cab98c0
SG
61 my($scalar, @array, %hash);
62 share($scalar);
63 share(@array);
aaf3876d 64 share(%hash);
caf25f3b
AB
65 my $bar = &share([]);
66 $hash{bar} = &share({});
b050c948 67
38875929
DM
68 { lock(%hash); ... }
69
b050c948 70 cond_wait($scalar);
a0e036c1 71 cond_timedwait($scalar, time() + 30);
515f0976
AB
72 cond_broadcast(@array);
73 cond_signal(%hash);
b050c948 74
a0e036c1
MP
75 my $lockvar : shared;
76 # condition var != lock var
77 cond_wait($var, $lockvar);
78 cond_timedwait($var, time()+30, $lockvar);
79
b050c948
AB
80=head1 DESCRIPTION
81
38875929
DM
82By default, variables are private to each thread, and each newly created
83thread gets a private copy of each existing variable. This module allows
32419a4c
JH
84you to share variables across different threads (and pseudoforks on Win32).
85It is used together with the threads module.
b050c948 86
515f0976 87=head1 EXPORT
b050c948 88
a0e036c1 89C<share>, C<cond_wait>, C<cond_timedwait>, C<cond_signal>, C<cond_broadcast>
515f0976 90
e67b86b3 91Note that if this module is imported when C<threads> has not yet been
32419a4c
JH
92loaded, then these functions all become no-ops. This makes it possible
93to write modules that will work in both threaded and non-threaded
e67b86b3
DM
94environments.
95
515f0976
AB
96=head1 FUNCTIONS
97
98=over 4
99
100=item share VARIABLE
101
86c43dd6
JH
102C<share> takes a value and marks it as shared. You can share a scalar,
103array, hash, scalar ref, array ref or hash ref. C<share> will return
0a9af0ff 104the shared rvalue but always as a reference.
515f0976
AB
105
106C<share> will traverse up references exactly I<one> level.
107C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not.
ca5ff8b2
DM
108This means that you must create nested shared data structures by first
109creating individual shared leaf notes, then adding them to a shared hash
110or array.
515f0976 111
38875929
DM
112A variable can also be marked as shared at compile time by using the
113C<shared> attribute: C<my $var : shared>.
114
86c43dd6
JH
115If you want to share a newly created reference unfortunately you
116need to use C<&share([])> and C<&share({})> syntax due to problems
117with Perl's prototyping.
caf25f3b 118
ca5ff8b2
DM
119The only values that can be assigned to a shared scalar are other scalar
120values, or shared refs, eg
121
122 my $var : shared;
123 $var = 1; # ok
124 $var = &share([]); # ok
125 $var = []; # error
126 $var = A->new; # error
127 $var = &share(A->new); # ok as long as the A object is not nested
128
129Note that it is often not wise to share an object unless the class itself
130has been written to support sharing; for example, an object's destructor
131may get called multiple times, one for each thread's scope exit.
132
515f0976
AB
133=item lock VARIABLE
134
32419a4c
JH
135C<lock> places a lock on a variable until the lock goes out of scope.
136If the variable is locked by another thread, the C<lock> call will
137block until it's available. C<lock> is recursive, so multiple calls
138to C<lock> are safe -- the variable will remain locked until the
139outermost lock on the variable goes out of scope.
515f0976 140
32419a4c
JH
141If a container object, such as a hash or array, is locked, all the
142elements of that container are not locked. For example, if a thread
143does a C<lock @a>, any other thread doing a C<lock($a[12])> won't block.
515f0976
AB
144
145C<lock> will traverse up references exactly I<one> level.
146C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
147
32419a4c
JH
148Note that you cannot explicitly unlock a variable; you can only wait
149for the lock to go out of scope. If you need more fine-grained
83272a45 150control, see L<Thread::Semaphore>.
515f0976
AB
151
152=item cond_wait VARIABLE
153
a0e036c1
MP
154=item cond_wait CONDVAR, LOCKVAR
155
515f0976 156The C<cond_wait> function takes a B<locked> variable as a parameter,
32419a4c
JH
157unlocks the variable, and blocks until another thread does a
158C<cond_signal> or C<cond_broadcast> for that same locked variable.
159The variable that C<cond_wait> blocked on is relocked after the
160C<cond_wait> is satisfied. If there are multiple threads
161C<cond_wait>ing on the same variable, all but one will reblock waiting
162to reacquire the lock on the variable. (So if you're only using
163C<cond_wait> for synchronisation, give up the lock as soon as
164possible). The two actions of unlocking the variable and entering the
a0e036c1 165blocked wait state are atomic, the two actions of exiting from the
38875929 166blocked wait state and relocking the variable are not.
515f0976 167
a0e036c1
MP
168In its second form, C<cond_wait> takes a shared, B<unlocked> variable
169followed by a shared, B<locked> variable. The second variable is
170unlocked and thread execution suspended until another thread signals
171the first variable.
172
32419a4c
JH
173It is important to note that the variable can be notified even if
174no thread C<cond_signal> or C<cond_broadcast> on the variable.
175It is therefore important to check the value of the variable and
a0e036c1
MP
176go back to waiting if the requirement is not fulfilled. For example,
177to pause until a shared counter drops to zero:
178
179 { lock($counter); cond_wait($count) until $counter == 0; }
180
181=item cond_timedwait VARIABLE, ABS_TIMEOUT
182
183=item cond_timedwait CONDVAR, ABS_TIMEOUT, LOCKVAR
184
185In its two-argument form, C<cond_timedwait> takes a B<locked> variable
186and an absolute timeout as parameters, unlocks the variable, and blocks
187until the timeout is reached or another thread signals the variable. A
188false value is returned if the timeout is reached, and a true value
189otherwise. In either case, the variable is re-locked upon return.
190
191Like C<cond_wait>, this function may take a shared, B<locked> variable
192as an additional parameter; in this case the first parameter is an
193B<unlocked> condition variable protected by a distinct lock variable.
194
195Again like C<cond_wait>, waking up and reacquiring the lock are not
196atomic, and you should always check your desired condition after this
197function returns. Since the timeout is an absolute value, however, it
198does not have to be recalculated with each pass:
199
200 lock($var);
201 my $abs = time() + 15;
202 until ($ok = desired_condition($var)) {
203 last if !cond_timedwait($var, $abs);
204 }
205 # we got it if $ok, otherwise we timed out!
515f0976
AB
206
207=item cond_signal VARIABLE
208
32419a4c
JH
209The C<cond_signal> function takes a B<locked> variable as a parameter
210and unblocks one thread that's C<cond_wait>ing on that variable. If
211more than one thread is blocked in a C<cond_wait> on that variable,
212only one (and which one is indeterminate) will be unblocked.
515f0976 213
32419a4c
JH
214If there are no threads blocked in a C<cond_wait> on the variable,
215the signal is discarded. By always locking before signaling, you can
216(with care), avoid signaling before another thread has entered cond_wait().
38875929
DM
217
218C<cond_signal> will normally generate a warning if you attempt to use it
219on an unlocked variable. On the rare occasions where doing this may be
220sensible, you can skip the warning with
221
222 { no warnings 'threads'; cond_signal($foo) }
515f0976
AB
223
224=item cond_broadcast VARIABLE
225
226The C<cond_broadcast> function works similarly to C<cond_signal>.
32419a4c
JH
227C<cond_broadcast>, though, will unblock B<all> the threads that are
228blocked in a C<cond_wait> on the locked variable, rather than only one.
b050c948 229
4cab98c0 230=back
dab065ea
AB
231
232=head1 NOTES
233
8c5dce87 234threads::shared is designed to disable itself silently if threads are
dab065ea
AB
235not available. If you want access to threads, you must C<use threads>
236before you C<use threads::shared>. threads will emit a warning if you
8c5dce87 237use it after threads::shared.
dab065ea 238
b050c948
AB
239=head1 BUGS
240
4cab98c0 241C<bless> is not supported on shared references. In the current version,
515f0976 242C<bless> will only bless the thread local reference and the blessing
4cab98c0
SG
243will not propagate to the other threads. This is expected to be
244implemented in a future version of Perl.
515f0976 245
b050c948 246Does not support splice on arrays!
b050c948 247
58122748
JH
248Taking references to the elements of shared arrays and hashes does not
249autovivify the elements, and neither does slicing a shared array/hash
250over non-existent indices/keys autovivify the elements.
251
72ac79b3
SB
252share() allows you to C<< share $hashref->{key} >> without giving any error
253message. But the C<< $hashref->{key} >> is B<not> shared, causing the error
3d32476b 254"locking can only be used on shared values" to occur when you attempt to
72ac79b3 255C<< lock $hasref->{key} >>.
3d32476b 256
b050c948
AB
257=head1 AUTHOR
258
aaf3876d 259Arthur Bergman E<lt>arthur at contiller.seE<gt>
b050c948 260
aaf3876d 261threads::shared is released under the same license as Perl
b050c948 262
5e549d84 263Documentation borrowed from the old Thread.pm
515f0976 264
b050c948
AB
265=head1 SEE ALSO
266
5e549d84 267L<threads>, L<perlthrtut>, L<http://www.perl.com/pub/a/2002/06/11/threads.html>
b050c948
AB
268
269=cut