From 4988a65f2a4d53c93a9dd19e6d70181529a115a4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Mart=C3=AD?= Date: Sun, 12 Nov 2023 10:04:15 +0000 Subject: [PATCH] all: make more use of the new atomic integer types Slightly simplifies the code and avoids human error. Change-Id: Ib76575e8bc5b3a699ba6cc3870d63cd7a55e6416 Reviewed-on: https://go-review.googlesource.com/c/go/+/541476 Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Reviewed-by: Bryan Mills --- src/cmd/compile/internal/types2/typeparam.go | 4 ++-- src/cmd/go/internal/trace/trace.go | 8 ++++---- src/expvar/expvar.go | 10 +++++----- src/go/types/typeparam.go | 4 ++-- src/internal/fuzz/pcg.go | 4 ++-- src/sync/export_test.go | 2 +- src/sync/poolqueue.go | 14 +++++++------- src/testing/benchmark.go | 14 +++++++------- 8 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/cmd/compile/internal/types2/typeparam.go b/src/cmd/compile/internal/types2/typeparam.go index 42b1a63915..5c6030b3fb 100644 --- a/src/cmd/compile/internal/types2/typeparam.go +++ b/src/cmd/compile/internal/types2/typeparam.go @@ -9,11 +9,11 @@ import "sync/atomic" // Note: This is a uint32 rather than a uint64 because the // respective 64 bit atomic instructions are not available // on all platforms. -var lastID uint32 +var lastID atomic.Uint32 // nextID returns a value increasing monotonically by 1 with // each call, starting with 1. It may be called concurrently. -func nextID() uint64 { return uint64(atomic.AddUint32(&lastID, 1)) } +func nextID() uint64 { return uint64(lastID.Add(1)) } // A TypeParam represents a type parameter type. type TypeParam struct { diff --git a/src/cmd/go/internal/trace/trace.go b/src/cmd/go/internal/trace/trace.go index d69dc4feac..17d3ee9e7f 100644 --- a/src/cmd/go/internal/trace/trace.go +++ b/src/cmd/go/internal/trace/trace.go @@ -121,8 +121,8 @@ func (s *Span) Done() { type tracer struct { file chan traceFile // 1-buffered - nextTID uint64 - nextFlowID uint64 + nextTID atomic.Uint64 + nextFlowID atomic.Uint64 } func (t *tracer) writeEvent(ev *traceviewer.Event) error { @@ -161,11 +161,11 @@ func (t *tracer) Close() error { } func (t *tracer) getNextTID() uint64 { - return atomic.AddUint64(&t.nextTID, 1) + return t.nextTID.Add(1) } func (t *tracer) getNextFlowID() uint64 { - return atomic.AddUint64(&t.nextFlowID, 1) + return t.nextFlowID.Add(1) } // traceKey is the context key for tracing information. It is unexported to prevent collisions with context keys defined in diff --git a/src/expvar/expvar.go b/src/expvar/expvar.go index 32e855f6c5..954d63d17f 100644 --- a/src/expvar/expvar.go +++ b/src/expvar/expvar.go @@ -50,11 +50,11 @@ type jsonVar interface { // Int is a 64-bit integer variable that satisfies the [Var] interface. type Int struct { - i int64 + i atomic.Int64 } func (v *Int) Value() int64 { - return atomic.LoadInt64(&v.i) + return v.i.Load() } func (v *Int) String() string { @@ -62,15 +62,15 @@ func (v *Int) String() string { } func (v *Int) appendJSON(b []byte) []byte { - return strconv.AppendInt(b, atomic.LoadInt64(&v.i), 10) + return strconv.AppendInt(b, v.i.Load(), 10) } func (v *Int) Add(delta int64) { - atomic.AddInt64(&v.i, delta) + v.i.Add(delta) } func (v *Int) Set(value int64) { - atomic.StoreInt64(&v.i, value) + v.i.Store(value) } // Float is a 64-bit float variable that satisfies the [Var] interface. diff --git a/src/go/types/typeparam.go b/src/go/types/typeparam.go index b23601dc3f..a13f86c213 100644 --- a/src/go/types/typeparam.go +++ b/src/go/types/typeparam.go @@ -11,11 +11,11 @@ import "sync/atomic" // Note: This is a uint32 rather than a uint64 because the // respective 64 bit atomic instructions are not available // on all platforms. -var lastID uint32 +var lastID atomic.Uint32 // nextID returns a value increasing monotonically by 1 with // each call, starting with 1. It may be called concurrently. -func nextID() uint64 { return uint64(atomic.AddUint32(&lastID, 1)) } +func nextID() uint64 { return uint64(lastID.Add(1)) } // A TypeParam represents a type parameter type. type TypeParam struct { diff --git a/src/internal/fuzz/pcg.go b/src/internal/fuzz/pcg.go index c9ea0afcf8..4fe8aeb50c 100644 --- a/src/internal/fuzz/pcg.go +++ b/src/internal/fuzz/pcg.go @@ -30,7 +30,7 @@ type mutatorRand interface { // creation and use, no reproducibility, no concurrency safety, just the // necessary methods, optimized for speed. -var globalInc uint64 // PCG stream +var globalInc atomic.Uint64 // PCG stream const multiplier uint64 = 6364136223846793005 @@ -63,7 +63,7 @@ func newPcgRand() *pcgRand { if seed := godebugSeed(); seed != nil { now = uint64(*seed) } - inc := atomic.AddUint64(&globalInc, 1) + inc := globalInc.Add(1) r.state = now r.inc = (inc << 1) | 1 r.step() diff --git a/src/sync/export_test.go b/src/sync/export_test.go index c020ef737d..b55cecd987 100644 --- a/src/sync/export_test.go +++ b/src/sync/export_test.go @@ -23,7 +23,7 @@ func NewPoolDequeue(n int) PoolDequeue { } // For testing purposes, set the head and tail indexes close // to wrapping around. - d.headTail = d.pack(1<