Pulling ancient RCS comments
[p5sagit/p5-mst-13.2.git] / t / op / threads.t
CommitLineData
f935b2f6 1#!./perl
2BEGIN {
3 chdir 't' if -d 't';
996dc718 4 @INC = '../lib';
f935b2f6 5 require './test.pl'; # for which_perl() etc
6}
7
8use strict;
9use Config;
10
11BEGIN {
12 if (!$Config{useithreads}) {
13 print "1..0 # Skip: no ithreads\n";
14 exit 0;
15 }
6765206c 16 if ($ENV{PERL_CORE_MINITEST}) {
17 print "1..0 # Skip: no dynamic loading on miniperl, no threads\n";
18 exit 0;
f935b2f6 19 }
9850bf21 20 plan(4);
f935b2f6 21}
6765206c 22use threads;
f935b2f6 23
24# test that we don't get:
25# Attempt to free unreferenced scalar: SV 0x40173f3c
26fresh_perl_is(<<'EOI', 'ok', { }, 'delete() under threads');
27use threads;
28threads->new(sub { my %h=(1,2); delete $h{1}})->join for 1..2;
29print "ok";
30EOI
31
32#PR24660
33# test that we don't get:
34# Attempt to free unreferenced scalar: SV 0x814e0dc.
35fresh_perl_is(<<'EOI', 'ok', { }, 'weaken ref under threads');
36use threads;
37use Scalar::Util;
38my $data = "a";
39my $obj = \$data;
40my $copy = $obj;
41Scalar::Util::weaken($copy);
42threads->new(sub { 1 })->join for (1..1);
43print "ok";
44EOI
45
46#PR24663
47# test that we don't get:
48# panic: magic_killbackrefs.
49# Scalars leaked: 3
50fresh_perl_is(<<'EOI', 'ok', { }, 'weaken ref #2 under threads');
51package Foo;
52sub new { bless {},shift }
53package main;
54use threads;
55use Scalar::Util qw(weaken);
56my $object = Foo->new;
57my $ref = $object;
58weaken $ref;
59threads->new(sub { $ref = $object } )->join; # $ref = $object causes problems
60print "ok";
61EOI
9850bf21 62
63#PR30333 - sort() crash with threads
64sub mycmp { length($b) <=> length($a) }
65
66sub do_sort_one_thread {
67 my $kid = shift;
68 print "# kid $kid before sort\n";
69 my @list = ( 'x', 'yy', 'zzz', 'a', 'bb', 'ccc', 'aaaaa', 'z',
70 'hello', 's', 'thisisalongname', '1', '2', '3',
71 'abc', 'xyz', '1234567890', 'm', 'n', 'p' );
72
73 for my $j (1..99999) {
74 for my $k (sort mycmp @list) {}
75 }
76 print "# kid $kid after sort, sleeping 1\n";
77 sleep(1);
78 print "# kid $kid exit\n";
79}
80
81sub do_sort_threads {
82 my $nthreads = shift;
83 my @kids = ();
84 for my $i (1..$nthreads) {
85 my $t = threads->new(\&do_sort_one_thread, $i);
86 print "# parent $$: continue\n";
87 push(@kids, $t);
88 }
89 for my $t (@kids) {
90 print "# parent $$: waiting for join\n";
91 $t->join();
92 print "# parent $$: thread exited\n";
93 }
94}
95
96do_sort_threads(2); # crashes
97ok(1);