This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Changed how ownership of mutexes are held. We never hold a mutex only a slot indicati...
authorArtur Bergman <sky@nanisky.com>
Tue, 18 Sep 2001 10:24:31 +0000 (10:24 +0000)
committerArtur Bergman <sky@nanisky.com>
Tue, 18 Sep 2001 10:24:31 +0000 (10:24 +0000)
p4raw-id: //depot/perl@12066

sharedsv.c
sharedsv.h

index db9b112..070a956 100644 (file)
@@ -63,6 +63,8 @@ Perl_sharedsv_new(pTHX)
     New(2555,ssv,1,shared_sv);
     MUTEX_INIT(&ssv->mutex);
     COND_INIT(&ssv->cond);
+    COND_INIT(&ssv->user_cond);
+    ssv->owner = 0;
     ssv->locks = 0;
     return ssv;
 }
@@ -97,15 +99,19 @@ Perl_sharedsv_lock(pTHX_ shared_sv* ssv)
 {
     if(!ssv)
         return;
+    MUTEX_LOCK(&ssv->mutex);
     if(ssv->owner && ssv->owner == my_perl) {
         ssv->locks++;
+       MUTEX_UNLOCK(&ssv->mutex);
         return;
     }
-    MUTEX_LOCK(&ssv->mutex);
+    while(ssv->owner) 
+      COND_WAIT(&ssv->cond,&ssv->mutex);
     ssv->locks++;
     ssv->owner = my_perl;
     if(ssv->locks == 1)
         SAVEDESTRUCTOR_X(Perl_sharedsv_unlock_scope,ssv);
+    MUTEX_UNLOCK(&ssv->mutex);
 }
 
 /*
@@ -119,22 +125,31 @@ Recursively unlocks a shared sv.
 void
 Perl_sharedsv_unlock(pTHX_ shared_sv* ssv)
 {
-    if(ssv->owner != my_perl)
+    MUTEX_LOCK(&ssv->mutex);
+    if(ssv->owner != my_perl) {
+        Perl_croak(aTHX_ "panic: Perl_sharedsv_unlock unlocking mutex that we don't own");
+        MUTEX_UNLOCK(&ssv->mutex); 
         return;
+    } 
 
     if(--ssv->locks == 0) {
         ssv->owner = NULL;
-        MUTEX_UNLOCK(&ssv->mutex);
+       COND_SIGNAL(&ssv->cond);
     }
+    MUTEX_UNLOCK(&ssv->mutex);
  }
 
 void
 Perl_sharedsv_unlock_scope(pTHX_ shared_sv* ssv)
 {
-    if(ssv->owner != my_perl)
+    MUTEX_LOCK(&ssv->mutex);
+    if(ssv->owner != my_perl) {
+        MUTEX_UNLOCK(&ssv->mutex);
         return;
+    }
     ssv->locks = 0;
     ssv->owner = NULL;
+    COND_SIGNAL(&ssv->cond);
     MUTEX_UNLOCK(&ssv->mutex);
 }
 
@@ -199,3 +214,4 @@ Perl_sharedsv_thrcnt_dec(pTHX_ shared_sv* ssv)
 }
 
 #endif /* USE_ITHREADS */
+
index 1dec854..f92a94a 100644 (file)
@@ -5,6 +5,7 @@ typedef struct {
     SV*              sv;    /* The actual data */
     perl_mutex       mutex; /* Our mutex */
     perl_cond        cond;  /* Our condition variable */
+    perl_cond        user_cond;  /* For user level conditions */
     IV               locks; /* Number of locks held */
     PerlInterpreter* owner; /* who owns the lock */
 } shared_sv;