Skip to content

Commit

Permalink
Take lock when reading clock information. This is to avoid races repo…
Browse files Browse the repository at this point in the history
…rted by the go race detector.
  • Loading branch information
Christian Worm Mortensen authored and aboch committed Jul 3, 2024
1 parent b54f850 commit 03cf170
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions qdisc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"strconv"
"strings"
"sync"
"syscall"

"github.com/vishvananda/netlink/nl"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -722,20 +726,26 @@ func initClock() {
}

func TickInUsec() float64 {
initClockMutex.Lock()
defer initClockMutex.Unlock()
if tickInUsec == 0.0 {
initClock()
}
return tickInUsec
}

func ClockFactor() float64 {
initClockMutex.Lock()
defer initClockMutex.Unlock()
if clockFactor == 0.0 {
initClock()
}
return clockFactor
}

func Hz() float64 {
initClockMutex.Lock()
defer initClockMutex.Unlock()
if hz == 0.0 {
initClock()
}
Expand Down

0 comments on commit 03cf170

Please sign in to comment.