]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal: use sync.OnceFunc, sync.OnceValue
authorKir Kolyshkin <kolyshkin@gmail.com>
Thu, 5 Sep 2024 00:16:03 +0000 (17:16 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 6 Sep 2024 13:19:58 +0000 (13:19 +0000)
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>
src/cmd/go/internal/base/path.go
src/cmd/go/internal/base/signal.go
src/cmd/go/internal/cache/default.go
src/cmd/go/internal/imports/tags.go
src/cmd/go/internal/work/buildid.go

index c17b14e67b397ac47f9ab60618ed4d7fbfe374cf..1c9dace54a82e886b052ad1fd2d1496f22b4bed8 100644 (file)
@@ -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,
@@ -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.
index 05befcf7f0c8eb84b07d9aee3b3f489fe04d6be3..c15dd47b7d57148d2fce4b43f950f3002b776bd4 100644 (file)
@@ -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()
 }
index 5430d9651ec1d7d117d5330e7f11e95d52810348..b0f442517358ebb8e2477da0fc87c6a89d6c5024 100644 (file)
@@ -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
@@ -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 {
@@ -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 (
index d1467b81b0b66083a3542ca0b27f9829500eb682..42b25af7b6fe00862af3961245d69bc975f5fbca 100644 (file)
@@ -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,
@@ -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}
+})
index 2134079f839ab1fff3aae4f6f97405629e58bb71..7ec3f94d9d361e834d0e8b9aabcce4cdd126431a 100644 (file)
@@ -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,
@@ -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()
                }