From: cuiweixie Date: Fri, 26 Aug 2022 03:24:39 +0000 (+0800) Subject: runtime: convert consistentHeapStats.gen to atomic type X-Git-Tag: go1.20rc1~1313 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=52d9e7f543f68a46d7a69443a1ad51283d2f0d4f;p=gostls13.git runtime: convert consistentHeapStats.gen to atomic type For #53821 Change-Id: I9f57b84f6a2c29d750fb20420daef903a9311a83 Reviewed-on: https://go-review.googlesource.com/c/go/+/425781 Run-TryBot: Michael Knyszek Reviewed-by: Michael Knyszek Reviewed-by: Michael Pratt TryBot-Result: Gopher Robot --- diff --git a/src/runtime/mstats.go b/src/runtime/mstats.go index 70c5a703e7..d4ef933611 100644 --- a/src/runtime/mstats.go +++ b/src/runtime/mstats.go @@ -727,8 +727,7 @@ type consistentHeapStats struct { // gen represents the current index into which writers // are writing, and can take on the value of 0, 1, or 2. - // This value is updated atomically. - gen uint32 + gen atomic.Uint32 // noPLock is intended to provide mutual exclusion for updating // stats when no P is available. It does not block other writers @@ -766,7 +765,7 @@ func (m *consistentHeapStats) acquire() *heapStatsDelta { } else { lock(&m.noPLock) } - gen := atomic.Load(&m.gen) % 3 + gen := m.gen.Load() % 3 return &m.stats[gen] } @@ -837,7 +836,7 @@ func (m *consistentHeapStats) read(out *heapStatsDelta) { // Get the current generation. We can be confident that this // will not change since read is serialized and is the only // one that modifies currGen. - currGen := atomic.Load(&m.gen) + currGen := m.gen.Load() prevGen := currGen - 1 if currGen == 0 { prevGen = 2 @@ -852,7 +851,7 @@ func (m *consistentHeapStats) read(out *heapStatsDelta) { // // This exchange is safe to do because we won't race // with anyone else trying to update this value. - atomic.Xchg(&m.gen, (currGen+1)%3) + m.gen.Swap((currGen + 1) % 3) // Allow P-less writers to continue. They'll be writing to the // next generation now.