add example code. add sync.onlyOnce

This commit is contained in:
Jeff Carr 2024-11-28 23:00:29 -06:00
parent a9e35d5429
commit 488550a081
3 changed files with 68 additions and 0 deletions

58
cache.go Normal file
View File

@ -0,0 +1,58 @@
// I put this here to look at and think about
// from mod/sumdb/cache.go
// Parallel cache.
// This file is copied from cmd/go/internal/par.
package forgepb
import (
"sync"
"sync/atomic"
)
// parCache runs an action once per key and caches the result.
type parCache struct {
m sync.Map
}
type cacheEntry struct {
done uint32
mu sync.Mutex
result interface{}
}
// Do calls the function f if and only if Do is being called for the first time with this key.
// No call to Do with a given key returns until the one call to f returns.
// Do returns the value returned by the one call to f.
func (c *parCache) Do(key interface{}, f func() interface{}) interface{} {
entryIface, ok := c.m.Load(key)
if !ok {
entryIface, _ = c.m.LoadOrStore(key, new(cacheEntry))
}
e := entryIface.(*cacheEntry)
if atomic.LoadUint32(&e.done) == 0 {
e.mu.Lock()
if atomic.LoadUint32(&e.done) == 0 {
e.result = f()
atomic.StoreUint32(&e.done, 1)
}
e.mu.Unlock()
}
return e.result
}
// Get returns the cached result associated with key.
// It returns nil if there is no such result.
// If the result for key is being computed, Get does not wait for the computation to finish.
func (c *parCache) Get(key interface{}) interface{} {
entryIface, ok := c.m.Load(key)
if !ok {
return nil
}
e := entryIface.(*cacheEntry)
if atomic.LoadUint32(&e.done) == 0 {
return nil
}
return e.result
}

View File

@ -24,6 +24,10 @@ func init() {
func Init() *Forge { func Init() *Forge {
f := new(Forge) f := new(Forge)
// cache.go has Do()
// f.initOnce.Do(f.initWork)
f.Config = new(ForgeConfigs) f.Config = new(ForgeConfigs)
// load the ~/.config/forge/ config // load the ~/.config/forge/ config

View File

@ -1,12 +1,18 @@
package forgepb package forgepb
import ( import (
sync "sync"
"go.wit.com/lib/protobuf/gitpb" "go.wit.com/lib/protobuf/gitpb"
"go.wit.com/lib/protobuf/zoopb" "go.wit.com/lib/protobuf/zoopb"
) )
// maybe an interface someday? // maybe an interface someday?
type Forge struct { type Forge struct {
// one-time initialized data
initOnce sync.Once
initErr error // init error, if any
goSrc string // the path to go/src goSrc string // the path to go/src
Config *ForgeConfigs // config repos for readonly, private, etc Config *ForgeConfigs // config repos for readonly, private, etc
Repos *gitpb.Repos Repos *gitpb.Repos