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