Commit | Line | Data |
385d56e4 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | BEGIN { |
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 | |
16 | use ExtUtils::testlib; |
17 | |
9feacc09 |
18 | use threads; |
9feacc09 |
19 | |
20 | BEGIN { |
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 |
34 | my $TEST; |
35 | BEGIN { |
36 | share($TEST); |
37 | $TEST = 1; |
38 | } |
9feacc09 |
39 | |
40 | ok(1, 'Loaded'); |
41 | |
385d56e4 |
42 | sub 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 |
65 | my ($COUNT, $STARTED) :shared; |
385d56e4 |
66 | |
67 | sub threading_1 { |
68 | my $tid = threads->tid(); |
9feacc09 |
69 | ok($tid, "Thread $tid started"); |
385d56e4 |
70 | |
f782ee33 |
71 | { |
72 | lock($STARTED); |
73 | $STARTED++; |
74 | } |
75 | if ($STARTED < 5) { |
385d56e4 |
76 | sleep(1); |
77 | threads->create('threading_1')->detach(); |
78 | } |
79 | |
80 | threads->yield(); |
81 | |
82 | if ($tid == 1) { |
83 | sleep(2); |
84 | } elsif ($tid == 2) { |
85 | sleep(6); |
86 | } elsif ($tid == 3) { |
87 | sleep(3); |
88 | } elsif ($tid == 4) { |
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 | { |
105 | lock($COUNT); |
106 | while ($COUNT < 3) { |
107 | cond_wait($COUNT); |
f782ee33 |
108 | threads->create(sub { |
109 | threads->create(sub { })->join(); |
110 | })->join(); |
385d56e4 |
111 | } |
112 | } |
113 | } |
114 | { |
115 | { |
116 | lock($COUNT); |
117 | while ($COUNT < 5) { |
118 | cond_wait($COUNT); |
f782ee33 |
119 | threads->create(sub { |
120 | threads->create(sub { })->join(); |
121 | })->join(); |
385d56e4 |
122 | } |
123 | } |
124 | threads->yield(); |
125 | sleep(1); |
126 | } |
9feacc09 |
127 | ok($COUNT == 5, "Done - $COUNT threads"); |
385d56e4 |
128 | |
129 | |
130 | sub threading_2 { |
131 | my $tid = threads->tid(); |
9feacc09 |
132 | ok($tid, "Thread $tid started"); |
385d56e4 |
133 | |
f782ee33 |
134 | { |
135 | lock($STARTED); |
136 | $STARTED++; |
137 | } |
138 | if ($STARTED < 5) { |
385d56e4 |
139 | threads->create('threading_2')->detach(); |
140 | } |
141 | |
142 | threads->yield(); |
143 | |
144 | lock($COUNT); |
145 | $COUNT++; |
146 | cond_signal($COUNT); |
147 | |
9feacc09 |
148 | ok($tid, "Thread $tid done"); |
385d56e4 |
149 | } |
150 | |
151 | { |
f782ee33 |
152 | $STARTED = 0; |
385d56e4 |
153 | $COUNT = 0; |
154 | threads->create('threading_2')->detach(); |
f782ee33 |
155 | threads->create(sub { |
156 | threads->create(sub { })->join(); |
157 | })->join(); |
385d56e4 |
158 | { |
159 | lock($COUNT); |
f782ee33 |
160 | while ($COUNT < 5) { |
385d56e4 |
161 | cond_wait($COUNT); |
162 | } |
163 | } |
164 | threads->yield(); |
165 | sleep(1); |
166 | } |
9feacc09 |
167 | ok($COUNT == 5, "Done - $COUNT threads"); |
385d56e4 |
168 | |
169 | |
170 | { |
171 | threads->create(sub { })->join(); |
172 | } |
9feacc09 |
173 | ok(1, 'Join'); |
385d56e4 |
174 | |
175 | |
176 | sub threading_3 { |
177 | my $tid = threads->tid(); |
9feacc09 |
178 | ok($tid, "Thread $tid started"); |
385d56e4 |
179 | |
180 | { |
181 | threads->create(sub { |
182 | my $tid = threads->tid(); |
9feacc09 |
183 | ok($tid, "Thread $tid started"); |
385d56e4 |
184 | |
185 | threads->yield(); |
186 | sleep(1); |
187 | |
188 | lock($COUNT); |
189 | $COUNT++; |
190 | cond_signal($COUNT); |
191 | |
9feacc09 |
192 | ok($tid, "Thread $tid done"); |
385d56e4 |
193 | })->join(); |
194 | } |
195 | |
196 | lock($COUNT); |
197 | $COUNT++; |
198 | cond_signal($COUNT); |
199 | |
9feacc09 |
200 | ok($tid, "Thread $tid done"); |
385d56e4 |
201 | } |
202 | |
203 | { |
204 | $COUNT = 0; |
205 | threads->create(sub { |
206 | threads->create('threading_3')->detach(); |
207 | { |
208 | lock($COUNT); |
209 | while ($COUNT < 2) { |
210 | cond_wait($COUNT); |
211 | } |
212 | } |
213 | })->join(); |
214 | threads->yield(); |
215 | sleep(1); |
216 | } |
9feacc09 |
217 | ok($COUNT == 2, "Done - $COUNT threads"); |
385d56e4 |
218 | |
219 | # EOF |