5 require($ENV{PERL_CORE} ? '../../t/test.pl' : './t/test.pl');
8 if (! $Config{'useithreads'}) {
9 skip_all(q/Perl not compiled with 'useithreads'/);
13 use ExtUtils::testlib;
18 if (! eval 'use threads::shared; 1') {
19 skip_all('threads::shared not available');
23 print("1..34\n"); ### Number of tests that will be run ###
26 print("ok 1 - Loaded\n");
28 ### Start of Testing ###
35 my $t = threads->create(\&content, "ok 2\n", "ok 3\n", 1..1000);
43 $t = threads->create(sub { lock($lock); print "ok 5\n"});
54 $ret = threads->create(\&dorecurse, @_);
59 my $t = threads->create(\&dorecurse, map { "ok $_\n" } 6..10);
64 # test that sleep lets other thread run
65 my $t = threads->create(\&dorecurse, "ok 11\n");
66 threads->yield; # help out non-preemptive thread implementations
79 $ret = threads->create(\&islocked, shift);
83 my $t = threads->create(\&islocked, "ok 13\n", "ok 14\n");
91 my $same = sprintf( "%0.f", $testno);
92 return $testno eq $same;
96 my ($string, $string_end) = @_;
98 # Do the match, saving the output in appropriate variables
99 $string =~ /(.*)(is)(.*)/;
100 # Yield control, allowing the other thread to fill in the match variables
102 # Examine the match variable contents; on broken perls this fails
103 return $3 eq $string_end;
110 my $thr1 = threads->create(\&testsprintf, 15);
111 my $thr2 = threads->create(\&testsprintf, 16);
113 my $short = "This is a long string that goes on and on.";
114 my $shorte = " a long string that goes on and on.";
115 my $long = "This is short.";
116 my $longe = " short.";
117 my $foo = "This is bar bar bar.";
118 my $fooe = " bar bar bar.";
119 my $thr3 = new threads \&threaded, $short, $shorte;
120 my $thr4 = new threads \&threaded, $long, $longe;
121 my $thr5 = new threads \&testsprintf, 19;
122 my $thr6 = new threads \&testsprintf, 20;
123 my $thr7 = new threads \&threaded, $foo, $fooe;
134 # test that 'yield' is importable
148 my $th = async {return 1 };
153 # There is a miniscule chance this test case may falsely fail
154 # since it tests using rand()
157 threads->create( sub { $rand{int(rand(10000000000))}++ } ) foreach 1..25;
158 $_->join foreach threads->list;
159 ok((keys %rand >= 23), "Check that rand() is randomized in new threads");
164 run_perl(prog => 'use threads 1.75;' .
165 'sub a{threads->create(shift)} $t = a sub{};' .
166 '$t->tid; $t->join; $t->tid',
167 nolib => ($ENV{PERL_CORE}) ? 0 : 1,
168 switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ]);
169 is($?, 0, 'coredump in global destruction');
171 # Attempt to free unreferenced scalar...
172 fresh_perl_is(<<'EOI', 'ok', { }, 'thread sub via scalar');
175 threads->create($test)->join();
179 # Attempt to free unreferenced scalar...
180 fresh_perl_is(<<'EOI', 'ok', { }, 'thread sub via $_[0]');
182 sub thr { threads->new($_[0]); }
187 # [perl #45053] Memory corruption from eval return in void context
188 fresh_perl_is(<<'EOI', 'ok', { }, 'void eval return');
190 threads->create(sub { eval '1' });
191 $_->join() for threads->list;
195 # test CLONE_SKIP() functionality
197 skip('CLONE_SKIP not implemented in Perl < 5.8.7', 5) if ($] < 5.008007);
205 sub CLONE_SKIP { $c{"A-$_[0]"}++; 1; }
206 sub DESTROY { $d{"A-". ref $_[0]}++ }
210 sub CLONE_SKIP { $c{"A1-$_[0]"}++; 1; }
211 sub DESTROY { $d{"A1-". ref $_[0]}++ }
219 sub CLONE_SKIP { $c{"B-$_[0]"}++; 0; }
220 sub DESTROY { $d{"B-" . ref $_[0]}++ }
224 sub CLONE_SKIP { $c{"B1-$_[0]"}++; 1; }
225 sub DESTROY { $d{"B1-" . ref $_[0]}++ }
233 sub CLONE_SKIP { $c{"C-$_[0]"}++; 1; }
234 sub DESTROY { $d{"C-" . ref $_[0]}++ }
238 sub CLONE_SKIP { $c{"C1-$_[0]"}++; 0; }
239 sub DESTROY { $d{"C1-" . ref $_[0]}++ }
247 sub DESTROY { $d{"D-" . ref $_[0]}++ }
256 for my $class (qw(A A1 A2 B B1 B2 C C1 C2 D D1)) {
257 push @objs, bless [], $class;
262 my $cloned = ""; # XXX due to recursion, doesn't get initialized
263 $cloned .= "$_" =~ /ARRAY/ ? '1' : '0' for @objs;
264 is($cloned, ($depth ? '00010001111' : '11111111111'),
265 "objs clone skip at depth $depth");
266 threads->create( \&f, $depth+1)->join if $depth < 2;
272 curr_test(curr_test()+2);
287 "counts of calls to CLONE_SKIP");
304 "counts of calls to DESTROY");