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