5 if ($ENV{'PERL_CORE'}){
7 unshift @INC, '../lib';
10 unless ($Config{'useithreads'}) {
11 print "1..0 # Skip: no useithreads\n";
15 require($ENV{PERL_CORE} ? "./test.pl" : "./t/test.pl");
18 use ExtUtils::testlib;
20 BEGIN { $| = 1; print "1..31\n" };
31 my $t = threads->create(\&content, "ok 2\n", "ok 3\n", 1..1000);
39 $t = threads->create(sub { lock($lock); print "ok 5\n"});
50 $ret = threads->create(\&dorecurse, @_);
55 my $t = threads->create(\&dorecurse, map { "ok $_\n" } 6..10);
60 # test that sleep lets other thread run
61 my $t = threads->create(\&dorecurse, "ok 11\n");
62 threads->yield; # help out non-preemptive thread implementations
75 $ret = threads->create(\&islocked, shift);
79 my $t = threads->create(\&islocked, "ok 13\n", "ok 14\n");
87 my $same = sprintf( "%0.f", $testno);
88 return $testno eq $same;
92 my ($string, $string_end) = @_;
94 # Do the match, saving the output in appropriate variables
95 $string =~ /(.*)(is)(.*)/;
96 # Yield control, allowing the other thread to fill in the match variables
98 # Examine the match variable contents; on broken perls this fails
99 return $3 eq $string_end;
106 my $thr1 = threads->create(\&testsprintf, 15);
107 my $thr2 = threads->create(\&testsprintf, 16);
109 my $short = "This is a long string that goes on and on.";
110 my $shorte = " a long string that goes on and on.";
111 my $long = "This is short.";
112 my $longe = " short.";
113 my $foo = "This is bar bar bar.";
114 my $fooe = " bar bar bar.";
115 my $thr3 = new threads \&threaded, $short, $shorte;
116 my $thr4 = new threads \&threaded, $long, $longe;
117 my $thr5 = new threads \&testsprintf, 19;
118 my $thr6 = new threads \&testsprintf, 20;
119 my $thr7 = new threads \&threaded, $foo, $fooe;
130 # test that 'yield' is importable
144 my $th = async {return 1 };
149 # there is a little chance this test case will falsly fail
150 # since it tests rand
153 threads->create( sub { $rand{int(rand(10000000000))}++ } ) foreach 1..25;
154 $_->join foreach threads->list;
155 # use Data::Dumper qw(Dumper);
156 # print Dumper(\%rand);
158 ok((keys %rand == 25), "Check that rand works after a new thread");
164 'use threads; sub a{threads->create(shift)} $t = a sub{}; $t->tid; $t->join; $t->tid');
165 is($?, 0, 'coredump in global destruction');
167 # test CLONE_SKIP() functionality
168 if ($] >= 5.008007) {
175 sub CLONE_SKIP { $c{"A-$_[0]"}++; 1; }
176 sub DESTROY { $d{"A-". ref $_[0]}++ }
180 sub CLONE_SKIP { $c{"A1-$_[0]"}++; 1; }
181 sub DESTROY { $d{"A1-". ref $_[0]}++ }
189 sub CLONE_SKIP { $c{"B-$_[0]"}++; 0; }
190 sub DESTROY { $d{"B-" . ref $_[0]}++ }
194 sub CLONE_SKIP { $c{"B1-$_[0]"}++; 1; }
195 sub DESTROY { $d{"B1-" . ref $_[0]}++ }
203 sub CLONE_SKIP { $c{"C-$_[0]"}++; 1; }
204 sub DESTROY { $d{"C-" . ref $_[0]}++ }
208 sub CLONE_SKIP { $c{"C1-$_[0]"}++; 0; }
209 sub DESTROY { $d{"C1-" . ref $_[0]}++ }
217 sub DESTROY { $d{"D-" . ref $_[0]}++ }
226 for my $class (qw(A A1 A2 B B1 B2 C C1 C2 D D1)) {
227 push @objs, bless [], $class;
232 my $cloned = ""; # XXX due to recursion, doesn't get initialized
233 $cloned .= "$_" =~ /ARRAY/ ? '1' : '0' for @objs;
234 is($cloned, ($depth ? '00010001111' : '11111111111'),
235 "objs clone skip at depth $depth");
236 threads->create( \&f, $depth+1)->join if $depth < 2;
242 curr_test(curr_test()+2);
257 "counts of calls to CLONE_SKIP");
274 "counts of calls to DESTROY");
277 print("ok 27 # Skip objs clone skip at depth 0\n");
278 print("ok 28 # Skip objs clone skip at depth 1\n");
279 print("ok 29 # Skip objs clone skip at depth 2\n");
280 print("ok 30 # Skip counts of calls to CLONE_SKIP\n");
281 print("ok 31 # Skip counts of calls to DESTROY\n");