5 if ($ENV{'PERL_CORE'}){
7 unshift @INC, '../lib';
10 require($ENV{PERL_CORE} ? "./test.pl" : "./t/test.pl");
13 if (! $Config{'useithreads'}) {
14 skip_all(q/Perl not compiled with 'useithreads'/);
18 use ExtUtils::testlib;
23 if (! eval 'use threads::shared; 1') {
24 skip_all('threads::shared not available');
28 print("1..34\n"); ### Number of tests that will be run ###
31 print("ok 1 - Loaded\n");
33 ### Start of Testing ###
40 my $t = threads->create(\&content, "ok 2\n", "ok 3\n", 1..1000);
48 $t = threads->create(sub { lock($lock); print "ok 5\n"});
59 $ret = threads->create(\&dorecurse, @_);
64 my $t = threads->create(\&dorecurse, map { "ok $_\n" } 6..10);
69 # test that sleep lets other thread run
70 my $t = threads->create(\&dorecurse, "ok 11\n");
71 threads->yield; # help out non-preemptive thread implementations
84 $ret = threads->create(\&islocked, shift);
88 my $t = threads->create(\&islocked, "ok 13\n", "ok 14\n");
96 my $same = sprintf( "%0.f", $testno);
97 return $testno eq $same;
101 my ($string, $string_end) = @_;
103 # Do the match, saving the output in appropriate variables
104 $string =~ /(.*)(is)(.*)/;
105 # Yield control, allowing the other thread to fill in the match variables
107 # Examine the match variable contents; on broken perls this fails
108 return $3 eq $string_end;
115 my $thr1 = threads->create(\&testsprintf, 15);
116 my $thr2 = threads->create(\&testsprintf, 16);
118 my $short = "This is a long string that goes on and on.";
119 my $shorte = " a long string that goes on and on.";
120 my $long = "This is short.";
121 my $longe = " short.";
122 my $foo = "This is bar bar bar.";
123 my $fooe = " bar bar bar.";
124 my $thr3 = new threads \&threaded, $short, $shorte;
125 my $thr4 = new threads \&threaded, $long, $longe;
126 my $thr5 = new threads \&testsprintf, 19;
127 my $thr6 = new threads \&testsprintf, 20;
128 my $thr7 = new threads \&threaded, $foo, $fooe;
139 # test that 'yield' is importable
153 my $th = async {return 1 };
158 # There is a miniscule chance this test case may falsely fail
159 # since it tests using rand()
162 threads->create( sub { $rand{int(rand(10000000000))}++ } ) foreach 1..25;
163 $_->join foreach threads->list;
164 ok((keys %rand >= 23), "Check that rand() is randomized in new threads");
169 run_perl(prog => 'use threads 1.72;' .
170 'sub a{threads->create(shift)} $t = a sub{};' .
171 '$t->tid; $t->join; $t->tid',
172 nolib => ($ENV{PERL_CORE}) ? 0 : 1,
173 switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ]);
174 is($?, 0, 'coredump in global destruction');
176 # Attempt to free unreferenced scalar...
177 fresh_perl_is(<<'EOI', 'ok', { }, 'thread sub via scalar');
180 threads->create($test)->join();
184 # Attempt to free unreferenced scalar...
185 fresh_perl_is(<<'EOI', 'ok', { }, 'thread sub via $_[0]');
187 sub thr { threads->new($_[0]); }
192 # [perl #45053] Memory corruption from eval return in void context
193 fresh_perl_is(<<'EOI', 'ok', { }, 'void eval return');
195 threads->create(sub { eval '1' });
196 $_->join() for threads->list;
200 # test CLONE_SKIP() functionality
202 skip('CLONE_SKIP not implemented in Perl < 5.8.7', 5) if ($] < 5.008007);
210 sub CLONE_SKIP { $c{"A-$_[0]"}++; 1; }
211 sub DESTROY { $d{"A-". ref $_[0]}++ }
215 sub CLONE_SKIP { $c{"A1-$_[0]"}++; 1; }
216 sub DESTROY { $d{"A1-". ref $_[0]}++ }
224 sub CLONE_SKIP { $c{"B-$_[0]"}++; 0; }
225 sub DESTROY { $d{"B-" . ref $_[0]}++ }
229 sub CLONE_SKIP { $c{"B1-$_[0]"}++; 1; }
230 sub DESTROY { $d{"B1-" . ref $_[0]}++ }
238 sub CLONE_SKIP { $c{"C-$_[0]"}++; 1; }
239 sub DESTROY { $d{"C-" . ref $_[0]}++ }
243 sub CLONE_SKIP { $c{"C1-$_[0]"}++; 0; }
244 sub DESTROY { $d{"C1-" . ref $_[0]}++ }
252 sub DESTROY { $d{"D-" . ref $_[0]}++ }
261 for my $class (qw(A A1 A2 B B1 B2 C C1 C2 D D1)) {
262 push @objs, bless [], $class;
267 my $cloned = ""; # XXX due to recursion, doesn't get initialized
268 $cloned .= "$_" =~ /ARRAY/ ? '1' : '0' for @objs;
269 is($cloned, ($depth ? '00010001111' : '11111111111'),
270 "objs clone skip at depth $depth");
271 threads->create( \&f, $depth+1)->join if $depth < 2;
277 curr_test(curr_test()+2);
292 "counts of calls to CLONE_SKIP");
309 "counts of calls to DESTROY");