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