]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: use sync.OnceFunc for copyenv
authorTobias Klauser <tklauser@distanz.ch>
Sat, 22 Feb 2025 20:56:36 +0000 (21:56 +0100)
committerGopher Robot <gobot@golang.org>
Sun, 23 Feb 2025 19:53:16 +0000 (11:53 -0800)
Change-Id: I64f658c1962878685ba7736f19d58e10fbdcb94a
Reviewed-on: https://go-review.googlesource.com/c/go/+/651835
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/syscall/env_unix.go

index 1144ed1416c0ee5e36b36f59ca870931efee5ee9..256048f6ff1ccb09d7e4f0fc02bcf202187cbaa1 100644 (file)
@@ -14,9 +14,6 @@ import (
 )
 
 var (
-       // envOnce guards initialization by copyenv, which populates env.
-       envOnce sync.Once
-
        // envLock guards env and envs.
        envLock sync.RWMutex
 
@@ -31,7 +28,7 @@ var (
 
 func runtime_envs() []string // in package runtime
 
-func copyenv() {
+var copyenv = sync.OnceFunc(func() {
        env = make(map[string]int)
        for i, s := range envs {
                for j := 0; j < len(s); j++ {
@@ -50,10 +47,10 @@ func copyenv() {
                        }
                }
        }
-}
+})
 
 func Unsetenv(key string) error {
-       envOnce.Do(copyenv)
+       copyenv()
 
        envLock.Lock()
        defer envLock.Unlock()
@@ -67,7 +64,7 @@ func Unsetenv(key string) error {
 }
 
 func Getenv(key string) (value string, found bool) {
-       envOnce.Do(copyenv)
+       copyenv()
        if len(key) == 0 {
                return "", false
        }
@@ -89,7 +86,7 @@ func Getenv(key string) (value string, found bool) {
 }
 
 func Setenv(key, value string) error {
-       envOnce.Do(copyenv)
+       copyenv()
        if len(key) == 0 {
                return EINVAL
        }
@@ -124,7 +121,7 @@ func Setenv(key, value string) error {
 }
 
 func Clearenv() {
-       envOnce.Do(copyenv)
+       copyenv()
 
        envLock.Lock()
        defer envLock.Unlock()
@@ -137,7 +134,7 @@ func Clearenv() {
 }
 
 func Environ() []string {
-       envOnce.Do(copyenv)
+       copyenv()
        envLock.RLock()
        defer envLock.RUnlock()
        a := make([]string, 0, len(envs))