threads::shared 1.22
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / t / stress.t
CommitLineData
05b59262 1use strict;
2use warnings;
3
4BEGIN {
5 if ($ENV{'PERL_CORE'}){
6 chdir 't';
7 unshift @INC, '../lib';
8 }
9 use Config;
10 if (! $Config{'useithreads'}) {
6c791b15 11 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
05b59262 12 exit(0);
13 }
eebceeb0 14 if ($^O eq 'hpux' && $Config{osvers} <= 10.20) {
6c791b15 15 print("1..0 # SKIP Broken under HP-UX 10.20\n");
eebceeb0 16 exit(0);
17 }
05b59262 18}
19
20use ExtUtils::testlib;
21
05b59262 22BEGIN {
23 $| = 1;
3b29be8d 24 print("1..1\n"); ### Number of tests that will be run ###
05b59262 25};
26
27use threads;
28use threads::shared;
29
30### Start of Testing ###
31
32#####
33#
34# Launches a bunch of threads which are then
35# restricted to finishing in numerical order
36#
05b59262 37#####
38{
39 my $cnt = 50;
40
41 my $TIMEOUT = 30;
42
43 my $mutex = 1;
44 share($mutex);
45
46 my @threads;
47 for (1..$cnt) {
48 $threads[$_] = threads->create(sub {
49 my $tnum = shift;
50 my $timeout = time() + $TIMEOUT;
51
52 # Randomize the amount of work the thread does
53 my $sum;
54 for (0..(500000+int(rand(500000)))) {
55 $sum++
56 }
57
58 # Lock the mutex
59 lock($mutex);
60
61 # Wait for my turn to finish
62 while ($mutex != $tnum) {
63 if (! cond_timedwait($mutex, $timeout)) {
64 if ($mutex == $tnum) {
65 return ('timed out - cond_broadcast not received');
66 } else {
67 return ('timed out');
68 }
69 }
70 }
71
72 # Finish up
73 $mutex++;
74 cond_broadcast($mutex);
75 return ('okay');
76 }, $_);
77 }
78
79 # Gather thread results
3b29be8d 80 my ($okay, $failures, $timeouts, $unknown) = (0, 0, 0, 0);
05b59262 81 for (1..$cnt) {
f6d55995 82 if (! $threads[$_]) {
3b29be8d 83 $failures++;
3b29be8d 84 } else {
f6d55995 85 my $rc = $threads[$_]->join();
86 if (! $rc) {
87 $failures++;
88 } elsif ($rc =~ /^timed out/) {
89 $timeouts++;
90 } elsif ($rc eq 'okay') {
91 $okay++;
92 } else {
93 $unknown++;
94 print(STDERR "# Unknown error: $rc\n");
95 }
3b29be8d 96 }
05b59262 97 }
f6d55995 98 if ($failures) {
99 # Most likely due to running out of memory
100 print(STDERR "# Warning: $failures threads failed\n");
101 print(STDERR "# Note: errno 12 = ENOMEM\n");
102 $cnt -= $failures;
103 }
05b59262 104
f6d55995 105 if ($unknown || (($okay + $timeouts) != $cnt)) {
291f766e 106 print("not ok 1\n");
f6d55995 107 my $too_few = $cnt - ($okay + $timeouts + $unknown);
291f766e 108 print(STDERR "# Test failed:\n");
109 print(STDERR "#\t$too_few too few threads reported\n") if $too_few;
291f766e 110 print(STDERR "#\t$unknown unknown errors\n") if $unknown;
111 print(STDERR "#\t$timeouts threads timed out\n") if $timeouts;
3b29be8d 112
113 } elsif ($timeouts) {
114 # Frequently fails under MSWin32 due to deadlocking bug in Windows
115 # hence test is TODO under MSWin32
116 # http://rt.perl.org/rt3/Public/Bug/Display.html?id=41574
117 # http://support.microsoft.com/kb/175332
291f766e 118 if ($^O eq 'MSWin32') {
119 print("not ok 1 # TODO - not reliable under MSWin32\n")
120 } else {
121 print("not ok 1\n");
122 print(STDERR "# Test failed: $timeouts threads timed out\n");
123 }
3b29be8d 124
125 } else {
126 print('ok 1');
127 print(' # TODO - not reliable under MSWin32') if ($^O eq 'MSWin32');
128 print("\n");
129 }
05b59262 130}
131
6c791b15 132exit(0);
133
05b59262 134# EOF