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