Upgrade to threads::shared 1.31
authorJerry D. Hedden <jdhedden@cpan.org>
Mon, 10 Aug 2009 19:26:25 +0000 (15:26 -0400)
committerH.Merijn Brand <h.m.brand@xs4all.nl>
Mon, 10 Aug 2009 20:08:43 +0000 (22:08 +0200)
Attached patch changes t/stress.t such that it more robustly handles
thread creation failures due to 'no more processes', 'out of memory',
etc..  Such failures are not indicative of problems with the
'threads::shared' module.

From 2440c97ff633f0a33bf1ca0cceecfd4448cd6167 Mon Sep 17 00:00:00 2001
From: Jerry D. Hedden <jdhedden@cpan.org>
Date: Mon, 10 Aug 2009 15:22:54 -0400
Subject: [PATCH] Upgrade to threads::shared 1.31

Signed-off-by: H.Merijn Brand <h.m.brand@xs4all.nl>
ext/threads-shared/shared.pm
ext/threads-shared/t/stress.t

index 722e3ce..b1b4552 100644 (file)
@@ -7,7 +7,7 @@ use warnings;
 
 use Scalar::Util qw(reftype refaddr blessed);
 
-our $VERSION = '1.29';
+our $VERSION = '1.31';
 my $XS_VERSION = $VERSION;
 $VERSION = eval $VERSION;
 
@@ -187,7 +187,7 @@ threads::shared - Perl extension for sharing data structures between threads
 
 =head1 VERSION
 
-This document describes threads::shared version 1.29
+This document describes threads::shared version 1.31
 
 =head1 SYNOPSIS
 
@@ -588,7 +588,7 @@ L<threads::shared> Discussion Forum on CPAN:
 L<http://www.cpanforum.com/dist/threads-shared>
 
 Annotated POD for L<threads::shared>:
-L<http://annocpan.org/~JDHEDDEN/threads-shared-1.29/shared.pm>
+L<http://annocpan.org/~JDHEDDEN/threads-shared-1.31/shared.pm>
 
 Source repository:
 L<http://code.google.com/p/threads-shared/>
index e36ab0a..6648867 100644 (file)
@@ -32,14 +32,18 @@ use threads::shared;
 #
 #####
 {
-    my $cnt = 50;
+    my $cnt = 200;
 
     my $TIMEOUT = 60;
 
     my $mutex = 1;
     share($mutex);
 
+    my $warning;
+    $SIG{__WARN__} = sub { $warning = shift; };
+
     my @threads;
+
     for (reverse(1..$cnt)) {
         $threads[$_] = threads->create(sub {
                             my $tnum = shift;
@@ -71,10 +75,26 @@ use threads::shared;
                             cond_broadcast($mutex);
                             return ('okay');
                       }, $_);
+
+        # Handle thread creation failures
+        if ($warning) {
+            my $printit = 1;
+            if ($warning =~ /returned 11/) {
+                $warning = "Thread creation failed due to 'No more processes'\n";
+                $printit = (! $ENV{'PERL_CORE'});
+            } elsif ($warning =~ /returned 12/) {
+                $warning = "Thread creation failed due to 'No more memory'\n";
+                $printit = (! $ENV{'PERL_CORE'});
+            }
+            print(STDERR "# Warning: $warning") if ($printit);
+            lock($mutex);
+            $mutex = $_ + 1;
+            last;
+        }
     }
 
     # Gather thread results
-    my ($okay, $failures, $timeouts, $unknown) = (0, 0, 0, 0);
+    my ($okay, $failures, $timeouts, $unknown) = (0, 0, 0, 0, 0);
     for (1..$cnt) {
         if (! $threads[$_]) {
             $failures++;
@@ -92,10 +112,10 @@ use threads::shared;
             }
         }
     }
+
     if ($failures) {
-        # Most likely due to running out of memory
-        print(STDERR "# Warning: $failures threads failed\n");
-        print(STDERR "# Note: errno 12 = ENOMEM\n");
+        my $only = $cnt - $failures;
+        print(STDERR "# Warning: Intended to use $cnt threads, but could only muster $only\n");
         $cnt -= $failures;
     }