improve thread linking options on VMS
[p5sagit/p5-mst-13.2.git] / ext / threads / t / problems.t
CommitLineData
da46a8d0 1
2BEGIN {
3 chdir 't' if -d 't';
974ec8aa 4 push @INC, '../lib';
da46a8d0 5 require Config; import Config;
6 unless ($Config{'useithreads'}) {
7 print "1..0 # Skip: no useithreads\n";
8 exit 0;
9 }
10}
11
997c206d 12use warnings;
b7bef491 13no warnings 'deprecated';
da46a8d0 14use strict;
15use threads;
16use threads::shared;
94a66813 17use Hash::Util 'lock_keys';
997c206d 18
19# Note that we can't use Test::More here, as we would need to
20# call is() from within the DESTROY() function at global destruction time,
21# and parts of Test::* may have already been freed by then
22
94a66813 23print "1..14\n";
997c206d 24
25my $test : shared = 1;
26
27sub is($$$) {
28 my ($got, $want, $desc) = @_;
29 unless ($got eq $want) {
30 print "# EXPECTED: $want\n";
94a66813 31 print "# GOT: $got\n";
997c206d 32 print "not ";
33 }
34 print "ok $test - $desc\n";
35 $test++;
36}
da46a8d0 37
38
39#
40# This tests for too much destruction
41# which was caused by cloning stashes
42# on join which led to double the dataspace
43#
44#########################
45
46$|++;
da46a8d0 47
48{
da46a8d0 49 sub Foo::DESTROY {
50 my $self = shift;
51 my ($package, $file, $line) = caller;
997c206d 52 is(threads->tid(),$self->{tid},
53 "In destroy[$self->{tid}] it should be correct too" )
da46a8d0 54 }
55 my $foo;
56 $foo = bless {tid => 0}, 'Foo';
57 my $bar = threads->create(sub {
997c206d 58 is(threads->tid(),1, "And tid be 1 here");
da46a8d0 59 $foo->{tid} = 1;
60 return $foo;
61 })->join();
62 $bar->{tid} = 0;
da46a8d0 63}
ad4404a3 64
65#
66# This tests whether we can call Config::myconfig after threads have been
67# started (interpreter cloned). 5.8.1 and 5.8.2 contained a bug that would
68# disallow that too be done, because an attempt was made to change a variable
69# with the : unique attribute.
70#
71#########################
72
73threads->new( sub {1} )->join;
74my $not = eval { Config::myconfig() } ? '' : 'not ';
75print "${not}ok $test - Are we able to call Config::myconfig after clone\n";
76$test++;
77
53c33732 78# bugid 24383 - :unique hashes weren't being made readonly on interpreter
79# clone; check that they are.
80
81our $unique_scalar : unique;
82our @unique_array : unique;
83our %unique_hash : unique;
84threads->new(
85 sub {
0abe3f7c 86 my $TODO = ":unique needs to be re-implemented in a non-broken way";
53c33732 87 eval { $unique_scalar = 1 };
0abe3f7c 88 print $@ =~ /read-only/
89 ? '' : 'not ', "ok $test # TODO $TODO unique_scalar\n";
53c33732 90 $test++;
91 eval { $unique_array[0] = 1 };
0abe3f7c 92 print $@ =~ /read-only/
93 ? '' : 'not ', "ok $test # TODO $TODO - unique_array\n";
53c33732 94 $test++;
95 eval { $unique_hash{abc} = 1 };
0abe3f7c 96 print $@ =~ /disallowed/
97 ? '' : 'not ', "ok $test # TODO $TODO - unique_hash\n";
53c33732 98 $test++;
99 }
100)->join;
101
371fce9b 102# bugid #24940 :unique should fail on my and sub declarations
103
104for my $decl ('my $x : unique', 'sub foo : unique') {
105 eval $decl;
106 print $@ =~
107 /^The 'unique' attribute may only be applied to 'our' variables/
108 ? '' : 'not ', "ok $test - $decl\n";
109 $test++;
110}
111
112
b23f1a86 113# Returing a closure from a thread caused problems. If the last index in
114# the anon sub's pad wasn't for a lexical, then a core dump could occur.
115# Otherwise, there might be leaked scalars.
116
a6144651 117# XXX DAPM 9-Jan-04 - backed this out for now - returning a closure from a
118# thread seems to crash win32
119
120# sub f {
121# my $x = "foo";
122# sub { $x."bar" };
123# }
124#
125# my $string = threads->new(\&f)->join->();
126# print $string eq 'foobar' ? '' : 'not ', "ok $test - returning closure\n";
127# $test++;
b23f1a86 128
94a66813 129# Nothing is checking that total keys gets cloned correctly.
130
131my %h = (1,2,3,4);
132is (keys %h, 2, "keys correct in parent");
133
134my $child = threads->new(sub { return scalar keys %h })->join;
135is ($child, 2, "keys correct in child");
136
137lock_keys (%h);
138delete $h{1};
139
140is (keys %h, 1, "keys correct in parent with restricted hash");
141
142$child = threads->new(sub { return scalar keys %h })->join;
143is ($child, 1, "keys correct in child with restricted hash");
144
da46a8d0 1451;