1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
6 // This file is copied from cmd/go/internal/par.
15 // parCache runs an action once per key and caches the result.
16 type parCache struct {
20 type cacheEntry struct {
26 // Do calls the function f if and only if Do is being called for the first time with this key.
27 // No call to Do with a given key returns until the one call to f returns.
28 // Do returns the value returned by the one call to f.
29 func (c *parCache) Do(key interface{}, f func() interface{}) interface{} {
30 entryIface, ok := c.m.Load(key)
32 entryIface, _ = c.m.LoadOrStore(key, new(cacheEntry))
34 e := entryIface.(*cacheEntry)
35 if atomic.LoadUint32(&e.done) == 0 {
37 if atomic.LoadUint32(&e.done) == 0 {
39 atomic.StoreUint32(&e.done, 1)
46 // Get returns the cached result associated with key.
47 // It returns nil if there is no such result.
48 // If the result for key is being computed, Get does not wait for the computation to finish.
49 func (c *parCache) Get(key interface{}) interface{} {
50 entryIface, ok := c.m.Load(key)
54 e := entryIface.(*cacheEntry)
55 if atomic.LoadUint32(&e.done) == 0 {