From 938785a29d4f7c59a64a93320a37f15bb52259b4 Mon Sep 17 00:00:00 2001 From: Artur Bergman Date: Fri, 26 Oct 2001 11:35:47 +0000 Subject: [PATCH] Extend support of change #12672 to support arrays and hashes. Shared reference support is complete bar support for blessed objects. TODO: tests for shared arrays and more hv reference tests. Also a complex test testing all types intermixed. p4raw-link: @12672 on //depot/perl: 409b1fd3668e7a4d9e663c5b16ba2364947b127b p4raw-id: //depot/perl@12673 --- ext/threads/shared/shared.xs | 28 ++++++++++++++++++++++++++++ ext/threads/shared/t/hv_refs.t | 15 +++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/ext/threads/shared/shared.xs b/ext/threads/shared/shared.xs index ed5ddfd..b297098 100644 --- a/ext/threads/shared/shared.xs +++ b/ext/threads/shared/shared.xs @@ -21,6 +21,34 @@ SV* shared_sv_attach_sv (SV* sv, shared_sv* shared) { } } else { switch(SvTYPE(SHAREDSvGET(shared))) { + case SVt_PVAV: { + SV* weakref; + SV* obj_ref = newSViv(0); + SV* obj = newSVrv(obj_ref,"threads::shared::av"); + AV* hv = newAV(); + sv_setiv(obj,(IV)shared); + weakref = newRV((SV*)hv); + sv = newRV_noinc((SV*)hv); + sv_rvweaken(weakref); + sv_magic((SV*) hv, obj_ref, PERL_MAGIC_tied, Nullch, 0); + hv_store(shared_hv, SvPV(id,length), length, weakref, 0); + Perl_sharedsv_thrcnt_inc(aTHX_ shared); + } + break; + case SVt_PVHV: { + SV* weakref; + SV* obj_ref = newSViv(0); + SV* obj = newSVrv(obj_ref,"threads::shared::hv"); + HV* hv = newHV(); + sv_setiv(obj,(IV)shared); + weakref = newRV((SV*)hv); + sv = newRV_noinc((SV*)hv); + sv_rvweaken(weakref); + sv_magic((SV*) hv, obj_ref, PERL_MAGIC_tied, Nullch, 0); + hv_store(shared_hv, SvPV(id,length), length, weakref, 0); + Perl_sharedsv_thrcnt_inc(aTHX_ shared); + } + break; default: { MAGIC* shared_magic; SV* value = newSVsv(SHAREDSvGET(shared)); diff --git a/ext/threads/shared/t/hv_refs.t b/ext/threads/shared/t/hv_refs.t index 5181dbd..c10b36d 100644 --- a/ext/threads/shared/t/hv_refs.t +++ b/ext/threads/shared/t/hv_refs.t @@ -24,7 +24,7 @@ sub ok { use ExtUtils::testlib; use strict; -BEGIN { print "1..7\n" }; +BEGIN { print "1..17\n" }; use threads; use threads::shared; ok(1,1,"loaded"); @@ -58,5 +58,16 @@ ok(11, threads::shared::_thrcnt($gg) == 1, "Check refcount"); ok(12, $gg == $gg2, "Check we get the same reference ($gg == $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"); - +{ + my (%hash1, %hash2); + share(%hash1); + share(%hash2); + $hash1{hash} = \%hash2; + $hash2{"bar"} = "foo"; + ok(15, $hash1{hash}->{bar} eq "foo", "Check hash references work"); + threads->create(sub { $hash2{"bar2"} = "foo2"})->join(); + ok(16, $hash1{hash}->{bar2} eq "foo2", "Check hash references work"); + threads->create(sub { my (%hash3); share(%hash3); $hash2{hash} = \%hash3; $hash3{"thread"} = "yes"})->join(); + ok(17, $hash1{hash}->{hash}->{thread} eq "yes", "Check hash created in another thread"); +} -- 2.7.4