aboutsummaryrefslogtreecommitdiff
path: root/src/syscall
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2025-02-22 21:56:36 +0100
committerGopher Robot <gobot@golang.org>2025-02-23 11:53:16 -0800
commitcf875b8af8fcb1f4bbc106c2d9cdb1a8ed46b878 (patch)
treeb7926c517b7d42d6226ab79eb0de4640a4d7aa59 /src/syscall
parent4dfae6aad54b9e399e5144c9b58a9c87a8afbe56 (diff)
downloadgo-cf875b8af8fcb1f4bbc106c2d9cdb1a8ed46b878.tar.xz
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>
Diffstat (limited to 'src/syscall')
-rw-r--r--src/syscall/env_unix.go17
1 files 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))