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