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 | } |
4271b527 |
9 | |
10 | # Import test.pl into its own package |
11 | { |
12 | package Test; |
13 | require($ENV{PERL_CORE} ? './test.pl' : './t/test.pl'); |
14 | } |
15 | |
385d56e4 |
16 | use Config; |
17 | if (! $Config{'useithreads'}) { |
4271b527 |
18 | Test::skip_all(q/Perl not compiled with 'useithreads'/); |
385d56e4 |
19 | } |
20 | } |
21 | |
22 | use ExtUtils::testlib; |
23 | |
9feacc09 |
24 | use threads; |
9feacc09 |
25 | |
26 | BEGIN { |
58a3a76c |
27 | eval { |
28 | require threads::shared; |
f3086ff0 |
29 | threads::shared->import(); |
58a3a76c |
30 | }; |
31 | if ($@ || ! $threads::shared::threads_shared) { |
4271b527 |
32 | Test::skip_all(q/threads::shared not available/); |
58a3a76c |
33 | } |
34 | |
18b9e6f5 |
35 | require Thread::Queue; |
36 | |
9feacc09 |
37 | $| = 1; |
38 | print("1..29\n"); ### Number of tests that will be run ### |
4dcb9e53 |
39 | } |
9feacc09 |
40 | |
4271b527 |
41 | Test::watchdog(120); # In case we get stuck |
9feacc09 |
42 | |
18b9e6f5 |
43 | my $q = Thread::Queue->new(); |
44 | my $TEST = 1; |
9feacc09 |
45 | |
18b9e6f5 |
46 | sub ok |
47 | { |
48 | $q->enqueue(@_); |
49 | |
50 | while ($q->pending()) { |
51 | my $ok = $q->dequeue(); |
52 | my $name = $q->dequeue(); |
53 | my $id = $TEST++; |
54 | |
55 | if ($ok) { |
56 | print("ok $id - $name\n"); |
57 | } else { |
58 | print("not ok $id - $name\n"); |
59 | printf("# Failed test at line %d\n", (caller)[2]); |
60 | } |
385d56e4 |
61 | } |
385d56e4 |
62 | } |
63 | |
385d56e4 |
64 | |
65 | ### Start of Testing ### |
18b9e6f5 |
66 | ok(1, 'Loaded'); |
385d56e4 |
67 | |
68 | # Tests freeing the Perl interperter for each thread |
69 | # See http://www.nntp.perl.org/group/perl.perl5.porters/110772 for details |
70 | |
f782ee33 |
71 | my ($COUNT, $STARTED) :shared; |
385d56e4 |
72 | |
73 | sub threading_1 { |
18b9e6f5 |
74 | my $q = shift; |
75 | |
385d56e4 |
76 | my $tid = threads->tid(); |
18b9e6f5 |
77 | $q->enqueue($tid, "Thread $tid started"); |
385d56e4 |
78 | |
d4315dd6 |
79 | my $id; |
f782ee33 |
80 | { |
81 | lock($STARTED); |
82 | $STARTED++; |
d4315dd6 |
83 | $id = $STARTED; |
f782ee33 |
84 | } |
85 | if ($STARTED < 5) { |
385d56e4 |
86 | sleep(1); |
18b9e6f5 |
87 | threads->create('threading_1', $q)->detach(); |
385d56e4 |
88 | } |
89 | |
d4315dd6 |
90 | if ($id == 1) { |
385d56e4 |
91 | sleep(2); |
d4315dd6 |
92 | } elsif ($id == 2) { |
385d56e4 |
93 | sleep(6); |
d4315dd6 |
94 | } elsif ($id == 3) { |
385d56e4 |
95 | sleep(3); |
d4315dd6 |
96 | } elsif ($id == 4) { |
385d56e4 |
97 | sleep(1); |
98 | } else { |
99 | sleep(2); |
100 | } |
101 | |
102 | lock($COUNT); |
103 | $COUNT++; |
104 | cond_signal($COUNT); |
18b9e6f5 |
105 | $q->enqueue($tid, "Thread $tid done"); |
385d56e4 |
106 | } |
107 | |
108 | { |
f782ee33 |
109 | $STARTED = 0; |
385d56e4 |
110 | $COUNT = 0; |
18b9e6f5 |
111 | threads->create('threading_1', $q)->detach(); |
385d56e4 |
112 | { |
d4315dd6 |
113 | my $cnt = 0; |
114 | while ($cnt < 5) { |
115 | { |
116 | lock($COUNT); |
117 | cond_wait($COUNT) if ($COUNT < 5); |
118 | $cnt = $COUNT; |
119 | } |
f782ee33 |
120 | threads->create(sub { |
121 | threads->create(sub { })->join(); |
122 | })->join(); |
385d56e4 |
123 | } |
124 | } |
385d56e4 |
125 | sleep(1); |
126 | } |
9feacc09 |
127 | ok($COUNT == 5, "Done - $COUNT threads"); |
385d56e4 |
128 | |
129 | |
130 | sub threading_2 { |
18b9e6f5 |
131 | my $q = shift; |
132 | |
385d56e4 |
133 | my $tid = threads->tid(); |
18b9e6f5 |
134 | $q->enqueue($tid, "Thread $tid started"); |
385d56e4 |
135 | |
f782ee33 |
136 | { |
137 | lock($STARTED); |
138 | $STARTED++; |
139 | } |
140 | if ($STARTED < 5) { |
18b9e6f5 |
141 | threads->create('threading_2', $q)->detach(); |
385d56e4 |
142 | } |
385d56e4 |
143 | threads->yield(); |
144 | |
145 | lock($COUNT); |
146 | $COUNT++; |
147 | cond_signal($COUNT); |
148 | |
18b9e6f5 |
149 | $q->enqueue($tid, "Thread $tid done"); |
385d56e4 |
150 | } |
151 | |
152 | { |
f782ee33 |
153 | $STARTED = 0; |
385d56e4 |
154 | $COUNT = 0; |
18b9e6f5 |
155 | threads->create('threading_2', $q)->detach(); |
f782ee33 |
156 | threads->create(sub { |
157 | threads->create(sub { })->join(); |
158 | })->join(); |
385d56e4 |
159 | { |
160 | lock($COUNT); |
f782ee33 |
161 | while ($COUNT < 5) { |
385d56e4 |
162 | cond_wait($COUNT); |
163 | } |
164 | } |
385d56e4 |
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 { |
18b9e6f5 |
177 | my $q = shift; |
178 | |
385d56e4 |
179 | my $tid = threads->tid(); |
18b9e6f5 |
180 | $q->enqueue($tid, "Thread $tid started"); |
385d56e4 |
181 | |
182 | { |
183 | threads->create(sub { |
18b9e6f5 |
184 | my $q = shift; |
185 | |
385d56e4 |
186 | my $tid = threads->tid(); |
18b9e6f5 |
187 | $q->enqueue($tid, "Thread $tid started"); |
385d56e4 |
188 | |
385d56e4 |
189 | sleep(1); |
190 | |
191 | lock($COUNT); |
192 | $COUNT++; |
193 | cond_signal($COUNT); |
194 | |
18b9e6f5 |
195 | $q->enqueue($tid, "Thread $tid done"); |
196 | }, $q)->detach(); |
385d56e4 |
197 | } |
198 | |
199 | lock($COUNT); |
200 | $COUNT++; |
201 | cond_signal($COUNT); |
202 | |
18b9e6f5 |
203 | $q->enqueue($tid, "Thread $tid done"); |
385d56e4 |
204 | } |
205 | |
206 | { |
207 | $COUNT = 0; |
208 | threads->create(sub { |
18b9e6f5 |
209 | threads->create('threading_3', $q)->detach(); |
385d56e4 |
210 | { |
211 | lock($COUNT); |
212 | while ($COUNT < 2) { |
213 | cond_wait($COUNT); |
214 | } |
215 | } |
216 | })->join(); |
385d56e4 |
217 | sleep(1); |
218 | } |
9feacc09 |
219 | ok($COUNT == 2, "Done - $COUNT threads"); |
385d56e4 |
220 | |
561ee912 |
221 | exit(0); |
222 | |
385d56e4 |
223 | # EOF |