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 ###
45 # You have to do it this way or VMS will get confused.
47 print("ok $id - $name\n");
49 print("not ok $id - $name\n");
50 printf("# Failed test at line %d\n", (caller)[2]);
57 ok(1, '# Skipped: ' . $_[0]);
61 ### Start of Testing ###
64 my $retval = threads->create(sub { return ("hi") })->join();
65 ok($retval eq 'hi', "Check basic returnvalue");
68 my ($thread) = threads->create(sub { return (1,2,3) });
69 my @retval = $thread->join();
70 ok($retval[0] == 1 && $retval[1] == 2 && $retval[2] == 3,'');
73 my $retval = threads->create(sub { return [1] })->join();
74 ok($retval->[0] == 1,"Check that a array ref works",);
77 my $retval = threads->create(sub { return { foo => "bar" }})->join();
78 ok($retval->{foo} eq 'bar',"Check that hash refs work");
81 my $retval = threads->create( sub {
82 open(my $fh, "+>threadtest") || die $!;
86 ok(ref($retval) eq 'GLOB', "Check that we can return FH $retval");
87 print $retval "test2\n";
93 my $retval = threads->create(sub { return $_[0]}, \$test)->join();
94 ok($$retval eq 'hi','');
99 my $retval = threads->create(sub { return $_[0]}, \$test)->join();
100 ok($$retval eq 'hi','');
102 ok($$retval eq 'foo','');
107 threads->create(sub {
111 return $foo{bar} = \$foo;
116 # We parse ps output so this is OS-dependent.
117 if ($^O eq 'linux') {
118 # First modify $0 in a subthread.
119 #print "# mainthread: \$0 = $0\n";
120 threads->create(sub{ #print "# subthread: \$0 = $0\n";
122 #print "# subthread: \$0 = $0\n"
124 #print "# mainthread: \$0 = $0\n";
125 #print "# pid = $$\n";
126 if (open PS, "ps -f |") { # Note: must work in (all) systems.
127 my ($sawpid, $sawexe);
131 if (/^\s*\S+\s+$$\s/) {
133 if (/\sfoobar\s*$/) { # Linux 2.2 leaves extra trailing spaces.
141 ok($sawpid && $sawexe, 'altering $0 is effective');
143 skip("\$0 check: did not see pid $$ in 'ps -f |'");
146 skip("\$0 check: opening 'ps -f |' failed: $!");
149 skip("\$0 check: only on Linux");
153 my $t = threads->create(sub {});
155 threads->create(sub {})->join();
156 eval { $t->join(); };
157 ok(($@ =~ /Thread already joined/), "Double join works");
158 eval { $t->detach(); };
159 ok(($@ =~ /Cannot detach a joined thread/), "Detach joined thread");
163 my $t = threads->create(sub {});
165 threads->create(sub {})->join();
166 eval { $t->detach(); };
167 ok(($@ =~ /Thread already detached/), "Double detach works");
168 eval { $t->join(); };
169 ok(($@ =~ /Cannot join a detached thread/), "Join detached thread");
173 # The "use IO::File" is not actually used for anything; its only purpose
174 # is incite a lot of calls to newCONSTSUB. See the p5p archives for
175 # the thread "maint@20974 or before broke mp2 ithreads test".
177 # This coredumped between #20930 and #21000
178 $_->join for map threads->create(sub{ok($_, "stress newCONSTSUB")}), 1..2;