5 if ($ENV{'PERL_CORE'}){
7 unshift @INC, '../lib';
10 if (! $Config{'useithreads'}) {
11 print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
15 require($ENV{PERL_CORE} ? "./test.pl" : "./t/test.pl");
18 use ExtUtils::testlib;
24 require threads::shared;
25 threads::shared->import();
27 if ($@ || ! $threads::shared::threads_shared) {
28 print("1..0 # Skip: threads::shared not available\n");
33 print("1..34\n"); ### Number of tests that will be run ###
36 print("ok 1 - Loaded\n");
38 ### Start of Testing ###
45 my $t = threads->create(\&content, "ok 2\n", "ok 3\n", 1..1000);
53 $t = threads->create(sub { lock($lock); print "ok 5\n"});
64 $ret = threads->create(\&dorecurse, @_);
69 my $t = threads->create(\&dorecurse, map { "ok $_\n" } 6..10);
74 # test that sleep lets other thread run
75 my $t = threads->create(\&dorecurse, "ok 11\n");
76 threads->yield; # help out non-preemptive thread implementations
89 $ret = threads->create(\&islocked, shift);
93 my $t = threads->create(\&islocked, "ok 13\n", "ok 14\n");
101 my $same = sprintf( "%0.f", $testno);
102 return $testno eq $same;
106 my ($string, $string_end) = @_;
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
112 # Examine the match variable contents; on broken perls this fails
113 return $3 eq $string_end;
120 my $thr1 = threads->create(\&testsprintf, 15);
121 my $thr2 = threads->create(\&testsprintf, 16);
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.";
129 my $thr3 = new threads \&threaded, $short, $shorte;
130 my $thr4 = new threads \&threaded, $long, $longe;
131 my $thr5 = new threads \&testsprintf, 19;
132 my $thr6 = new threads \&testsprintf, 20;
133 my $thr7 = new threads \&threaded, $foo, $fooe;
144 # test that 'yield' is importable
158 my $th = async {return 1 };
163 # There is a miniscule chance this test case may falsely fail
164 # since it tests using rand()
167 threads->create( sub { $rand{int(rand(10000000000))}++ } ) foreach 1..25;
168 $_->join foreach threads->list;
169 ok((keys %rand >= 23), "Check that rand() is randomized in new threads");
174 run_perl(prog => 'use threads 1.65;' .
175 'sub a{threads->create(shift)} $t = a sub{};' .
176 '$t->tid; $t->join; $t->tid',
177 nolib => ($ENV{PERL_CORE}) ? 0 : 1,
178 switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ]);
179 is($?, 0, 'coredump in global destruction');
181 # Attempt to free unreferenced scalar...
182 fresh_perl_is(<<'EOI', 'ok', { }, 'thread sub via scalar');
185 threads->create($test)->join();
189 # Attempt to free unreferenced scalar...
190 fresh_perl_is(<<'EOI', 'ok', { }, 'thread sub via $_[0]');
192 sub thr { threads->new($_[0]); }
197 # [perl #45053] Memory corruption from eval return in void context
198 fresh_perl_is(<<'EOI', 'ok', { }, 'void eval return');
200 threads->create(sub { eval '1' });
201 $_->join() for threads->list;
205 # test CLONE_SKIP() functionality
206 if ($] >= 5.008007) {
213 sub CLONE_SKIP { $c{"A-$_[0]"}++; 1; }
214 sub DESTROY { $d{"A-". ref $_[0]}++ }
218 sub CLONE_SKIP { $c{"A1-$_[0]"}++; 1; }
219 sub DESTROY { $d{"A1-". ref $_[0]}++ }
227 sub CLONE_SKIP { $c{"B-$_[0]"}++; 0; }
228 sub DESTROY { $d{"B-" . ref $_[0]}++ }
232 sub CLONE_SKIP { $c{"B1-$_[0]"}++; 1; }
233 sub DESTROY { $d{"B1-" . ref $_[0]}++ }
241 sub CLONE_SKIP { $c{"C-$_[0]"}++; 1; }
242 sub DESTROY { $d{"C-" . ref $_[0]}++ }
246 sub CLONE_SKIP { $c{"C1-$_[0]"}++; 0; }
247 sub DESTROY { $d{"C1-" . ref $_[0]}++ }
255 sub DESTROY { $d{"D-" . ref $_[0]}++ }
264 for my $class (qw(A A1 A2 B B1 B2 C C1 C2 D D1)) {
265 push @objs, bless [], $class;
270 my $cloned = ""; # XXX due to recursion, doesn't get initialized
271 $cloned .= "$_" =~ /ARRAY/ ? '1' : '0' for @objs;
272 is($cloned, ($depth ? '00010001111' : '11111111111'),
273 "objs clone skip at depth $depth");
274 threads->create( \&f, $depth+1)->join if $depth < 2;
280 curr_test(curr_test()+2);
295 "counts of calls to CLONE_SKIP");
312 "counts of calls to DESTROY");
315 print("ok 29 # Skip objs clone skip at depth 0\n");
316 print("ok 30 # Skip objs clone skip at depth 1\n");
317 print("ok 31 # Skip objs clone skip at depth 2\n");
318 print("ok 32 # Skip counts of calls to CLONE_SKIP\n");
319 print("ok 33 # Skip counts of calls to DESTROY\n");