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