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