Merge "Add OncePer.Get()"

This commit is contained in:
Colin Cross
2017-11-10 22:48:38 +00:00
committed by Gerrit Code Review

View File

@@ -15,12 +15,12 @@
package android package android
import ( import (
"fmt"
"sync" "sync"
"sync/atomic"
) )
type OncePer struct { type OncePer struct {
values atomic.Value values sync.Map
valuesLock sync.Mutex valuesLock sync.Mutex
} }
@@ -29,33 +29,32 @@ type valueMap map[interface{}]interface{}
// Once computes a value the first time it is called with a given key per OncePer, and returns the // Once computes a value the first time it is called with a given key per OncePer, and returns the
// value without recomputing when called with the same key. key must be hashable. // value without recomputing when called with the same key. key must be hashable.
func (once *OncePer) Once(key interface{}, value func() interface{}) interface{} { func (once *OncePer) Once(key interface{}, value func() interface{}) interface{} {
// Atomically load the map without locking. If this is the first call Load() will return nil // Fast path: check if the key is already in the map
// and the type assertion will fail, leaving a nil map in m, but that's OK since m is only used if v, ok := once.values.Load(key); ok {
// for reads.
m, _ := once.values.Load().(valueMap)
if v, ok := m[key]; ok {
return v return v
} }
// Slow path: lock so that we don't call the value function twice concurrently
once.valuesLock.Lock() once.valuesLock.Lock()
defer once.valuesLock.Unlock() defer once.valuesLock.Unlock()
// Check again with the lock held // Check again with the lock held
m, _ = once.values.Load().(valueMap) if v, ok := once.values.Load(key); ok {
if v, ok := m[key]; ok {
return v return v
} }
// Copy the existing map // Still not in the map, call the value function and store it
newMap := make(valueMap, len(m))
for k, v := range m {
newMap[k] = v
}
v := value() v := value()
once.values.Store(key, v)
newMap[key] = v return v
once.values.Store(newMap) }
func (once *OncePer) Get(key interface{}) interface{} {
v, ok := once.values.Load(key)
if !ok {
panic(fmt.Errorf("Get() called before Once()"))
}
return v return v
} }