Commit | Line | Data |
0f1612a7 |
1 | use strict; |
2 | use warnings; |
f9dff5f5 |
3 | |
4 | BEGIN { |
0f1612a7 |
5 | if ($ENV{'PERL_CORE'}){ |
6 | chdir 't'; |
7 | unshift @INC, '../lib'; |
8 | } |
9 | use Config; |
fc04eb16 |
10 | if (! $Config{'useithreads'}) { |
11 | print("1..0 # Skip: Perl not compiled with 'useithreads'\n"); |
12 | exit(0); |
f9dff5f5 |
13 | } |
0f1612a7 |
14 | |
15 | require($ENV{PERL_CORE} ? "./test.pl" : "./t/test.pl"); |
f9dff5f5 |
16 | } |
17 | |
18 | use ExtUtils::testlib; |
0f1612a7 |
19 | |
58a3a76c |
20 | use threads; |
21 | |
fc04eb16 |
22 | BEGIN { |
58a3a76c |
23 | eval { |
24 | require threads::shared; |
f3086ff0 |
25 | threads::shared->import(); |
58a3a76c |
26 | }; |
27 | if ($@ || ! $threads::shared::threads_shared) { |
28 | print("1..0 # Skip: threads::shared not available\n"); |
29 | exit(0); |
30 | } |
31 | |
fc04eb16 |
32 | $| = 1; |
863e9b4a |
33 | print("1..34\n"); ### Number of tests that will be run ### |
fc04eb16 |
34 | }; |
35 | |
fc04eb16 |
36 | print("ok 1 - Loaded\n"); |
f9dff5f5 |
37 | |
fc04eb16 |
38 | ### Start of Testing ### |
f9dff5f5 |
39 | |
40 | sub content { |
41 | print shift; |
42 | return shift; |
43 | } |
44 | { |
f4cc38af |
45 | my $t = threads->create(\&content, "ok 2\n", "ok 3\n", 1..1000); |
f9dff5f5 |
46 | print $t->join(); |
47 | } |
48 | { |
49 | my $lock : shared; |
50 | my $t; |
51 | { |
fc04eb16 |
52 | lock($lock); |
53 | $t = threads->create(sub { lock($lock); print "ok 5\n"}); |
54 | print "ok 4\n"; |
f9dff5f5 |
55 | } |
56 | $t->join(); |
57 | } |
58 | |
59 | sub dorecurse { |
60 | my $val = shift; |
61 | my $ret; |
74bf223e |
62 | print $val; |
f9dff5f5 |
63 | if(@_) { |
fc04eb16 |
64 | $ret = threads->create(\&dorecurse, @_); |
65 | $ret->join; |
f9dff5f5 |
66 | } |
67 | } |
68 | { |
f4cc38af |
69 | my $t = threads->create(\&dorecurse, map { "ok $_\n" } 6..10); |
74bf223e |
70 | $t->join(); |
f9dff5f5 |
71 | } |
72 | |
73 | { |
74 | # test that sleep lets other thread run |
f4cc38af |
75 | my $t = threads->create(\&dorecurse, "ok 11\n"); |
da32f63e |
76 | threads->yield; # help out non-preemptive thread implementations |
f9dff5f5 |
77 | sleep 1; |
74bf223e |
78 | print "ok 12\n"; |
79 | $t->join(); |
f9dff5f5 |
80 | } |
81 | { |
82 | my $lock : shared; |
83 | sub islocked { |
fc04eb16 |
84 | lock($lock); |
85 | my $val = shift; |
86 | my $ret; |
87 | print $val; |
88 | if (@_) { |
89 | $ret = threads->create(\&islocked, shift); |
90 | } |
91 | return $ret; |
f9dff5f5 |
92 | } |
f4cc38af |
93 | my $t = threads->create(\&islocked, "ok 13\n", "ok 14\n"); |
f9dff5f5 |
94 | $t->join->join; |
95 | } |
96 | |
97 | |
98 | |
99 | sub testsprintf { |
100 | my $testno = shift; |
101 | my $same = sprintf( "%0.f", $testno); |
8abd20a8 |
102 | return $testno eq $same; |
f9dff5f5 |
103 | } |
104 | |
105 | sub threaded { |
8abd20a8 |
106 | my ($string, $string_end) = @_; |
f9dff5f5 |
107 | |
108 | # Do the match, saving the output in appropriate variables |
109 | $string =~ /(.*)(is)(.*)/; |
110 | # Yield control, allowing the other thread to fill in the match variables |
111 | threads->yield(); |
112 | # Examine the match variable contents; on broken perls this fails |
8abd20a8 |
113 | return $3 eq $string_end; |
f9dff5f5 |
114 | } |
115 | |
116 | |
117 | { |
74bf223e |
118 | curr_test(15); |
8abd20a8 |
119 | |
f4cc38af |
120 | my $thr1 = threads->create(\&testsprintf, 15); |
121 | my $thr2 = threads->create(\&testsprintf, 16); |
f9dff5f5 |
122 | |
123 | my $short = "This is a long string that goes on and on."; |
124 | my $shorte = " a long string that goes on and on."; |
125 | my $long = "This is short."; |
126 | my $longe = " short."; |
127 | my $foo = "This is bar bar bar."; |
128 | my $fooe = " bar bar bar."; |
8abd20a8 |
129 | my $thr3 = new threads \&threaded, $short, $shorte; |
130 | my $thr4 = new threads \&threaded, $long, $longe; |
74bf223e |
131 | my $thr5 = new threads \&testsprintf, 19; |
132 | my $thr6 = new threads \&testsprintf, 20; |
8abd20a8 |
133 | my $thr7 = new threads \&threaded, $foo, $fooe; |
134 | |
135 | ok($thr1->join()); |
136 | ok($thr2->join()); |
137 | ok($thr3->join()); |
138 | ok($thr4->join()); |
139 | ok($thr5->join()); |
140 | ok($thr6->join()); |
141 | ok($thr7->join()); |
f9dff5f5 |
142 | } |
38875929 |
143 | |
144 | # test that 'yield' is importable |
145 | |
146 | package Test1; |
147 | |
148 | use threads 'yield'; |
149 | yield; |
150 | main::ok(1); |
151 | |
152 | package main; |
153 | |
154 | |
155 | # test async |
156 | |
157 | { |
158 | my $th = async {return 1 }; |
159 | ok($th); |
160 | ok($th->join()); |
161 | } |
9c98058e |
162 | { |
4dcb9e53 |
163 | # There is a miniscule chance this test case may falsely fail |
4acc73f2 |
164 | # since it tests using rand() |
9c98058e |
165 | my %rand : shared; |
166 | rand(10); |
f4cc38af |
167 | threads->create( sub { $rand{int(rand(10000000000))}++ } ) foreach 1..25; |
9c98058e |
168 | $_->join foreach threads->list; |
4dcb9e53 |
169 | ok((keys %rand >= 23), "Check that rand() is randomized in new threads"); |
9c98058e |
170 | } |
171 | |
4e380990 |
172 | # bugid #24165 |
173 | |
821f5ffa |
174 | run_perl(prog => 'use threads 1.67;' . |
60bd5ef6 |
175 | 'sub a{threads->create(shift)} $t = a sub{};' . |
176 | '$t->tid; $t->join; $t->tid', |
69a9b4b8 |
177 | nolib => ($ENV{PERL_CORE}) ? 0 : 1, |
178 | switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ]); |
4e380990 |
179 | is($?, 0, 'coredump in global destruction'); |
9c98058e |
180 | |
09576c7d |
181 | # Attempt to free unreferenced scalar... |
182 | fresh_perl_is(<<'EOI', 'ok', { }, 'thread sub via scalar'); |
183 | use threads; |
184 | my $test = sub {}; |
185 | threads->create($test)->join(); |
186 | print 'ok'; |
187 | EOI |
188 | |
189 | # Attempt to free unreferenced scalar... |
190 | fresh_perl_is(<<'EOI', 'ok', { }, 'thread sub via $_[0]'); |
191 | use threads; |
192 | sub thr { threads->new($_[0]); } |
193 | thr(sub { })->join; |
194 | print 'ok'; |
195 | EOI |
196 | |
863e9b4a |
197 | # [perl #45053] Memory corruption from eval return in void context |
198 | fresh_perl_is(<<'EOI', 'ok', { }, 'void eval return'); |
199 | use threads; |
200 | threads->create(sub { eval '1' }); |
201 | $_->join() for threads->list; |
202 | print 'ok'; |
203 | EOI |
204 | |
9660f481 |
205 | # test CLONE_SKIP() functionality |
821f5ffa |
206 | SKIP: { |
207 | skip('CLONE_SKIP not implemented in Perl < 5.8.7', 5) if ($] < 5.008007); |
208 | |
9660f481 |
209 | my %c : shared; |
210 | my %d : shared; |
211 | |
212 | # --- |
213 | |
214 | package A; |
215 | sub CLONE_SKIP { $c{"A-$_[0]"}++; 1; } |
216 | sub DESTROY { $d{"A-". ref $_[0]}++ } |
217 | |
218 | package A1; |
219 | our @ISA = qw(A); |
220 | sub CLONE_SKIP { $c{"A1-$_[0]"}++; 1; } |
221 | sub DESTROY { $d{"A1-". ref $_[0]}++ } |
222 | |
223 | package A2; |
224 | our @ISA = qw(A1); |
225 | |
226 | # --- |
227 | |
228 | package B; |
229 | sub CLONE_SKIP { $c{"B-$_[0]"}++; 0; } |
230 | sub DESTROY { $d{"B-" . ref $_[0]}++ } |
231 | |
232 | package B1; |
233 | our @ISA = qw(B); |
234 | sub CLONE_SKIP { $c{"B1-$_[0]"}++; 1; } |
235 | sub DESTROY { $d{"B1-" . ref $_[0]}++ } |
236 | |
237 | package B2; |
238 | our @ISA = qw(B1); |
239 | |
240 | # --- |
241 | |
242 | package C; |
243 | sub CLONE_SKIP { $c{"C-$_[0]"}++; 1; } |
244 | sub DESTROY { $d{"C-" . ref $_[0]}++ } |
245 | |
246 | package C1; |
247 | our @ISA = qw(C); |
248 | sub CLONE_SKIP { $c{"C1-$_[0]"}++; 0; } |
249 | sub DESTROY { $d{"C1-" . ref $_[0]}++ } |
250 | |
251 | package C2; |
252 | our @ISA = qw(C1); |
253 | |
254 | # --- |
255 | |
256 | package D; |
257 | sub DESTROY { $d{"D-" . ref $_[0]}++ } |
258 | |
259 | package D1; |
260 | our @ISA = qw(D); |
261 | |
262 | package main; |
263 | |
264 | { |
fc04eb16 |
265 | my @objs; |
266 | for my $class (qw(A A1 A2 B B1 B2 C C1 C2 D D1)) { |
267 | push @objs, bless [], $class; |
268 | } |
269 | |
270 | sub f { |
271 | my $depth = shift; |
272 | my $cloned = ""; # XXX due to recursion, doesn't get initialized |
273 | $cloned .= "$_" =~ /ARRAY/ ? '1' : '0' for @objs; |
274 | is($cloned, ($depth ? '00010001111' : '11111111111'), |
275 | "objs clone skip at depth $depth"); |
276 | threads->create( \&f, $depth+1)->join if $depth < 2; |
277 | @objs = (); |
278 | } |
279 | f(0); |
9660f481 |
280 | } |
281 | |
282 | curr_test(curr_test()+2); |
283 | ok(eq_hash(\%c, |
fc04eb16 |
284 | { |
285 | qw( |
286 | A-A 2 |
287 | A1-A1 2 |
288 | A1-A2 2 |
289 | B-B 2 |
290 | B1-B1 2 |
291 | B1-B2 2 |
292 | C-C 2 |
293 | C1-C1 2 |
294 | C1-C2 2 |
295 | ) |
296 | }), |
297 | "counts of calls to CLONE_SKIP"); |
9660f481 |
298 | ok(eq_hash(\%d, |
fc04eb16 |
299 | { |
300 | qw( |
301 | A-A 1 |
302 | A1-A1 1 |
303 | A1-A2 1 |
304 | B-B 3 |
305 | B1-B1 1 |
306 | B1-B2 1 |
307 | C-C 1 |
308 | C1-C1 3 |
309 | C1-C2 3 |
310 | D-D 3 |
311 | D-D1 3 |
312 | ) |
313 | }), |
314 | "counts of calls to DESTROY"); |
9660f481 |
315 | } |
38875929 |
316 | |
0f1612a7 |
317 | # EOF |