Retract #17455, #17427, #17425 on t/op/magic.t (but leave
[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
70you to share variables across different threads (and pseudoforks on
71win32). It 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
77=head1 FUNCTIONS
78
79=over 4
80
81=item share VARIABLE
82
86c43dd6 83C<share> takes a value and marks it as shared. You can share a scalar,
84array, hash, scalar ref, array ref or hash ref. C<share> will return
85the shared rvalue.
515f0976 86
87C<share> will traverse up references exactly I<one> level.
88C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not.
89
38875929 90A variable can also be marked as shared at compile time by using the
91C<shared> attribute: C<my $var : shared>.
92
86c43dd6 93If you want to share a newly created reference unfortunately you
94need to use C<&share([])> and C<&share({})> syntax due to problems
95with Perl's prototyping.
caf25f3b 96
515f0976 97=item lock VARIABLE
98
99C<lock> places a lock on a variable until the lock goes out of scope. If
100the variable is locked by another thread, the C<lock> call will block until
101it's available. C<lock> is recursive, so multiple calls to C<lock> are
4cab98c0 102safe -- the variable will remain locked until the outermost lock on the
38875929 103variable goes out of scope.
515f0976 104
105If a container object, such as a hash or array, is locked, all the elements
106of that container are not locked. For example, if a thread does a C<lock
107@a>, any other thread doing a C<lock($a[12])> won't block.
108
109C<lock> will traverse up references exactly I<one> level.
110C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
111
38875929 112Note that you cannot explicitly unlock a variable; you can only wait for
113the lock to go out of scope. If you need more fine-grained control, see
114L<threads::shared::semaphore>.
515f0976 115
116=item cond_wait VARIABLE
117
118The C<cond_wait> function takes a B<locked> variable as a parameter,
119unlocks the variable, and blocks until another thread does a C<cond_signal>
120or C<cond_broadcast> for that same locked variable. The variable that
121C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
122If there are multiple threads C<cond_wait>ing on the same variable, all but
4cab98c0 123one will reblock waiting to reacquire the lock on the variable. (So if
38875929 124you're only using C<cond_wait> for synchronisation, give up the lock as
125soon as possible). The two actions of unlocking the variable and entering
126the blocked wait state are atomic, The two actions of exiting from the
127blocked wait state and relocking the variable are not.
515f0976 128
129It is important to note that the variable can be notified even if no
130thread C<cond_signal> or C<cond_broadcast> on the variable. It is therefore
131important to check the value of the variable and go back to waiting if the
4cab98c0 132requirement is not fulfilled.
515f0976 133
134=item cond_signal VARIABLE
135
136The C<cond_signal> function takes a B<locked> variable as a parameter and
137unblocks one thread that's C<cond_wait>ing on that variable. If more than
138one thread is blocked in a C<cond_wait> on that variable, only one (and
139which one is indeterminate) will be unblocked.
140
141If there are no threads blocked in a C<cond_wait> on the variable, the
38875929 142signal is discarded. By always locking before signaling, you can (with
143care), avoid signaling before another thread has entered cond_wait().
144
145C<cond_signal> will normally generate a warning if you attempt to use it
146on an unlocked variable. On the rare occasions where doing this may be
147sensible, you can skip the warning with
148
149 { no warnings 'threads'; cond_signal($foo) }
515f0976 150
151=item cond_broadcast VARIABLE
152
153The C<cond_broadcast> function works similarly to C<cond_signal>.
154C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
155in a C<cond_wait> on the locked variable, rather than only one.
b050c948 156
4cab98c0 157=back
dab065ea 158
159=head1 NOTES
160
8c5dce87 161threads::shared is designed to disable itself silently if threads are
dab065ea 162not available. If you want access to threads, you must C<use threads>
163before you C<use threads::shared>. threads will emit a warning if you
8c5dce87 164use it after threads::shared.
dab065ea 165
b050c948 166=head1 BUGS
167
4cab98c0 168C<bless> is not supported on shared references. In the current version,
515f0976 169C<bless> will only bless the thread local reference and the blessing
4cab98c0 170will not propagate to the other threads. This is expected to be
171implemented in a future version of Perl.
515f0976 172
b050c948 173Does not support splice on arrays!
b050c948 174
58122748 175Taking references to the elements of shared arrays and hashes does not
176autovivify the elements, and neither does slicing a shared array/hash
177over non-existent indices/keys autovivify the elements.
178
b050c948 179=head1 AUTHOR
180
aaf3876d 181Arthur Bergman E<lt>arthur at contiller.seE<gt>
b050c948 182
aaf3876d 183threads::shared is released under the same license as Perl
b050c948 184
5e549d84 185Documentation borrowed from the old Thread.pm
515f0976 186
b050c948 187=head1 SEE ALSO
188
5e549d84 189L<threads>, L<perlthrtut>, L<http://www.perl.com/pub/a/2002/06/11/threads.html>
b050c948 190
191=cut