Fixed race condtions and deadlocks in interaction with
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / t / hv_simple.t
CommitLineData
49485a95 1
2BEGIN {
3# chdir 't' if -d 't';
4# push @INC ,'../lib';
5 require Config; import Config;
6 unless ($Config{'useithreads'}) {
7 print "1..0 # Skip: no useithreads\n";
8 exit 0;
9 }
10}
11
12
13sub ok {
14 my ($id, $ok, $name) = @_;
15
16 # You have to do it this way or VMS will get confused.
17 print $ok ? "ok $id - $name\n" : "not ok $id - $name\n";
18
19 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
20
21 return $ok;
22}
23
6b85e4fe 24sub skip {
25 my ($id, $ok, $name) = @_;
26 print "ok $id # skip _thrcnt - $name \n";
27}
28
49485a95 29
30
31use ExtUtils::testlib;
32use strict;
33BEGIN { print "1..21\n" };
34use threads;
35use threads::shared;
36ok(1,1,"loaded");
37my %hash;
38share(%hash);
39$hash{"foo"} = "bar";
40ok(2,$hash{"foo"} eq "bar","Check hash get");
41threads->create(sub { $hash{"bar"} = "thread1"})->join();
42threads->create(sub { ok(3,$hash{"bar"} eq "thread1", "Check thread get and write")})->join();
43{
44 my $foo = delete($hash{"bar"});
45 ok(4, $foo eq "thread1", "Check delete, want 'thread1' got '$foo'");
46 $foo = delete($hash{"bar"});
47 ok(5, $foo == undef, "Check delete on empty value");
48}
49ok(6, keys %hash == 1, "Check keys");
50$hash{"1"} = 1;
51$hash{"2"} = 2;
52$hash{"3"} = 3;
53ok(7, keys %hash == 4, "Check keys");
54ok(8, exists($hash{"1"}) == 1, "Exist on existing key");
55ok(9, exists($hash{"4"}) == undef, "Exists on non existing key");
56my %seen;
57foreach my $key ( keys %hash) {
58 $seen{$key}++;
59}
60ok(10, $seen{1} == 1, "Keys..");
61ok(11, $seen{2} == 1, "Keys..");
62ok(12, $seen{3} == 1, "Keys..");
63ok(13, $seen{"foo"} == 1, "Keys..");
64threads->create(sub { %hash = () })->join();
65ok(14, keys %hash == 0, "Check clear");
6b85e4fe 66skip(15, threads::shared::_thrcnt(%hash) == 1, "thrcnt");
67threads->create(sub { skip(16, threads::shared::_thrcnt(%hash) == 2, "thrcnt is up")})->join();
68skip(17, threads::shared::_thrcnt(%hash) == 1, "thrcnt is down");
6d56dc1c 69{
49485a95 70 my $test;
71 my $test2;
72 share($test);
73 $test = \%hash;
74 $test2 = \%hash;
6b85e4fe 75 skip(18, threads::shared::_thrcnt(%hash) == 2, "thrcnt is up on shared reference");
49485a95 76 $test = "bar";
6b85e4fe 77 skip(19 , threads::shared::_thrcnt(%hash) == 1, "thrcnt is down when shared reference is dropped");
49485a95 78 $test = $test2;
6b85e4fe 79 skip(20, threads::shared::_thrcnt(%hash) == 2, "thrcnt is up on shared reference");
49485a95 80}
6b85e4fe 81skip(21 , threads::shared::_thrcnt(%hash) == 1, "thrcnt is down when shared reference is killed");