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