From 03cf170900a47e66b256f8466c05d850e027bd79 Mon Sep 17 00:00:00 2001 From: Christian Worm Mortensen Date: Tue, 9 Apr 2024 15:10:17 +0200 Subject: [PATCH] Take lock when reading clock information. This is to avoid races reported by the go race detector. --- qdisc_linux.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/qdisc_linux.go b/qdisc_linux.go index 3c3780d..9c53885 100644 --- a/qdisc_linux.go +++ b/qdisc_linux.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "strconv" "strings" + "sync" "syscall" "github.com/vishvananda/netlink/nl" @@ -688,6 +689,9 @@ var ( tickInUsec float64 clockFactor float64 hz float64 + + // Without this, the go race detector may report races. + initClockMutex sync.Mutex ) func initClock() { @@ -722,6 +726,8 @@ func initClock() { } func TickInUsec() float64 { + initClockMutex.Lock() + defer initClockMutex.Unlock() if tickInUsec == 0.0 { initClock() } @@ -729,6 +735,8 @@ func TickInUsec() float64 { } func ClockFactor() float64 { + initClockMutex.Lock() + defer initClockMutex.Unlock() if clockFactor == 0.0 { initClock() } @@ -736,6 +744,8 @@ func ClockFactor() float64 { } func Hz() float64 { + initClockMutex.Lock() + defer initClockMutex.Unlock() if hz == 0.0 { initClock() }