Commit | Line | Data |
0f1612a7 |
1 | use strict; |
2 | use warnings; |
3 | |
e1c44605 |
4 | BEGIN { |
0f1612a7 |
5 | if ($ENV{'PERL_CORE'}){ |
6 | chdir 't'; |
7 | unshift @INC, '../lib'; |
8 | } |
9 | use Config; |
e1c44605 |
10 | unless ($Config{'useithreads'}) { |
11 | print "1..0 # Skip: no useithreads\n"; |
12 | exit 0; |
13 | } |
14 | } |
15 | |
16 | use ExtUtils::testlib; |
0f1612a7 |
17 | |
f2cba68d |
18 | BEGIN { print "1..17\n" }; |
e1c44605 |
19 | use threads; |
20 | use threads::shared; |
21 | |
22 | my $test_id = 1; |
23 | share($test_id); |
e1c44605 |
24 | |
25 | sub ok { |
26 | my ($ok, $name) = @_; |
27 | |
d94cde48 |
28 | lock $test_id; # make print and increment atomic |
29 | |
e1c44605 |
30 | # You have to do it this way or VMS will get confused. |
31 | print $ok ? "ok $test_id - $name\n" : "not ok $test_id - $name\n"; |
32 | |
33 | printf "# Failed test at line %d\n", (caller)[2] unless $ok; |
34 | $test_id++; |
35 | return $ok; |
36 | } |
37 | |
d90a703e |
38 | sub skip { |
39 | ok(1, "# Skipped: @_"); |
40 | } |
41 | |
e1c44605 |
42 | ok(1,""); |
43 | |
44 | |
45 | { |
46 | my $retval = threads->create(sub { return ("hi") })->join(); |
47 | ok($retval eq 'hi', "Check basic returnvalue"); |
48 | } |
49 | { |
50 | my ($thread) = threads->create(sub { return (1,2,3) }); |
51 | my @retval = $thread->join(); |
a31a65c0 |
52 | ok($retval[0] == 1 && $retval[1] == 2 && $retval[2] == 3,''); |
e1c44605 |
53 | } |
54 | { |
55 | my $retval = threads->create(sub { return [1] })->join(); |
a31a65c0 |
56 | ok($retval->[0] == 1,"Check that a array ref works",); |
e1c44605 |
57 | } |
58 | { |
59 | my $retval = threads->create(sub { return { foo => "bar" }})->join(); |
60 | ok($retval->{foo} eq 'bar',"Check that hash refs work"); |
61 | } |
62 | { |
63 | my $retval = threads->create( sub { |
64 | open(my $fh, "+>threadtest") || die $!; |
65 | print $fh "test\n"; |
66 | return $fh; |
67 | })->join(); |
68 | ok(ref($retval) eq 'GLOB', "Check that we can return FH $retval"); |
69 | print $retval "test2\n"; |
70 | # seek($retval,0,0); |
71 | # ok(<$retval> eq "test\n"); |
72 | close($retval); |
73 | unlink("threadtest"); |
74 | } |
75 | { |
76 | my $test = "hi"; |
77 | my $retval = threads->create(sub { return $_[0]}, \$test)->join(); |
a31a65c0 |
78 | ok($$retval eq 'hi',''); |
e1c44605 |
79 | } |
80 | { |
81 | my $test = "hi"; |
82 | share($test); |
83 | my $retval = threads->create(sub { return $_[0]}, \$test)->join(); |
a31a65c0 |
84 | ok($$retval eq 'hi',''); |
e1c44605 |
85 | $test = "foo"; |
a31a65c0 |
86 | ok($$retval eq 'foo',''); |
e1c44605 |
87 | } |
88 | { |
89 | my %foo; |
90 | share(%foo); |
91 | threads->create(sub { |
92 | my $foo; |
93 | share($foo); |
94 | $foo = "thread1"; |
95 | return $foo{bar} = \$foo; |
96 | })->join(); |
97 | ok(1,""); |
98 | } |
e2975953 |
99 | |
3cb9023d |
100 | # We parse ps output so this is OS-dependent. |
1e6e959c |
101 | if ($^O eq 'linux') { |
e2975953 |
102 | # First modify $0 in a subthread. |
c8c7fdd1 |
103 | print "# mainthread: \$0 = $0\n"; |
f4cc38af |
104 | threads->create( sub { |
c8c7fdd1 |
105 | print "# subthread: \$0 = $0\n"; |
106 | $0 = "foobar"; |
107 | print "# subthread: \$0 = $0\n" } )->join; |
108 | print "# mainthread: \$0 = $0\n"; |
109 | print "# pid = $$\n"; |
3cb9023d |
110 | if (open PS, "ps -f |") { # Note: must work in (all) systems. |
00c4b2c0 |
111 | my ($sawpid, $sawexe); |
e2975953 |
112 | while (<PS>) { |
ecce83c2 |
113 | chomp; |
114 | print "# [$_]\n"; |
00c4b2c0 |
115 | if (/^\S+\s+$$\s/) { |
116 | $sawpid++; |
228cf569 |
117 | if (/\sfoobar\s*$/) { # Linux 2.2 leaves extra trailing spaces. |
00c4b2c0 |
118 | $sawexe++; |
119 | } |
e2975953 |
120 | last; |
121 | } |
122 | } |
ecce83c2 |
123 | close PS or die; |
00c4b2c0 |
124 | if ($sawpid) { |
125 | ok($sawpid && $sawexe, 'altering $0 is effective'); |
126 | } else { |
127 | skip("\$0 check: did not see pid $$ in 'ps -f |'"); |
128 | } |
e2975953 |
129 | } else { |
130 | skip("\$0 check: opening 'ps -f |' failed: $!"); |
131 | } |
132 | } else { |
133 | skip("\$0 check: only on Linux"); |
134 | } |
57b48062 |
135 | |
136 | { |
f4cc38af |
137 | my $t = threads->create(sub {}); |
f2cba68d |
138 | $t->join(); |
139 | threads->create(sub {})->join(); |
140 | eval { $t->join(); }; |
141 | ok(($@ =~ /Thread already joined/), "Double join works"); |
142 | eval { $t->detach(); }; |
143 | ok(($@ =~ /Cannot detach a joined thread/), "Detach joined thread"); |
144 | } |
145 | |
146 | { |
147 | my $t = threads->create(sub {}); |
148 | $t->detach(); |
149 | threads->create(sub {})->join(); |
150 | eval { $t->detach(); }; |
151 | ok(($@ =~ /Thread already detached/), "Double detach works"); |
152 | eval { $t->join(); }; |
153 | ok(($@ =~ /Cannot join a detached thread/), "Join detached thread"); |
57b48062 |
154 | } |
014f91c3 |
155 | |
156 | { |
ec54d15e |
157 | # The "use IO::File" is not actually used for anything; its only |
158 | # purpose is to incite a lot of calls to newCONSTSUB. See the p5p |
159 | # archives for the thread "maint@20974 or before broke mp2 ithreads test". |
160 | use IO::File; |
d94cde48 |
161 | # this coredumped between #20930 and #21000 |
f4cc38af |
162 | $_->join for map threads->create(sub{ok($_, "stress newCONSTSUB")}), 1..2; |
014f91c3 |
163 | } |
164 | |