From e0f08b938a37490cd6e4f6bb33360678ac5f42b0 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 4 Sep 2014 23:14:21 -0400 Subject: [PATCH] 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 --- src/pkg/runtime/proc.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) 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); -- 2.48.1