This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More signedness nits.
[perl5.git] / ext / threads / shared / shared.pm
CommitLineData
b050c948 1package threads::shared;
b050c948
AB
2use strict;
3use warnings;
4use Config;
a446a88f
NIS
5
6require Exporter;
7our @ISA = qw(Exporter);
81f1a921 8our @EXPORT = qw(share cond_wait cond_broadcast cond_signal _refcnt _id _thrcnt);
a446a88f
NIS
9our $VERSION = '0.90';
10
81f1a921
AB
11use Attribute::Handlers;
12
b050c948 13
9c4972d9 14if ($Config{'useithreads'}) {
6f942b98
AB
15 *cond_wait = \&cond_wait_enabled;
16 *cond_signal = \&cond_signal_enabled;
17 *cond_broadcast = \&cond_broadcast_enabled;
9c4972d9
NIS
18 require XSLoader;
19 XSLoader::load('threads::shared',$VERSION);
20}
21else {
a6b94e59
AB
22 *share = \&share_disabled;
23 *cond_wait = \&cond_wait_disabled;
24 *cond_signal = \&cond_signal_disabled;
dab065ea 25 *cond_broadcast = \&cond_broadcast_disabled;
b050c948
AB
26}
27
b050c948 28
b050c948
AB
29sub cond_wait_disabled { return @_ };
30sub cond_signal_disabled { return @_};
31sub cond_broadcast_disabled { return @_};
b050c948
AB
32sub share_disabled { return @_}
33
dab065ea
AB
34$threads::shared::threads_shared = 1;
35
6b85e4fe
NIS
36sub _thrcnt { 42 }
37
38sub threads::shared::tie::SPLICE
39{
40 die "Splice not implemented for shared arrays";
41}
42
81f1a921
AB
43sub UNIVERSAL::shared : ATTR {
44 my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
45 share($referent);
46}
b050c948
AB
47
48__END__
49
50=head1 NAME
51
52threads::shared - Perl extension for sharing data structures between threads
53
54=head1 SYNOPSIS
55
56 use threads::shared;
57
58 my($foo, @foo, %foo);
aaf3876d
AB
59 share($foo);
60 share(@foo);
61 share(%hash);
b050c948
AB
62 my $bar = share([]);
63 $hash{bar} = share({});
64
515f0976
AB
65 lock(%hash);
66 unlock(%hash);
b050c948 67 cond_wait($scalar);
515f0976
AB
68 cond_broadcast(@array);
69 cond_signal(%hash);
b050c948
AB
70
71=head1 DESCRIPTION
72
ad91d581
JH
73This modules allows you to share() variables. These variables will
74then be shared across different threads (and pseudoforks on
75win32). They are used together with the threads module.
b050c948 76
515f0976 77=head1 EXPORT
b050c948 78
515f0976
AB
79C<share>, C<lock>, C<unlock>, C<cond_wait>, C<cond_signal>, C<cond_broadcast>
80
81=head1 FUNCTIONS
82
83=over 4
84
85=item share VARIABLE
86
d1be9408 87C<share> takes a value and marks it as shared, you can share a scalar, array, hash
515f0976
AB
88scalar ref, array ref and hash ref, C<share> will return the shared value.
89
90C<share> will traverse up references exactly I<one> level.
91C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not.
92
93=item lock VARIABLE
94
95C<lock> places a lock on a variable until the lock goes out of scope. If
96the variable is locked by another thread, the C<lock> call will block until
97it's available. C<lock> is recursive, so multiple calls to C<lock> are
98safe--the variable will remain locked until the outermost lock on the
21312124 99variable goes out of scope or C<unlock> is called enough times to match
515f0976
AB
100the number of calls to <lock>.
101
102If a container object, such as a hash or array, is locked, all the elements
103of that container are not locked. For example, if a thread does a C<lock
104@a>, any other thread doing a C<lock($a[12])> won't block.
105
106C<lock> will traverse up references exactly I<one> level.
107C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
108
109
110=item unlock VARIABLE
111
112C<unlock> takes a locked shared value and decrements the lock count.
113If the lock count is zero the variable is unlocked. It is not necessary
114to call C<unlock> but it can be usefull to reduce lock contention.
115
116C<unlock> will traverse up references exactly I<one> level.
117C<unlock(\$a)> is equivalent to C<unlock($a)>, while C<unlock(\\$a)> is not.
118
119=item cond_wait VARIABLE
120
121The C<cond_wait> function takes a B<locked> variable as a parameter,
122unlocks the variable, and blocks until another thread does a C<cond_signal>
123or C<cond_broadcast> for that same locked variable. The variable that
124C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
125If there are multiple threads C<cond_wait>ing on the same variable, all but
126one will reblock waiting to reaquire the lock on the variable. (So if
127you're only using C<cond_wait> for synchronization, give up the lock as
128soon as possible)
129
130It is important to note that the variable can be notified even if no
131thread C<cond_signal> or C<cond_broadcast> on the variable. It is therefore
132important to check the value of the variable and go back to waiting if the
133requirment is not fullfilled.
134
135=item cond_signal VARIABLE
136
137The C<cond_signal> function takes a B<locked> variable as a parameter and
138unblocks one thread that's C<cond_wait>ing on that variable. If more than
139one thread is blocked in a C<cond_wait> on that variable, only one (and
140which one is indeterminate) will be unblocked.
141
142If there are no threads blocked in a C<cond_wait> on the variable, the
143signal is discarded.
144
145=item cond_broadcast VARIABLE
146
147The C<cond_broadcast> function works similarly to C<cond_signal>.
148C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
149in a C<cond_wait> on the locked variable, rather than only one.
b050c948 150
dab065ea
AB
151
152=head1 NOTES
153
8c5dce87 154threads::shared is designed to disable itself silently if threads are
dab065ea
AB
155not available. If you want access to threads, you must C<use threads>
156before you C<use threads::shared>. threads will emit a warning if you
8c5dce87 157use it after threads::shared.
dab065ea 158
b050c948
AB
159=head1 BUGS
160
515f0976
AB
161C<bless> is not supported on shared references, in the current version
162C<bless> will only bless the thread local reference and the blessing
163will not propagate to the other threads, this is expected to be implmented
164in the future.
165
b050c948 166Does not support splice on arrays!
b050c948
AB
167
168=head1 AUTHOR
169
aaf3876d 170Arthur Bergman E<lt>arthur at contiller.seE<gt>
b050c948 171
aaf3876d 172threads::shared is released under the same license as Perl
b050c948 173
515f0976
AB
174Documentation borrowed from Thread.pm
175
b050c948
AB
176=head1 SEE ALSO
177
178L<perl> L<threads>
179
180=cut
515f0976
AB
181
182
183
184
185