Resync with mainline
[p5sagit/p5-mst-13.2.git] / ext / Thread / Thread.pm
CommitLineData
d9bb3666 1package Thread;
2require Exporter;
146174a9 3use XSLoader ();
cb50131a 4our($VERSION, @ISA, @EXPORT);
52e1cb5e 5
6$VERSION = "1.0";
7
146174a9 8@ISA = qw(Exporter);
8f4f90ac 9@EXPORT_OK = qw(yield cond_signal cond_broadcast cond_wait async);
734689b1 10
d516a115 11=head1 NAME
12
cb50131a 13Thread - manipulate threads in Perl (EXPERIMENTAL, subject to change)
d516a115 14
15=head1 SYNOPSIS
16
17 use Thread;
18
19 my $t = new Thread \&start_sub, @start_args;
20
23a4f76c 21 $result = $t->join;
22 $result = $t->eval;
23 $t->detach;
d516a115 24
23a4f76c 25 if($t->equal($another_thread)) {
26 # ...
27 }
d516a115 28
23a4f76c 29 my $tid = Thread->self->tid;
d516a115 30 my $tlist = Thread->list;
31
32 lock($scalar);
23a4f76c 33 yield();
d516a115 34
35 use Thread 'async';
36
d516a115 37=head1 DESCRIPTION
38
589fe9d5 39The C<Thread> module provides multithreading support for perl.
40
23a4f76c 41WARNING: Threading is an experimental feature. Both the interface
42and implementation are subject to change drastically.
43
cb50131a 44In fact, this documentation describes the flavor of threads that was in
45version 5.005. Perl v5.6 has the beginnings of support for interpreter
46threads, which (when finished) is expected to be significantly different
47from what is described here. The information contained here may therefore
48soon be obsolete. Use at your own risk!
49
589fe9d5 50=head1 FUNCTIONS
51
52=over 8
53
54=item new \&start_sub
55
56=item new \&start_sub, LIST
57
58C<new> starts a new thread of execution in the referenced subroutine. The
59optional list is passed as parameters to the subroutine. Execution
60continues in both the subroutine and the code after the C<new> call.
61
62C<new Thread> returns a thread object representing the newly created
63thread.
64
65=item lock VARIABLE
66
67C<lock> places a lock on a variable until the lock goes out of scope. If
68the variable is locked by another thread, the C<lock> call will block until
69it's available. C<lock> is recursive, so multiple calls to C<lock> are
70safe--the variable will remain locked until the outermost lock on the
71variable goes out of scope.
72
73Locks on variables only affect C<lock> calls--they do I<not> affect normal
74access to a variable. (Locks on subs are different, and covered in a bit)
75If you really, I<really> want locks to block access, then go ahead and tie
76them to something and manage this yourself. This is done on purpose. While
77managing access to variables is a good thing, perl doesn't force you out of
78its living room...
79
80If a container object, such as a hash or array, is locked, all the elements
81of that container are not locked. For example, if a thread does a C<lock
82@a>, any other thread doing a C<lock($a[12])> won't block.
83
84You may also C<lock> a sub, using C<lock &sub>. Any calls to that sub from
85another thread will block until the lock is released. This behaviour is not
146174a9 86equivalent to declaring the sub with the C<locked> attribute. The C<locked>
87attribute serializes access to a subroutine, but allows different threads
589fe9d5 88non-simultaneous access. C<lock &sub>, on the other hand, will not allow
89I<any> other thread access for the duration of the lock.
90
91Finally, C<lock> will traverse up references exactly I<one> level.
92C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
93
94=item async BLOCK;
95
96C<async> creates a thread to execute the block immediately following
97it. This block is treated as an anonymous sub, and so must have a
98semi-colon after the closing brace. Like C<new Thread>, C<async> returns a
99thread object.
100
101=item Thread->self
102
103The C<Thread-E<gt>self> function returns a thread object that represents
104the thread making the C<Thread-E<gt>self> call.
105
106=item Thread->list
107
108C<Thread-E<gt>list> returns a list of thread objects for all running and
109finished but un-C<join>ed threads.
110
111=item cond_wait VARIABLE
112
113The C<cond_wait> function takes a B<locked> variable as a parameter,
114unlocks the variable, and blocks until another thread does a C<cond_signal>
115or C<cond_broadcast> for that same locked variable. The variable that
116C<cond_wait> blocked on is relocked after the C<cond_wait> is satisfied.
117If there are multiple threads C<cond_wait>ing on the same variable, all but
118one will reblock waiting to reaquire the lock on the variable. (So if
119you're only using C<cond_wait> for synchronization, give up the lock as
120soon as possible)
121
122=item cond_signal VARIABLE
123
124The C<cond_signal> function takes a locked variable as a parameter and
125unblocks one thread that's C<cond_wait>ing on that variable. If more than
126one thread is blocked in a C<cond_wait> on that variable, only one (and
127which one is indeterminate) will be unblocked.
128
129If there are no threads blocked in a C<cond_wait> on the variable, the
130signal is discarded.
131
132=item cond_broadcast VARIABLE
133
134The C<cond_broadcast> function works similarly to C<cond_wait>.
135C<cond_broadcast>, though, will unblock B<all> the threads that are blocked
136in a C<cond_wait> on the locked variable, rather than only one.
137
23a4f76c 138=item yield
139
140The C<yield> function allows another thread to take control of the
141CPU. The exact results are implementation-dependent.
142
589fe9d5 143=back
144
145=head1 METHODS
146
147=over 8
148
149=item join
150
151C<join> waits for a thread to end and returns any values the thread exited
152with. C<join> will block until the thread has ended, though it won't block
153if the thread has already terminated.
154
155If the thread being C<join>ed C<die>d, the error it died with will be
156returned at this time. If you don't want the thread performing the C<join>
157to die as well, you should either wrap the C<join> in an C<eval> or use the
158C<eval> thread method instead of C<join>.
159
160=item eval
161
162The C<eval> method wraps an C<eval> around a C<join>, and so waits for a
163thread to exit, passing along any values the thread might have returned.
164Errors, of course, get placed into C<$@>.
165
23a4f76c 166=item detach
167
168C<detach> tells a thread that it is never going to be joined i.e.
169that all traces of its existence can be removed once it stops running.
170Errors in detached threads will not be visible anywhere - if you want
171to catch them, you should use $SIG{__DIE__} or something like that.
172
173=item equal
174
175C<equal> tests whether two thread objects represent the same thread and
176returns true if they do.
177
589fe9d5 178=item tid
179
180The C<tid> method returns the tid of a thread. The tid is a monotonically
181increasing integer assigned when a thread is created. The main thread of a
182program will have a tid of zero, while subsequent threads will have tids
183assigned starting with one.
184
cb50131a 185=back
186
589fe9d5 187=head1 LIMITATIONS
188
189The sequence number used to assign tids is a simple integer, and no
190checking is done to make sure the tid isn't currently in use. If a program
191creates more than 2^32 - 1 threads in a single run, threads may be assigned
192duplicate tids. This limitation may be lifted in a future version of Perl.
d516a115 193
194=head1 SEE ALSO
195
146174a9 196L<attributes>, L<Thread::Queue>, L<Thread::Semaphore>, L<Thread::Specific>.
d516a115 197
198=cut
199
734689b1 200#
201# Methods
202#
203
204#
205# Exported functions
206#
207sub async (&) {
208 return new Thread $_[0];
209}
d9bb3666 210
458fb581 211sub eval {
212 return eval { shift->join; };
213}
214
146174a9 215XSLoader::load 'Thread';
d9bb3666 216
d9bb3666 2171;