1. Move #30327 to the generation location
[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 }
14}
15
16use ExtUtils::testlib;
17
18my $Base = 0;
19sub ok {
20 my ($id, $ok, $why) = @_;
21 $id += $Base;
22
23 # You have to do it this way or VMS will get confused.
24 if ($ok) {
25 print("ok $id\n");
26 } else {
27 print ("not ok $id\n");
28 printf("# Failed test at line %d\n", (caller)[2]);
05b59262 29 print ("# Reason: $why\n");
30 }
31
32 return ($ok);
33}
34
35BEGIN {
36 $| = 1;
37 print("1..50\n"); ### Number of tests that will be run ###
38};
39
40use threads;
41use threads::shared;
42
43### Start of Testing ###
44
45#####
46#
47# Launches a bunch of threads which are then
48# restricted to finishing in numerical order
49#
50# Frequently fails under MSWin32 due to deadlocking bug in Windows
51# http://rt.perl.org/rt3/Public/Bug/Display.html?id=41574
52# http://support.microsoft.com/kb/175332
53#
54#####
55{
56 my $cnt = 50;
57
58 my $TIMEOUT = 30;
59
60 my $mutex = 1;
61 share($mutex);
62
63 my @threads;
64 for (1..$cnt) {
65 $threads[$_] = threads->create(sub {
66 my $tnum = shift;
67 my $timeout = time() + $TIMEOUT;
68
69 # Randomize the amount of work the thread does
70 my $sum;
71 for (0..(500000+int(rand(500000)))) {
72 $sum++
73 }
74
75 # Lock the mutex
76 lock($mutex);
77
78 # Wait for my turn to finish
79 while ($mutex != $tnum) {
80 if (! cond_timedwait($mutex, $timeout)) {
81 if ($mutex == $tnum) {
82 return ('timed out - cond_broadcast not received');
83 } else {
84 return ('timed out');
85 }
86 }
87 }
88
89 # Finish up
90 $mutex++;
91 cond_broadcast($mutex);
92 return ('okay');
93 }, $_);
94 }
95
96 # Gather thread results
97 for (1..$cnt) {
98 my $rc = $threads[$_]->join() || 'Thread failed';
99 ok($_, ($rc eq 'okay'), $rc);
100 }
101
102 $Base += $cnt;
103}
104
105# EOF