Re: 5.8.3-RC1, ext/threads/shared/t/wait still hanging
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.pm
CommitLineData
47ba8780 1package threads;
2
32419a4c 3use 5.008;
47ba8780 4use strict;
5use warnings;
73e09c8f 6use Config;
7
8BEGIN {
9 unless ($Config{useithreads}) {
10 my @caller = caller(2);
11 die <<EOF;
12$caller[1] line $caller[2]:
13
14This Perl hasn't been configured and built properly for the threads
15module to work. (The 'useithreads' configuration option hasn't been used.)
16
5e549d84 17Having threads support requires all of Perl and all of the XS modules in
73e09c8f 18the Perl installation to be rebuilt, it is not just a question of adding
19the threads module. (In other words, threaded and non-threaded Perls
20are binary incompatible.)
21
22If you want to the use the threads module, please contact the people
23who built your Perl.
24
25Cannot continue, aborting.
26EOF
27 }
28}
47ba8780 29
68795e93 30use overload
43d3ddbe 31 '==' => \&equal,
47ba8780 32 'fallback' => 1;
33
dab065ea 34BEGIN {
35 warn "Warning, threads::shared has already been loaded. ".
36 "To enable shared variables for these modules 'use threads' ".
37 "must be called before any of those modules are loaded\n"
38 if($threads::shared::threads_shared);
39}
40
47ba8780 41require Exporter;
42require DynaLoader;
43
47ba8780 44our @ISA = qw(Exporter DynaLoader);
45
70f2e746 46our %EXPORT_TAGS = ( all => [qw(yield)]);
47ba8780 47
48our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
49
50our @EXPORT = qw(
dcb6ccbc 51async
47ba8780 52);
35bc0dc8 53our $VERSION = '1.01';
47ba8780 54
47ba8780 55
abec23e7 56# || 0 to ensure compatibility with previous versions
57sub equal { ($_[0]->tid == $_[1]->tid) || 0 }
47ba8780 58
abec23e7 59# use "goto" trick to avoid pad problems from 5.8.1, should also be faster
60sub async (&;@) { unshift @_,'threads'; goto &new }
dcb6ccbc 61
8c9849ff 62sub object {
63 return undef unless @_ > 1;
64 foreach (threads->list) {
65 return $_ if $_->tid == $_[1];
66 }
67 return undef;
68}
69
8222d950 70$threads::threads = 1;
47ba8780 71
72bootstrap threads $VERSION;
73
68795e93 74# why document 'new' then use 'create' in the tests!
75*create = \&new;
76
47ba8780 77# Preloaded methods go here.
78
791;
80__END__
81
82=head1 NAME
83
84threads - Perl extension allowing use of interpreter based threads from perl
85
86=head1 SYNOPSIS
87
38875929 88 use threads;
47ba8780 89
38875929 90 sub start_thread {
91 print "Thread started\n";
92 }
47ba8780 93
38875929 94 my $thread = threads->create("start_thread","argument");
95 my $thread2 = $thread->create(sub { print "I am a thread"},"argument");
96 my $thread3 = async { foreach (@files) { ... } };
47ba8780 97
38875929 98 $thread->join();
99 $thread->detach();
47ba8780 100
38875929 101 $thread = threads->self();
8c9849ff 102 $thread = threads->object( $tid );
11c51ed3 103
38875929 104 $thread->tid();
105 threads->tid();
106 threads->self->tid();
47ba8780 107
38875929 108 threads->yield();
f9dff5f5 109
38875929 110 threads->list();
678a9b6c 111
47ba8780 112=head1 DESCRIPTION
113
43d3ddbe 114Perl 5.6 introduced something called interpreter threads. Interpreter
115threads are different from "5005threads" (the thread model of Perl
1165.005) by creating a new perl interpreter per thread and not sharing
32419a4c 117any data or state between threads by default.
11c51ed3 118
43d3ddbe 119Prior to perl 5.8 this has only been available to people embedding
120perl and for emulating fork() on windows.
11c51ed3 121
43d3ddbe 122The threads API is loosely based on the old Thread.pm API. It is very
123important to note that variables are not shared between threads, all
124variables are per default thread local. To use shared variables one
125must use threads::shared.
11c51ed3 126
6bc4bdd0 127It is also important to note that you must enable threads by doing
128C<use threads> as early as possible in the script itself and that it
129is not possible to enable threading inside an C<eval "">, C<do>,
130C<require>, or C<use>. In particular, if you are intending to share
131variables with threads::shared, you must C<use threads> before you
132C<use threads::shared> and C<threads> will emit a warning if you do
133it the other way around.
47ba8780 134
135=over
136
9c4972d9 137=item $thread = threads->create(function, LIST)
47ba8780 138
ad91d581 139This will create a new thread with the entry point function and give
140it LIST as parameters. It will return the corresponding threads
38875929 141object. The new() method is an alias for create().
47ba8780 142
11c51ed3 143=item $thread->join
47ba8780 144
32419a4c 145This will wait for the corresponding thread to join. When the thread
146finishes, join() will return the return values of the entry point
147function. If the thread has been detached, an error will be thrown.
93512b4d 148
149The context (scalar or list) of the thread creation is also the
150context for join(). This means that if you intend to return an array
151from a thread, you must use C<my ($thread) = threads->new(...)>, and
152that if you intend to return a scalar, you must use C<my $thread = ...>.
153
32419a4c 154If the program exits without all other threads having been either
155joined or detached, then a warning will be issued. (A program exits
156either because one of its threads explicitly calls exit(), or in the
157case of the main thread, reaches the end of the main program file.)
47ba8780 158
93512b4d 159
11c51ed3 160=item $thread->detach
47ba8780 161
32419a4c 162Will make the thread unjoinable, and cause any eventual return value
163to be discarded.
47ba8780 164
165=item threads->self
166
38875929 167This will return the thread object for the current thread.
47ba8780 168
11c51ed3 169=item $thread->tid
47ba8780 170
32419a4c 171This will return the id of the thread. Thread IDs are integers, with
172the main thread in a program being 0. Currently Perl assigns a unique
173tid to every thread ever created in your program, assigning the first
174thread to be created a tid of 1, and increasing the tid by 1 for each
175new thread that's created.
38875929 176
177NB the class method C<< threads->tid() >> is a quick way to get the
178current thread id if you don't have your thread object handy.
47ba8780 179
8c9849ff 180=item threads->object( tid )
181
182This will return the thread object for the thread associated with the
183specified tid. Returns undef if there is no thread associated with the tid
184or no tid is specified or the specified tid is undef.
185
f9dff5f5 186=item threads->yield();
187
38875929 188This is a suggestion to the OS to let this thread yield CPU time to other
189threads. What actually happens is highly dependent upon the underlying
190thread implementation.
f9dff5f5 191
70f2e746 192You may do C<use threads qw(yield)> then use just a bare C<yield> in your
193code.
194
678a9b6c 195=item threads->list();
196
197This will return a list of all non joined, non detached threads.
198
386c44e5 199=item async BLOCK;
200
201C<async> creates a thread to execute the block immediately following
202it. This block is treated as an anonymous sub, and so must have a
38875929 203semi-colon after the closing brace. Like C<< threads->new >>, C<async>
386c44e5 204returns a thread object.
205
47ba8780 206=back
207
e4f9f4fe 208=head1 WARNINGS
209
210=over 4
211
c133c03f 212=item A thread exited while %d other threads were still running
e4f9f4fe 213
c133c03f 214A thread (not necessarily the main thread) exited while there were
215still other threads running. Usually it's a good idea to first collect
216the return values of the created threads by joining them, and only then
32419a4c 217exit from the main thread.
e4f9f4fe 218
219=back
47ba8780 220
ab80e3f2 221=head1 TODO
678a9b6c 222
38875929 223The current implementation of threads has been an attempt to get
678a9b6c 224a correct threading system working that could be built on,
225and optimized, in newer versions of perl.
226
38875929 227Currently the overhead of creating a thread is rather large,
678a9b6c 228also the cost of returning values can be large. These are areas
229were there most likely will be work done to optimize what data
230that needs to be cloned.
47ba8780 231
ab80e3f2 232=head1 BUGS
233
47ba8780 234=over
235
678a9b6c 236=item Parent-Child threads.
237
238On some platforms it might not be possible to destroy "parent"
239threads while there are still existing child "threads".
240
ab80e3f2 241This will possibly be fixed in later versions of perl.
242
678a9b6c 243=item tid is I32
244
32419a4c 245The thread id is a 32 bit integer, it can potentially overflow.
678a9b6c 246This might be fixed in a later version of perl.
47ba8780 247
678a9b6c 248=item Returning objects
47ba8780 249
678a9b6c 250When you return an object the entire stash that the object is blessed
32419a4c 251as well. This will lead to a large memory usage. The ideal situation
252would be to detect the original stash if it existed.
678a9b6c 253
88f8c1df 254=item Creating threads inside BEGIN blocks
255
256Creating threads inside BEGIN blocks (or during the compilation phase
257in general) does not work. (In Windows, trying to use fork() inside
258BEGIN blocks is an equally losing proposition, since it has been
259implemented in very much the same way as threads.)
260
678a9b6c 261=item PERL_OLD_SIGNALS are not threadsafe, will not be.
47ba8780 262
88f8c1df 263If your Perl has been built with PERL_OLD_SIGNALS (one has
264to explicitly add that symbol to ccflags, see C<perl -V>),
265signal handling is not threadsafe.
266
ff24cc8d 267=item Detached threads on Windows
268
35bc0dc8 269These aren't yet supported (as of perl 5.8.3), as they may lead to
ff24cc8d 270memory access violation problems.
271
47ba8780 272=back
273
274=head1 AUTHOR and COPYRIGHT
275
35bc0dc8 276Arthur Bergman E<lt>sky at nanisky.comE<gt>
47ba8780 277
43d3ddbe 278threads is released under the same license as Perl.
47ba8780 279
68795e93 280Thanks to
47ba8780 281
ca9279ba 282Richard Soderberg E<lt>perl at crystalflame.netE<gt>
ad91d581 283Helping me out tons, trying to find reasons for races and other weird bugs!
47ba8780 284
ad91d581 285Simon Cozens E<lt>simon at brecon.co.ukE<gt>
286Being there to answer zillions of annoying questions
47ba8780 287
ad91d581 288Rocco Caputo E<lt>troc at netrus.netE<gt>
47ba8780 289
ad91d581 290Vipul Ved Prakash E<lt>mail at vipul.netE<gt>
47ba8780 291Helping with debugging.
292
293please join perl-ithreads@perl.org for more information
294
47ba8780 295=head1 SEE ALSO
296
5e549d84 297L<threads::shared>, L<perlthrtut>,
298L<http://www.perl.com/pub/a/2002/06/11/threads.html>,
299L<perlcall>, L<perlembed>, L<perlguts>
47ba8780 300
301=cut