1
0
mirror of https://github.com/golang/go synced 2024-09-29 01:14:29 -06:00

runtime: tricky replacements of g in proc.go

Change-Id: I36cd167ed77e123b3ba7dd4a1a8577cbc51a84d7
Reviewed-on: https://go-review.googlesource.com/c/go/+/418588
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
This commit is contained in:
Michael Pratt 2022-07-20 13:31:10 -04:00
parent 4400238ec8
commit 4509e951ec

View File

@ -143,11 +143,11 @@ var initSigmask sigset
// The main goroutine. // The main goroutine.
func main() { func main() {
g := getg() mp := getg().m
// Racectx of m0->g0 is used only as the parent of the main goroutine. // Racectx of m0->g0 is used only as the parent of the main goroutine.
// It must not be used for anything else. // It must not be used for anything else.
g.m.g0.racectx = 0 mp.g0.racectx = 0
// Max stack size is 1 GB on 64-bit, 250 MB on 32-bit. // Max stack size is 1 GB on 64-bit, 250 MB on 32-bit.
// Using decimal instead of binary GB and MB because // Using decimal instead of binary GB and MB because
@ -180,7 +180,7 @@ func main() {
// to preserve the lock. // to preserve the lock.
lockOSThread() lockOSThread()
if g.m != &m0 { if mp != &m0 {
throw("runtime.main not on m0") throw("runtime.main not on m0")
} }
@ -1468,10 +1468,9 @@ func mPark() {
// //
//go:yeswritebarrierrec //go:yeswritebarrierrec
func mexit(osStack bool) { func mexit(osStack bool) {
g := getg() mp := getg().m
m := g.m
if m == &m0 { if mp == &m0 {
// This is the main thread. Just wedge it. // This is the main thread. Just wedge it.
// //
// On Linux, exiting the main thread puts the process // On Linux, exiting the main thread puts the process
@ -1496,20 +1495,20 @@ func mexit(osStack bool) {
unminit() unminit()
// Free the gsignal stack. // Free the gsignal stack.
if m.gsignal != nil { if mp.gsignal != nil {
stackfree(m.gsignal.stack) stackfree(mp.gsignal.stack)
// On some platforms, when calling into VDSO (e.g. nanotime) // On some platforms, when calling into VDSO (e.g. nanotime)
// we store our g on the gsignal stack, if there is one. // we store our g on the gsignal stack, if there is one.
// Now the stack is freed, unlink it from the m, so we // Now the stack is freed, unlink it from the m, so we
// won't write to it when calling VDSO code. // won't write to it when calling VDSO code.
m.gsignal = nil mp.gsignal = nil
} }
// Remove m from allm. // Remove m from allm.
lock(&sched.lock) lock(&sched.lock)
for pprev := &allm; *pprev != nil; pprev = &(*pprev).alllink { for pprev := &allm; *pprev != nil; pprev = &(*pprev).alllink {
if *pprev == m { if *pprev == mp {
*pprev = m.alllink *pprev = mp.alllink
goto found goto found
} }
} }
@ -1520,17 +1519,17 @@ found:
// //
// If this is using an OS stack, the OS will free it // If this is using an OS stack, the OS will free it
// so there's no need for reaping. // so there's no need for reaping.
atomic.Store(&m.freeWait, 1) atomic.Store(&mp.freeWait, 1)
// Put m on the free list, though it will not be reaped until // Put m on the free list, though it will not be reaped until
// freeWait is 0. Note that the free list must not be linked // freeWait is 0. Note that the free list must not be linked
// through alllink because some functions walk allm without // through alllink because some functions walk allm without
// locking, so may be using alllink. // locking, so may be using alllink.
m.freelink = sched.freem mp.freelink = sched.freem
sched.freem = m sched.freem = mp
} }
unlock(&sched.lock) unlock(&sched.lock)
atomic.Xadd64(&ncgocall, int64(m.ncgocall)) atomic.Xadd64(&ncgocall, int64(mp.ncgocall))
// Release the P. // Release the P.
handoffp(releasep()) handoffp(releasep())
@ -1547,14 +1546,14 @@ found:
if GOOS == "darwin" || GOOS == "ios" { if GOOS == "darwin" || GOOS == "ios" {
// Make sure pendingPreemptSignals is correct when an M exits. // Make sure pendingPreemptSignals is correct when an M exits.
// For #41702. // For #41702.
if atomic.Load(&m.signalPending) != 0 { if atomic.Load(&mp.signalPending) != 0 {
atomic.Xadd(&pendingPreemptSignals, -1) atomic.Xadd(&pendingPreemptSignals, -1)
} }
} }
// Destroy all allocated resources. After this is called, we may no // Destroy all allocated resources. After this is called, we may no
// longer take any locks. // longer take any locks.
mdestroy(m) mdestroy(mp)
if osStack { if osStack {
// Return from mstart and let the system thread // Return from mstart and let the system thread
@ -1566,7 +1565,7 @@ found:
// return to. Exit the thread directly. exitThread will clear // return to. Exit the thread directly. exitThread will clear
// m.freeWait when it's done with the stack and the m can be // m.freeWait when it's done with the stack and the m can be
// reaped. // reaped.
exitThread(&m.freeWait) exitThread(&mp.freeWait)
} }
// forEachP calls fn(p) for every P p when p reaches a GC safe point. // forEachP calls fn(p) for every P p when p reaches a GC safe point.