Skip to content

Commit

Permalink
cmd/go/internal: use sync.OnceFunc, sync.OnceValue
Browse files Browse the repository at this point in the history
Cleaner code, less global variables

Change-Id: I6d842932e538849260b36fa408bc5ddae68c05ab
Reviewed-on: https://go-review.googlesource.com/c/go/+/611018
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 6, 2024
1 parent 4777fd3 commit d1ce116
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 39 deletions.
10 changes: 3 additions & 7 deletions src/cmd/go/internal/base/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
"cmd/go/internal/str"
)

var cwd string
var cwdOnce sync.Once

// UncachedCwd returns the current working directory.
// Most callers should use Cwd, which caches the result for future use.
// UncachedCwd is appropriate to call early in program startup before flag parsing,
Expand All @@ -30,12 +27,11 @@ func UncachedCwd() string {
return wd
}

var cwdOnce = sync.OnceValue(UncachedCwd)

// Cwd returns the current working directory at the time of the first call.
func Cwd() string {
cwdOnce.Do(func() {
cwd = UncachedCwd()
})
return cwd
return cwdOnce()
}

// ShortPath returns an absolute or relative name for path, whatever is shorter.
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/internal/base/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func processSignals() {
}()
}

var onceProcessSignals sync.Once
var processSignalsOnce = sync.OnceFunc(processSignals)

// StartSigHandlers starts the signal handlers.
func StartSigHandlers() {
onceProcessSignals.Do(processSignals)
processSignalsOnce()
}
16 changes: 6 additions & 10 deletions src/cmd/go/internal/cache/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ import (
// Default returns the default cache to use.
// It never returns nil.
func Default() Cache {
defaultOnce.Do(initDefaultCache)
return defaultCache
return initDefaultCacheOnce()
}

var (
defaultOnce sync.Once
defaultCache Cache
)
var initDefaultCacheOnce = sync.OnceValue(initDefaultCache)

// cacheREADME is a message stored in a README in the cache directory.
// Because the cache lives outside the normal Go trees, we leave the
Expand All @@ -38,7 +34,7 @@ See golang.org to learn more about Go.

// initDefaultCache does the work of finding the default cache
// the first time Default is called.
func initDefaultCache() {
func initDefaultCache() Cache {
dir, _ := DefaultDir()
if dir == "off" {
if defaultDirErr != nil {
Expand All @@ -60,10 +56,10 @@ func initDefaultCache() {
}

if v := cfg.Getenv("GOCACHEPROG"); v != "" && goexperiment.CacheProg {
defaultCache = startCacheProg(v, diskCache)
} else {
defaultCache = diskCache
return startCacheProg(v, diskCache)
}

return diskCache
}

var (
Expand Down
26 changes: 8 additions & 18 deletions src/cmd/go/internal/imports/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@ import (
"sync"
)

var (
tags map[string]bool
tagsOnce sync.Once
)

// Tags returns a set of build tags that are true for the target platform.
// It includes GOOS, GOARCH, the compiler, possibly "cgo",
// release tags like "go1.13", and user-specified build tags.
func Tags() map[string]bool {
tagsOnce.Do(func() {
tags = loadTags()
})
return tags
return loadTagsOnce()
}

var loadTagsOnce = sync.OnceValue(loadTags)

func loadTags() map[string]bool {
tags := map[string]bool{
cfg.BuildContext.GOOS: true,
Expand All @@ -45,17 +39,13 @@ func loadTags() map[string]bool {
return tags
}

var (
anyTags map[string]bool
anyTagsOnce sync.Once
)

// AnyTags returns a special set of build tags that satisfy nearly all
// build tag expressions. Only "ignore" and malformed build tag requirements
// are considered false.
func AnyTags() map[string]bool {
anyTagsOnce.Do(func() {
anyTags = map[string]bool{"*": true}
})
return anyTags
return anyTagsOnce()
}

var anyTagsOnce = sync.OnceValue(func() map[string]bool {
return map[string]bool{"*": true}
})
4 changes: 2 additions & 2 deletions src/cmd/go/internal/work/buildid.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ var (
counterCacheHit = counter.New("go/buildcache/hit")
counterCacheMiss = counter.New("go/buildcache/miss")

onceIncStdlibRecompiled sync.Once
stdlibRecompiled = counter.New("go/buildcache/stdlib-recompiled")
stdlibRecompiledIncOnce = sync.OnceFunc(stdlibRecompiled.Inc)
)

// useCache tries to satisfy the action a, which has action ID actionHash,
Expand Down Expand Up @@ -467,7 +467,7 @@ func (b *Builder) useCache(a *Action, actionHash cache.ActionID, target string,
counterCacheHit.Inc()
} else {
if a.Package != nil && a.Package.Standard {
onceIncStdlibRecompiled.Do(stdlibRecompiled.Inc)
stdlibRecompiledIncOnce()
}
counterCacheMiss.Inc()
}
Expand Down

0 comments on commit d1ce116

Please sign in to comment.