mirror of
https://github.com/golang/go
synced 2024-09-24 13:20:12 -06:00
expvar: add locking to String, and use RWMutex properly throughout.
R=bradfitz CC=golang-dev https://golang.org/cl/5754043
This commit is contained in:
parent
5e46a8c9f9
commit
63e383cff8
@ -41,12 +41,12 @@ type Var interface {
|
||||
// Int is a 64-bit integer variable that satisfies the Var interface.
|
||||
type Int struct {
|
||||
i int64
|
||||
mu sync.Mutex
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func (v *Int) String() string {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
v.mu.RLock()
|
||||
defer v.mu.RUnlock()
|
||||
return strconv.FormatInt(v.i, 10)
|
||||
}
|
||||
|
||||
@ -65,12 +65,12 @@ func (v *Int) Set(value int64) {
|
||||
// Float is a 64-bit float variable that satisfies the Var interface.
|
||||
type Float struct {
|
||||
f float64
|
||||
mu sync.Mutex
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func (v *Float) String() string {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
v.mu.RLock()
|
||||
defer v.mu.RUnlock()
|
||||
return strconv.FormatFloat(v.f, 'g', -1, 64)
|
||||
}
|
||||
|
||||
@ -189,11 +189,20 @@ func (v *Map) Do(f func(KeyValue)) {
|
||||
// String is a string variable, and satisfies the Var interface.
|
||||
type String struct {
|
||||
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
|
||||
// and formatting the returned value using JSON.
|
||||
|
Loading…
Reference in New Issue
Block a user