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;
22 require threads::shared;
23 import threads::shared;
25 if ($@ || ! $threads::shared::threads_shared) {
26 print("1..0 # Skip: threads::shared not available\n");
31 print("1..17\n"); ### Number of tests that will be run ###
48 # You have to do it this way or VMS will get confused.
50 print("ok $id - $name\n");
52 print("not ok $id - $name\n");
53 printf("# Failed test at line %d\n", (caller)[2]);
60 ok(1, '# Skipped: ' . $_[0]);
64 ### Start of Testing ###
67 my $retval = threads->create(sub { return ("hi") })->join();
68 ok($retval eq 'hi', "Check basic returnvalue");
71 my ($thread) = threads->create(sub { return (1,2,3) });
72 my @retval = $thread->join();
73 ok($retval[0] == 1 && $retval[1] == 2 && $retval[2] == 3,'');
76 my $retval = threads->create(sub { return [1] })->join();
77 ok($retval->[0] == 1,"Check that a array ref works",);
80 my $retval = threads->create(sub { return { foo => "bar" }})->join();
81 ok($retval->{foo} eq 'bar',"Check that hash refs work");
84 my $retval = threads->create( sub {
85 open(my $fh, "+>threadtest") || die $!;
89 ok(ref($retval) eq 'GLOB', "Check that we can return FH $retval");
90 print $retval "test2\n";
96 my $retval = threads->create(sub { return $_[0]}, \$test)->join();
97 ok($$retval eq 'hi','');
102 my $retval = threads->create(sub { return $_[0]}, \$test)->join();
103 ok($$retval eq 'hi','');
105 ok($$retval eq 'foo','');
110 threads->create(sub {
114 return $foo{bar} = \$foo;
119 # We parse ps output so this is OS-dependent.
120 if ($^O eq 'linux') {
121 # First modify $0 in a subthread.
122 #print "# mainthread: \$0 = $0\n";
123 threads->create(sub{ #print "# subthread: \$0 = $0\n";
125 #print "# subthread: \$0 = $0\n"
127 #print "# mainthread: \$0 = $0\n";
128 #print "# pid = $$\n";
129 if (open PS, "ps -f |") { # Note: must work in (all) systems.
130 my ($sawpid, $sawexe);
134 if (/^\s*\S+\s+$$\s/) {
136 if (/\sfoobar\s*$/) { # Linux 2.2 leaves extra trailing spaces.
144 ok($sawpid && $sawexe, 'altering $0 is effective');
146 skip("\$0 check: did not see pid $$ in 'ps -f |'");
149 skip("\$0 check: opening 'ps -f |' failed: $!");
152 skip("\$0 check: only on Linux");
156 my $t = threads->create(sub {});
158 threads->create(sub {})->join();
159 eval { $t->join(); };
160 ok(($@ =~ /Thread already joined/), "Double join works");
161 eval { $t->detach(); };
162 ok(($@ =~ /Cannot detach a joined thread/), "Detach joined thread");
166 my $t = threads->create(sub {});
168 threads->create(sub {})->join();
169 eval { $t->detach(); };
170 ok(($@ =~ /Thread already detached/), "Double detach works");
171 eval { $t->join(); };
172 ok(($@ =~ /Cannot join a detached thread/), "Join detached thread");
176 # The "use IO::File" is not actually used for anything; its only purpose
177 # is incite a lot of calls to newCONSTSUB. See the p5p archives for
178 # the thread "maint@20974 or before broke mp2 ithreads test".
180 # This coredumped between #20930 and #21000
181 $_->join for map threads->create(sub{ok($_, "stress newCONSTSUB")}), 1..2;