Nearly-working threads re-structuring. Do not integrate,
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.pm
CommitLineData
47ba8780 1package threads;
2
3use 5.7.2;
4use strict;
5use warnings;
6
68795e93 7use overload
43d3ddbe 8 '==' => \&equal,
47ba8780 9 'fallback' => 1;
10
47ba8780 11#use threads::Shared;
12
dab065ea 13BEGIN {
14 warn "Warning, threads::shared has already been loaded. ".
15 "To enable shared variables for these modules 'use threads' ".
16 "must be called before any of those modules are loaded\n"
17 if($threads::shared::threads_shared);
18}
19
47ba8780 20require Exporter;
21require DynaLoader;
22
47ba8780 23our @ISA = qw(Exporter DynaLoader);
24
25our %EXPORT_TAGS = ( all => [qw()]);
26
27our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
28
29our @EXPORT = qw(
30
31);
32our $VERSION = '0.05';
33
47ba8780 34
43d3ddbe 35sub equal {
47ba8780 36 return 1 if($_[0]->tid() == $_[1]->tid());
37 return 0;
38}
39
8222d950 40$threads::threads = 1;
47ba8780 41
42bootstrap threads $VERSION;
43
68795e93 44# why document 'new' then use 'create' in the tests!
45*create = \&new;
46
47ba8780 47# Preloaded methods go here.
48
491;
50__END__
51
52=head1 NAME
53
54threads - Perl extension allowing use of interpreter based threads from perl
55
56=head1 SYNOPSIS
57
47ba8780 58use threads;
59
60sub start_thread {
61 print "Thread started\n";
62}
63
47ba8780 64my $thread = threads->new("start_thread","argument");
65
66$thread->new(sub { print "I am a thread"},"argument");
67
68$thread->join();
69
70$thread->detach();
71
72$thread = threads->self();
73
11c51ed3 74threads->tid();
75threads->self->tid();
76
77$thread->tid();
47ba8780 78
47ba8780 79=head1 DESCRIPTION
80
43d3ddbe 81Perl 5.6 introduced something called interpreter threads. Interpreter
82threads are different from "5005threads" (the thread model of Perl
835.005) by creating a new perl interpreter per thread and not sharing
84any data or state between threads.
11c51ed3 85
43d3ddbe 86Prior to perl 5.8 this has only been available to people embedding
87perl and for emulating fork() on windows.
11c51ed3 88
43d3ddbe 89The threads API is loosely based on the old Thread.pm API. It is very
90important to note that variables are not shared between threads, all
91variables are per default thread local. To use shared variables one
92must use threads::shared.
11c51ed3 93
43d3ddbe 94It is also important to note that you preferably enable threads by
95doing C<use threads> as early as possible and that it is not possible
dab065ea 96to enable threading inside an eval ""; In particular, if you are
97intending to share variables with threads::shared, you must
98C<use threads> before you C<use threads::shared> and threads will emit
99a warning if you do it the other way around.
47ba8780 100
101=over
102
43d3ddbe 103=item $thread = new(function, LIST)
47ba8780 104
ad91d581 105This will create a new thread with the entry point function and give
106it LIST as parameters. It will return the corresponding threads
107object.
47ba8780 108
43d3ddbe 109create() is an alias to new.
11c51ed3 110
111=item $thread->join
47ba8780 112
43d3ddbe 113This will wait for the corresponding thread to join. When it finishes
114join will return the return values of the entry point function. If a
115thread has been detached, join will return without wait.
47ba8780 116
11c51ed3 117=item $thread->detach
47ba8780 118
43d3ddbe 119Will throw away the return value from the thread and make it
120non-joinable.
47ba8780 121
122=item threads->self
123
124This will return the object for the current thread.
125
11c51ed3 126=item $thread->tid
47ba8780 127
43d3ddbe 128This will return the id of the thread. threads->self->tid() is a
129quick way to get current thread id.
47ba8780 130
131=back
132
133
134=head1 TODO
135
136=over
137
43d3ddbe 138=item Fix so the return value is returned when you join.
47ba8780 139
43d3ddbe 140=item Add join_all.
47ba8780 141
142=item Fix memory leaks!
143
144=back
145
146=head1 AUTHOR and COPYRIGHT
147
11c51ed3 148Arthur Bergman E<lt>arthur at contiller.seE<gt>
47ba8780 149
43d3ddbe 150threads is released under the same license as Perl.
47ba8780 151
68795e93 152Thanks to
47ba8780 153
68795e93 154Richard Soderberg E<lt>rs at crystalflame.netE<gt>
ad91d581 155Helping me out tons, trying to find reasons for races and other weird bugs!
47ba8780 156
ad91d581 157Simon Cozens E<lt>simon at brecon.co.ukE<gt>
158Being there to answer zillions of annoying questions
47ba8780 159
ad91d581 160Rocco Caputo E<lt>troc at netrus.netE<gt>
47ba8780 161
ad91d581 162Vipul Ved Prakash E<lt>mail at vipul.netE<gt>
47ba8780 163Helping with debugging.
164
165please join perl-ithreads@perl.org for more information
166
167=head1 BUGS
168
169=over
170
171=item creating a thread from within a thread is unsafe under win32
172
b5807cdb 173=item PERL_OLD_SIGNALS are not threadsafe, will not be.
174
11c51ed3 175
47ba8780 176=back
177
178=head1 SEE ALSO
179
11c51ed3 180L<perl>, L<threads::shared>, L<perlcall>, L<perlembed>, L<perlguts>
47ba8780 181
182=cut