diff mbox

libgo patch committed: Avoid deadlock creating new thread

Message ID mcr39oldcfu.fsf@google.com
State New
Headers show

Commit Message

Ian Lance Taylor Jan. 22, 2011, 12:11 a.m. UTC
This patch to libgo avoids a potential deadlock when creating a new
thread: allocating the initial malloc data structures requires a lock
which may also be needed by the garbage collector.  Bootstrapped and ran
Go testsuite on x86_64-unknown-linux-gnu.  Committed to mainline.

Ian
diff mbox

Patch

diff -r ab8f4a4c7096 libgo/runtime/malloc.goc
--- a/libgo/runtime/malloc.goc	Fri Jan 21 15:32:33 2011 -0800
+++ b/libgo/runtime/malloc.goc	Fri Jan 21 16:01:44 2011 -0800
@@ -255,6 +255,9 @@ 
 {
 	MCache *c;
 
+	if(!__sync_bool_compare_and_swap(&m->mallocing, 0, 1))
+		runtime_throw("allocmcache - deadlock");
+
 	runtime_lock(&runtime_mheap);
 	c = runtime_FixAlloc_Alloc(&runtime_mheap.cachealloc);
 
@@ -264,6 +267,11 @@ 
 	mstats.mcache_inuse = runtime_mheap.cachealloc.inuse;
 	mstats.mcache_sys = runtime_mheap.cachealloc.sys;
 	runtime_unlock(&runtime_mheap);
+
+	__sync_bool_compare_and_swap(&m->mallocing, 1, 0);
+	if(__sync_bool_compare_and_swap(&m->gcing, 1, 0))
+		__go_run_goroutine_gc(2);
+
 	return c;
 }