This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
libgo patch committed: Avoid deadlock creating new thread
- From: Ian Lance Taylor <iant at google dot com>
- To: gcc-patches at gcc dot gnu dot org, gofrontend-dev at googlegroups dot com
- Date: Fri, 21 Jan 2011 16:11:49 -0800
- Subject: libgo patch committed: Avoid deadlock creating new thread
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 -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;
}