Skip to content

Commit

Permalink
internal/sysinfo: use sync.OnceValue for CPUName
Browse files Browse the repository at this point in the history
Change-Id: I0f3ae97f2bd5ff3f533c5bf4570a8cda8b92b16a
Reviewed-on: https://go-review.googlesource.com/c/go/+/582836
Reviewed-by: David Chase <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Tobias Klauser <[email protected]>
  • Loading branch information
tklauser authored and gopherbot committed May 3, 2024
1 parent 44e48c7 commit 0bc093a
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/internal/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
})

0 comments on commit 0bc093a

Please sign in to comment.