Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / lib / 5.8.0 / sun4-solaris / threads.pm
CommitLineData
86530b38
AT
1package threads;
2
3use 5.008;
4use strict;
5use warnings;
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
17Having threads support requires all of Perl and all of the XS modules in
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}
29
30use overload
31 '==' => \&equal,
32 'fallback' => 1;
33
34#use threads::Shared;
35
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
43require Exporter;
44require DynaLoader;
45
46our @ISA = qw(Exporter DynaLoader);
47
48our %EXPORT_TAGS = ( all => [qw(yield)]);
49
50our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
51
52our @EXPORT = qw(
53async
54);
55our $VERSION = '0.99';
56
57
58sub equal {
59 return 1 if($_[0]->tid() == $_[1]->tid());
60 return 0;
61}
62
63sub async (&;@) {
64 my $cref = shift;
65 return threads->new($cref,@_);
66}
67
68sub 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
78bootstrap threads $VERSION;
79
80# why document 'new' then use 'create' in the tests!
81*create = \&new;
82
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
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
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
123any data or state between threads by default.
124
125Prior to perl 5.8 this has only been available to people embedding
126perl and for emulating fork() on windows.
127
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.
132
133It is also important to note that you must enable threads by doing
134C<use threads> as early as possible in the script itself and that it
135is not possible to enable threading inside an C<eval "">, C<do>,
136C<require>, or C<use>. In particular, if you are intending to share
137variables with threads::shared, you must C<use threads> before you
138C<use threads::shared> and C<threads> will emit a warning if you do
139it the other way around.
140
141=over
142
143=item $thread = threads->create(function, LIST)
144
145This will create a new thread with the entry point function and give
146it LIST as parameters. It will return the corresponding threads
147object. The new() method is an alias for create().
148
149=item $thread->join
150
151This will wait for the corresponding thread to join. When the thread
152finishes, join() will return the return values of the entry point
153function. If the thread has been detached, an error will be thrown.
154If the program exits without all other threads having been either
155joined or detached, then a warning will be issued. (A program exits
156either because one of its threads explicitly calls exit(), or in the
157case of the main thread, reaches the end of the main program file.)
158
159=item $thread->detach
160
161Will make the thread unjoinable, and cause any eventual return value
162to be discarded.
163
164=item threads->self
165
166This will return the thread object for the current thread.
167
168=item $thread->tid
169
170This will return the id of the thread. Thread IDs are integers, with
171the main thread in a program being 0. Currently Perl assigns a unique
172tid to every thread ever created in your program, assigning the first
173thread to be created a tid of 1, and increasing the tid by 1 for each
174new thread that's created.
175
176NB the class method C<< threads->tid() >> is a quick way to get the
177current thread id if you don't have your thread object handy.
178
179=item threads->object( tid )
180
181This will return the thread object for the thread associated with the
182specified tid. Returns undef if there is no thread associated with the tid
183or no tid is specified or the specified tid is undef.
184
185=item threads->yield();
186
187This is a suggestion to the OS to let this thread yield CPU time to other
188threads. What actually happens is highly dependent upon the underlying
189thread implementation.
190
191You may do C<use threads qw(yield)> then use just a bare C<yield> in your
192code.
193
194=item threads->list();
195
196This will return a list of all non joined, non detached threads.
197
198=item async BLOCK;
199
200C<async> creates a thread to execute the block immediately following
201it. This block is treated as an anonymous sub, and so must have a
202semi-colon after the closing brace. Like C<< threads->new >>, C<async>
203returns a thread object.
204
205=back
206
207=head1 WARNINGS
208
209=over 4
210
211=item A thread exited while %d other threads were still running
212
213A thread (not necessarily the main thread) exited while there were
214still other threads running. Usually it's a good idea to first collect
215the return values of the created threads by joining them, and only then
216exit from the main thread.
217
218=back
219
220=head1 TODO
221
222The current implementation of threads has been an attempt to get
223a correct threading system working that could be built on,
224and optimized, in newer versions of perl.
225
226Currently the overhead of creating a thread is rather large,
227also the cost of returning values can be large. These are areas
228were there most likely will be work done to optimize what data
229that needs to be cloned.
230
231=head1 BUGS
232
233=over
234
235=item Parent-Child threads.
236
237On some platforms it might not be possible to destroy "parent"
238threads while there are still existing child "threads".
239
240This will possibly be fixed in later versions of perl.
241
242=item tid is I32
243
244The thread id is a 32 bit integer, it can potentially overflow.
245This might be fixed in a later version of perl.
246
247=item Returning objects
248
249When you return an object the entire stash that the object is blessed
250as well. This will lead to a large memory usage. The ideal situation
251would be to detect the original stash if it existed.
252
253=item Creating threads inside BEGIN blocks
254
255Creating threads inside BEGIN blocks (or during the compilation phase
256in general) does not work. (In Windows, trying to use fork() inside
257BEGIN blocks is an equally losing proposition, since it has been
258implemented in very much the same way as threads.)
259
260=item PERL_OLD_SIGNALS are not threadsafe, will not be.
261
262If your Perl has been built with PERL_OLD_SIGNALS (one has
263to explicitly add that symbol to ccflags, see C<perl -V>),
264signal handling is not threadsafe.
265
266=back
267
268=head1 AUTHOR and COPYRIGHT
269
270Arthur Bergman E<lt>arthur at contiller.seE<gt>
271
272threads is released under the same license as Perl.
273
274Thanks to
275
276Richard Soderberg E<lt>rs at crystalflame.netE<gt>
277Helping me out tons, trying to find reasons for races and other weird bugs!
278
279Simon Cozens E<lt>simon at brecon.co.ukE<gt>
280Being there to answer zillions of annoying questions
281
282Rocco Caputo E<lt>troc at netrus.netE<gt>
283
284Vipul Ved Prakash E<lt>mail at vipul.netE<gt>
285Helping with debugging.
286
287please join perl-ithreads@perl.org for more information
288
289=head1 SEE ALSO
290
291L<threads::shared>, L<perlthrtut>,
292L<http://www.perl.com/pub/a/2002/06/11/threads.html>,
293L<perlcall>, L<perlembed>, L<perlguts>
294
295=cut