threads::shared::queue and semaphore become Thread::Semaphore
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / shared.pm
CommitLineData
b050c948 1package threads::shared;
73e09c8f 2
3use 5.007_003;
b050c948 4use strict;
5use warnings;
73e09c8f 6
a446a88f 7require Exporter;
8our @ISA = qw(Exporter);
81f1a921 9our @EXPORT = qw(share cond_wait cond_broadcast cond_signal _refcnt _id _thrcnt);
a446a88f 10our $VERSION = '0.90';
11
caf25f3b 12if ($threads::threads) {
6f942b98 13 *cond_wait = \&cond_wait_enabled;
14 *cond_signal = \&cond_signal_enabled;
15 *cond_broadcast = \&cond_broadcast_enabled;
9c4972d9 16 require XSLoader;
17 XSLoader::load('threads::shared',$VERSION);
18}
19else {
a6b94e59 20 *share = \&share_disabled;
21 *cond_wait = \&cond_wait_disabled;
22 *cond_signal = \&cond_signal_disabled;
dab065ea 23 *cond_broadcast = \&cond_broadcast_disabled;
b050c948 24}
25
b050c948 26
b050c948 27sub cond_wait_disabled { return @_ };
28sub cond_signal_disabled { return @_};
29sub cond_broadcast_disabled { return @_};
b050c948 30sub share_disabled { return @_}
31
dab065ea 32$threads::shared::threads_shared = 1;
33
6b85e4fe 34
35sub threads::shared::tie::SPLICE
36{
37 die "Splice not implemented for shared arrays";
38}
39
b050c948 40__END__
41
42=head1 NAME
43
44threads::shared - Perl extension for sharing data structures between threads
45
46=head1 SYNOPSIS
47
73e09c8f 48 use threads;
b050c948 49 use threads::shared;
50
38875929 51 my $var : shared;
52
4cab98c0 53 my($scalar, @array, %hash);
54 share($scalar);
55 share(@array);
aaf3876d 56 share(%hash);
caf25f3b 57 my $bar = &share([]);
58 $hash{bar} = &share({});
b050c948 59
38875929 60 { lock(%hash); ... }
61
b050c948 62 cond_wait($scalar);
515f0976 63 cond_broadcast(@array);
64 cond_signal(%hash);
b050c948 65
66=head1 DESCRIPTION
67
38875929 68By default, variables are private to each thread, and each newly created
69thread gets a private copy of each existing variable. This module allows
32419a4c 70you to share variables across different threads (and pseudoforks on Win32).
71It is used together with the threads module.
b050c948 72
515f0976 73=head1 EXPORT
b050c948 74
38875929 75C<share>, C<lock>, C<cond_wait>, C<cond_signal>, C<cond_broadcast>
515f0976 76
e67b86b3 77Note that if this module is imported when C<threads> has not yet been
32419a4c 78loaded, then these functions all become no-ops. This makes it possible
79to write modules that will work in both threaded and non-threaded
e67b86b3 80environments.
81
515f0976 82=head1 FUNCTIONS
83
84=over 4
85
86=item share VARIABLE
87
86c43dd6 88C<share> takes a value and marks it as shared. You can share a scalar,
89array, hash, scalar ref, array ref or hash ref. C<share> will return
90the shared rvalue.
515f0976 91
92C<share> will traverse up references exactly I<one> level.
93C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not.
94
38875929 95A variable can also be marked as shared at compile time by using the
96C<shared> attribute: C<my $var : shared>.
97
86c43dd6 98If you want to share a newly created reference unfortunately you
99need to use C<&share([])> and C<&share({})> syntax due to problems
100with Perl's prototyping.
caf25f3b 101
515f0976 102=item lock VARIABLE
103
32419a4c 104C<lock> places a lock on a variable until the lock goes out of scope.
105If the variable is locked by another thread, the C<lock> call will
106block until it's available. C<lock> is recursive, so multiple calls
107to C<lock> are safe -- the variable will remain locked until the
108outermost lock on the variable goes out of scope.
515f0976 109
32419a4c 110If a container object, such as a hash or array, is locked, all the
111elements of that container are not locked. For example, if a thread
112does a C<lock @a>, any other thread doing a C<lock($a[12])> won't block.
515f0976 113
114C<lock> will traverse up references exactly I<one> level.
115C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
116
32419a4c 117Note that you cannot explicitly unlock a variable; you can only wait
118for the lock to go out of scope. If you need more fine-grained
83272a45 119control, see L<Thread::Semaphore>.
515f0976 120
121=item cond_wait VARIABLE
122
123The C<cond_wait> function takes a B<locked> variable as a parameter,
32419a4c 124unlocks the variable, and blocks until another thread does a
125C<cond_signal> or C<cond_broadcast> for that same locked variable.
126The variable that C<cond_wait> blocked on is relocked after the
127C<cond_wait> is satisfied. If there are multiple threads
128C<cond_wait>ing on the same variable, all but one will reblock waiting
129to reacquire the lock on the variable. (So if you're only using
130C<cond_wait> for synchronisation, give up the lock as soon as
131possible). The two actions of unlocking the variable and entering the
132blocked wait state are atomic, The two actions of exiting from the
38875929 133blocked wait state and relocking the variable are not.
515f0976 134
32419a4c 135It is important to note that the variable can be notified even if
136no thread C<cond_signal> or C<cond_broadcast> on the variable.
137It is therefore important to check the value of the variable and
138go back to waiting if the requirement is not fulfilled.
515f0976 139
140=item cond_signal VARIABLE
141
32419a4c 142The C<cond_signal> function takes a B<locked> variable as a parameter
143and unblocks one thread that's C<cond_wait>ing on that variable. If
144more than one thread is blocked in a C<cond_wait> on that variable,
145only one (and which one is indeterminate) will be unblocked.
515f0976 146
32419a4c 147If there are no threads blocked in a C<cond_wait> on the variable,
148the signal is discarded. By always locking before signaling, you can
149(with care), avoid signaling before another thread has entered cond_wait().
38875929 150
151C<cond_signal> will normally generate a warning if you attempt to use it
152on an unlocked variable. On the rare occasions where doing this may be
153sensible, you can skip the warning with
154
155 { no warnings 'threads'; cond_signal($foo) }
515f0976 156
157=item cond_broadcast VARIABLE
158
159The C<cond_broadcast> function works similarly to C<cond_signal>.
32419a4c 160C<cond_broadcast>, though, will unblock B<all> the threads that are
161blocked in a C<cond_wait> on the locked variable, rather than only one.
b050c948 162
4cab98c0 163=back
dab065ea 164
165=head1 NOTES
166
8c5dce87 167threads::shared is designed to disable itself silently if threads are
dab065ea 168not available. If you want access to threads, you must C<use threads>
169before you C<use threads::shared>. threads will emit a warning if you
8c5dce87 170use it after threads::shared.
dab065ea 171
b050c948 172=head1 BUGS
173
4cab98c0 174C<bless> is not supported on shared references. In the current version,
515f0976 175C<bless> will only bless the thread local reference and the blessing
4cab98c0 176will not propagate to the other threads. This is expected to be
177implemented in a future version of Perl.
515f0976 178
b050c948 179Does not support splice on arrays!
b050c948 180
58122748 181Taking references to the elements of shared arrays and hashes does not
182autovivify the elements, and neither does slicing a shared array/hash
183over non-existent indices/keys autovivify the elements.
184
b050c948 185=head1 AUTHOR
186
aaf3876d 187Arthur Bergman E<lt>arthur at contiller.seE<gt>
b050c948 188
aaf3876d 189threads::shared is released under the same license as Perl
b050c948 190
5e549d84 191Documentation borrowed from the old Thread.pm
515f0976 192
b050c948 193=head1 SEE ALSO
194
5e549d84 195L<threads>, L<perlthrtut>, L<http://www.perl.com/pub/a/2002/06/11/threads.html>
b050c948 196
197=cut