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