]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use cas loop to coordinate with sigprof
authorRuss Cox <rsc@golang.org>
Fri, 5 Sep 2014 03:14:21 +0000 (23:14 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 5 Sep 2014 03:14:21 +0000 (23:14 -0400)
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

index 3c5e244a92260a0dee0693e7b4b89f972614ecef..56c35c5a4475a24bc7b7ef5b316928a01bced49f 100644 (file)
@@ -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);