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