- Add _id() function to shared.xs so we can test refs point at same thing.
- Use that rather that comparing stringified ref in shared/t/hv_refs.t
- Allow no_share to pass if sharing to works despite mis-ordering.
- Change docs of threads to document ->create() (as used in tests),
and not mention "new".
p4raw-id: //depot/perlio@14410
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(share cond_wait cond_broadcast cond_signal unlock);
+our @EXPORT_OK = qw(_id _thrcnt _refcnt);
our $VERSION = '0.90';
-use XSLoader;
-XSLoader::load('threads::shared',$VERSION);
-BEGIN {
- if ($Config{'useithreads'}) {
+if ($Config{'useithreads'}) {
*cond_wait = \&cond_wait_enabled;
*cond_signal = \&cond_signal_enabled;
*cond_broadcast = \&cond_broadcast_enabled;
*unlock = \&unlock_enabled;
- } else {
+ require XSLoader;
+ XSLoader::load('threads::shared',$VERSION);
+}
+else {
*share = \&share_disabled;
*cond_wait = \&cond_wait_disabled;
*cond_signal = \&cond_signal_disabled;
*cond_broadcast = \&cond_broadcast_disabled;
*unlock = \&unlock_disabled;
- }
}
PROTOTYPES: ENABLE
void
+_id(SV *ref)
+ PROTOTYPE: \[$@%]
+CODE:
+ shared_sv *shared;
+ if(SvROK(ref))
+ ref = SvRV(ref);
+ if (shared = Perl_sharedsv_find(aTHX_ ref)) {
+ ST(0) = sv_2mortal(newSViv(PTR2IV(shared)));
+ XSRETURN(1);
+ }
+ XSRETURN_UNDEF;
+
+
+void
_refcnt(SV *ref)
PROTOTYPE: \[$@%]
CODE:
use strict;
BEGIN { print "1..17\n" };
use threads;
-use threads::shared;
+use threads::shared qw(:DEFAULT _thrcnt _refcnt _id);
ok(1,1,"loaded");
my $foo;
share($foo);
ok(3, ${$foo{foo}} eq "test", "Check deref after assign");
threads->create(sub{${$foo{foo}} = "test2";})->join();
ok(4, $foo eq "test2", "Check after assign in another thread");
-skip(5, threads::shared::_thrcnt($foo) == 2, "Check refcount");
+skip(5, _thrcnt($foo) == 2, "Check refcount");
my $bar = delete($foo{foo});
ok(6, $$bar eq "test2", "check delete");
-skip(7, threads::shared::_thrcnt($foo) == 1, "Check refcount after delete");
+skip(7, _thrcnt($foo) == 1, "Check refcount after delete");
threads->create( sub {
-my $test;
-share($test);
-$test = "thread3";
-$foo{test} = \$test;
-})->join();
+ my $test;
+ share($test);
+ $test = "thread3";
+ $foo{test} = \$test;
+ })->join();
ok(8, ${$foo{test}} eq "thread3", "Check reference created in another thread");
my $gg = $foo{test};
$$gg = "test";
ok(9, ${$foo{test}} eq "test", "Check reference");
-skip(10, threads::shared::_thrcnt($gg) == 2, "Check refcount");
+skip(10, _thrcnt($gg) == 2, "Check refcount");
my $gg2 = delete($foo{test});
-skip(11, threads::shared::_thrcnt($gg) == 1, "Check refcount");
-ok(12, $gg == $gg2, "Check we get the same reference ($gg == $gg2)");
+skip(11, _thrcnt($gg) == 1, "Check refcount");
+ok(12, _id($gg) == _id($gg2),
+ sprintf("Check we get the same thing (%x vs %x)",
+ _id($$gg),_id($$gg2)));
ok(13, $$gg eq $$gg2, "And check the values are the same");
ok(14, keys %foo == 0, "And make sure we realy have deleted the values");
{
-
-
-
-
BEGIN {
# chdir 't' if -d 't';
# push @INC ,'../lib';
use threads;
ok(1,1,"loaded");
ok(2,$warnmsg =~ /Warning, threads::shared has already been loaded/,
- "threads has warned us");
+ "threads has warned us");
my $test = "bar";
share($test);
ok(3,$test eq "bar","Test disabled share not interfering");
ok(4,$test eq "bar","Test disabled share after thread");
$test = "baz";
})->join();
-ok(5,$test eq "bar","Test that value hasn't changed in another thread");
+# Value should either remain unchanged or be value set by other thread
+ok(5,$test eq "bar" || $test eq 'baz',"Test that value is an expected one");
+
-
print "Thread started\n";
}
-my $thread = threads->new("start_thread","argument");
+my $thread = threads->create("start_thread","argument");
-$thread->new(sub { print "I am a thread"},"argument");
+$thread->create(sub { print "I am a thread"},"argument");
$thread->join();
=over
-=item $thread = new(function, LIST)
+=item $thread = threads->create(function, LIST)
This will create a new thread with the entry point function and give
it LIST as parameters. It will return the corresponding threads
object.
-create() is an alias to new.
-
=item $thread->join
This will wait for the corresponding thread to join. When it finishes