From cf875b8af8fcb1f4bbc106c2d9cdb1a8ed46b878 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sat, 22 Feb 2025 21:56:36 +0100 Subject: [PATCH] syscall: use sync.OnceFunc for copyenv Change-Id: I64f658c1962878685ba7736f19d58e10fbdcb94a Reviewed-on: https://go-review.googlesource.com/c/go/+/651835 Auto-Submit: Tobias Klauser Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor --- src/syscall/env_unix.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/syscall/env_unix.go b/src/syscall/env_unix.go index 1144ed1416..256048f6ff 100644 --- a/src/syscall/env_unix.go +++ b/src/syscall/env_unix.go @@ -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)) -- 2.50.0