]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use acquirem/releasem more widely
authorAustin Clements <austin@google.com>
Tue, 26 Mar 2019 01:16:33 +0000 (21:16 -0400)
committerAustin Clements <austin@google.com>
Fri, 5 Apr 2019 19:00:12 +0000 (19:00 +0000)
We've copy-pasted the pattern of releasem in many places. This CL
replaces almost everywhere that manipulates g.m.locks and g.preempt
with calls to acquirem/releasem. There are a few where we do something
more complicated, like where exitsyscall has to restore the stack
bound differently depending on the preempt flag, which this CL leaves
alone.

Change-Id: Ia7a46c261daea6e7802b80e7eb9227499f460433
Reviewed-on: https://go-review.googlesource.com/c/go/+/170064
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/runtime/proc.go

index 78940625b806e7399d59ea359de83a3df628cdb3..6b5b3e2b2b7313b7ee3b6341e3df19acbce5a901 100644 (file)
@@ -645,7 +645,7 @@ func ready(gp *g, traceskip int, next bool) {
 
        // Mark runnable.
        _g_ := getg()
-       _g_.m.locks++ // disable preemption because it can be holding p in a local var
+       mp := acquirem() // disable preemption because it can be holding p in a local var
        if status&^_Gscan != _Gwaiting {
                dumpgstatus(gp)
                throw("bad g->status in ready")
@@ -657,10 +657,7 @@ func ready(gp *g, traceskip int, next bool) {
        if atomic.Load(&sched.npidle) != 0 && atomic.Load(&sched.nmspinning) == 0 {
                wakep()
        }
-       _g_.m.locks--
-       if _g_.m.locks == 0 && _g_.preempt { // restore the preemption request in Case we've cleared it in newstack
-               _g_.stackguard0 = stackPreempt
-       }
+       releasem(mp)
 }
 
 // freezeStopWait is a large value that freezetheworld sets
@@ -1080,9 +1077,7 @@ func stopTheWorldWithSema() {
 }
 
 func startTheWorldWithSema(emitTraceEvent bool) int64 {
-       _g_ := getg()
-
-       _g_.m.locks++ // disable preemption because it can be holding p in a local var
+       mp := acquirem() // disable preemption because it can be holding p in a local var
        if netpollinited() {
                list := netpoll(false) // non-blocking
                injectglist(&list)
@@ -1132,10 +1127,7 @@ func startTheWorldWithSema(emitTraceEvent bool) int64 {
                wakep()
        }
 
-       _g_.m.locks--
-       if _g_.m.locks == 0 && _g_.preempt { // restore the preemption request in case we've cleared it in newstack
-               _g_.stackguard0 = stackPreempt
-       }
+       releasem(mp)
 
        return startTime
 }
@@ -1464,7 +1456,7 @@ type cgothreadstart struct {
 //go:yeswritebarrierrec
 func allocm(_p_ *p, fn func()) *m {
        _g_ := getg()
-       _g_.m.locks++ // disable GC because it can be called from sysmon
+       acquirem() // disable GC because it can be called from sysmon
        if _g_.m.p == 0 {
                acquirep(_p_) // temporarily borrow p for mallocs in this function
        }
@@ -1505,10 +1497,7 @@ func allocm(_p_ *p, fn func()) *m {
        if _p_ == _g_.m.p.ptr() {
                releasep()
        }
-       _g_.m.locks--
-       if _g_.m.locks == 0 && _g_.preempt { // restore the preemption request in case we've cleared it in newstack
-               _g_.stackguard0 = stackPreempt
-       }
+       releasem(_g_.m)
 
        return mp
 }
@@ -3255,7 +3244,7 @@ func newproc1(fn *funcval, argp *uint8, narg int32, callergp *g, callerpc uintpt
                _g_.m.throwing = -1 // do not dump full stacks
                throw("go of nil func value")
        }
-       _g_.m.locks++ // disable preemption because it can be holding p in a local var
+       acquirem() // disable preemption because it can be holding p in a local var
        siz := narg
        siz = (siz + 7) &^ 7
 
@@ -3350,10 +3339,7 @@ func newproc1(fn *funcval, argp *uint8, narg int32, callergp *g, callerpc uintpt
        if atomic.Load(&sched.npidle) != 0 && atomic.Load(&sched.nmspinning) == 0 && mainStarted {
                wakep()
        }
-       _g_.m.locks--
-       if _g_.m.locks == 0 && _g_.preempt { // restore the preemption request in case we've cleared it in newstack
-               _g_.stackguard0 = stackPreempt
-       }
+       releasem(_g_.m)
 }
 
 // saveAncestors copies previous ancestors of the given caller g and