Take lock when reading clock information. This is to avoid races reported by the go race detector.

This commit is contained in:
Christian Worm Mortensen 2024-04-09 15:10:17 +02:00 committed by Alessandro Boch
parent b54f85093f
commit 03cf170900
1 changed files with 10 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"strings" "strings"
"sync"
"syscall" "syscall"
"github.com/vishvananda/netlink/nl" "github.com/vishvananda/netlink/nl"
@ -688,6 +689,9 @@ var (
tickInUsec float64 tickInUsec float64
clockFactor float64 clockFactor float64
hz float64 hz float64
// Without this, the go race detector may report races.
initClockMutex sync.Mutex
) )
func initClock() { func initClock() {
@ -722,6 +726,8 @@ func initClock() {
} }
func TickInUsec() float64 { func TickInUsec() float64 {
initClockMutex.Lock()
defer initClockMutex.Unlock()
if tickInUsec == 0.0 { if tickInUsec == 0.0 {
initClock() initClock()
} }
@ -729,6 +735,8 @@ func TickInUsec() float64 {
} }
func ClockFactor() float64 { func ClockFactor() float64 {
initClockMutex.Lock()
defer initClockMutex.Unlock()
if clockFactor == 0.0 { if clockFactor == 0.0 {
initClock() initClock()
} }
@ -736,6 +744,8 @@ func ClockFactor() float64 {
} }
func Hz() float64 { func Hz() float64 {
initClockMutex.Lock()
defer initClockMutex.Unlock()
if hz == 0.0 { if hz == 0.0 {
initClock() initClock()
} }