mirror of https://github.com/golang/go.git
expvar: convert f to atomic type
For #53821
Change-Id: I2e7c5376e6ca3e3dbb2f92ad771aed62fca8b793
GitHub-Last-Rev: b67ddf81ec
GitHub-Pull-Request: golang/go#54864
Reviewed-on: https://go-review.googlesource.com/c/go/+/428195
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
db259cdd80
commit
d2aa787f2a
|
|
@ -67,26 +67,26 @@ func (v *Int) Set(value int64) {
|
||||||
|
|
||||||
// Float is a 64-bit float variable that satisfies the Var interface.
|
// Float is a 64-bit float variable that satisfies the Var interface.
|
||||||
type Float struct {
|
type Float struct {
|
||||||
f uint64
|
f atomic.Uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Float) Value() float64 {
|
func (v *Float) Value() float64 {
|
||||||
return math.Float64frombits(atomic.LoadUint64(&v.f))
|
return math.Float64frombits(v.f.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Float) String() string {
|
func (v *Float) String() string {
|
||||||
return strconv.FormatFloat(
|
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.
|
// Add adds delta to v.
|
||||||
func (v *Float) Add(delta float64) {
|
func (v *Float) Add(delta float64) {
|
||||||
for {
|
for {
|
||||||
cur := atomic.LoadUint64(&v.f)
|
cur := v.f.Load()
|
||||||
curVal := math.Float64frombits(cur)
|
curVal := math.Float64frombits(cur)
|
||||||
nxtVal := curVal + delta
|
nxtVal := curVal + delta
|
||||||
nxt := math.Float64bits(nxtVal)
|
nxt := math.Float64bits(nxtVal)
|
||||||
if atomic.CompareAndSwapUint64(&v.f, cur, nxt) {
|
if v.f.CompareAndSwap(cur, nxt) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -94,7 +94,7 @@ func (v *Float) Add(delta float64) {
|
||||||
|
|
||||||
// Set sets v to value.
|
// Set sets v to value.
|
||||||
func (v *Float) Set(value float64) {
|
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.
|
// Map is a string-to-Var map variable that satisfies the Var interface.
|
||||||
|
|
|
||||||
|
|
@ -87,8 +87,8 @@ func BenchmarkIntSet(b *testing.B) {
|
||||||
func TestFloat(t *testing.T) {
|
func TestFloat(t *testing.T) {
|
||||||
RemoveAll()
|
RemoveAll()
|
||||||
reqs := NewFloat("requests-float")
|
reqs := NewFloat("requests-float")
|
||||||
if reqs.f != 0.0 {
|
if reqs.f.Load() != 0.0 {
|
||||||
t.Errorf("reqs.f = %v, want 0", reqs.f)
|
t.Errorf("reqs.f = %v, want 0", reqs.f.Load())
|
||||||
}
|
}
|
||||||
if reqs != Get("requests-float").(*Float) {
|
if reqs != Get("requests-float").(*Float) {
|
||||||
t.Errorf("Get() failed.")
|
t.Errorf("Get() failed.")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue