Skip to content

Commit b67ddf8

Browse files
committed
expvar: convert f to atomic type
Signed-off-by: cui fliter <[email protected]>
1 parent 535fe2b commit b67ddf8

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/expvar/expvar.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,34 @@ func (v *Int) Set(value int64) {
6767

6868
// Float is a 64-bit float variable that satisfies the Var interface.
6969
type Float struct {
70-
f uint64
70+
f atomic.Uint64
7171
}
7272

7373
func (v *Float) Value() float64 {
74-
return math.Float64frombits(atomic.LoadUint64(&v.f))
74+
return math.Float64frombits(v.f.Load())
7575
}
7676

7777
func (v *Float) String() string {
7878
return strconv.FormatFloat(
79-
math.Float64frombits(atomic.LoadUint64(&v.f)), 'g', -1, 64)
79+
math.Float64frombits(v.f.Load()), 'g', -1, 64)
8080
}
8181

8282
// Add adds delta to v.
8383
func (v *Float) Add(delta float64) {
8484
for {
85-
cur := atomic.LoadUint64(&v.f)
85+
cur := v.f.Load()
8686
curVal := math.Float64frombits(cur)
8787
nxtVal := curVal + delta
8888
nxt := math.Float64bits(nxtVal)
89-
if atomic.CompareAndSwapUint64(&v.f, cur, nxt) {
89+
if v.f.CompareAndSwap(cur, nxt) {
9090
return
9191
}
9292
}
9393
}
9494

9595
// Set sets v to value.
9696
func (v *Float) Set(value float64) {
97-
atomic.StoreUint64(&v.f, math.Float64bits(value))
97+
v.f.Store(math.Float64bits(value))
9898
}
9999

100100
// Map is a string-to-Var map variable that satisfies the Var interface.

src/expvar/expvar_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func BenchmarkIntSet(b *testing.B) {
8787
func TestFloat(t *testing.T) {
8888
RemoveAll()
8989
reqs := NewFloat("requests-float")
90-
if reqs.f != 0.0 {
91-
t.Errorf("reqs.f = %v, want 0", reqs.f)
90+
if reqs.f.Load() != 0.0 {
91+
t.Errorf("reqs.f = %v, want 0", reqs.f.Load())
9292
}
9393
if reqs != Get("requests-float").(*Float) {
9494
t.Errorf("Get() failed.")

0 commit comments

Comments
 (0)