From: Jarkko Hietaniemi Date: Fri, 16 Nov 2001 04:37:52 +0000 (+0000) Subject: Make Thread a wrapper for both ithreads and 5005threads. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=43d3ddbe925b286b82a05bbdbe845c119bb8b987;p=p5sagit%2Fp5-mst-13.2.git Make Thread a wrapper for both ithreads and 5005threads. Needs a lot of work and Arthur's eyeballing (and finishing few missing odds and ends of threads.pm). Now Thread is an extension without its own .pm. If there are people who have experience working with either the 5005threads or the ithreads: cleaning up the documentation (Thread.pm, threads.pm, and perl*thrtut.pod), and porting the various Thread::* modules, like Thread::Queue, to work also with ithreads, is needed. Removed the 5005threads requirement from the 5005 tests for easier testing (not all of them work yet since they use the Thread::* modules that have not been made to work with ithreads). Leave the 5005 tests named as *.tx so that they are not run by "make test". p4raw-id: //depot/perl@13036 --- diff --git a/MANIFEST b/MANIFEST index 07cee63..87bd671 100644 --- a/MANIFEST +++ b/MANIFEST @@ -561,7 +561,6 @@ ext/Thread/specific.tx Test thread-specific user data ext/Thread/sync.tx Test thread synchronisation ext/Thread/sync2.tx Test thread synchronisation ext/Thread/thr5005.t Test 5.005-style threading (skipped if no use5005threads) -ext/Thread/Thread.pm Thread extension Perl module ext/Thread/Thread.xs Thread extension external subroutines ext/Thread/Thread/Queue.pm Thread synchronised queue objects ext/Thread/Thread/Semaphore.pm Thread semaphore objects @@ -1260,6 +1259,7 @@ lib/Text/TabsWrap/t/fill.t See if Text::Wrap::fill works lib/Text/TabsWrap/t/tabs.t See if Text::Tabs works lib/Text/TabsWrap/t/wrap.t See if Text::Wrap::wrap works lib/Text/Wrap.pm Paragraph formatter +lib/Thread.pm Thread extensions frontend lib/Tie/Array.pm Base class for tied arrays lib/Tie/Array/push.t Test for Tie::Array lib/Tie/Array/splice.t Test for Tie::Array::SPLICE diff --git a/ext/Thread/Makefile.PL b/ext/Thread/Makefile.PL index e67fbb7..fc09e7f 100644 --- a/ext/Thread/Makefile.PL +++ b/ext/Thread/Makefile.PL @@ -1,7 +1,7 @@ use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Thread', - VERSION_FROM => 'Thread.pm', - MAN3PODS => {} + VERSION => '2.00', + MAN3PODS => {} ); diff --git a/ext/Thread/Thread.pm b/ext/Thread/Thread.pm deleted file mode 100644 index 6220a60..0000000 --- a/ext/Thread/Thread.pm +++ /dev/null @@ -1,241 +0,0 @@ -package Thread; -require Exporter; -use XSLoader (); -our($VERSION, @ISA, @EXPORT); - -$VERSION = "1.01"; - -@ISA = qw(Exporter); -@EXPORT_OK = qw(yield cond_signal cond_broadcast cond_wait async); - -=head1 NAME - -Thread - manipulate threads in Perl (EXPERIMENTAL, subject to change) - -=head1 CAVEAT - -The Thread extension requires Perl to be built in a particular way to -enable the older 5.005 threading model. Just to confuse matters, there -is an alternate threading model known as "ithreads" that does NOT -support this extension. If you are using a binary distribution such -as ActivePerl that is built with ithreads support, this extension CANNOT -be used. - -=head1 SYNOPSIS - - use Thread; - - my $t = new Thread \&start_sub, @start_args; - - $result = $t->join; - $result = $t->eval; - $t->detach; - $flags = $t->flags; - - if ($t->done) { - $t->join; - } - - if($t->equal($another_thread)) { - # ... - } - - my $tid = Thread->self->tid; - my $tlist = Thread->list; - - lock($scalar); - yield(); - - use Thread 'async'; - -=head1 DESCRIPTION - - WARNING: Threading is an experimental feature. Both the interface - and implementation are subject to change drastically. In fact, this - documentation describes the flavor of threads that was in version - 5.005. Perl 5.6.0 and later have the beginnings of support for - interpreter threads, which (when finished) is expected to be - significantly different from what is described here. The information - contained here may therefore soon be obsolete. Use at your own risk! - -The C module provides multithreading support for perl. - -=head1 FUNCTIONS - -=over 8 - -=item new \&start_sub - -=item new \&start_sub, LIST - -C starts a new thread of execution in the referenced subroutine. The -optional list is passed as parameters to the subroutine. Execution -continues in both the subroutine and the code after the C call. - -C returns a thread object representing the newly created -thread. - -=item lock VARIABLE - -C places a lock on a variable until the lock goes out of scope. If -the variable is locked by another thread, the C call will block until -it's available. C is recursive, so multiple calls to C are -safe--the variable will remain locked until the outermost lock on the -variable goes out of scope. - -Locks on variables only affect C calls--they do I affect normal -access to a variable. (Locks on subs are different, and covered in a bit) -If you really, I want locks to block access, then go ahead and tie -them to something and manage this yourself. This is done on purpose. While -managing access to variables is a good thing, perl doesn't force you out of -its living room... - -If a container object, such as a hash or array, is locked, all the elements -of that container are not locked. For example, if a thread does a C, any other thread doing a C won't block. - -You may also C a sub, using C. Any calls to that sub from -another thread will block until the lock is released. This behaviour is not -equivalent to declaring the sub with the C attribute. The C -attribute serializes access to a subroutine, but allows different threads -non-simultaneous access. C, on the other hand, will not allow -I other thread access for the duration of the lock. - -Finally, C will traverse up references exactly I level. -C is equivalent to C, while C is not. - -=item async BLOCK; - -C creates a thread to execute the block immediately following -it. This block is treated as an anonymous sub, and so must have a -semi-colon after the closing brace. Like C, C returns a -thread object. - -=item Thread->self - -The Cself> function returns a thread object that represents -the thread making the Cself> call. - -=item Thread->list - -Clist> returns a list of thread objects for all running and -finished but un-Ced threads. - -=item cond_wait VARIABLE - -The C function takes a B variable as a parameter, -unlocks the variable, and blocks until another thread does a C -or C for that same locked variable. The variable that -C blocked on is relocked after the C is satisfied. -If there are multiple threads Cing on the same variable, all but -one will reblock waiting to reaquire the lock on the variable. (So if -you're only using C for synchronization, give up the lock as -soon as possible) - -=item cond_signal VARIABLE - -The C function takes a locked variable as a parameter and -unblocks one thread that's Cing on that variable. If more than -one thread is blocked in a C on that variable, only one (and -which one is indeterminate) will be unblocked. - -If there are no threads blocked in a C on the variable, the -signal is discarded. - -=item cond_broadcast VARIABLE - -The C function works similarly to C. -C, though, will unblock B the threads that are blocked -in a C on the locked variable, rather than only one. - -=item yield - -The C function allows another thread to take control of the -CPU. The exact results are implementation-dependent. - -=back - -=head1 METHODS - -=over 8 - -=item join - -C waits for a thread to end and returns any values the thread exited -with. C will block until the thread has ended, though it won't block -if the thread has already terminated. - -If the thread being Ced Cd, the error it died with will be -returned at this time. If you don't want the thread performing the C -to die as well, you should either wrap the C in an C or use the -C thread method instead of C. - -=item eval - -The C method wraps an C around a C, and so waits for a -thread to exit, passing along any values the thread might have returned. -Errors, of course, get placed into C<$@>. - -=item detach - -C tells a thread that it is never going to be joined i.e. -that all traces of its existence can be removed once it stops running. -Errors in detached threads will not be visible anywhere - if you want -to catch them, you should use $SIG{__DIE__} or something like that. - -=item equal - -C tests whether two thread objects represent the same thread and -returns true if they do. - -=item tid - -The C method returns the tid of a thread. The tid is a monotonically -increasing integer assigned when a thread is created. The main thread of a -program will have a tid of zero, while subsequent threads will have tids -assigned starting with one. - -=item flags - -The C method returns the flags for the thread. This is the -integer value corresponding to the internal flags for the thread, and -the value may not be all that meaningful to you. - -=item done - -The C method returns true if the thread you're checking has -finished, and false otherwise. - -=back - -=head1 LIMITATIONS - -The sequence number used to assign tids is a simple integer, and no -checking is done to make sure the tid isn't currently in use. If a program -creates more than 2^32 - 1 threads in a single run, threads may be assigned -duplicate tids. This limitation may be lifted in a future version of Perl. - -=head1 SEE ALSO - -L, L, L, L. - -=cut - -# -# Methods -# - -# -# Exported functions -# -sub async (&) { - return new Thread $_[0]; -} - -sub eval { - return eval { shift->join; }; -} - -XSLoader::load 'Thread'; - -1; diff --git a/ext/Thread/create.tx b/ext/Thread/create.tx index 7cc8334..13f3852 100644 --- a/ext/Thread/create.tx +++ b/ext/Thread/create.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread 'async'; diff --git a/ext/Thread/die.tx b/ext/Thread/die.tx index 1a65f14..2581416 100644 --- a/ext/Thread/die.tx +++ b/ext/Thread/die.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread 'async'; diff --git a/ext/Thread/die2.tx b/ext/Thread/die2.tx index 4bc611a..d84b3d0 100644 --- a/ext/Thread/die2.tx +++ b/ext/Thread/die2.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread 'async'; diff --git a/ext/Thread/io.tx b/ext/Thread/io.tx index 693d910..8855897 100644 --- a/ext/Thread/io.tx +++ b/ext/Thread/io.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/join.tx b/ext/Thread/join.tx index d24473f..db9e219 100644 --- a/ext/Thread/join.tx +++ b/ext/Thread/join.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/join2.tx b/ext/Thread/join2.tx index 04f244d..c7d5e15 100644 --- a/ext/Thread/join2.tx +++ b/ext/Thread/join2.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/list.tx b/ext/Thread/list.tx index f03fa06..424b46f 100644 --- a/ext/Thread/list.tx +++ b/ext/Thread/list.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread qw(async); diff --git a/ext/Thread/lock.tx b/ext/Thread/lock.tx index b888787..d8199b5 100644 --- a/ext/Thread/lock.tx +++ b/ext/Thread/lock.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/queue.tx b/ext/Thread/queue.tx index 5c1a7e7..de3a895 100644 --- a/ext/Thread/queue.tx +++ b/ext/Thread/queue.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/specific.tx b/ext/Thread/specific.tx index c2bf1a1..4747b6a 100644 --- a/ext/Thread/specific.tx +++ b/ext/Thread/specific.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/sync.tx b/ext/Thread/sync.tx index c5cf1a7..4fa25a7 100644 --- a/ext/Thread/sync.tx +++ b/ext/Thread/sync.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/sync2.tx b/ext/Thread/sync2.tx index ace0cbd..7311231 100644 --- a/ext/Thread/sync2.tx +++ b/ext/Thread/sync2.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/unsync.tx b/ext/Thread/unsync.tx index 51faa8c..b4adb3b 100644 --- a/ext/Thread/unsync.tx +++ b/ext/Thread/unsync.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/unsync2.tx b/ext/Thread/unsync2.tx index 0016646..1576537 100644 --- a/ext/Thread/unsync2.tx +++ b/ext/Thread/unsync2.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/unsync3.tx b/ext/Thread/unsync3.tx index a1d2c56..ecf77d1 100644 --- a/ext/Thread/unsync3.tx +++ b/ext/Thread/unsync3.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/Thread/unsync4.tx b/ext/Thread/unsync4.tx index 1186fc0..8a6c1241 100644 --- a/ext/Thread/unsync4.tx +++ b/ext/Thread/unsync4.tx @@ -4,10 +4,6 @@ BEGIN { print "1..0 # Skip: no Config\n"; exit(0); } - if ($Config{extensions} !~ /\bThread\b/) { - print "1..0 # Skip: no use5005threads\n"; - exit(0); - } } use Thread; diff --git a/ext/threads/threads.pm b/ext/threads/threads.pm index 1e4182d..e92f1c9 100755 --- a/ext/threads/threads.pm +++ b/ext/threads/threads.pm @@ -5,7 +5,7 @@ use strict; use warnings; use overload - '==' => \&equals, + '==' => \&equal, 'fallback' => 1; #use threads::Shared; @@ -25,7 +25,7 @@ our @EXPORT = qw( our $VERSION = '0.05'; -sub equals { +sub equal { return 1 if($_[0]->tid() == $_[1]->tid()); return 0; } @@ -68,39 +68,43 @@ $thread->tid(); =head1 DESCRIPTION -Perl 5.6 introduced something called interpreter threads. Interpreter -threads are different from 5005 threads by creating a new perl -interpreter per thread and not sharing any data or state between threads. +Perl 5.6 introduced something called interpreter threads. Interpreter +threads are different from "5005threads" (the thread model of Perl +5.005) by creating a new perl interpreter per thread and not sharing +any data or state between threads. -Prior to perl 5.8 this has only been available to people embedding perl and -for emulating fork() on windows. +Prior to perl 5.8 this has only been available to people embedding +perl and for emulating fork() on windows. -The threads API is loosly based on the old Thread.pm API. It is very important -to note that variables are not shared between threads, all variables are per -default thread local. To use shared variables one must use threads::shared. +The threads API is loosely based on the old Thread.pm API. It is very +important to note that variables are not shared between threads, all +variables are per default thread local. To use shared variables one +must use threads::shared. -It is also important to note that you preferebly enable threads by doing -C as early as possible and it is not possible to enable threading -by in an eval ""; +It is also important to note that you preferably enable threads by +doing C as early as possible and that it is not possible +to enable threading inside an eval ""; =over -=item new, function, LIST +=item $thread = new(function, LIST) This will create a new thread with the entry point function and give it LIST as parameters. It will return the corresponding threads object. -create is an alias to new +create() is an alias to new. =item $thread->join -This will wait for the corresponding thread to join. When it finishes join will return the return values of the root function. -If a thread has been detached, join will return without wait. +This will wait for the corresponding thread to join. When it finishes +join will return the return values of the entry point function. If a +thread has been detached, join will return without wait. =item $thread->detach -Will throw away the return value from the thread and make non joinable +Will throw away the return value from the thread and make it +non-joinable. =item threads->self @@ -108,8 +112,8 @@ This will return the object for the current thread. =item $thread->tid -This will return the id of the thread. -threads->self->tid() is a quick way to get current thread id +This will return the id of the thread. threads->self->tid() is a +quick way to get current thread id. =back @@ -118,9 +122,9 @@ threads->self->tid() is a quick way to get current thread id =over -=item Fix so the return value is returned when you join +=item Fix so the return value is returned when you join. -=item Add join_all +=item Add join_all. =item Fix memory leaks! @@ -130,7 +134,7 @@ threads->self->tid() is a quick way to get current thread id Arthur Bergman Earthur at contiller.seE -threads is released under the same license as Perl +threads is released under the same license as Perl. Thanks to diff --git a/lib/Thread.pm b/lib/Thread.pm new file mode 100644 index 0000000..b6be663 --- /dev/null +++ b/lib/Thread.pm @@ -0,0 +1,321 @@ +package Thread; + +$VERSION = '2.00'; + +BEGIN { + use Config; + our $ithreads = $Config{useithreads}; + our $othreads = $Config{use5005threads}; +} + +require Exporter; +use XSLoader (); +our($VERSION, @ISA, @EXPORT); + +@ISA = qw(Exporter); + +BEGIN { + if ($ithreads) { + @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock) + } elsif ($othreads) { + @EXPORT_OK = qw(cond_signal cond_broadcast cond_wait); + } + push @EXPORT_OK, qw(async yield); +} + +=head1 NAME + +Thread - manipulate threads in Perl + +=head1 CAVEAT + +Perl has two thread models. + +In Perl 5.005 the thread model was that all data is implicitly shared +and shared access to data has to be explicitly synchronized. +This model is called "5005threads". + +In Perl 5.6 a new model was introduced in which all is was thread +local and shared access to data has to be explicitly declared. +This model is called "ithreads", for "interpreter threads". + +In Perl 5.6 the ithreads model was not available as a public API, +only as an internal API that was available for extension writers, +and to implement fork() emulation on Win32 platforms. + +In Perl 5.8 the ithreads model became available through the C +module. + +Neither model is configured by default into Perl (except, as mentioned +above, in Win32 ithreads are always available.) + +For backwards compatibility, the Thread module has been reworked +to function as a frontend for both 5005threads and ithreads. +Note that the compatibility is not complete: because the data sharing +models are directly opposed, anything to do with data sharing has to +be thought differently. With the ithreads you must explicitly share() +variables between the threads. + +Finally, note that there are many known serious problems with the +5005threads, one of the least of which is that regular expression +match variables like $1 are not threadsafe, that is, they easily get +corrupted by competing threads. Other problems include more insidious +data corruption and mysterious crashes. You are seriously urged to +use ithreads instead. + +=head1 SYNOPSIS + + use Thread; + + my $t = Thread->new(\&start_sub, @start_args); + + $result = $t->join; + $result = $t->eval; + $t->detach; + + if ($t->done) { + $t->join; + } + + if($t->equal($another_thread)) { + # ... + } + + yield(); + + my $tid = Thread->self->tid; + + lock($scalar); + lock(@array); + lock(%hash); + + lock(\&sub); # not available with ithreads + + $flags = $t->flags; # not available with ithreads + + my @list = Thread->list; # not available with ithreads + + unlock(...); # not available with the 5.005 threads + + use Thread 'async'; + +=head1 DESCRIPTION + +The C module provides multithreading support for perl. + +=head1 FUNCTIONS + +=over 8 + +=item $thread = Thread->new(\&start_sub) + +=item $thread = Thread->new(\&start_sub, LIST) + +C starts a new thread of execution in the referenced subroutine. The +optional list is passed as parameters to the subroutine. Execution +continues in both the subroutine and the code after the C call. + +C returns a thread object representing the newly created +thread. + +=item lock VARIABLE + +C places a lock on a variable until the lock goes out of scope +(with ithreads you can also explicitly unlock()). + +If the variable is locked by another thread, the C call will +block until it's available. C is recursive, so multiple calls +to C are safe--the variable will remain locked until the +outermost lock on the variable goes out of scope. + +Locks on variables only affect C calls--they do I affect normal +access to a variable. (Locks on subs are different, and covered in a bit.) +If you really, I want locks to block access, then go ahead and tie +them to something and manage this yourself. This is done on purpose. +While managing access to variables is a good thing, Perl doesn't force +you out of its living room... + +If a container object, such as a hash or array, is locked, all the +elements of that container are not locked. For example, if a thread +does a C, any other thread doing a C won't +block. + +With 5005threads you may also C a sub, using C. +Any calls to that sub from another thread will block until the lock +is released. This behaviour is not equivalent to declaring the sub +with the C attribute. The C attribute serializes +access to a subroutine, but allows different threads non-simultaneous +access. C, on the other hand, will not allow I other +thread access for the duration of the lock. + +Finally, C will traverse up references exactly I level. +C is equivalent to C, while C is not. + +=item async BLOCK; + +C creates a thread to execute the block immediately following +it. This block is treated as an anonymous sub, and so must have a +semi-colon after the closing brace. Like C, C +returns a thread object. + +=item Thread->self + +The Cself> function returns a thread object that represents +the thread making the Cself> call. + +=item cond_wait VARIABLE + +The C function takes a B variable as +a parameter, unlocks the variable, and blocks until another thread +does a C or C for that same locked +variable. The variable that C blocked on is relocked +after the C is satisfied. If there are multiple threads +Cing on the same variable, all but one will reblock waiting +to reaquire the lock on the variable. (So if you're only using +C for synchronization, give up the lock as soon as +possible.) + +=item cond_signal VARIABLE + +The C function takes a locked variable as a parameter and +unblocks one thread that's Cing on that variable. If more than +one thread is blocked in a C on that variable, only one (and +which one is indeterminate) will be unblocked. + +If there are no threads blocked in a C on the variable, +the signal is discarded. + +=item cond_broadcast VARIABLE + +The C function works similarly to C. +C, though, will unblock B the threads that are +blocked in a C on the locked variable, rather than only +one. + +=item yield + +The C function allows another thread to take control of the +CPU. The exact results are implementation-dependent. + +=back + +=head1 METHODS + +=over 8 + +=item join + +C waits for a thread to end and returns any values the thread +exited with. C will block until the thread has ended, though +it won't block if the thread has already terminated. + +If the thread being Ced Cd, the error it died with will +be returned at this time. If you don't want the thread performing +the C to die as well, you should either wrap the C in +an C or use the C thread method instead of C. + +=item eval + +The C method wraps an C around a C, and so waits for +a thread to exit, passing along any values the thread might have returned. +Errors, of course, get placed into C<$@>. (Not available with ithreads.) + +=item detach + +C tells a thread that it is never going to be joined i.e. +that all traces of its existence can be removed once it stops running. +Errors in detached threads will not be visible anywhere - if you want +to catch them, you should use $SIG{__DIE__} or something like that. + +=item equal + +C tests whether two thread objects represent the same thread and +returns true if they do. + +=item tid + +The C method returns the tid of a thread. The tid is +a monotonically increasing integer assigned when a thread is +created. The main thread of a program will have a tid of zero, +while subsequent threads will have tids assigned starting with one. + +=item flags + +The C method returns the flags for the thread. This is the +integer value corresponding to the internal flags for the thread, +and the value may not be all that meaningful to you. +(Not available with ithreads.) + +=item done + +The C method returns true if the thread you're checking has +finished, and false otherwise. (Not available with ithreads.) + +=back + +=head1 LIMITATIONS + +The sequence number used to assign tids is a simple integer, and no +checking is done to make sure the tid isn't currently in use. If a +program creates more than 2**32 - 1 threads in a single run, threads +may be assigned duplicate tids. This limitation may be lifted in +a future version of Perl. + +=head1 SEE ALSO + +L (not available with 5005threads) + +L, L, L, +L (not available with ithreads) + +=cut + +# +# Methods +# + +# +# Exported functions +# + +sub async (&) { + return Thread->new($_[0]); +} + +sub eval { + return eval { shift->join; }; +} + +sub unimplemented { + print $_[0], " unimplemented with ", + $Config{useithreads} ? "ithreads" : "5005threads", "\n"; + +} + +sub unimplement { + for my $m (@_) { + *{"Thread::$m"} = sub { unimplemented $m }; + } +} + +BEGIN { + if ($ithreads) { + XSLoader::load 'threads'; + for my $m (qw(new join detach yield self tid equal)) { + *{"Thread::$m"} = \&{"threads::$m"}; + } + XSLoader::load 'threads::shared'; + for my $m (qw(cond_signal cond_broadcast cond_wait unlock share)) { + *{"Thread::$m"} = \&{"threads::shared::${m}_enabled"}; + } + unimplement(qw(list done eval flags)); + } elsif ($othreads) { + XSLoader::load 'Thread'; + unimplement(qw(unlock)); + } else { + require Carp; + Carp::croak("This Perl has neither ithreads not 5005threads"); + } +} + +1; diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 5ad0afb..74778ac 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -2444,9 +2444,9 @@ and the month of the year, may not necessarily be three characters wide. This function places an advisory lock on a variable, subroutine, or referenced object contained in I until the lock goes out of scope. This is a built-in function only if your version of Perl -was built with threading enabled, and if you've said C. -Otherwise a user-defined function by this name will be called. See -L. +was built with threading enabled, and if you've said C. +Otherwise a user-defined function by this name will be called. +See L. =item log EXPR