[M3commit] CVS Update: cm3

Jay K jay.krell at cornell.edu
Sat Jan 8 06:05:19 CET 2011


Index: ThreadPThread.m3
===================================================================
RCS file: /usr/cvs/cm3/m3-libs/m3core/src/thread/PTHREAD/ThreadPThread.m3,v
retrieving revision 1.245
diff -u -r1.245 ThreadPThread.m3
--- ThreadPThread.m3    8 Jan 2011 04:57:15 -0000    1.245
+++ ThreadPThread.m3    8 Jan 2011 04:58:57 -0000
@@ -96,6 +96,7 @@
 PROCEDURE InitMutex (VAR m: pthread_mutex_t; root: REFANY;
                      Clean: PROCEDURE(root: REFANY)) =
   VAR mutex := pthread_mutex_new();
+      register := FALSE;
   BEGIN
     TRY
       WITH r = pthread_mutex_lock(initMu) DO <*ASSERT r=0*> END;
@@ -103,13 +104,24 @@
       IF m # NIL THEN RETURN END;
       (* We won the race, but we might have failed to allocate. *)
       IF mutex = NIL THEN RTE.Raise (RTE.T.OutOfMemory) END;
-      RTHeapRep.RegisterFinalCleanup (root, Clean);
       m := mutex;
       mutex := NIL;
+      register := TRUE;
     FINALLY
       WITH r = pthread_mutex_unlock(initMu) DO <*ASSERT r=0*> END;
       pthread_mutex_delete(mutex);
     END;
+    IF register THEN
+      (* RegisterFinalCleanup serializes itself using RTOS.LockHeap.
+         Call it outside initMu to avoid deadlock with AtForkPrepare.
+         Register can happen outside a lock because the root is yet
+         alive and therefore will not be collected between pthread_mutex_new
+         and RegisterFinalCleanup. We are careful to only RegisterFinalCleanup
+         once -- not in the lost race cases -- though it does not clearly
+         matter.
+       *)
+      RTHeapRep.RegisterFinalCleanup (root, Clean);
+    END;
   END InitMutex;
 
 PROCEDURE LockMutex (m: Mutex) =



I wonder if we should dispense with initMu and eagerly initialize all mutexes.


 - Jay


----------------------------------------
> Date: Sat, 8 Jan 2011 06:01:20 +0000
> To: m3commit at elegosoft.com
> From: jkrell at elego.de
> Subject: [M3commit] CVS Update: cm3
>
> CVSROOT: /usr/cvs
> Changes by: jkrell at birch. 11/01/08 06:01:20
>
> Modified files:
> cm3/m3-libs/m3core/src/thread/PTHREAD/: ThreadPThread.m3
>
> Log message:
> leave initMu before calling RegisterFinalCleanup, to avoid deadlock
> with AtForkPrepare
>
 		 	   		  


More information about the M3commit mailing list