From: Russ Cox Date: Fri, 5 Sep 2014 03:14:21 +0000 (-0400) Subject: runtime: use cas loop to coordinate with sigprof X-Git-Tag: go1.4beta1~523 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e0f08b938a37490cd6e4f6bb33360678ac5f42b0;p=gostls13.git runtime: use cas loop to coordinate with sigprof sigprof and setcpuprofilerate coordinate the enabling/disabling of the handler using a Mutex. This has always been a bit dodgy: setcpuprofilerate must be careful to turn off signals before acquiring the lock to avoid a deadlock. Now the lock implementations use onM, and onM isn't okay on the signal stack. We know how to make it okay, but it's more work than is probably worth doing. Since this is super-dodgy anyway, replace the lock with a simple cas loop. It is only contended if setcpuprofilerate is being called, and that doesn't happen frequently enough to care about the raw speed or about using futexes/semaphores. TBR to fix freebsd/amd64 and dragonfly/amd64 builds. Happy to make changes in a follow-up CL. TBR=dvyukov CC=golang-codereviews https://golang.org/cl/141080044 --- diff --git a/src/pkg/runtime/proc.c b/src/pkg/runtime/proc.c index 3c5e244a92..56c35c5a44 100644 --- a/src/pkg/runtime/proc.c +++ b/src/pkg/runtime/proc.c @@ -2630,7 +2630,7 @@ runtime·badreflectcall(void) // called from assembly } static struct { - Mutex lock; + uint32 lock; int32 hz; } prof; @@ -2774,10 +2774,12 @@ runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp, M *mp) } if(prof.hz != 0) { - runtime·lock(&prof.lock); + // Simple cas-lock to coordinate with setcpuprofilerate. + while(!runtime·cas(&prof.lock, 0, 1)) + runtime·osyield(); if(prof.hz != 0) runtime·cpuproftick(stk, n); - runtime·unlock(&prof.lock); + runtime·atomicstore(&prof.lock, 0); } mp->mallocing--; } @@ -2804,9 +2806,11 @@ runtime·setcpuprofilerate_m(void) // it would deadlock. runtime·resetcpuprofiler(0); - runtime·lock(&prof.lock); + while(!runtime·cas(&prof.lock, 0, 1)) + runtime·osyield(); prof.hz = hz; - runtime·unlock(&prof.lock); + runtime·atomicstore(&prof.lock, 0); + runtime·lock(&runtime·sched.lock); runtime·sched.profilehz = hz; runtime·unlock(&runtime·sched.lock);