This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
We only need to fetch the SV from the backend if the index
authorArtur Bergman <sky@nanisky.com>
Wed, 24 Oct 2001 11:06:35 +0000 (11:06 +0000)
committerArtur Bergman <sky@nanisky.com>
Wed, 24 Oct 2001 11:06:35 +0000 (11:06 +0000)
is not the same as mg_private. In theory this could be
a problem if there are an exact multiple of U16 changes
of a shared variable between two access in a given thread,
we choose to avoid theory for now.

p4raw-id: //depot/perl@12616

ext/threads/shared/shared.xs
sharedsv.c
sharedsv.h

index 90049e2..86ad419 100644 (file)
@@ -24,11 +24,14 @@ void shared_sv_attach_sv (SV* sv, shared_sv* shared) {
 int shared_sv_fetch_mg (pTHX_ SV* sv, MAGIC *mg) {
     shared_sv* shared = (shared_sv*) SvIV(mg->mg_obj);
     SHAREDSvLOCK(shared);
-    if(SvROK(SHAREDSvGET(shared))) {
-        shared_sv* target = (shared_sv*) SvIV(SvRV(SHAREDSvGET(shared)));
-       shared_sv_attach_sv(sv, target);
-    } else {
-        sv_setsv(sv, SHAREDSvGET(shared));
+    if(mg->mg_private != shared->index) {
+        if(SvROK(SHAREDSvGET(shared))) {
+            shared_sv* target = (shared_sv*) SvIV(SvRV(SHAREDSvGET(shared)));
+           shared_sv_attach_sv(sv, target);
+        } else {
+            sv_setsv(sv, SHAREDSvGET(shared));
+        }
+        mg->mg_private = shared->index;
     }
     SHAREDSvUNLOCK(shared);
 
@@ -51,10 +54,11 @@ int shared_sv_store_mg (pTHX_ SV* sv, MAGIC *mg) {
         }
         Perl_sv_free(PL_sharedsv_space,SHAREDSvGET(shared));
         SHAREDSvGET(shared) = newRV_noinc(newSViv((IV)target));
-        SvROK_off(sv);
     } else {
         sv_setsv(SHAREDSvGET(shared), sv);
     }
+    shared->index++;
+    mg->mg_private = shared->index;
     SHAREDSvRELEASE(shared);
     if(SvROK(SHAREDSvGET(shared)))
        Perl_sharedsv_thrcnt_inc(aTHX_ (shared_sv*) SvIV(SvRV(SHAREDSvGET(shared))));       
@@ -136,6 +140,7 @@ new(class, value)
         shared_magic->mg_virtual = &svtable;
         shared_magic->mg_obj = newSViv((IV)shared);
         shared_magic->mg_flags |= MGf_REFCOUNTED;
+        shared_magic->mg_private = 0;
         SvMAGICAL_on(value);
         RETVAL = obj;
         OUTPUT:                
index 0deabb2..dde1153 100644 (file)
@@ -68,6 +68,7 @@ Perl_sharedsv_new(pTHX)
     COND_INIT(&ssv->user_cond);
     ssv->owner = 0;
     ssv->locks = 0;
+    ssv->index = 0;
     return ssv;
 }
 
index 5a55be6..f82804d 100644 (file)
@@ -7,6 +7,7 @@ typedef struct {
     perl_cond           user_cond;      /* For user-level conditions */
     IV                  locks;          /* Number of locks held */
     PerlInterpreter    *owner;          /* Who owns the lock? */
+    U16                 index;          /* Update index */
 } shared_sv;
 
 #define SHAREDSvGET(a)      (a->sv)