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