syscall: use sync.OnceFunc for copyenv

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>
This commit is contained in:
Tobias Klauser 2025-02-22 21:56:36 +01:00 committed by Gopher Robot
parent 4dfae6aad5
commit cf875b8af8
1 changed files with 7 additions and 10 deletions

View 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))