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..33\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 # test CLONE_SKIP() functionality
198 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");
307 print("ok 29 # Skip objs clone skip at depth 0\n");
308 print("ok 30 # Skip objs clone skip at depth 1\n");
309 print("ok 31 # Skip objs clone skip at depth 2\n");
310 print("ok 32 # Skip counts of calls to CLONE_SKIP\n");
311 print("ok 33 # Skip counts of calls to DESTROY\n");