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;
22 print("1..31\n"); ### Number of tests that will be run ###
27 print("ok 1 - Loaded\n");
29 ### Start of Testing ###
36 my $t = threads->create(\&content, "ok 2\n", "ok 3\n", 1..1000);
44 $t = threads->create(sub { lock($lock); print "ok 5\n"});
55 $ret = threads->create(\&dorecurse, @_);
60 my $t = threads->create(\&dorecurse, map { "ok $_\n" } 6..10);
65 # test that sleep lets other thread run
66 my $t = threads->create(\&dorecurse, "ok 11\n");
67 threads->yield; # help out non-preemptive thread implementations
80 $ret = threads->create(\&islocked, shift);
84 my $t = threads->create(\&islocked, "ok 13\n", "ok 14\n");
92 my $same = sprintf( "%0.f", $testno);
93 return $testno eq $same;
97 my ($string, $string_end) = @_;
99 # Do the match, saving the output in appropriate variables
100 $string =~ /(.*)(is)(.*)/;
101 # Yield control, allowing the other thread to fill in the match variables
103 # Examine the match variable contents; on broken perls this fails
104 return $3 eq $string_end;
111 my $thr1 = threads->create(\&testsprintf, 15);
112 my $thr2 = threads->create(\&testsprintf, 16);
114 my $short = "This is a long string that goes on and on.";
115 my $shorte = " a long string that goes on and on.";
116 my $long = "This is short.";
117 my $longe = " short.";
118 my $foo = "This is bar bar bar.";
119 my $fooe = " bar bar bar.";
120 my $thr3 = new threads \&threaded, $short, $shorte;
121 my $thr4 = new threads \&threaded, $long, $longe;
122 my $thr5 = new threads \&testsprintf, 19;
123 my $thr6 = new threads \&testsprintf, 20;
124 my $thr7 = new threads \&threaded, $foo, $fooe;
135 # test that 'yield' is importable
149 my $th = async {return 1 };
154 # there is a little chance this test case will falsly fail
155 # since it tests rand
158 threads->create( sub { $rand{int(rand(10000000000))}++ } ) foreach 1..25;
159 $_->join foreach threads->list;
160 # use Data::Dumper qw(Dumper);
161 # print Dumper(\%rand);
163 ok((keys %rand == 25), "Check that rand works after a new thread");
169 'use threads; sub a{threads->create(shift)} $t = a sub{}; $t->tid; $t->join; $t->tid');
170 is($?, 0, 'coredump in global destruction');
172 # test CLONE_SKIP() functionality
173 if ($] >= 5.008007) {
180 sub CLONE_SKIP { $c{"A-$_[0]"}++; 1; }
181 sub DESTROY { $d{"A-". ref $_[0]}++ }
185 sub CLONE_SKIP { $c{"A1-$_[0]"}++; 1; }
186 sub DESTROY { $d{"A1-". ref $_[0]}++ }
194 sub CLONE_SKIP { $c{"B-$_[0]"}++; 0; }
195 sub DESTROY { $d{"B-" . ref $_[0]}++ }
199 sub CLONE_SKIP { $c{"B1-$_[0]"}++; 1; }
200 sub DESTROY { $d{"B1-" . ref $_[0]}++ }
208 sub CLONE_SKIP { $c{"C-$_[0]"}++; 1; }
209 sub DESTROY { $d{"C-" . ref $_[0]}++ }
213 sub CLONE_SKIP { $c{"C1-$_[0]"}++; 0; }
214 sub DESTROY { $d{"C1-" . ref $_[0]}++ }
222 sub DESTROY { $d{"D-" . ref $_[0]}++ }
231 for my $class (qw(A A1 A2 B B1 B2 C C1 C2 D D1)) {
232 push @objs, bless [], $class;
237 my $cloned = ""; # XXX due to recursion, doesn't get initialized
238 $cloned .= "$_" =~ /ARRAY/ ? '1' : '0' for @objs;
239 is($cloned, ($depth ? '00010001111' : '11111111111'),
240 "objs clone skip at depth $depth");
241 threads->create( \&f, $depth+1)->join if $depth < 2;
247 curr_test(curr_test()+2);
262 "counts of calls to CLONE_SKIP");
279 "counts of calls to DESTROY");
282 print("ok 27 # Skip objs clone skip at depth 0\n");
283 print("ok 28 # Skip objs clone skip at depth 1\n");
284 print("ok 29 # Skip objs clone skip at depth 2\n");
285 print("ok 30 # Skip counts of calls to CLONE_SKIP\n");
286 print("ok 31 # Skip counts of calls to DESTROY\n");