(retracted by #16258)
[p5sagit/p5-mst-13.2.git] / ext / threads / t / list.t
index e5929ed..0adaa23 100644 (file)
@@ -1,12 +1,13 @@
 
 BEGIN {
     chdir 't' if -d 't';
-    @INC = '../lib';
+    @INC = qw(../lib .);
     require Config; import Config;
     unless ($Config{'useithreads'}) {
         print "1..0 # Skip: no useithreads\n";
         exit 0;
     }
+    require "test.pl";
 }
 
 use ExtUtils::testlib;
@@ -15,39 +16,40 @@ use strict;
 
 
 BEGIN { $| = 1; print "1..8\n" };
-use threads;
 
+use_ok('threads');
 
+ok(threads->self == (threads->list)[0]);
 
-print "ok 1\n";
 
+threads->create(sub {})->join();
+ok(scalar @{[threads->list]} == 1);
 
-#########################
-sub ok {       
-    my ($id, $ok, $name) = @_;
-
-    # You have to do it this way or VMS will get confused.
-    print $ok ? "ok $id - $name\n" : "not ok $id - $name\n";
+my $thread = threads->create(sub {});
+ok(scalar @{[threads->list]} == 2);
+$thread->join();
+ok(scalar @{[threads->list]} == 1);
 
-    printf "# Failed test at line %d\n", (caller)[2] unless $ok;
+curr_test(6);
 
-    return $ok;
-}
+# Just a sleep() would not guarantee that we sleep and will not
+# wake up before the just created thread finishes.  Instead, let's
+# use the filesystem as a semaphore.  Creating a directory and removing
+# it should be a reasonably atomic operation even over NFS. 
+# Also, we do not want to depend here on shared variables.
 
+mkdir "thrsem", 0700;
 
-ok(2, threads->self == (threads->list)[0]);
+$thread = threads->create(sub { my $ret = threads->self == (threads->list)[1];
+                               rmdir "thrsem";
+                               return $ret });
 
+sleep 1 while -d "thrsem";
 
-threads->create(sub {})->join();
-ok(3, scalar @{[threads->list]} == 1);
+ok($thread == (threads->list)[1]);
+ok($thread->join());
+ok(scalar @{[threads->list]} == 1);
 
-my $thread = threads->create(sub {});
-ok(4, scalar @{[threads->list]} == 2);
-$thread->join();
-ok(5, scalar @{[threads->list]} == 1);
-
-$thread = threads->create(sub { ok(6, threads->self == (threads->list)[1])});
-sleep 1;
-ok(7, $thread == (threads->list)[1]);
-$thread->join();
-ok(8, scalar @{[threads->list]} == 1);
+END {
+    1 while rmdir "thrsem";
+}