[kaffe] CVS kaffe (guilhem): Deadlock fix for kaffe-gc.

Kaffe CVS cvs-commits at kaffe.org
Sun Nov 27 11:23:23 PST 2005


PatchSet 6976 
Date: 2005/11/27 18:18:41
Author: guilhem
Branch: HEAD
Tag: (none) 
Log:
Deadlock fix for kaffe-gc.

Members: 
	ChangeLog:1.4498->1.4499 
	kaffe/kaffevm/kaffe-gc/gc-refs.c:1.15->1.16 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.4498 kaffe/ChangeLog:1.4499
--- kaffe/ChangeLog:1.4498	Sun Nov 27 18:10:33 2005
+++ kaffe/ChangeLog	Sun Nov 27 18:18:41 2005
@@ -1,5 +1,12 @@
 2005-11-27  Guilhem Lavaux  <guilhem at kaffe.org>
 
+	* kaffe/kaffevm/kaffe-gc/gc-refs.c
+	(insertInWeakRef, findWeakRef): New internal function.
+	(KaffeGC_addWeakRef): Fixed a deadlock if GC is triggered in some of
+	the malloc call.
+
+2005-11-27  Guilhem Lavaux  <guilhem at kaffe.org>
+
 	* developers/math_symbols: Added more symbols to be renamed
 	(needed for darwin5).
 
Index: kaffe/kaffe/kaffevm/kaffe-gc/gc-refs.c
diff -u kaffe/kaffe/kaffevm/kaffe-gc/gc-refs.c:1.15 kaffe/kaffe/kaffevm/kaffe-gc/gc-refs.c:1.16
--- kaffe/kaffe/kaffevm/kaffe-gc/gc-refs.c:1.15	Fri May  6 15:24:30 2005
+++ kaffe/kaffe/kaffevm/kaffe-gc/gc-refs.c	Sun Nov 27 18:19:08 2005
@@ -189,48 +189,86 @@
   while (1);
 }
 
-bool
-KaffeGC_addWeakRef(Collector *collector, void* mem, void** refobj)
+static weakRefObject *
+findWeakRefObject(void *mem)
 {
   int idx;
   weakRefObject* obj;
 
   idx = REFOBJHASH(mem);
 
-  lockStaticMutex(&weakRefLock);
   for (obj = weakRefObjects.hash[idx]; obj != 0; obj = obj->next) {
     /* Found it - just register a new weak reference */
-    if (obj->mem == mem) {
-      obj->ref++;
+    if (obj->mem == mem)
+      return obj;
+  }
 
-      if (obj->ref >= obj->allRefSize)
-	if (!resizeWeakReferenceObject(collector, obj, obj->ref * 2 + 1))
-	  {
-	    unlockStaticMutex(&weakRefLock);
-	    return false;
-	  }
+  return NULL;
+}     
 
-      obj->allRefs[obj->ref-1] = refobj;
+static bool
+insertInWeakRef(Collector *collector, weakRefObject *obj, void **refobj)
+{
+  obj->ref++;
+  
+  if (obj->ref >= obj->allRefSize)
+    if (!resizeWeakReferenceObject(collector, obj, obj->ref * 2 + 1))
+      return false;
+  
+  obj->allRefs[obj->ref-1] = refobj;
 
+  return true;
+}
+
+bool
+KaffeGC_addWeakRef(Collector *collector, void* mem, void** refobj)
+{
+  weakRefObject* obj, *obj2;
+  int idx;
+
+  lockStaticMutex(&weakRefLock);
+  obj = findWeakRefObject(mem);
+  if (obj != NULL)
+    {
+      bool ret = insertInWeakRef(collector, obj, refobj);
+      
       unlockStaticMutex(&weakRefLock);
-      return true;
+      return ret;
     }
-  }
 
   /* Not found - create a new one */
+  unlockStaticMutex(&weakRefLock);
   obj = (weakRefObject*)KGC_malloc(collector, sizeof(weakRefObject), KGC_ALLOC_REF);
   if (obj == NULL)
-    {
-      unlockStaticMutex(&weakRefLock);
-      return false;
-    }
+    return false;
 
   obj->mem = mem;
   obj->ref = 1;
-  unlockStaticMutex(&weakRefLock);
   obj->allRefs = (void ***)KGC_malloc(collector, sizeof(void ***), KGC_ALLOC_REF);
   lockStaticMutex(&weakRefLock);
   obj->allRefs[0] = refobj;
+
+  /* Now we check whether has inserted the reference
+   * in the meantime.
+   */
+  obj2 = findWeakRefObject(mem);
+  if (obj2 != NULL)
+    {
+      bool ret;
+      
+      /* Calling free is safe as the GC thread is not
+       * called at that time.
+       */
+      KGC_free(collector, obj->allRefs);
+      KGC_free(collector, obj);
+
+      ret = insertInWeakRef(collector, obj2, refobj);
+      unlockStaticMutex(&weakRefLock);
+
+      return ret;
+    }
+
+  idx = REFOBJHASH(mem);
   obj->next = weakRefObjects.hash[idx];
   weakRefObjects.hash[idx] = obj;
   unlockStaticMutex(&weakRefLock);



More information about the kaffe mailing list