1
0
mirror of https://github.com/golang/go synced 2024-11-15 00:20:30 -07:00

internal/sysinfo: use sync.OnceValue for CPUName

Change-Id: I0f3ae97f2bd5ff3f533c5bf4570a8cda8b92b16a
Reviewed-on: https://go-review.googlesource.com/c/go/+/582836
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
Tobias Klauser 2024-05-03 13:14:00 +02:00 committed by Gopher Robot
parent 44e48c7e6c
commit 0bc093a1aa

View File

@ -11,25 +11,14 @@ import (
"sync"
)
var cpuInfo struct {
once sync.Once
name string
}
var CPUName = sync.OnceValue(func() string {
if name := cpu.Name(); name != "" {
return name
}
func CPUName() string {
cpuInfo.once.Do(func() {
// Try to get the information from internal/cpu.
if name := cpu.Name(); name != "" {
cpuInfo.name = name
return
}
if name := osCpuInfoName(); name != "" {
return name
}
// TODO(martisch): use /proc/cpuinfo and /sys/devices/system/cpu/ on Linux as fallback.
if name := osCpuInfoName(); name != "" {
cpuInfo.name = name
return
}
})
return cpuInfo.name
}
return ""
})