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