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;
21 if (! eval 'use threads::shared; 1') {
22 print("1..0 # SKIP threads::shared not available\n");
27 print("1..20\n"); ### Number of tests that will be run ###
44 # You have to do it this way or VMS will get confused.
46 print("ok $id - $name\n");
48 print("not ok $id - $name\n");
49 printf("# Failed test at line %d\n", (caller)[2]);
56 ok(1, '# SKIP ' . $_[0]);
60 ### Start of Testing ###
63 my $retval = threads->create(sub { return ("hi") })->join();
64 ok($retval eq 'hi', "Check basic returnvalue");
67 my ($thread) = threads->create(sub { return (1,2,3) });
68 my @retval = $thread->join();
69 ok($retval[0] == 1 && $retval[1] == 2 && $retval[2] == 3,'');
72 my $retval = threads->create(sub { return [1] })->join();
73 ok($retval->[0] == 1,"Check that a array ref works",);
76 my $retval = threads->create(sub { return { foo => "bar" }})->join();
77 ok($retval->{foo} eq 'bar',"Check that hash refs work");
80 my $retval = threads->create( sub {
81 open(my $fh, "+>threadtest") || die $!;
85 ok(ref($retval) eq 'GLOB', "Check that we can return FH $retval");
86 print $retval "test2\n";
92 my $retval = threads->create(sub { return $_[0]}, \$test)->join();
93 ok($$retval eq 'hi','');
98 my $retval = threads->create(sub { return $_[0]}, \$test)->join();
99 ok($$retval eq 'hi','');
101 ok($$retval eq 'foo','');
106 threads->create(sub {
110 return $foo{bar} = \$foo;
115 # We parse ps output so this is OS-dependent.
116 if ($^O eq 'linux') {
117 # First modify $0 in a subthread.
118 #print "# mainthread: \$0 = $0\n";
119 threads->create(sub{ #print "# subthread: \$0 = $0\n";
121 #print "# subthread: \$0 = $0\n"
123 #print "# mainthread: \$0 = $0\n";
124 #print "# pid = $$\n";
125 if (open PS, "ps -f |") { # Note: must work in (all) systems.
126 my ($sawpid, $sawexe);
130 if (/^\s*\S+\s+$$\s/) {
132 if (/\sfoobar\s*$/) { # Linux 2.2 leaves extra trailing spaces.
140 ok($sawpid && $sawexe, 'altering $0 is effective');
142 skip("\$0 check: did not see pid $$ in 'ps -f |'");
145 skip("\$0 check: opening 'ps -f |' failed: $!");
148 skip("\$0 check: only on Linux");
152 my $t = threads->create(sub {});
154 threads->create(sub {})->join();
155 eval { $t->join(); };
156 ok(($@ =~ /Thread already joined/), "Double join works");
157 eval { $t->detach(); };
158 ok(($@ =~ /Cannot detach a joined thread/), "Detach joined thread");
162 my $t = threads->create(sub {});
164 threads->create(sub {})->join();
165 eval { $t->detach(); };
166 ok(($@ =~ /Thread already detached/), "Double detach works");
167 eval { $t->join(); };
168 ok(($@ =~ /Cannot join a detached thread/), "Join detached thread");
172 # The "use IO::File" is not actually used for anything; its only purpose
173 # is incite a lot of calls to newCONSTSUB. See the p5p archives for
174 # the thread "maint@20974 or before broke mp2 ithreads test".
176 # This coredumped between #20930 and #21000
177 $_->join for map threads->create(sub{ok($_, "stress newCONSTSUB")}), 1..2;
183 my $t = threads->create( sub {
185 cond_wait($go) until $go;
188 my $joiner = threads->create(sub { $_[0]->join }, $t);
193 ok(($@ =~ /^Thread already joined at/)?1:0, "Join pending join");
195 { lock($go); $go = 1; cond_signal($go); }
201 my $t = threads->create( sub {
202 eval { threads->self->join; };
203 ok(($@ =~ /^Cannot join self/), "Join self");
204 lock($go); $go = 1; cond_signal($go);
207 { lock ($go); cond_wait($go) until $go; }
213 my $t = threads->create( sub {
214 lock($go); cond_wait($go) until $go;
216 my $joiner = threads->create(sub { $_[0]->join; }, $t);
221 ok(($@ =~ /^Cannot detach a joined thread at/)?1:0, "Detach pending join");
223 { lock($go); $go = 1; cond_signal($go); }