diff options
Diffstat (limited to 'ext/threads')
-rw-r--r-- | ext/threads/shared/shared.pm | 14 | ||||
-rw-r--r-- | ext/threads/shared/t/disabled.t | 53 | ||||
-rw-r--r-- | ext/threads/shared/t/hv_refs.t | 6 |
3 files changed, 63 insertions, 10 deletions
diff --git a/ext/threads/shared/shared.pm b/ext/threads/shared/shared.pm index cd39da6bf8..3b41a30c14 100644 --- a/ext/threads/shared/shared.pm +++ b/ext/threads/shared/shared.pm @@ -6,7 +6,7 @@ use warnings; require Exporter; our @ISA = qw(Exporter); -our @EXPORT = qw(share cond_wait cond_broadcast cond_signal _refcnt _id _thrcnt); +our @EXPORT = qw(share cond_wait cond_broadcast cond_signal); our $VERSION = '0.90'; if ($threads::threads) { @@ -24,10 +24,10 @@ else { } -sub cond_wait_disabled { return @_ }; -sub cond_signal_disabled { return @_}; -sub cond_broadcast_disabled { return @_}; -sub share_disabled { return @_} +sub cond_wait_disabled (\[$@%]) { undef } +sub cond_signal_disabled (\[$@%]) { undef } +sub cond_broadcast_disabled (\[$@%]) { undef } +sub share_disabled (\[$@%]) { return $_[0] } $threads::shared::threads_shared = 1; @@ -72,7 +72,7 @@ It is used together with the threads module. =head1 EXPORT -C<share>, C<lock>, C<cond_wait>, C<cond_signal>, C<cond_broadcast> +C<share>, C<cond_wait>, C<cond_signal>, C<cond_broadcast> Note that if this module is imported when C<threads> has not yet been loaded, then these functions all become no-ops. This makes it possible @@ -87,7 +87,7 @@ environments. C<share> takes a value and marks it as shared. You can share a scalar, array, hash, scalar ref, array ref or hash ref. C<share> will return -the shared rvalue. +the shared rvalue but always as a reference. C<share> will traverse up references exactly I<one> level. C<share(\$a)> is equivalent to C<share($a)>, while C<share(\\$a)> is not. diff --git a/ext/threads/shared/t/disabled.t b/ext/threads/shared/t/disabled.t new file mode 100644 index 0000000000..067cf2561e --- /dev/null +++ b/ext/threads/shared/t/disabled.t @@ -0,0 +1,53 @@ +#!./perl -Tw + +# Tests of threads::shared's behavior when threads are disabled. + +BEGIN { + chdir 't'; + @INC = '../lib'; +} + +# Can't use Test::More, it turns threads on. +use Test; +plan tests => 31; + +use threads::shared; + +# Make sure threads are really off. +ok( !$INC{"threads.pm"} ); + +# Check each faked function. +foreach my $func (qw(share cond_wait cond_signal cond_broadcast)) { + ok( my $func_ref = __PACKAGE__->can($func) ? 1 : 0 ); + + eval qq{$func()}; + ok( $@, qr/^Not enough arguments / ); + + my %hash = (foo => 42, bar => 23); + eval qq{$func(\%hash)}; + ok( $@, '' ); + ok( $hash{foo}, 42 ); + ok( $hash{bar}, 23 ); +} + +# These all have no return value. +foreach my $func (qw(cond_wait cond_signal cond_broadcast)) { + my @array = qw(1 2 3 4); + ok( eval qq{$func(\@array)}, undef ); + ok( "@array", "1 2 3 4" ); +} + +# share() is supposed to return back it's argument as a ref. +{ + my @array = qw(1 2 3 4); + ok( share(@array), \@array ); + ok( ref &share({}), 'HASH' ); + ok( "@array", "1 2 3 4" ); +} + +# lock() should be a no-op. The return value is currently undefined. +{ + my @array = qw(1 2 3 4); + lock(@array); + ok( "@array", "1 2 3 4" ); +} diff --git a/ext/threads/shared/t/hv_refs.t b/ext/threads/shared/t/hv_refs.t index 31ea5d964f..94bf822f8f 100644 --- a/ext/threads/shared/t/hv_refs.t +++ b/ext/threads/shared/t/hv_refs.t @@ -32,7 +32,7 @@ use ExtUtils::testlib; use strict; BEGIN { print "1..13\n" }; use threads; -use threads::shared qw(:DEFAULT _refcnt _id); +use threads::shared; ok(1,1,"loaded"); my $foo; share($foo); @@ -57,9 +57,9 @@ my $gg = $foo{test}; $$gg = "test"; ok(7, ${$foo{test}} eq "test", "Check reference"); my $gg2 = delete($foo{test}); -ok(8, _id($$gg) == _id($$gg2), +ok(8, threads::shared::_id($$gg) == threads::shared::_id($$gg2), sprintf("Check we get the same thing (%x vs %x)", - _id($$gg),_id($$gg2))); + threads::shared::_id($$gg),threads::shared::_id($$gg2))); ok(9, $$gg eq $$gg2, "And check the values are the same"); ok(10, keys %foo == 0, "And make sure we realy have deleted the values"); { |