Commit 63e383cf authored by David Symonds's avatar David Symonds

expvar: add locking to String, and use RWMutex properly throughout.

R=bradfitz
CC=golang-dev
https://golang.org/cl/5754043
parent 5e46a8c9
...@@ -41,12 +41,12 @@ type Var interface { ...@@ -41,12 +41,12 @@ type Var interface {
// Int is a 64-bit integer variable that satisfies the Var interface. // Int is a 64-bit integer variable that satisfies the Var interface.
type Int struct { type Int struct {
i int64 i int64
mu sync.Mutex mu sync.RWMutex
} }
func (v *Int) String() string { func (v *Int) String() string {
v.mu.Lock() v.mu.RLock()
defer v.mu.Unlock() defer v.mu.RUnlock()
return strconv.FormatInt(v.i, 10) return strconv.FormatInt(v.i, 10)
} }
...@@ -65,12 +65,12 @@ func (v *Int) Set(value int64) { ...@@ -65,12 +65,12 @@ 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 float64 f float64
mu sync.Mutex mu sync.RWMutex
} }
func (v *Float) String() string { func (v *Float) String() string {
v.mu.Lock() v.mu.RLock()
defer v.mu.Unlock() defer v.mu.RUnlock()
return strconv.FormatFloat(v.f, 'g', -1, 64) return strconv.FormatFloat(v.f, 'g', -1, 64)
} }
...@@ -188,12 +188,21 @@ func (v *Map) Do(f func(KeyValue)) { ...@@ -188,12 +188,21 @@ func (v *Map) Do(f func(KeyValue)) {
// String is a string variable, and satisfies the Var interface. // String is a string variable, and satisfies the Var interface.
type String struct { type String struct {
s string s string
mu sync.RWMutex
} }
func (v *String) String() string { return strconv.Quote(v.s) } func (v *String) String() string {
v.mu.RLock()
defer v.mu.RUnlock()
return strconv.Quote(v.s)
}
func (v *String) Set(value string) { v.s = value } func (v *String) Set(value string) {
v.mu.Lock()
defer v.mu.Unlock()
v.s = value
}
// Func implements Var by calling the function // Func implements Var by calling the function
// and formatting the returned value using JSON. // and formatting the returned value using JSON.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment