6 set_up_inc( '../lib' );
9 skip_all_without_config('useithreads');
10 skip_all_if_miniperl("no dynamic loading on miniperl, no threads");
19 # test that we don't get:
20 # Attempt to free unreferenced scalar: SV 0x40173f3c
21 fresh_perl_is(<<'EOI', 'ok', { }, 'delete() under threads');
23 threads->create(sub { my %h=(1,2); delete $h{1}})->join for 1..2;
28 # test that we don't get:
29 # Attempt to free unreferenced scalar: SV 0x814e0dc.
30 fresh_perl_is(<<'EOI', 'ok', { }, 'weaken ref under threads');
32 no warnings 'experimental::builtin';
38 threads->create(sub { 1 })->join for (1..1);
43 # test that we don't get:
44 # panic: magic_killbackrefs.
46 fresh_perl_is(<<'EOI', 'ok', { }, 'weaken ref #2 under threads');
48 sub new { bless {},shift }
51 no warnings 'experimental::builtin';
53 my $object = Foo->new;
56 threads->create(sub { $ref = $object } )->join; # $ref = $object causes problems
60 #PR30333 - sort() crash with threads
61 sub mycmp { length($b) <=> length($a) }
63 sub do_sort_one_thread {
65 print "# kid $kid before sort\n";
66 my @list = ( 'x', 'yy', 'zzz', 'a', 'bb', 'ccc', 'aaaaa', 'z',
67 'hello', 's', 'thisisalongname', '1', '2', '3',
68 'abc', 'xyz', '1234567890', 'm', 'n', 'p' );
70 for my $j (1..99999) {
71 for my $k (sort mycmp @list) {}
73 print "# kid $kid after sort, sleeping 1\n";
75 print "# kid $kid exit\n";
81 for my $i (1..$nthreads) {
82 my $t = threads->create(\&do_sort_one_thread, $i);
83 print "# parent $$: continue\n";
87 print "# parent $$: waiting for join\n";
89 print "# parent $$: thread exited\n";
93 do_sort_threads(2); # crashes
96 # Change 24643 made the mistake of assuming that CvCONST can only be true on
97 # XSUBs. Somehow it can also end up on perl subs.
98 fresh_perl_is(<<'EOI', 'ok', { }, 'cloning constant subs');
101 $SIG{__WARN__} = sub{};
106 # From a test case by Tim Bunce in
107 # http://www.nntp.perl.org/group/perl.perl5.porters/63123
108 fresh_perl_is(<<'EOI', 'ok', { }, 'Ensure PL_linestr can be cloned');
110 print do 'op/threads_create.pl' || die $@;
115 foreach my $BLOCK (qw(CHECK INIT)) {
116 fresh_perl_is(<<EOI, 'ok', { }, "threads in $BLOCK block");
118 $BLOCK { threads->create(sub {})->join; }
124 fresh_perl_is(<<'EOI', 'ok', { }, 'Bug #41138');
130 threads->create(sub {})->join();
136 # [perl #45053] Memory corruption with heavy module loading in threads
138 # run-time usage of newCONSTSUB (as done by the IO boot code) wasn't
139 # thread-safe - got occasional coredumps or malloc corruption
140 watchdog(180, "process");
142 local $SIG{__WARN__} = sub {}; # Ignore any thread creation failure warnings
145 my $thr = threads->create( sub { require IO });
146 last if !defined($thr); # Probably ran out of memory
150 ok(1, '[perl #45053]');
154 is (ref $_[1], "Regexp");
158 threads->new(\&matchit, "Pie", qr/pie/i)->join();
160 # tests in threads don't get counted, so
161 curr_test(curr_test() + 2);
164 # the seen_evals field of a regexp was getting zeroed on clone, so
165 # within a thread it didn't know that a regex object contained a 'safe'
166 # code expression, so it later died with 'Eval-group not allowed' when
167 # you tried to interpolate the object
170 my $re = qr/(?{1})/; # this is literal, so safe
171 eval { "a" =~ /$re$re/ }; # interpolating safe values, so safe
172 ok($@ eq "", 'clone seen-evals');
174 threads->new(\&safe_re)->join();
176 # tests in threads don't get counted, so
177 curr_test(curr_test() + 1);
179 # This used to crash in 5.10.0 [perl #64954]
182 threads->new(sub {})->join;
183 pass("undefing a typeglob doesn't cause a crash during cloning");
187 # panic: del_backref during global destruction.
188 # when returning a non-closure sub from a thread and subsequently starting
190 fresh_perl_is(<<'EOI', 'ok', { }, 'No del_backref panic [perl #70748]');
192 sub foo { return (sub { }); }
193 my $bar = threads->create(\&foo)->join();
194 threads->create(sub { })->join();
198 # Another, more reliable test for the same del_backref bug:
200 <<' EOJ', 'ok', {}, 'No del_backref panic [perl #70748] (2)'
202 push @bar, threads->create(sub{sub{}})->join() for 1...10;
207 # Simple closure-returning test: At least this case works (though it
208 # leaks), and we don't want to break it.
209 fresh_perl_is(<<'EOJ', 'foo', {}, 'returning a closure');
211 print create threads sub {
218 # At the point of thread creation, $h{1} is on the temps stack.
219 # The weak reference $a, however, is visible from the symbol table.
220 fresh_perl_is(<<'EOI', 'ok', { }, 'Test for 34394ecd06e704e9');
222 no warnings 'experimental::builtin';
223 use builtin 'weaken';
227 delete $h{1} && threads->create(sub {}, shift)->join();
231 # This will fail in "interesting" ways if stashes in clone_params is not
232 # initialised correctly.
233 fresh_perl_like(<<'EOI', qr/\AThread 1 terminated abnormally: Not a CODE reference/, { }, 'RT #73046');
239 my %h = (1, *{$::{'foo::'}}{HASH});
240 *{$::{'foo::'}} = {};
242 threads->create({}, delete $h{1})->join();
247 fresh_perl_is(<<'EOI', 'ok', { }, '0 refcnt neither on tmps stack nor in @_');
249 no warnings 'experimental::builtin';
250 use builtin 'weaken';
254 delete $h{1} && threads->create(sub {}, shift)->join();
264 threads->create(\&stuff)->join();
266 is ($a, undef, 'RT #73086 - clone used to clone active pads');
272 curr_test(curr_test() + 1);
282 threads->create(\&more_stuff)->join();
284 is ($a, undef, 'Just special casing lexicals in ?{ ... }');
290 curr_test(curr_test() + 1);
293 # Test from Jerry Hedden, reduced by him from Object::InsideOut's tests.
294 fresh_perl_is(<<'EOI', 'ok', { }, '0 refcnt during CLONE');
302 no warnings 'experimental::builtin';
303 use builtin 'weaken';
309 # Create object with ID = 1
312 my $obj = bless(\do{ my $scalar = $id; }, $class);
314 # Save weak copy of object for reference during cloning
315 weaken($reg{$id} = $obj);
321 # Return the internal ID of the object
328 # During cloning 'look' at the object
330 foreach my $id (keys(%reg)) {
331 # This triggers SvREFCNT_inc() then SvREFCNT_dec() on the referent.
337 # Create object in 'main' thread
338 my $obj = My::Obj->new();
340 die "\$id is '$id'" unless $id == 1;
342 # Access object in thread
345 print $obj->id() == 1 ? "ok\n" : "not ok '" . $obj->id() . "'\n";
351 # make sure peephole optimiser doesn't recurse heavily.
352 # (We run this inside a thread to get a small stack)
355 # lots of constructs that have o->op_other etc
368 # this one will fail since we removed tail recursion optimisation
370 #while (1) { $x = 0 };
372 while (0) { $x = 0 };
373 for ($x=0; $y; $z=0) { $r = 0 };
378 $code = 'my ($r, $x,$y,$z,@a); return 5; ' . ($code x 1000);
379 my $res = threads->create(sub { eval $code})->join;
380 is($res, 5, "avoid peephole recursion");
384 # [perl #78494] Pipes shared between threads block when closed
386 my $perl = which_perl;
387 $perl = qq'"$perl"' if $perl =~ /\s/;
388 open(my $OUT, "|$perl") || die("ERROR: $!");
389 threads->create(sub { })->join;
390 ok(1, "Pipes shared between threads do not block when closed");
393 # [perl #105208] Typeglob clones should not be cloned again during a join
395 threads->create(sub { sub { $::hypogamma = 3 } })->join->();
396 is $::hypogamma, 3, 'globs cloned and joined are not recloned';
401 'async { delete $::{INC}; eval q"my $foo : bar" } ->join; print "ok\n";',
404 'no crash when deleting $::{INC} in thread'
407 fresh_perl_is(<<'CODE', 'ok', {}, 'no crash modifying extended array element');
410 threads->create(sub { $#a = 1; $a[1] = 2; print qq/ok\n/ })->join;
413 fresh_perl_is(<<'CODE', '3.5,3.5', {}, 'RT #36664: Strange behavior of shared array');
417 our @List : shared = (1..5);
421 printf "%s,%s", @List[(3)], $List[3];
424 fresh_perl_like(<<'CODE', qr/ok/, {}, 'RT #41121 binmode(STDOUT,":encoding(utf8) does not crash');
426 binmode(STDOUT,":encoding(utf8)");
427 threads->create(sub{});