xref: /openbsd-src/gnu/usr.bin/perl/dist/threads-shared/t/hv_refs.t (revision b8851fcc53cbe24fd20b090f26dd149e353f6174)
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
14b39c5158Smillertsub ok {
15b39c5158Smillert    my ($id, $ok, $name) = @_;
16b39c5158Smillert
17b39c5158Smillert    # You have to do it this way or VMS will get confused.
18b39c5158Smillert    if ($ok) {
19b39c5158Smillert        print("ok $id - $name\n");
20b39c5158Smillert    } else {
21b39c5158Smillert        print("not ok $id - $name\n");
22b39c5158Smillert        printf("# Failed test at line %d\n", (caller)[2]);
23b39c5158Smillert    }
24b39c5158Smillert
25b39c5158Smillert    return ($ok);
26b39c5158Smillert}
27b39c5158Smillert
28b39c5158SmillertBEGIN {
29b39c5158Smillert    $| = 1;
30b39c5158Smillert    print("1..20\n");   ### Number of tests that will be run ###
31b39c5158Smillert};
32b39c5158Smillert
33b39c5158Smillertuse threads;
34b39c5158Smillertuse threads::shared;
35b39c5158Smillertok(1, 1, 'Loaded');
36b39c5158Smillert
37b39c5158Smillert### Start of Testing ###
38b39c5158Smillert
39b39c5158Smillertmy $foo;
40b39c5158Smillertshare($foo);
41b39c5158Smillertmy %foo;
42b39c5158Smillertshare(%foo);
43b39c5158Smillert$foo{"foo"} = \$foo;
44b39c5158Smillertok(2, !defined ${$foo{foo}}, "Check deref");
45b39c5158Smillert$foo = "test";
46b39c5158Smillertok(3, ${$foo{foo}} eq "test", "Check deref after assign");
47b39c5158Smillertthreads->create(sub{${$foo{foo}} = "test2";})->join();
48b39c5158Smillertok(4, $foo eq "test2", "Check after assign in another thread");
49b39c5158Smillertmy $bar = delete($foo{foo});
50b39c5158Smillertok(5, $$bar eq "test2", "check delete");
51b39c5158Smillertthreads->create( sub {
52b39c5158Smillert   my $test;
53b39c5158Smillert   share($test);
54b39c5158Smillert   $test = "thread3";
55b39c5158Smillert   $foo{test} = \$test;
56b39c5158Smillert   })->join();
57b39c5158Smillertok(6, ${$foo{test}} eq "thread3", "Check reference created in another thread");
58b39c5158Smillertmy $gg = $foo{test};
59b39c5158Smillert$$gg = "test";
60b39c5158Smillertok(7, ${$foo{test}} eq "test", "Check reference");
61b39c5158Smillertmy $gg2 = delete($foo{test});
62b39c5158Smillertok(8, threads::shared::_id($$gg) == threads::shared::_id($$gg2),
63b39c5158Smillert       sprintf("Check we get the same thing (%x vs %x)",
64b39c5158Smillert       threads::shared::_id($$gg),threads::shared::_id($$gg2)));
65b39c5158Smillertok(9, $$gg eq $$gg2, "And check the values are the same");
66*b8851fccSafresh1ok(10, keys %foo == 0, "And make sure we really have deleted the values");
67b39c5158Smillert{
68b39c5158Smillert    my (%hash1, %hash2);
69b39c5158Smillert    share(%hash1);
70b39c5158Smillert    share(%hash2);
71b39c5158Smillert    $hash1{hash} = \%hash2;
72b39c5158Smillert    $hash2{"bar"} = "foo";
73b39c5158Smillert    ok(11, $hash1{hash}->{bar} eq "foo", "Check hash references work");
74b39c5158Smillert    threads->create(sub { $hash2{"bar2"} = "foo2"})->join();
75b39c5158Smillert    ok(12, $hash1{hash}->{bar2} eq "foo2", "Check hash references work");
76b39c5158Smillert    threads->create(sub { my (%hash3); share(%hash3); $hash2{hash} = \%hash3; $hash3{"thread"} = "yes"})->join();
77b39c5158Smillert    ok(13, $hash1{hash}->{hash}->{thread} eq "yes", "Check hash created in another thread");
78b39c5158Smillert}
79b39c5158Smillert
80b39c5158Smillert{
81b39c5158Smillert    my $h = {a=>14};
82b39c5158Smillert    my $r = \$h->{a};
83b39c5158Smillert    share($r);
84b39c5158Smillert    if ($] > 5.008) {
85b39c5158Smillert        eval { lock($r); };
86b39c5158Smillert        ok(14, !$@, "lock on helems ref: $@");
87b39c5158Smillert        eval { lock($h->{a}); };
88b39c5158Smillert        ok(15, !$@, "lock on helems: $@");
89b39c5158Smillert    } else {
90b39c5158Smillert        ok(14, 1, "skipped.  < 5.8");
91b39c5158Smillert        ok(15, 1, "skipped.  < 5.8");
92b39c5158Smillert    }
93b39c5158Smillert}
94b39c5158Smillert{
95b39c5158Smillert    my $object : shared = &share({});
96b39c5158Smillert    threads->create(sub {
97b39c5158Smillert                     bless $object, 'test1';
98b39c5158Smillert                 })->join;
99b39c5158Smillert    ok(16, ref($object) eq 'test1', "blessing does work");
100b39c5158Smillert    my %test = (object => $object);
101b39c5158Smillert    ok(17, ref($test{object}) eq 'test1', "and some more work");
102b39c5158Smillert    bless $object, 'test2';
103b39c5158Smillert    ok(18, ref($test{object}) eq 'test2', "reblessing works!");
104b39c5158Smillert}
105b39c5158Smillert
106b39c5158Smillertok(19, is_shared($foo), "Check for sharing");
107b39c5158Smillertok(20, is_shared(%foo), "Check for sharing");
108b39c5158Smillert
109b39c5158Smillertexit(0);
110b39c5158Smillert
111b39c5158Smillert# EOF
112