Fix case where shared reference does not exist in current thread.
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / t / hv_refs.t
CommitLineData
0dc5f2bb 1BEGIN {
2# chdir 't' if -d 't';
3# push @INC ,'../lib';
4 require Config; import Config;
5 unless ($Config{'useithreads'}) {
6 print "1..0 # Skip: no useithreads\n";
7 exit 0;
8 }
9}
10
11
12sub ok {
13 my ($id, $ok, $name) = @_;
14
15 # You have to do it this way or VMS will get confused.
16 print $ok ? "ok $id - $name\n" : "not ok $id - $name\n";
17
18 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
19
20 return $ok;
21}
22
23
24
25use ExtUtils::testlib;
26use strict;
27BEGIN { print "1..7\n" };
28use threads;
29use threads::shared;
30ok(1,1,"loaded");
31my $foo;
32share($foo);
33my %foo;
34share(%foo);
35$foo{"foo"} = \$foo;
36ok(2, ${$foo{foo}} == undef, "Check deref");
37$foo = "test";
38ok(3, ${$foo{foo}} eq "test", "Check deref after assign");
39threads->create(sub{${$foo{foo}} = "test2";})->join();
40ok(4, $foo eq "test2", "Check after assign in another thread");
41ok(5, threads::shared::_thrcnt($foo) == 2, "Check refcount");
42my $bar = delete($foo{foo});
43ok(6, $$bar eq "test2", "check delete");
44ok(7, threads::shared::_thrcnt($foo) == 1, "Check refcount after delete");
409b1fd3 45threads->create( sub {
46my $test;
47share($test);
48$test = "thread3";
49$foo{test} = \$test;
50})->join();
51ok(8, ${$foo{test}} eq "thread3", "Check refernece created in another thread");
52my $gg = $foo{test};
53$$gg = "test";
54ok(9, ${$foo{test}} eq "test", "Check refernece");
55ok(10, threads::shared::_thrcnt($gg) == 2, "Check refcount");
56my $gg2 = delete($foo{test});
57ok(11, threads::shared::_thrcnt($gg) == 1, "Check refcount");
58ok(12, $gg == $gg2, "Check we get the same reference ($gg == $gg2)");
59ok(13, $$gg eq $$gg2, "And check the values are the same");
60ok(14, keys %foo == 0, "And make sure we realy have deleted the values");
61
62