Skip to content

expvar: convert f to atomic type #54864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/expvar/expvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,34 @@ func (v *Int) Set(value int64) {

// Float is a 64-bit float variable that satisfies the Var interface.
type Float struct {
f uint64
f atomic.Uint64
}

func (v *Float) Value() float64 {
return math.Float64frombits(atomic.LoadUint64(&v.f))
return math.Float64frombits(v.f.Load())
}

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

// Add adds delta to v.
func (v *Float) Add(delta float64) {
for {
cur := atomic.LoadUint64(&v.f)
cur := v.f.Load()
curVal := math.Float64frombits(cur)
nxtVal := curVal + delta
nxt := math.Float64bits(nxtVal)
if atomic.CompareAndSwapUint64(&v.f, cur, nxt) {
if v.f.CompareAndSwap(cur, nxt) {
return
}
}
}

// Set sets v to value.
func (v *Float) Set(value float64) {
atomic.StoreUint64(&v.f, math.Float64bits(value))
v.f.Store(math.Float64bits(value))
}

// Map is a string-to-Var map variable that satisfies the Var interface.
Expand Down
4 changes: 2 additions & 2 deletions src/expvar/expvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func BenchmarkIntSet(b *testing.B) {
func TestFloat(t *testing.T) {
RemoveAll()
reqs := NewFloat("requests-float")
if reqs.f != 0.0 {
t.Errorf("reqs.f = %v, want 0", reqs.f)
if reqs.f.Load() != 0.0 {
t.Errorf("reqs.f = %v, want 0", reqs.f.Load())
}
if reqs != Get("requests-float").(*Float) {
t.Errorf("Get() failed.")
Expand Down