From: cuiweixie Date: Thu, 25 Aug 2022 03:10:52 +0000 (+0800) Subject: runtime: convert mcache.flushGen to atomic type X-Git-Tag: go1.20rc1~1329 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a25a34abe986bd78ab9a543d2a96cbce427a4f3c;p=gostls13.git runtime: convert mcache.flushGen to atomic type For #53821 Change-Id: I90ab52a45b7fb6b9e3ff1d6ea97251549306c7aa Reviewed-on: https://go-review.googlesource.com/c/go/+/425435 Reviewed-by: Michael Pratt Reviewed-by: Michael Knyszek Run-TryBot: xie cui <523516579@qq.com> TryBot-Result: Gopher Robot --- diff --git a/src/runtime/mcache.go b/src/runtime/mcache.go index e01a99bd6e..ba45034943 100644 --- a/src/runtime/mcache.go +++ b/src/runtime/mcache.go @@ -50,7 +50,7 @@ type mcache struct { // was last flushed. If flushGen != mheap_.sweepgen, the spans // in this mcache are stale and need to the flushed so they // can be swept. This is done in acquirep. - flushGen uint32 + flushGen atomic.Uint32 } // A gclink is a node in a linked list of blocks, like mlink, @@ -87,7 +87,7 @@ func allocmcache() *mcache { systemstack(func() { lock(&mheap_.lock) c = (*mcache)(mheap_.cachealloc.alloc()) - c.flushGen = mheap_.sweepgen + c.flushGen.Store(mheap_.sweepgen) unlock(&mheap_.lock) }) for i := range c.alloc { @@ -318,13 +318,14 @@ func (c *mcache) prepareForSweep() { // allocate-black. However, with this approach it's difficult // to avoid spilling mark bits into the *next* GC cycle. sg := mheap_.sweepgen - if c.flushGen == sg { + flushGen := c.flushGen.Load() + if flushGen == sg { return - } else if c.flushGen != sg-2 { - println("bad flushGen", c.flushGen, "in prepareForSweep; sweepgen", sg) + } else if flushGen != sg-2 { + println("bad flushGen", flushGen, "in prepareForSweep; sweepgen", sg) throw("bad flushGen") } c.releaseAll() stackcache_clear(c) - atomic.Store(&c.flushGen, mheap_.sweepgen) // Synchronizes with gcStart + c.flushGen.Store(mheap_.sweepgen) // Synchronizes with gcStart } diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index 8021a56b9a..3243a15b4d 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -636,7 +636,7 @@ func gcStart(trigger gcTrigger) { // Check that all Ps have finished deferred mcache flushes. for _, p := range allp { - if fg := atomic.Load(&p.mcache.flushGen); fg != mheap_.sweepgen { + if fg := p.mcache.flushGen.Load(); fg != mheap_.sweepgen { println("runtime: p", p.id, "flushGen", fg, "!= sweepgen", mheap_.sweepgen) throw("p mcache not flushed") }