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');
36 Scalar::Util::weaken($copy);
37 threads->create(sub { 1 })->join for (1..1);
42 # test that we don't get:
43 # panic: magic_killbackrefs.
45 fresh_perl_is(<<'EOI', 'ok', { }, 'weaken ref #2 under threads');
47 sub new { bless {},shift }
50 use Scalar::Util qw(weaken);
51 my $object = Foo->new;
54 threads->create(sub { $ref = $object } )->join; # $ref = $object causes problems
58 #PR30333 - sort() crash with threads
59 sub mycmp { length($b) <=> length($a) }
61 sub do_sort_one_thread {
63 print "# kid $kid before sort\n";
64 my @list = ( 'x', 'yy', 'zzz', 'a', 'bb', 'ccc', 'aaaaa', 'z',
65 'hello', 's', 'thisisalongname', '1', '2', '3',
66 'abc', 'xyz', '1234567890', 'm', 'n', 'p' );
68 for my $j (1..99999) {
69 for my $k (sort mycmp @list) {}
71 print "# kid $kid after sort, sleeping 1\n";
73 print "# kid $kid exit\n";
79 for my $i (1..$nthreads) {
80 my $t = threads->create(\&do_sort_one_thread, $i);
81 print "# parent $$: continue\n";
85 print "# parent $$: waiting for join\n";
87 print "# parent $$: thread exited\n";
91 do_sort_threads(2); # crashes
94 # Change 24643 made the mistake of assuming that CvCONST can only be true on
95 # XSUBs. Somehow it can also end up on perl subs.
96 fresh_perl_is(<<'EOI', 'ok', { }, 'cloning constant subs');
99 $SIG{__WARN__} = sub{};
104 # From a test case by Tim Bunce in
105 # http://www.nntp.perl.org/group/perl.perl5.porters/63123
106 fresh_perl_is(<<'EOI', 'ok', { }, 'Ensure PL_linestr can be cloned');
108 print do 'op/threads_create.pl' || die $@;
113 foreach my $BLOCK (qw(CHECK INIT)) {
114 fresh_perl_is(<<EOI, 'ok', { }, "threads in $BLOCK block");
116 $BLOCK { threads->create(sub {})->join; }
122 fresh_perl_is(<<'EOI', 'ok', { }, 'Bug #41138');
128 threads->create(sub {})->join();
134 # [perl #45053] Memory corruption with heavy module loading in threads
136 # run-time usage of newCONSTSUB (as done by the IO boot code) wasn't
137 # thread-safe - got occasional coredumps or malloc corruption
138 watchdog(180, "process");
140 local $SIG{__WARN__} = sub {}; # Ignore any thread creation failure warnings
143 my $thr = threads->create( sub { require IO });
144 last if !defined($thr); # Probably ran out of memory
148 ok(1, '[perl #45053]');
152 is (ref $_[1], "Regexp");
156 threads->new(\&matchit, "Pie", qr/pie/i)->join();
158 # tests in threads don't get counted, so
159 curr_test(curr_test() + 2);
162 # the seen_evals field of a regexp was getting zeroed on clone, so
163 # within a thread it didn't know that a regex object contained a 'safe'
164 # code expression, so it later died with 'Eval-group not allowed' when
165 # you tried to interpolate the object
168 my $re = qr/(?{1})/; # this is literal, so safe
169 eval { "a" =~ /$re$re/ }; # interpolating safe values, so safe
170 ok($@ eq "", 'clone seen-evals');
172 threads->new(\&safe_re)->join();
174 # tests in threads don't get counted, so
175 curr_test(curr_test() + 1);
177 # This used to crash in 5.10.0 [perl #64954]
180 threads->new(sub {})->join;
181 pass("undefing a typeglob doesn't cause a crash during cloning");
185 # panic: del_backref during global destruction.
186 # when returning a non-closure sub from a thread and subsequently starting
188 fresh_perl_is(<<'EOI', 'ok', { }, 'No del_backref panic [perl #70748]');
190 sub foo { return (sub { }); }
191 my $bar = threads->create(\&foo)->join();
192 threads->create(sub { })->join();
196 # Another, more reliable test for the same del_backref bug:
198 <<' EOJ', 'ok', {}, 'No del_backref panic [perl #70748] (2)'
200 push @bar, threads->create(sub{sub{}})->join() for 1...10;
205 # Simple closure-returning test: At least this case works (though it
206 # leaks), and we don't want to break it.
207 fresh_perl_is(<<'EOJ', 'foo', {}, 'returning a closure');
209 print create threads sub {
216 # At the point of thread creation, $h{1} is on the temps stack.
217 # The weak reference $a, however, is visible from the symbol table.
218 fresh_perl_is(<<'EOI', 'ok', { }, 'Test for 34394ecd06e704e9');
221 use Scalar::Util 'weaken';
224 delete $h{1} && threads->create(sub {}, shift)->join();
228 # This will fail in "interesting" ways if stashes in clone_params is not
229 # initialised correctly.
230 fresh_perl_like(<<'EOI', qr/\AThread 1 terminated abnormally: Not a CODE reference/, { }, 'RT #73046');
236 my %h = (1, *{$::{'foo::'}}{HASH});
237 *{$::{'foo::'}} = {};
239 threads->create({}, delete $h{1})->join();
244 fresh_perl_is(<<'EOI', 'ok', { }, '0 refcnt neither on tmps stack nor in @_');
247 use Scalar::Util 'weaken';
250 delete $h{1} && threads->create(sub {}, shift)->join();
260 threads->create(\&stuff)->join();
262 is ($a, undef, 'RT #73086 - clone used to clone active pads');
268 curr_test(curr_test() + 1);
278 threads->create(\&more_stuff)->join();
280 is ($a, undef, 'Just special casing lexicals in ?{ ... }');
286 curr_test(curr_test() + 1);
289 # Test from Jerry Hedden, reduced by him from Object::InsideOut's tests.
290 fresh_perl_is(<<'EOI', 'ok', { }, '0 refcnt during CLONE');
298 use Scalar::Util 'weaken';
304 # Create object with ID = 1
307 my $obj = bless(\do{ my $scalar = $id; }, $class);
309 # Save weak copy of object for reference during cloning
310 weaken($reg{$id} = $obj);
316 # Return the internal ID of the object
323 # During cloning 'look' at the object
325 foreach my $id (keys(%reg)) {
326 # This triggers SvREFCNT_inc() then SvREFCNT_dec() on the referant.
332 # Create object in 'main' thread
333 my $obj = My::Obj->new();
335 die "\$id is '$id'" unless $id == 1;
337 # Access object in thread
340 print $obj->id() == 1 ? "ok\n" : "not ok '" . $obj->id() . "'\n";
346 # make sure peephole optimiser doesn't recurse heavily.
347 # (We run this inside a thread to get a small stack)
350 # lots of constructs that have o->op_other etc
363 # this one will fail since we removed tail recursion optimisation
365 #while (1) { $x = 0 };
367 while (0) { $x = 0 };
368 for ($x=0; $y; $z=0) { $r = 0 };
373 $code = 'my ($r, $x,$y,$z,@a); return 5; ' . ($code x 1000);
374 my $res = threads->create(sub { eval $code})->join;
375 is($res, 5, "avoid peephole recursion");
379 # [perl #78494] Pipes shared between threads block when closed
381 my $perl = which_perl;
382 $perl = qq'"$perl"' if $perl =~ /\s/;
383 open(my $OUT, "|$perl") || die("ERROR: $!");
384 threads->create(sub { })->join;
385 ok(1, "Pipes shared between threads do not block when closed");
388 # [perl #105208] Typeglob clones should not be cloned again during a join
390 threads->create(sub { sub { $::hypogamma = 3 } })->join->();
391 is $::hypogamma, 3, 'globs cloned and joined are not recloned';
396 'async { delete $::{INC}; eval q"my $foo : bar" } ->join; print "ok\n";',
399 'no crash when deleting $::{INC} in thread'