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 import threads::shared;
27 if ($@ || ! $threads::shared::threads_shared) {
28 print("1..0 # Skip: threads::shared not available\n");
33 print("1..31\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.63;' .
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 # test CLONE_SKIP() functionality
182 if ($] >= 5.008007) {
189 sub CLONE_SKIP { $c{"A-$_[0]"}++; 1; }
190 sub DESTROY { $d{"A-". ref $_[0]}++ }
194 sub CLONE_SKIP { $c{"A1-$_[0]"}++; 1; }
195 sub DESTROY { $d{"A1-". ref $_[0]}++ }
203 sub CLONE_SKIP { $c{"B-$_[0]"}++; 0; }
204 sub DESTROY { $d{"B-" . ref $_[0]}++ }
208 sub CLONE_SKIP { $c{"B1-$_[0]"}++; 1; }
209 sub DESTROY { $d{"B1-" . ref $_[0]}++ }
217 sub CLONE_SKIP { $c{"C-$_[0]"}++; 1; }
218 sub DESTROY { $d{"C-" . ref $_[0]}++ }
222 sub CLONE_SKIP { $c{"C1-$_[0]"}++; 0; }
223 sub DESTROY { $d{"C1-" . ref $_[0]}++ }
231 sub DESTROY { $d{"D-" . ref $_[0]}++ }
240 for my $class (qw(A A1 A2 B B1 B2 C C1 C2 D D1)) {
241 push @objs, bless [], $class;
246 my $cloned = ""; # XXX due to recursion, doesn't get initialized
247 $cloned .= "$_" =~ /ARRAY/ ? '1' : '0' for @objs;
248 is($cloned, ($depth ? '00010001111' : '11111111111'),
249 "objs clone skip at depth $depth");
250 threads->create( \&f, $depth+1)->join if $depth < 2;
256 curr_test(curr_test()+2);
271 "counts of calls to CLONE_SKIP");
288 "counts of calls to DESTROY");
291 print("ok 27 # Skip objs clone skip at depth 0\n");
292 print("ok 28 # Skip objs clone skip at depth 1\n");
293 print("ok 29 # Skip objs clone skip at depth 2\n");
294 print("ok 30 # Skip counts of calls to CLONE_SKIP\n");
295 print("ok 31 # Skip counts of calls to DESTROY\n");