Prechádzať zdrojové kódy

lib/ur: Prevent trivial race in CPU bench

Jakob Borg 6 rokov pred
rodič
commit
0832285d79
1 zmenil súbory, kde vykonal 9 pridanie a 2 odobranie
  1. 9 2
      lib/ur/usage_report.go

+ 9 - 2
lib/ur/usage_report.go

@@ -17,6 +17,7 @@ import (
 	"runtime"
 	"sort"
 	"strings"
+	"sync"
 	"time"
 
 	"github.com/syncthing/syncthing/lib/build"
@@ -425,8 +426,16 @@ func (*Service) String() string {
 	return "ur.Service"
 }
 
+var (
+	blocksResult    []protocol.BlockInfo // so the result is not optimized away
+	blocksResultMut sync.Mutex
+)
+
 // CpuBench returns CPU performance as a measure of single threaded SHA-256 MiB/s
 func CpuBench(iterations int, duration time.Duration, useWeakHash bool) float64 {
+	blocksResultMut.Lock()
+	defer blocksResultMut.Unlock()
+
 	dataSize := 16 * protocol.MinBlockSize
 	bs := make([]byte, dataSize)
 	rand.Reader.Read(bs)
@@ -441,8 +450,6 @@ func CpuBench(iterations int, duration time.Duration, useWeakHash bool) float64
 	return perf
 }
 
-var blocksResult []protocol.BlockInfo // so the result is not optimized away
-
 func cpuBenchOnce(duration time.Duration, useWeakHash bool, bs []byte) float64 {
 	t0 := time.Now()
 	b := 0