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