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