Fwd: CPAN Upload: Y/YV/YVES/ExtUtils-Install-1.45.tar.gz
[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'}) {
11 print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
12 exit(0);
13 }
eebceeb0 14 if ($^O eq 'hpux' && $Config{osvers} <= 10.20) {
15 print("1..0 # Skip: Broken under HP-UX 10.20\n");
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) {
3b29be8d 82 my $rc = $threads[$_]->join();
83 if (! $rc) {
84 $failures++;
85 } elsif ($rc =~ /^timed out/) {
86 $timeouts++;
87 } elsif ($rc eq 'okay') {
88 $okay++;
89 } else {
90 $unknown++;
91 print("# Unknown error: $rc\n");
92 }
05b59262 93 }
94
3b29be8d 95 if ($failures || $unknown || (($okay + $timeouts) != $cnt)) {
96 print('not ok 1');
97 my $too_few = $cnt - ($okay + $failures + $timeouts + $unknown);
98 print(" - $too_few too few threads reported") if $too_few;
99 print(" - $failures threads failed") if $failures;
100 print(" - $unknown unknown errors") if $unknown;
101 print(" - $timeouts threads timed out") if $timeouts;
102 print("\n");
103
104 } elsif ($timeouts) {
105 # Frequently fails under MSWin32 due to deadlocking bug in Windows
106 # hence test is TODO under MSWin32
107 # http://rt.perl.org/rt3/Public/Bug/Display.html?id=41574
108 # http://support.microsoft.com/kb/175332
109 print('not ok 1');
110 print(' # TODO - not reliable under MSWin32') if ($^O eq 'MSWin32');
111 print(" - $timeouts threads timed out\n");
112
113 } else {
114 print('ok 1');
115 print(' # TODO - not reliable under MSWin32') if ($^O eq 'MSWin32');
116 print("\n");
117 }
05b59262 118}
119
120# EOF