Add OncePer.Get()
Allow functions to get the result associated with a OncePer key without also specifiying a function. Panics if the key has not already been set. Also replace the open-coded concurrent map implementation with the new sync.Map. Test: m checkbuild Change-Id: I814fdb1ffffaee8398dc877af146e29638c8a6a8
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user