xref: /openbsd-src/gnu/usr.bin/perl/dist/threads/t/problems.t (revision 5759b3d249badf144a6240f7eec4dcf9df003e6b)
1b39c5158Smillertuse strict;
2b39c5158Smillertuse warnings;
3b39c5158Smillert
4b39c5158SmillertBEGIN {
5b39c5158Smillert    use Config;
6b39c5158Smillert    if (! $Config{'useithreads'}) {
7b39c5158Smillert        print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
8b39c5158Smillert        exit(0);
9b39c5158Smillert    }
10b39c5158Smillert}
11b39c5158Smillert
12b39c5158Smillertuse ExtUtils::testlib;
13b39c5158Smillert
14b39c5158Smillertuse threads;
15b39c5158Smillert
16b39c5158SmillertBEGIN {
17b39c5158Smillert    if (! eval 'use threads::shared; 1') {
18b39c5158Smillert        print("1..0 # SKIP threads::shared not available\n");
19b39c5158Smillert        exit(0);
20b39c5158Smillert    }
21b39c5158Smillert
22b39c5158Smillert    $| = 1;
23b39c5158Smillert    if ($] == 5.008) {
24*5759b3d2Safresh1        print("1..6\n");    ### Number of tests that will be run ###
25b39c5158Smillert    } else {
26*5759b3d2Safresh1        print("1..10\n");   ### Number of tests that will be run ###
27b39c5158Smillert    }
28b39c5158Smillert};
29b39c5158Smillert
30b39c5158Smillertprint("ok 1 - Loaded\n");
31b39c5158Smillert
32b39c5158Smillertuse Hash::Util 'lock_keys';
33b39c5158Smillert
34b39c5158Smillertmy $test :shared = 2;
35b39c5158Smillert
36b39c5158Smillert# Note that we can't use Test::More here, as we would need to call is()
37b39c5158Smillert# from within the DESTROY() function at global destruction time, and
38b39c5158Smillert# parts of Test::* may have already been freed by then
39b39c5158Smillertsub is($$$)
40b39c5158Smillert{
41b39c5158Smillert    my ($got, $want, $desc) = @_;
42b39c5158Smillert    lock($test);
43b39c5158Smillert    if ($got ne $want) {
44b39c5158Smillert        print("# EXPECTED: $want\n");
45b39c5158Smillert        print("# GOT:      $got\n");
46b39c5158Smillert        print("not ");
47b39c5158Smillert    }
48b39c5158Smillert    print("ok $test - $desc\n");
49b39c5158Smillert    $test++;
50b39c5158Smillert}
51b39c5158Smillert
52b39c5158Smillert
53b39c5158Smillert# This tests for too much destruction which was caused by cloning stashes
54b39c5158Smillert# on join which led to double the dataspace under 5.8.0
55b39c5158Smillertif ($] != 5.008)
56b39c5158Smillert{
57b39c5158Smillert    sub Foo::DESTROY
58b39c5158Smillert    {
59b39c5158Smillert        my $self = shift;
60b39c5158Smillert        my ($package, $file, $line) = caller;
61b39c5158Smillert        is(threads->tid(), $self->{tid}, "In destroy[$self->{tid}] it should be correct too" );
62b39c5158Smillert    }
63b39c5158Smillert
64b39c5158Smillert    my $foo = bless {tid => 0}, 'Foo';
65b39c5158Smillert    my $bar = threads->create(sub {
66b39c5158Smillert        is(threads->tid(), 1, "And tid be 1 here");
67b39c5158Smillert        $foo->{tid} = 1;
68b39c5158Smillert        return ($foo);
69b39c5158Smillert    })->join();
70b39c5158Smillert    $bar->{tid} = 0;
71b39c5158Smillert}
72b39c5158Smillert
73b39c5158Smillert
74b39c5158Smillert# This tests whether we can call Config::myconfig after threads have been
75b39c5158Smillert# started (interpreter cloned).  5.8.1 and 5.8.2 contained a bug that would
76b39c5158Smillert# disallow that to be done because an attempt was made to change a variable
77b39c5158Smillert# with the :unique attribute.
78b39c5158Smillert
79b39c5158Smillert{
80b39c5158Smillert    lock($test);
81b39c5158Smillert    if ($] == 5.008 || $] >= 5.008003) {
82b39c5158Smillert        threads->create( sub {1} )->join;
83b39c5158Smillert        my $not = eval { Config::myconfig() } ? '' : 'not ';
84b39c5158Smillert        print "${not}ok $test - Are we able to call Config::myconfig after clone\n";
85b39c5158Smillert    } else {
86b39c5158Smillert        print "ok $test # SKIP Are we able to call Config::myconfig after clone\n";
87b39c5158Smillert    }
88b39c5158Smillert    $test++;
89b39c5158Smillert}
90b39c5158Smillert
91b39c5158Smillert
92898184e3Ssthen# Returning a closure from a thread caused problems. If the last index in
93b39c5158Smillert# the anon sub's pad wasn't for a lexical, then a core dump could occur.
94b39c5158Smillert# Otherwise, there might be leaked scalars.
95b39c5158Smillert
96b39c5158Smillert# XXX DAPM 9-Jan-04 - backed this out for now - returning a closure from a
97b39c5158Smillert# thread seems to crash win32
98b39c5158Smillert
99b39c5158Smillert# sub f {
100b39c5158Smillert#     my $x = "foo";
101b39c5158Smillert#     sub { $x."bar" };
102b39c5158Smillert# }
103b39c5158Smillert#
104b39c5158Smillert# my $string = threads->create(\&f)->join->();
105b39c5158Smillert# print $string eq 'foobar' ?  '' : 'not ', "ok $test - returning closure\n";
106b39c5158Smillert# $test++;
107b39c5158Smillert
108b39c5158Smillert
109b39c5158Smillert# Nothing is checking that total keys gets cloned correctly.
110b39c5158Smillert
111b39c5158Smillertmy %h = (1,2,3,4);
112b39c5158Smillertis(keys(%h), 2, "keys correct in parent");
113b39c5158Smillert
114b39c5158Smillertmy $child = threads->create(sub { return (scalar(keys(%h))); })->join;
115b39c5158Smillertis($child, 2, "keys correct in child");
116b39c5158Smillert
117b39c5158Smillertlock_keys(%h);
118b39c5158Smillertdelete($h{1});
119b39c5158Smillert
120b39c5158Smillertis(keys(%h), 1, "keys correct in parent with restricted hash");
121b39c5158Smillert
122b39c5158Smillert$child = threads->create(sub { return (scalar(keys(%h))); })->join;
123b39c5158Smillertis($child, 1, "keys correct in child with restricted hash");
124b39c5158Smillert
125b39c5158Smillertexit(0);
126b39c5158Smillert
127b39c5158Smillert# EOF
128