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
139 local $SIG{__WARN__} = sub {}; # Ignore any thread creation failure warnings
142 my $thr = threads->create( sub { require IO });
143 last if !defined($thr); # Probably ran out of memory
147 ok(1, '[perl #45053]');
151 is (ref $_[1], "Regexp");
155 threads->new(\&matchit, "Pie", qr/pie/i)->join();
157 # tests in threads don't get counted, so
158 curr_test(curr_test() + 2);
161 # the seen_evals field of a regexp was getting zeroed on clone, so
162 # within a thread it didn't know that a regex object contained a 'safe'
163 # re_eval expression, so it later died with 'Eval-group not allowed' when
164 # you tried to interpolate the object
167 my $re = qr/(?{1})/; # this is literal, so safe
168 eval { "a" =~ /$re$re/ }; # interpolating safe values, so safe
169 ok($@ eq "", 'clone seen-evals');
171 threads->new(\&safe_re)->join();
173 # tests in threads don't get counted, so
174 curr_test(curr_test() + 1);
176 # This used to crash in 5.10.0 [perl #64954]
179 threads->new(sub {})->join;
180 pass("undefing a typeglob doesn't cause a crash during cloning");
184 # panic: del_backref during global destruction.
185 # when returning a non-closure sub from a thread and subsequently starting
187 fresh_perl_is(<<'EOI', 'ok', { }, 'No del_backref panic [perl #70748]');
189 sub foo { return (sub { }); }
190 my $bar = threads->create(\&foo)->join();
191 threads->create(sub { })->join();
195 # Another, more reliable test for the same del_backref bug:
197 <<' EOJ', 'ok', {}, 'No del_backref panic [perl #70748] (2)'
199 push @bar, threads->create(sub{sub{}})->join() for 1...10;
204 # Simple closure-returning test: At least this case works (though it
205 # leaks), and we don't want to break it.
206 fresh_perl_is(<<'EOJ', 'foo', {}, 'returning a closure');
208 print create threads sub {
215 # At the point of thread creation, $h{1} is on the temps stack.
216 # The weak reference $a, however, is visible from the symbol table.
217 fresh_perl_is(<<'EOI', 'ok', { }, 'Test for 34394ecd06e704e9');
220 use Scalar::Util 'weaken';
223 delete $h{1} && threads->create(sub {}, shift)->join();
227 # This will fail in "interesting" ways if stashes in clone_params is not
228 # initialised correctly.
229 fresh_perl_like(<<'EOI', qr/\AThread 1 terminated abnormally: Not a CODE reference/, { }, 'RT #73046');
235 my %h = (1, *{$::{'foo::'}}{HASH});
236 *{$::{'foo::'}} = {};
238 threads->create({}, delete $h{1})->join();
243 fresh_perl_is(<<'EOI', 'ok', { }, '0 refcnt neither on tmps stack nor in @_');
246 use Scalar::Util 'weaken';
249 delete $h{1} && threads->create(sub {}, shift)->join();
259 threads->create(\&stuff)->join();
261 is ($a, undef, 'RT #73086 - clone used to clone active pads');
267 curr_test(curr_test() + 1);
277 threads->create(\&more_stuff)->join();
279 is ($a, undef, 'Just special casing lexicals in ?{ ... }');
285 curr_test(curr_test() + 1);
288 # Test from Jerry Hedden, reduced by him from Object::InsideOut's tests.
289 fresh_perl_is(<<'EOI', 'ok', { }, '0 refcnt during CLONE');
297 use Scalar::Util 'weaken';
303 # Create object with ID = 1
306 my $obj = bless(\do{ my $scalar = $id; }, $class);
308 # Save weak copy of object for reference during cloning
309 weaken($reg{$id} = $obj);
315 # Return the internal ID of the object
322 # During cloning 'look' at the object
324 foreach my $id (keys(%reg)) {
325 # This triggers SvREFCNT_inc() then SvREFCNT_dec() on the referant.
331 # Create object in 'main' thread
332 my $obj = My::Obj->new();
334 die "\$id is '$id'" unless $id == 1;
336 # Access object in thread
339 print $obj->id() == 1 ? "ok\n" : "not ok '" . $obj->id() . "'\n";
345 # make sure peephole optimiser doesn't recurse heavily.
346 # (We run this inside a thread to get a small stack)
349 # lots of constructs that have o->op_other etc
362 # this one will fail since we removed tail recursion optimisation
364 #while (1) { $x = 0 };
366 while (0) { $x = 0 };
367 for ($x=0; $y; $z=0) { $r = 0 };
372 $code = 'my ($r, $x,$y,$z,@a); return 5; ' . ($code x 1000);
373 my $res = threads->create(sub { eval $code})->join;
374 is($res, 5, "avoid peephole recursion");
378 # [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");