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 28d40c8..44ef1cb 100644 (file)
@@ -16,15 +16,26 @@ BEGIN {
 use ExtUtils::testlib;
 
 use threads;
-use threads::shared;
 
 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 = 1;
-share($TEST);
+my $TEST;
+BEGIN {
+    share($TEST);
+    $TEST = 1;
+}
 
 ok(1, 'Loaded');
 
@@ -51,27 +62,30 @@ sub ok {
 # 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 ($COUNT, $STARTED) :shared;
 
 sub threading_1 {
     my $tid = threads->tid();
     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);
@@ -84,23 +98,22 @@ sub threading_1 {
 }
 
 {
+    $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($COUNT == 5, "Done - $COUNT threads");
@@ -110,10 +123,13 @@ sub threading_2 {
     my $tid = threads->tid();
     ok($tid, "Thread $tid started");
 
-    if ($tid < 10) {
+    {
+        lock($STARTED);
+        $STARTED++;
+    }
+    if ($STARTED < 5) {
         threads->create('threading_2')->detach();
     }
-
     threads->yield();
 
     lock($COUNT);
@@ -124,15 +140,18 @@ sub threading_2 {
 }
 
 {
+    $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($COUNT == 5, "Done - $COUNT threads");
@@ -153,7 +172,6 @@ sub threading_3 {
             my $tid = threads->tid();
             ok($tid, "Thread $tid started");
 
-            threads->yield();
             sleep(1);
 
             lock($COUNT);
@@ -161,7 +179,7 @@ sub threading_3 {
             cond_signal($COUNT);
 
             ok($tid, "Thread $tid done");
-        })->join();
+        })->detach();
     }
 
     lock($COUNT);
@@ -182,7 +200,6 @@ sub threading_3 {
             }
         }
     })->join();
-    threads->yield();
     sleep(1);
 }
 ok($COUNT == 2, "Done - $COUNT threads");