As we're not passing over (or copying in) a NUL, don't need that extra
[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
f782ee33 65my ($COUNT, $STARTED) :shared;
385d56e4 66
67sub threading_1 {
68 my $tid = threads->tid();
9feacc09 69 ok($tid, "Thread $tid started");
385d56e4 70
d4315dd6 71 my $id;
f782ee33 72 {
73 lock($STARTED);
74 $STARTED++;
d4315dd6 75 $id = $STARTED;
f782ee33 76 }
77 if ($STARTED < 5) {
385d56e4 78 sleep(1);
79 threads->create('threading_1')->detach();
80 }
81
d4315dd6 82 if ($id == 1) {
385d56e4 83 sleep(2);
d4315dd6 84 } elsif ($id == 2) {
385d56e4 85 sleep(6);
d4315dd6 86 } elsif ($id == 3) {
385d56e4 87 sleep(3);
d4315dd6 88 } elsif ($id == 4) {
385d56e4 89 sleep(1);
90 } else {
91 sleep(2);
92 }
93
94 lock($COUNT);
95 $COUNT++;
96 cond_signal($COUNT);
9feacc09 97 ok($tid, "Thread $tid done");
385d56e4 98}
99
100{
f782ee33 101 $STARTED = 0;
385d56e4 102 $COUNT = 0;
103 threads->create('threading_1')->detach();
104 {
d4315dd6 105 my $cnt = 0;
106 while ($cnt < 5) {
107 {
108 lock($COUNT);
109 cond_wait($COUNT) if ($COUNT < 5);
110 $cnt = $COUNT;
111 }
f782ee33 112 threads->create(sub {
113 threads->create(sub { })->join();
114 })->join();
385d56e4 115 }
116 }
385d56e4 117 sleep(1);
118}
9feacc09 119ok($COUNT == 5, "Done - $COUNT threads");
385d56e4 120
121
122sub threading_2 {
123 my $tid = threads->tid();
9feacc09 124 ok($tid, "Thread $tid started");
385d56e4 125
f782ee33 126 {
127 lock($STARTED);
128 $STARTED++;
129 }
130 if ($STARTED < 5) {
385d56e4 131 threads->create('threading_2')->detach();
132 }
385d56e4 133 threads->yield();
134
135 lock($COUNT);
136 $COUNT++;
137 cond_signal($COUNT);
138
9feacc09 139 ok($tid, "Thread $tid done");
385d56e4 140}
141
142{
f782ee33 143 $STARTED = 0;
385d56e4 144 $COUNT = 0;
145 threads->create('threading_2')->detach();
f782ee33 146 threads->create(sub {
147 threads->create(sub { })->join();
148 })->join();
385d56e4 149 {
150 lock($COUNT);
f782ee33 151 while ($COUNT < 5) {
385d56e4 152 cond_wait($COUNT);
153 }
154 }
385d56e4 155 sleep(1);
156}
9feacc09 157ok($COUNT == 5, "Done - $COUNT threads");
385d56e4 158
159
160{
161 threads->create(sub { })->join();
162}
9feacc09 163ok(1, 'Join');
385d56e4 164
165
166sub threading_3 {
167 my $tid = threads->tid();
9feacc09 168 ok($tid, "Thread $tid started");
385d56e4 169
170 {
171 threads->create(sub {
172 my $tid = threads->tid();
9feacc09 173 ok($tid, "Thread $tid started");
385d56e4 174
385d56e4 175 sleep(1);
176
177 lock($COUNT);
178 $COUNT++;
179 cond_signal($COUNT);
180
9feacc09 181 ok($tid, "Thread $tid done");
d4315dd6 182 })->detach();
385d56e4 183 }
184
185 lock($COUNT);
186 $COUNT++;
187 cond_signal($COUNT);
188
9feacc09 189 ok($tid, "Thread $tid done");
385d56e4 190}
191
192{
193 $COUNT = 0;
194 threads->create(sub {
195 threads->create('threading_3')->detach();
196 {
197 lock($COUNT);
198 while ($COUNT < 2) {
199 cond_wait($COUNT);
200 }
201 }
202 })->join();
385d56e4 203 sleep(1);
204}
9feacc09 205ok($COUNT == 2, "Done - $COUNT threads");
385d56e4 206
207# EOF