5 if ($ENV{'PERL_CORE'}){
7 unshift @INC, '../lib';
10 if (! $Config{'useithreads'}) {
11 print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
16 use ExtUtils::testlib;
23 print("1..17\n"); ### Number of tests that will be run ###
37 # You have to do it this way or VMS will get confused.
39 print("ok $id - $name\n");
41 print("not ok $id - $name\n");
42 printf("# Failed test at line %d\n", (caller)[2]);
49 ok(1, '# Skipped: ' . $_[0]);
53 ### Start of Testing ###
56 my $retval = threads->create(sub { return ("hi") })->join();
57 ok($retval eq 'hi', "Check basic returnvalue");
60 my ($thread) = threads->create(sub { return (1,2,3) });
61 my @retval = $thread->join();
62 ok($retval[0] == 1 && $retval[1] == 2 && $retval[2] == 3,'');
65 my $retval = threads->create(sub { return [1] })->join();
66 ok($retval->[0] == 1,"Check that a array ref works",);
69 my $retval = threads->create(sub { return { foo => "bar" }})->join();
70 ok($retval->{foo} eq 'bar',"Check that hash refs work");
73 my $retval = threads->create( sub {
74 open(my $fh, "+>threadtest") || die $!;
78 ok(ref($retval) eq 'GLOB', "Check that we can return FH $retval");
79 print $retval "test2\n";
85 my $retval = threads->create(sub { return $_[0]}, \$test)->join();
86 ok($$retval eq 'hi','');
91 my $retval = threads->create(sub { return $_[0]}, \$test)->join();
92 ok($$retval eq 'hi','');
94 ok($$retval eq 'foo','');
103 return $foo{bar} = \$foo;
108 # We parse ps output so this is OS-dependent.
109 if ($^O eq 'linux') {
110 # First modify $0 in a subthread.
111 #print "# mainthread: \$0 = $0\n";
112 threads->create(sub{ #print "# subthread: \$0 = $0\n";
114 #print "# subthread: \$0 = $0\n"
116 #print "# mainthread: \$0 = $0\n";
117 #print "# pid = $$\n";
118 if (open PS, "ps -f |") { # Note: must work in (all) systems.
119 my ($sawpid, $sawexe);
123 if (/^\s*\S+\s+$$\s/) {
125 if (/\sfoobar\s*$/) { # Linux 2.2 leaves extra trailing spaces.
133 ok($sawpid && $sawexe, 'altering $0 is effective');
135 skip("\$0 check: did not see pid $$ in 'ps -f |'");
138 skip("\$0 check: opening 'ps -f |' failed: $!");
141 skip("\$0 check: only on Linux");
145 my $t = threads->create(sub {});
147 threads->create(sub {})->join();
148 eval { $t->join(); };
149 ok(($@ =~ /Thread already joined/), "Double join works");
150 eval { $t->detach(); };
151 ok(($@ =~ /Cannot detach a joined thread/), "Detach joined thread");
155 my $t = threads->create(sub {});
157 threads->create(sub {})->join();
158 eval { $t->detach(); };
159 ok(($@ =~ /Thread already detached/), "Double detach works");
160 eval { $t->join(); };
161 ok(($@ =~ /Cannot join a detached thread/), "Join detached thread");
165 # The "use IO::File" is not actually used for anything; its only purpose
166 # is incite a lot of calls to newCONSTSUB. See the p5p archives for
167 # the thread "maint@20974 or before broke mp2 ithreads test".
169 # This coredumped between #20930 and #21000
170 $_->join for map threads->create(sub{ok($_, "stress newCONSTSUB")}), 1..2;