As we're not passing over (or copying in) a NUL, don't need that extra
[p5sagit/p5-mst-13.2.git] / ext / threads / t / free.t
index bf9e3a7..44ef1cb 100644 (file)
@@ -15,8 +15,35 @@ BEGIN {
 
 use ExtUtils::testlib;
 
+use threads;
+
+BEGIN {
+    eval {
+        require threads::shared;
+        import threads::shared;
+    };
+    if ($@ || ! $threads::shared::threads_shared) {
+        print("1..0 # Skip: threads::shared not available\n");
+        exit(0);
+    }
+
+    $| = 1;
+    print("1..29\n");   ### Number of tests that will be run ###
+};
+
+my $TEST;
+BEGIN {
+    share($TEST);
+    $TEST = 1;
+}
+
+ok(1, 'Loaded');
+
 sub ok {
-    my ($id, $ok, $name) = @_;
+    my ($ok, $name) = @_;
+
+    lock($TEST);
+    my $id = $TEST++;
 
     # You have to do it this way or VMS will get confused.
     if ($ok) {
@@ -29,43 +56,36 @@ sub ok {
     return ($ok);
 }
 
-BEGIN {
-    $| = 1;
-    print("1..29\n");   ### Number of tests that will be run ###
-};
-
-use threads;
-use threads::shared;
-ok(1, 1, 'Loaded');
 
 ### Start of Testing ###
 
 # Tests freeing the Perl interperter for each thread
 # See http://www.nntp.perl.org/group/perl.perl5.porters/110772 for details
 
-my $COUNT;
-share($COUNT);
-my $TEST = 2;
-share($TEST);
+my ($COUNT, $STARTED) :shared;
 
 sub threading_1 {
     my $tid = threads->tid();
-    ok($TEST++, $tid, "Thread $tid started");
+    ok($tid, "Thread $tid started");
 
-    if ($tid < 5) {
+    my $id;
+    {
+        lock($STARTED);
+        $STARTED++;
+        $id = $STARTED;
+    }
+    if ($STARTED < 5) {
         sleep(1);
         threads->create('threading_1')->detach();
     }
 
-    threads->yield();
-
-    if ($tid == 1) {
+    if ($id == 1) {
         sleep(2);
-    } elsif ($tid == 2) {
+    } elsif ($id == 2) {
         sleep(6);
-    } elsif ($tid == 3) {
+    } elsif ($id == 3) {
         sleep(3);
-    } elsif ($tid == 4) {
+    } elsif ($id == 4) {
         sleep(1);
     } else {
         sleep(2);
@@ -74,95 +94,99 @@ sub threading_1 {
     lock($COUNT);
     $COUNT++;
     cond_signal($COUNT);
-    ok($TEST++, $tid, "Thread $tid done");
+    ok($tid, "Thread $tid done");
 }
 
 {
+    $STARTED = 0;
     $COUNT = 0;
     threads->create('threading_1')->detach();
     {
-        lock($COUNT);
-        while ($COUNT < 3) {
-            cond_wait($COUNT);
-        }
-    }
-}
-{
-    {
-        lock($COUNT);
-        while ($COUNT < 5) {
-            cond_wait($COUNT);
+        my $cnt = 0;
+        while ($cnt < 5) {
+            {
+                lock($COUNT);
+                cond_wait($COUNT) if ($COUNT < 5);
+                $cnt = $COUNT;
+            }
+            threads->create(sub {
+                threads->create(sub { })->join();
+            })->join();
         }
     }
-    threads->yield();
     sleep(1);
 }
-ok($TEST++, $COUNT == 5, "Done - $COUNT threads");
+ok($COUNT == 5, "Done - $COUNT threads");
 
 
 sub threading_2 {
     my $tid = threads->tid();
-    ok($TEST++, $tid, "Thread $tid started");
+    ok($tid, "Thread $tid started");
 
-    if ($tid < 10) {
+    {
+        lock($STARTED);
+        $STARTED++;
+    }
+    if ($STARTED < 5) {
         threads->create('threading_2')->detach();
     }
-
     threads->yield();
 
     lock($COUNT);
     $COUNT++;
     cond_signal($COUNT);
 
-    ok($TEST++, $tid, "Thread $tid done");
+    ok($tid, "Thread $tid done");
 }
 
 {
+    $STARTED = 0;
     $COUNT = 0;
     threads->create('threading_2')->detach();
+    threads->create(sub {
+        threads->create(sub { })->join();
+    })->join();
     {
         lock($COUNT);
-        while ($COUNT < 3) {
+        while ($COUNT < 5) {
             cond_wait($COUNT);
         }
     }
-    threads->yield();
     sleep(1);
 }
-ok($TEST++, $COUNT == 5, "Done - $COUNT threads");
+ok($COUNT == 5, "Done - $COUNT threads");
 
 
 {
     threads->create(sub { })->join();
 }
-ok($TEST++, 1, 'Join');
+ok(1, 'Join');
 
 
 sub threading_3 {
     my $tid = threads->tid();
-    ok($TEST++, $tid, "Thread $tid started");
+    ok($tid, "Thread $tid started");
 
     {
         threads->create(sub {
             my $tid = threads->tid();
-            ok($TEST++, $tid, "Thread $tid started");
+            ok($tid, "Thread $tid started");
 
-            threads->yield();
             sleep(1);
 
             lock($COUNT);
             $COUNT++;
             cond_signal($COUNT);
 
-            ok($TEST++, $tid, "Thread $tid done");
-        })->join();
+            ok($tid, "Thread $tid done");
+        })->detach();
     }
 
     lock($COUNT);
     $COUNT++;
     cond_signal($COUNT);
 
-    ok($TEST++, $tid, "Thread $tid done");
+    ok($tid, "Thread $tid done");
 }
 
 {
@@ -176,9 +200,8 @@ sub threading_3 {
             }
         }
     })->join();
-    threads->yield();
     sleep(1);
 }
-ok($TEST++, $COUNT == 2, "Done - $COUNT threads");
+ok($COUNT == 2, "Done - $COUNT threads");
 
 # EOF