As suggested by Arthur: the threads and threads::shared
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.pm
CommitLineData
47ba8780 1package threads;
2
73e09c8f 3use 5.007_003;
47ba8780 4use strict;
5use warnings;
73e09c8f 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 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}
47ba8780 29
68795e93 30use overload
43d3ddbe 31 '==' => \&equal,
47ba8780 32 'fallback' => 1;
33
47ba8780 34#use threads::Shared;
35
dab065ea 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
47ba8780 43require Exporter;
44require DynaLoader;
45
47ba8780 46our @ISA = qw(Exporter DynaLoader);
47
48our %EXPORT_TAGS = ( all => [qw()]);
49
50our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
51
52our @EXPORT = qw(
53
54);
55our $VERSION = '0.05';
56
47ba8780 57
43d3ddbe 58sub equal {
47ba8780 59 return 1 if($_[0]->tid() == $_[1]->tid());
60 return 0;
61}
62
8222d950 63$threads::threads = 1;
47ba8780 64
65bootstrap 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
721;
73__END__
74
75=head1 NAME
76
77threads - Perl extension allowing use of interpreter based threads from perl
78
79=head1 SYNOPSIS
80
47ba8780 81use threads;
82
83sub start_thread {
84 print "Thread started\n";
85}
86
9c4972d9 87my $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 97threads->tid();
98threads->self->tid();
99
100$thread->tid();
47ba8780 101
47ba8780 102=head1 DESCRIPTION
103
43d3ddbe 104Perl 5.6 introduced something called interpreter threads. Interpreter
105threads are different from "5005threads" (the thread model of Perl
1065.005) by creating a new perl interpreter per thread and not sharing
107any data or state between threads.
11c51ed3 108
43d3ddbe 109Prior to perl 5.8 this has only been available to people embedding
110perl and for emulating fork() on windows.
11c51ed3 111
43d3ddbe 112The threads API is loosely based on the old Thread.pm API. It is very
113important to note that variables are not shared between threads, all
114variables are per default thread local. To use shared variables one
115must use threads::shared.
11c51ed3 116
43d3ddbe 117It is also important to note that you preferably enable threads by
118doing C<use threads> as early as possible and that it is not possible
dab065ea 119to enable threading inside an eval ""; In particular, if you are
120intending to share variables with threads::shared, you must
121C<use threads> before you C<use threads::shared> and threads will emit
122a warning if you do it the other way around.
47ba8780 123
124=over
125
9c4972d9 126=item $thread = threads->create(function, LIST)
47ba8780 127
ad91d581 128This will create a new thread with the entry point function and give
129it LIST as parameters. It will return the corresponding threads
130object.
47ba8780 131
11c51ed3 132=item $thread->join
47ba8780 133
43d3ddbe 134This will wait for the corresponding thread to join. When it finishes
135join will return the return values of the entry point function. If a
136thread has been detached, join will return without wait.
47ba8780 137
11c51ed3 138=item $thread->detach
47ba8780 139
43d3ddbe 140Will throw away the return value from the thread and make it
141non-joinable.
47ba8780 142
143=item threads->self
144
145This will return the object for the current thread.
146
11c51ed3 147=item $thread->tid
47ba8780 148
43d3ddbe 149This will return the id of the thread. threads->self->tid() is a
150quick way to get current thread id.
47ba8780 151
152=back
153
e4f9f4fe 154=head1 WARNINGS
155
156=over 4
157
158=item Cleanup skipped %d active threads
159
160The main thread exited while there were still other threads running.
4f5d0762 161This is not a good sign: you should either explicitly join the threads,
162or somehow be certain that all the non-main threads have finished.
e4f9f4fe 163
164=back
47ba8780 165
166=head1 TODO
167
168=over
169
43d3ddbe 170=item Fix so the return value is returned when you join.
47ba8780 171
43d3ddbe 172=item Add join_all.
47ba8780 173
174=item Fix memory leaks!
175
176=back
177
178=head1 AUTHOR and COPYRIGHT
179
11c51ed3 180Arthur Bergman E<lt>arthur at contiller.seE<gt>
47ba8780 181
43d3ddbe 182threads is released under the same license as Perl.
47ba8780 183
68795e93 184Thanks to
47ba8780 185
68795e93 186Richard Soderberg E<lt>rs at crystalflame.netE<gt>
ad91d581 187Helping me out tons, trying to find reasons for races and other weird bugs!
47ba8780 188
ad91d581 189Simon Cozens E<lt>simon at brecon.co.ukE<gt>
190Being there to answer zillions of annoying questions
47ba8780 191
ad91d581 192Rocco Caputo E<lt>troc at netrus.netE<gt>
47ba8780 193
ad91d581 194Vipul Ved Prakash E<lt>mail at vipul.netE<gt>
47ba8780 195Helping with debugging.
196
197please join perl-ithreads@perl.org for more information
198
199=head1 BUGS
200
201=over
202
203=item creating a thread from within a thread is unsafe under win32
204
b5807cdb 205=item PERL_OLD_SIGNALS are not threadsafe, will not be.
206
11c51ed3 207
47ba8780 208=back
209
210=head1 SEE ALSO
211
11c51ed3 212L<perl>, L<threads::shared>, L<perlcall>, L<perlembed>, L<perlguts>
47ba8780 213
214=cut