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