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