1
0
mirror of https://github.com/golang/go synced 2024-11-26 14:26:51 -07:00

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 <mknyszek@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
cuiweixie 2022-08-26 11:24:39 +08:00 committed by Michael Pratt
parent ce77a46405
commit 52d9e7f543

View File

@ -727,8 +727,7 @@ type consistentHeapStats struct {
// gen represents the current index into which writers // gen represents the current index into which writers
// are writing, and can take on the value of 0, 1, or 2. // are writing, and can take on the value of 0, 1, or 2.
// This value is updated atomically. gen atomic.Uint32
gen uint32
// noPLock is intended to provide mutual exclusion for updating // noPLock is intended to provide mutual exclusion for updating
// stats when no P is available. It does not block other writers // stats when no P is available. It does not block other writers
@ -766,7 +765,7 @@ func (m *consistentHeapStats) acquire() *heapStatsDelta {
} else { } else {
lock(&m.noPLock) lock(&m.noPLock)
} }
gen := atomic.Load(&m.gen) % 3 gen := m.gen.Load() % 3
return &m.stats[gen] return &m.stats[gen]
} }
@ -837,7 +836,7 @@ func (m *consistentHeapStats) read(out *heapStatsDelta) {
// Get the current generation. We can be confident that this // Get the current generation. We can be confident that this
// will not change since read is serialized and is the only // will not change since read is serialized and is the only
// one that modifies currGen. // one that modifies currGen.
currGen := atomic.Load(&m.gen) currGen := m.gen.Load()
prevGen := currGen - 1 prevGen := currGen - 1
if currGen == 0 { if currGen == 0 {
prevGen = 2 prevGen = 2
@ -852,7 +851,7 @@ func (m *consistentHeapStats) read(out *heapStatsDelta) {
// //
// This exchange is safe to do because we won't race // This exchange is safe to do because we won't race
// with anyone else trying to update this value. // 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 // Allow P-less writers to continue. They'll be writing to the
// next generation now. // next generation now.