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