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