As suggested by Arthur: the threads and threads::shared
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.pm
index e92f1c9..11878eb 100755 (executable)
@@ -1,15 +1,45 @@
 package threads;
 
-use 5.7.2;
+use 5.007_003;
 use strict;
 use warnings;
+use Config;
 
-use overload 
+BEGIN {
+    unless ($Config{useithreads}) {
+       my @caller = caller(2);
+        die <<EOF;
+$caller[1] line $caller[2]:
+
+This Perl hasn't been configured and built properly for the threads
+module to work.  (The 'useithreads' configuration option hasn't been used.)
+
+Having threads support requires all of Perl and all of the modules in
+the Perl installation to be rebuilt, it is not just a question of adding
+the threads module.  (In other words, threaded and non-threaded Perls
+are binary incompatible.)
+
+If you want to the use the threads module, please contact the people
+who built your Perl.
+
+Cannot continue, aborting.
+EOF
+    }
+}
+
+use overload
     '==' => \&equal,
     'fallback' => 1;
 
 #use threads::Shared;
 
+BEGIN {
+    warn "Warning, threads::shared has already been loaded. ".
+       "To enable shared variables for these modules 'use threads' ".
+       "must be called before any of those modules are loaded\n"
+               if($threads::shared::threads_shared);
+}
+
 require Exporter;
 require DynaLoader;
 
@@ -34,6 +64,9 @@ $threads::threads = 1;
 
 bootstrap threads $VERSION;
 
+# why document 'new' then use 'create' in the tests!
+*create = \&new;
+
 # Preloaded methods go here.
 
 1;
@@ -51,9 +84,9 @@ sub start_thread {
     print "Thread started\n";
 }
 
-my $thread = threads->new("start_thread","argument");
+my $thread = threads->create("start_thread","argument");
 
-$thread->new(sub { print "I am a thread"},"argument");
+$thread->create(sub { print "I am a thread"},"argument");
 
 $thread->join();
 
@@ -83,18 +116,19 @@ must use threads::shared.
 
 It is also important to note that you preferably enable threads by
 doing C<use threads> as early as possible and that it is not possible
-to enable threading inside an eval "";
+to enable threading inside an eval "";  In particular, if you are
+intending to share variables with threads::shared, you must
+C<use threads> before you C<use threads::shared> and threads will emit
+a warning if you do it the other way around.
 
 =over
 
-=item $thread = new(function, LIST)
+=item $thread = threads->create(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.
-
 =item $thread->join
 
 This will wait for the corresponding thread to join. When it finishes
@@ -117,6 +151,17 @@ quick way to get current thread id.
 
 =back
 
+=head1 WARNINGS
+
+=over 4
+
+=item Cleanup skipped %d active threads
+
+The main thread exited while there were still other threads running.
+This is not a good sign: you should either explicitly join the threads,
+or somehow be certain that all the non-main threads have finished.
+
+=back
 
 =head1 TODO
 
@@ -136,9 +181,9 @@ Arthur Bergman E<lt>arthur at contiller.seE<gt>
 
 threads is released under the same license as Perl.
 
-Thanks to 
+Thanks to
 
-Richard Soderberg E<lt>rs at crystalflame.netE<gt> 
+Richard Soderberg E<lt>rs at crystalflame.netE<gt>
 Helping me out tons, trying to find reasons for races and other weird bugs!
 
 Simon Cozens E<lt>simon at brecon.co.ukE<gt>