Commit | Line | Data |
---|---|---|
43d3ddbe JH |
1 | package Thread; |
2 | ||
4038bebf JH |
3 | use strict; |
4 | ||
4cf4ea45 | 5 | our($VERSION, $ithreads, $othreads); |
4038bebf | 6 | |
43d3ddbe | 7 | BEGIN { |
9d40afd1 | 8 | $VERSION = '2.01'; |
43d3ddbe | 9 | use Config; |
4038bebf JH |
10 | $ithreads = $Config{useithreads}; |
11 | $othreads = $Config{use5005threads}; | |
43d3ddbe JH |
12 | } |
13 | ||
14 | require Exporter; | |
15 | use XSLoader (); | |
4cf4ea45 | 16 | our(@ISA, @EXPORT, @EXPORT_OK); |
43d3ddbe JH |
17 | |
18 | @ISA = qw(Exporter); | |
19 | ||
20 | BEGIN { | |
21 | if ($ithreads) { | |
dfca11dd | 22 | @EXPORT = qw(cond_wait cond_broadcast cond_signal) |
43d3ddbe JH |
23 | } elsif ($othreads) { |
24 | @EXPORT_OK = qw(cond_signal cond_broadcast cond_wait); | |
25 | } | |
26 | push @EXPORT_OK, qw(async yield); | |
27 | } | |
28 | ||
29 | =head1 NAME | |
30 | ||
3d1f1caf | 31 | Thread - manipulate threads in Perl (for old code only) |
43d3ddbe JH |
32 | |
33 | =head1 CAVEAT | |
34 | ||
35 | Perl has two thread models. | |
36 | ||
37 | In Perl 5.005 the thread model was that all data is implicitly shared | |
38 | and shared access to data has to be explicitly synchronized. | |
39 | This model is called "5005threads". | |
40 | ||
41 | In Perl 5.6 a new model was introduced in which all is was thread | |
42 | local and shared access to data has to be explicitly declared. | |
43 | This model is called "ithreads", for "interpreter threads". | |
44 | ||
45 | In Perl 5.6 the ithreads model was not available as a public API, | |
46 | only as an internal API that was available for extension writers, | |
47 | and to implement fork() emulation on Win32 platforms. | |
48 | ||
49 | In Perl 5.8 the ithreads model became available through the C<threads> | |
50 | module. | |
51 | ||
9d40afd1 JH |
52 | In Perl 5.10, the 5005threads model will be removed from the Perl interpreter. |
53 | ||
43d3ddbe | 54 | Neither model is configured by default into Perl (except, as mentioned |
8a20485c JH |
55 | above, in Win32 ithreads are always available.) You can see your |
56 | Perl's threading configuration by running C<perl -V> and looking for | |
57 | the I<use...threads> variables, or inside script by C<use Config;> | |
58 | and testing for C<$Config{use5005threads}> and C<$Config{useithreads}>. | |
59 | ||
60 | For old code and interim backwards compatibility, the Thread module | |
61 | has been reworked to function as a frontend for both 5005threads and | |
62 | ithreads. | |
43d3ddbe | 63 | |
43d3ddbe JH |
64 | Note that the compatibility is not complete: because the data sharing |
65 | models are directly opposed, anything to do with data sharing has to | |
66 | be thought differently. With the ithreads you must explicitly share() | |
67 | variables between the threads. | |
68 | ||
8a20485c | 69 | For new code the use of the C<Thread> module is discouraged and |
3d1f1caf | 70 | the direct use of the C<threads> and C<threads::shared> modules |
8a20485c JH |
71 | is encouraged instead. |
72 | ||
43d3ddbe JH |
73 | Finally, note that there are many known serious problems with the |
74 | 5005threads, one of the least of which is that regular expression | |
75 | match variables like $1 are not threadsafe, that is, they easily get | |
76 | corrupted by competing threads. Other problems include more insidious | |
77 | data corruption and mysterious crashes. You are seriously urged to | |
78 | use ithreads instead. | |
79 | ||
80 | =head1 SYNOPSIS | |
81 | ||
9d40afd1 | 82 | use Thread qw(:DEFAULT async yield); |
43d3ddbe JH |
83 | |
84 | my $t = Thread->new(\&start_sub, @start_args); | |
85 | ||
86 | $result = $t->join; | |
9d40afd1 | 87 | $result = $t->eval; # not available with ithreads |
43d3ddbe JH |
88 | $t->detach; |
89 | ||
90 | if ($t->done) { | |
91 | $t->join; | |
92 | } | |
93 | ||
94 | if($t->equal($another_thread)) { | |
9d40afd1 | 95 | # ... |
43d3ddbe JH |
96 | } |
97 | ||
98 | yield(); | |
99 | ||
9d40afd1 | 100 | my $tid = Thread->self->tid; |
43d3ddbe JH |
101 | |
102 | lock($scalar); | |
103 | lock(@array); | |
104 | lock(%hash); | |
105 | ||
9d40afd1 | 106 | lock(\&sub); # not available with ithreads |
43d3ddbe | 107 | |
9d40afd1 | 108 | $flags = $t->flags; # not available with ithreads |
43d3ddbe | 109 | |
9d40afd1 | 110 | my @list = Thread->list; |
43d3ddbe JH |
111 | |
112 | =head1 DESCRIPTION | |
113 | ||
114 | The C<Thread> module provides multithreading support for perl. | |
115 | ||
116 | =head1 FUNCTIONS | |
117 | ||
118 | =over 8 | |
119 | ||
120 | =item $thread = Thread->new(\&start_sub) | |
121 | ||
122 | =item $thread = Thread->new(\&start_sub, LIST) | |
123 | ||
124 | C<new> starts a new thread of execution in the referenced subroutine. The | |
125 | optional list is passed as parameters to the subroutine. Execution | |
126 | continues in both the subroutine and the code after the C<new> call. | |
127 | ||
128 | C<Thread->new> returns a thread object representing the newly created | |
129 | thread. | |
130 | ||
131 | =item lock VARIABLE | |
132 | ||
dfca11dd | 133 | C<lock> places a lock on a variable until the lock goes out of scope. |
43d3ddbe JH |
134 | |
135 | If the variable is locked by another thread, the C<lock> call will | |
136 | block until it's available. C<lock> is recursive, so multiple calls | |
137 | to C<lock> are safe--the variable will remain locked until the | |
138 | outermost lock on the variable goes out of scope. | |
139 | ||
140 | Locks on variables only affect C<lock> calls--they do I<not> affect normal | |
141 | access to a variable. (Locks on subs are different, and covered in a bit.) | |
142 | If you really, I<really> want locks to block access, then go ahead and tie | |
143 | them to something and manage this yourself. This is done on purpose. | |
144 | While managing access to variables is a good thing, Perl doesn't force | |
145 | you out of its living room... | |
146 | ||
147 | If a container object, such as a hash or array, is locked, all the | |
148 | elements of that container are not locked. For example, if a thread | |
149 | does a C<lock @a>, any other thread doing a C<lock($a[12])> won't | |
150 | block. | |
151 | ||
152 | With 5005threads you may also C<lock> a sub, using C<lock &sub>. | |
153 | Any calls to that sub from another thread will block until the lock | |
9d40afd1 JH |
154 | is released. This behaviour is not equivalent to declaring the sub |
155 | with the C<:locked> attribute (5005threads only). The C<:locked> | |
156 | attribute serializes | |
43d3ddbe JH |
157 | access to a subroutine, but allows different threads non-simultaneous |
158 | access. C<lock &sub>, on the other hand, will not allow I<any> other | |
159 | thread access for the duration of the lock. | |
160 | ||
161 | Finally, C<lock> will traverse up references exactly I<one> level. | |
162 | C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not. | |
163 | ||
164 | =item async BLOCK; | |
165 | ||
166 | C<async> creates a thread to execute the block immediately following | |
167 | it. This block is treated as an anonymous sub, and so must have a | |
168 | semi-colon after the closing brace. Like C<Thread->new>, C<async> | |
169 | returns a thread object. | |
170 | ||
171 | =item Thread->self | |
172 | ||
173 | The C<Thread-E<gt>self> function returns a thread object that represents | |
174 | the thread making the C<Thread-E<gt>self> call. | |
175 | ||
9d40afd1 JH |
176 | =item Thread->list |
177 | ||
178 | Returns a list of all non-joined, non-detached Thread objects. | |
179 | ||
43d3ddbe JH |
180 | =item cond_wait VARIABLE |
181 | ||
182 | The C<cond_wait> function takes a B<locked> variable as | |
183 | a parameter, unlocks the variable, and blocks until another thread | |
184 | does a C<cond_signal> or C<cond_broadcast> for that same locked | |
185 | variable. The variable that C<cond_wait> blocked on is relocked | |
186 | after the C<cond_wait> is satisfied. If there are multiple threads | |
187 | C<cond_wait>ing on the same variable, all but one will reblock waiting | |
188 | to reaquire the lock on the variable. (So if you're only using | |
189 | C<cond_wait> for synchronization, give up the lock as soon as | |
190 | possible.) | |
191 | ||
192 | =item cond_signal VARIABLE | |
193 | ||
194 | The C<cond_signal> function takes a locked variable as a parameter and | |
195 | unblocks one thread that's C<cond_wait>ing on that variable. If more than | |
196 | one thread is blocked in a C<cond_wait> on that variable, only one (and | |
197 | which one is indeterminate) will be unblocked. | |
198 | ||
199 | If there are no threads blocked in a C<cond_wait> on the variable, | |
200 | the signal is discarded. | |
201 | ||
202 | =item cond_broadcast VARIABLE | |
203 | ||
204 | The C<cond_broadcast> function works similarly to C<cond_signal>. | |
205 | C<cond_broadcast>, though, will unblock B<all> the threads that are | |
206 | blocked in a C<cond_wait> on the locked variable, rather than only | |
207 | one. | |
208 | ||
209 | =item yield | |
210 | ||
211 | The C<yield> function allows another thread to take control of the | |
212 | CPU. The exact results are implementation-dependent. | |
213 | ||
214 | =back | |
215 | ||
216 | =head1 METHODS | |
217 | ||
218 | =over 8 | |
219 | ||
220 | =item join | |
221 | ||
222 | C<join> waits for a thread to end and returns any values the thread | |
223 | exited with. C<join> will block until the thread has ended, though | |
224 | it won't block if the thread has already terminated. | |
225 | ||
226 | If the thread being C<join>ed C<die>d, the error it died with will | |
227 | be returned at this time. If you don't want the thread performing | |
228 | the C<join> to die as well, you should either wrap the C<join> in | |
229 | an C<eval> or use the C<eval> thread method instead of C<join>. | |
230 | ||
231 | =item eval | |
232 | ||
233 | The C<eval> method wraps an C<eval> around a C<join>, and so waits for | |
234 | a thread to exit, passing along any values the thread might have returned. | |
235 | Errors, of course, get placed into C<$@>. (Not available with ithreads.) | |
236 | ||
237 | =item detach | |
238 | ||
239 | C<detach> tells a thread that it is never going to be joined i.e. | |
240 | that all traces of its existence can be removed once it stops running. | |
241 | Errors in detached threads will not be visible anywhere - if you want | |
242 | to catch them, you should use $SIG{__DIE__} or something like that. | |
243 | ||
9d40afd1 | 244 | =item equal |
43d3ddbe JH |
245 | |
246 | C<equal> tests whether two thread objects represent the same thread and | |
247 | returns true if they do. | |
248 | ||
249 | =item tid | |
250 | ||
251 | The C<tid> method returns the tid of a thread. The tid is | |
252 | a monotonically increasing integer assigned when a thread is | |
253 | created. The main thread of a program will have a tid of zero, | |
254 | while subsequent threads will have tids assigned starting with one. | |
255 | ||
256 | =item flags | |
257 | ||
258 | The C<flags> method returns the flags for the thread. This is the | |
259 | integer value corresponding to the internal flags for the thread, | |
260 | and the value may not be all that meaningful to you. | |
261 | (Not available with ithreads.) | |
262 | ||
263 | =item done | |
264 | ||
265 | The C<done> method returns true if the thread you're checking has | |
9d40afd1 | 266 | finished, and false otherwise. |
43d3ddbe JH |
267 | |
268 | =back | |
269 | ||
270 | =head1 LIMITATIONS | |
271 | ||
272 | The sequence number used to assign tids is a simple integer, and no | |
273 | checking is done to make sure the tid isn't currently in use. If a | |
274 | program creates more than 2**32 - 1 threads in a single run, threads | |
275 | may be assigned duplicate tids. This limitation may be lifted in | |
276 | a future version of Perl. | |
277 | ||
278 | =head1 SEE ALSO | |
279 | ||
280 | L<threads::shared> (not available with 5005threads) | |
281 | ||
282 | L<attributes>, L<Thread::Queue>, L<Thread::Semaphore>, | |
283 | L<Thread::Specific> (not available with ithreads) | |
284 | ||
285 | =cut | |
286 | ||
287 | # | |
288 | # Methods | |
289 | # | |
290 | ||
291 | # | |
292 | # Exported functions | |
293 | # | |
294 | ||
295 | sub async (&) { | |
9d40afd1 | 296 | return Thread->new(shift); |
43d3ddbe JH |
297 | } |
298 | ||
299 | sub eval { | |
300 | return eval { shift->join; }; | |
301 | } | |
302 | ||
303 | sub unimplemented { | |
304 | print $_[0], " unimplemented with ", | |
305 | $Config{useithreads} ? "ithreads" : "5005threads", "\n"; | |
43d3ddbe JH |
306 | } |
307 | ||
308 | sub unimplement { | |
9d40afd1 JH |
309 | no strict 'refs'; |
310 | no warnings 'redefine'; | |
43d3ddbe JH |
311 | for my $m (@_) { |
312 | *{"Thread::$m"} = sub { unimplemented $m }; | |
313 | } | |
314 | } | |
315 | ||
316 | BEGIN { | |
317 | if ($ithreads) { | |
733129fe JH |
318 | if ($othreads) { |
319 | require Carp; | |
320 | Carp::croak("This Perl has both ithreads and 5005threads (serious malconfiguration)"); | |
321 | } | |
9d40afd1 JH |
322 | no strict 'refs'; |
323 | require threads; | |
e7b4e24c | 324 | for my $m (qw(new join detach yield self tid equal list)) { |
43d3ddbe JH |
325 | *{"Thread::$m"} = \&{"threads::$m"}; |
326 | } | |
9d40afd1 | 327 | require threads::shared; |
dfca11dd | 328 | for my $m (qw(cond_signal cond_broadcast cond_wait)) { |
9d40afd1 | 329 | *{"Thread::$m"} = \&{"threads::shared::$m"}; |
43d3ddbe | 330 | } |
9d40afd1 JH |
331 | *Thread::done = sub { return ! shift->threads::is_running(); }; |
332 | unimplement(qw(eval flags)); | |
43d3ddbe JH |
333 | } elsif ($othreads) { |
334 | XSLoader::load 'Thread'; | |
43d3ddbe JH |
335 | } else { |
336 | require Carp; | |
733129fe | 337 | Carp::croak("This Perl has neither ithreads nor 5005threads"); |
43d3ddbe JH |
338 | } |
339 | } | |
340 | ||
341 | 1; |