1package Thread; 2 3use strict; 4 5our($VERSION, $ithreads, $othreads); 6 7BEGIN { 8 $VERSION = '2.00'; 9 use Config; 10 $ithreads = $Config{useithreads}; 11 $othreads = $Config{use5005threads}; 12} 13 14require Exporter; 15use XSLoader (); 16our(@ISA, @EXPORT, @EXPORT_OK); 17 18@ISA = qw(Exporter); 19 20BEGIN { 21 if ($ithreads) { 22 @EXPORT = qw(cond_wait cond_broadcast cond_signal) 23 } elsif ($othreads) { 24 @EXPORT_OK = qw(cond_signal cond_broadcast cond_wait); 25 } 26 push @EXPORT_OK, qw(async yield); 27} 28 29=head1 NAME 30 31Thread - manipulate threads in Perl (for old code only) 32 33=head1 CAVEAT 34 35Perl has two thread models. 36 37In Perl 5.005 the thread model was that all data is implicitly shared 38and shared access to data has to be explicitly synchronized. 39This model is called "5005threads". 40 41In Perl 5.6 a new model was introduced in which all is was thread 42local and shared access to data has to be explicitly declared. 43This model is called "ithreads", for "interpreter threads". 44 45In Perl 5.6 the ithreads model was not available as a public API, 46only as an internal API that was available for extension writers, 47and to implement fork() emulation on Win32 platforms. 48 49In Perl 5.8 the ithreads model became available through the C<threads> 50module. 51 52Neither model is configured by default into Perl (except, as mentioned 53above, in Win32 ithreads are always available.) You can see your 54Perl's threading configuration by running C<perl -V> and looking for 55the I<use...threads> variables, or inside script by C<use Config;> 56and testing for C<$Config{use5005threads}> and C<$Config{useithreads}>. 57 58For old code and interim backwards compatibility, the Thread module 59has been reworked to function as a frontend for both 5005threads and 60ithreads. 61 62Note that the compatibility is not complete: because the data sharing 63models are directly opposed, anything to do with data sharing has to 64be thought differently. With the ithreads you must explicitly share() 65variables between the threads. 66 67For new code the use of the C<Thread> module is discouraged and 68the direct use of the C<threads> and C<threads::shared> modules 69is encouraged instead. 70 71Finally, note that there are many known serious problems with the 725005threads, one of the least of which is that regular expression 73match variables like $1 are not threadsafe, that is, they easily get 74corrupted by competing threads. Other problems include more insidious 75data corruption and mysterious crashes. You are seriously urged to 76use ithreads instead. 77 78=head1 SYNOPSIS 79 80 use Thread; 81 82 my $t = Thread->new(\&start_sub, @start_args); 83 84 $result = $t->join; 85 $result = $t->eval; 86 $t->detach; 87 88 if ($t->done) { 89 $t->join; 90 } 91 92 if($t->equal($another_thread)) { 93 # ... 94 } 95 96 yield(); 97 98 my $tid = Thread->self->tid; 99 100 lock($scalar); 101 lock(@array); 102 lock(%hash); 103 104 lock(\&sub); # not available with ithreads 105 106 $flags = $t->flags; # not available with ithreads 107 108 my @list = Thread->list; # not available with ithreads 109 110 use Thread 'async'; 111 112=head1 DESCRIPTION 113 114The C<Thread> module provides multithreading support for perl. 115 116=head1 FUNCTIONS 117 118=over 8 119 120=item $thread = Thread->new(\&start_sub) 121 122=item $thread = Thread->new(\&start_sub, LIST) 123 124C<new> starts a new thread of execution in the referenced subroutine. The 125optional list is passed as parameters to the subroutine. Execution 126continues in both the subroutine and the code after the C<new> call. 127 128C<Thread->new> returns a thread object representing the newly created 129thread. 130 131=item lock VARIABLE 132 133C<lock> places a lock on a variable until the lock goes out of scope. 134 135If the variable is locked by another thread, the C<lock> call will 136block until it's available. C<lock> is recursive, so multiple calls 137to C<lock> are safe--the variable will remain locked until the 138outermost lock on the variable goes out of scope. 139 140Locks on variables only affect C<lock> calls--they do I<not> affect normal 141access to a variable. (Locks on subs are different, and covered in a bit.) 142If you really, I<really> want locks to block access, then go ahead and tie 143them to something and manage this yourself. This is done on purpose. 144While managing access to variables is a good thing, Perl doesn't force 145you out of its living room... 146 147If a container object, such as a hash or array, is locked, all the 148elements of that container are not locked. For example, if a thread 149does a C<lock @a>, any other thread doing a C<lock($a[12])> won't 150block. 151 152With 5005threads you may also C<lock> a sub, using C<lock &sub>. 153Any calls to that sub from another thread will block until the lock 154is released. This behaviour is not equivalent to declaring the sub 155with the C<locked> attribute. The C<locked> attribute serializes 156access to a subroutine, but allows different threads non-simultaneous 157access. C<lock &sub>, on the other hand, will not allow I<any> other 158thread access for the duration of the lock. 159 160Finally, C<lock> will traverse up references exactly I<one> level. 161C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not. 162 163=item async BLOCK; 164 165C<async> creates a thread to execute the block immediately following 166it. This block is treated as an anonymous sub, and so must have a 167semi-colon after the closing brace. Like C<Thread->new>, C<async> 168returns a thread object. 169 170=item Thread->self 171 172The C<Thread-E<gt>self> function returns a thread object that represents 173the thread making the C<Thread-E<gt>self> call. 174 175=item cond_wait VARIABLE 176 177The C<cond_wait> function takes a B<locked> variable as 178a parameter, unlocks the variable, and blocks until another thread 179does a C<cond_signal> or C<cond_broadcast> for that same locked 180variable. The variable that C<cond_wait> blocked on is relocked 181after the C<cond_wait> is satisfied. If there are multiple threads 182C<cond_wait>ing on the same variable, all but one will reblock waiting 183to reaquire the lock on the variable. (So if you're only using 184C<cond_wait> for synchronization, give up the lock as soon as 185possible.) 186 187=item cond_signal VARIABLE 188 189The C<cond_signal> function takes a locked variable as a parameter and 190unblocks one thread that's C<cond_wait>ing on that variable. If more than 191one thread is blocked in a C<cond_wait> on that variable, only one (and 192which one is indeterminate) will be unblocked. 193 194If there are no threads blocked in a C<cond_wait> on the variable, 195the signal is discarded. 196 197=item cond_broadcast VARIABLE 198 199The C<cond_broadcast> function works similarly to C<cond_signal>. 200C<cond_broadcast>, though, will unblock B<all> the threads that are 201blocked in a C<cond_wait> on the locked variable, rather than only 202one. 203 204=item yield 205 206The C<yield> function allows another thread to take control of the 207CPU. The exact results are implementation-dependent. 208 209=back 210 211=head1 METHODS 212 213=over 8 214 215=item join 216 217C<join> waits for a thread to end and returns any values the thread 218exited with. C<join> will block until the thread has ended, though 219it won't block if the thread has already terminated. 220 221If the thread being C<join>ed C<die>d, the error it died with will 222be returned at this time. If you don't want the thread performing 223the C<join> to die as well, you should either wrap the C<join> in 224an C<eval> or use the C<eval> thread method instead of C<join>. 225 226=item eval 227 228The C<eval> method wraps an C<eval> around a C<join>, and so waits for 229a thread to exit, passing along any values the thread might have returned. 230Errors, of course, get placed into C<$@>. (Not available with ithreads.) 231 232=item detach 233 234C<detach> tells a thread that it is never going to be joined i.e. 235that all traces of its existence can be removed once it stops running. 236Errors in detached threads will not be visible anywhere - if you want 237to catch them, you should use $SIG{__DIE__} or something like that. 238 239=item equal 240 241C<equal> tests whether two thread objects represent the same thread and 242returns true if they do. 243 244=item tid 245 246The C<tid> method returns the tid of a thread. The tid is 247a monotonically increasing integer assigned when a thread is 248created. The main thread of a program will have a tid of zero, 249while subsequent threads will have tids assigned starting with one. 250 251=item flags 252 253The C<flags> method returns the flags for the thread. This is the 254integer value corresponding to the internal flags for the thread, 255and the value may not be all that meaningful to you. 256(Not available with ithreads.) 257 258=item done 259 260The C<done> method returns true if the thread you're checking has 261finished, and false otherwise. (Not available with ithreads.) 262 263=back 264 265=head1 LIMITATIONS 266 267The sequence number used to assign tids is a simple integer, and no 268checking is done to make sure the tid isn't currently in use. If a 269program creates more than 2**32 - 1 threads in a single run, threads 270may be assigned duplicate tids. This limitation may be lifted in 271a future version of Perl. 272 273=head1 SEE ALSO 274 275L<threads::shared> (not available with 5005threads) 276 277L<attributes>, L<Thread::Queue>, L<Thread::Semaphore>, 278L<Thread::Specific> (not available with ithreads) 279 280=cut 281 282# 283# Methods 284# 285 286# 287# Exported functions 288# 289 290sub async (&) { 291 return Thread->new($_[0]); 292} 293 294sub eval { 295 return eval { shift->join; }; 296} 297 298sub unimplemented { 299 print $_[0], " unimplemented with ", 300 $Config{useithreads} ? "ithreads" : "5005threads", "\n"; 301 302} 303 304sub unimplement { 305 for my $m (@_) { 306 no strict 'refs'; 307 *{"Thread::$m"} = sub { unimplemented $m }; 308 } 309} 310 311BEGIN { 312 if ($ithreads) { 313 if ($othreads) { 314 require Carp; 315 Carp::croak("This Perl has both ithreads and 5005threads (serious malconfiguration)"); 316 } 317 XSLoader::load 'threads'; 318 for my $m (qw(new join detach yield self tid equal list)) { 319 no strict 'refs'; 320 *{"Thread::$m"} = \&{"threads::$m"}; 321 } 322 require 'threads/shared.pm'; 323 for my $m (qw(cond_signal cond_broadcast cond_wait)) { 324 no strict 'refs'; 325 *{"Thread::$m"} = \&{"threads::shared::${m}_enabled"}; 326 } 327 # trying to unimplement eval gives redefined warning 328 unimplement(qw(done flags)); 329 } elsif ($othreads) { 330 XSLoader::load 'Thread'; 331 } else { 332 require Carp; 333 Carp::croak("This Perl has neither ithreads nor 5005threads"); 334 } 335} 336 3371; 338