threads 1.36 - Signalling terminated threads core dumps
[p5sagit/p5-mst-13.2.git] / ext / threads / t / free.t
CommitLineData
385d56e4 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
9feacc09 18use threads;
9feacc09 19
20BEGIN {
58a3a76c 21 eval {
22 require threads::shared;
23 import threads::shared;
24 };
25 if ($@ || ! $threads::shared::threads_shared) {
26 print("1..0 # Skip: threads::shared not available\n");
27 exit(0);
28 }
29
9feacc09 30 $| = 1;
31 print("1..29\n"); ### Number of tests that will be run ###
32};
33
4dcb9e53 34my $TEST;
35BEGIN {
36 share($TEST);
37 $TEST = 1;
38}
9feacc09 39
40ok(1, 'Loaded');
41
385d56e4 42sub ok {
9feacc09 43 my ($ok, $name) = @_;
44
45 lock($TEST);
46 my $id = $TEST++;
385d56e4 47
48 # You have to do it this way or VMS will get confused.
49 if ($ok) {
50 print("ok $id - $name\n");
51 } else {
52 print("not ok $id - $name\n");
53 printf("# Failed test at line %d\n", (caller)[2]);
54 }
55
56 return ($ok);
57}
58
385d56e4 59
60### Start of Testing ###
61
62# Tests freeing the Perl interperter for each thread
63# See http://www.nntp.perl.org/group/perl.perl5.porters/110772 for details
64
65my $COUNT;
66share($COUNT);
385d56e4 67
68sub threading_1 {
69 my $tid = threads->tid();
9feacc09 70 ok($tid, "Thread $tid started");
385d56e4 71
72 if ($tid < 5) {
73 sleep(1);
74 threads->create('threading_1')->detach();
75 }
76
77 threads->yield();
78
79 if ($tid == 1) {
80 sleep(2);
81 } elsif ($tid == 2) {
82 sleep(6);
83 } elsif ($tid == 3) {
84 sleep(3);
85 } elsif ($tid == 4) {
86 sleep(1);
87 } else {
88 sleep(2);
89 }
90
91 lock($COUNT);
92 $COUNT++;
93 cond_signal($COUNT);
9feacc09 94 ok($tid, "Thread $tid done");
385d56e4 95}
96
97{
98 $COUNT = 0;
99 threads->create('threading_1')->detach();
100 {
101 lock($COUNT);
102 while ($COUNT < 3) {
103 cond_wait($COUNT);
104 }
105 }
106}
107{
108 {
109 lock($COUNT);
110 while ($COUNT < 5) {
111 cond_wait($COUNT);
112 }
113 }
114 threads->yield();
115 sleep(1);
116}
9feacc09 117ok($COUNT == 5, "Done - $COUNT threads");
385d56e4 118
119
120sub threading_2 {
121 my $tid = threads->tid();
9feacc09 122 ok($tid, "Thread $tid started");
385d56e4 123
124 if ($tid < 10) {
125 threads->create('threading_2')->detach();
126 }
127
128 threads->yield();
129
130 lock($COUNT);
131 $COUNT++;
132 cond_signal($COUNT);
133
9feacc09 134 ok($tid, "Thread $tid done");
385d56e4 135}
136
137{
138 $COUNT = 0;
139 threads->create('threading_2')->detach();
140 {
141 lock($COUNT);
142 while ($COUNT < 3) {
143 cond_wait($COUNT);
144 }
145 }
146 threads->yield();
147 sleep(1);
148}
9feacc09 149ok($COUNT == 5, "Done - $COUNT threads");
385d56e4 150
151
152{
153 threads->create(sub { })->join();
154}
9feacc09 155ok(1, 'Join');
385d56e4 156
157
158sub threading_3 {
159 my $tid = threads->tid();
9feacc09 160 ok($tid, "Thread $tid started");
385d56e4 161
162 {
163 threads->create(sub {
164 my $tid = threads->tid();
9feacc09 165 ok($tid, "Thread $tid started");
385d56e4 166
167 threads->yield();
168 sleep(1);
169
170 lock($COUNT);
171 $COUNT++;
172 cond_signal($COUNT);
173
9feacc09 174 ok($tid, "Thread $tid done");
385d56e4 175 })->join();
176 }
177
178 lock($COUNT);
179 $COUNT++;
180 cond_signal($COUNT);
181
9feacc09 182 ok($tid, "Thread $tid done");
385d56e4 183}
184
185{
186 $COUNT = 0;
187 threads->create(sub {
188 threads->create('threading_3')->detach();
189 {
190 lock($COUNT);
191 while ($COUNT < 2) {
192 cond_wait($COUNT);
193 }
194 }
195 })->join();
196 threads->yield();
197 sleep(1);
198}
9feacc09 199ok($COUNT == 2, "Done - $COUNT threads");
385d56e4 200
201# EOF