Fix small data races

This commit is contained in:
Jakob Borg 2014-04-27 21:33:57 +02:00
parent 76ef42ee07
commit 89f5f3bf9a
2 changed files with 12 additions and 5 deletions

View File

@ -1,6 +1,10 @@
package main package main
import "github.com/calmh/syncthing/scanner" import (
"sync/atomic"
"github.com/calmh/syncthing/scanner"
)
type bqAdd struct { type bqAdd struct {
file scanner.File file scanner.File
@ -20,6 +24,7 @@ type blockQueue struct {
outbox chan bqBlock outbox chan bqBlock
queued []bqBlock queued []bqBlock
qlen uint32
} }
func newBlockQueue() *blockQueue { func newBlockQueue() *blockQueue {
@ -77,6 +82,7 @@ func (q *blockQueue) run() {
q.queued = q.queued[1:] q.queued = q.queued[1:]
} }
} }
atomic.StoreUint32(&q.qlen, uint32(len(q.queued)))
} }
} }
@ -89,6 +95,7 @@ func (q *blockQueue) get() bqBlock {
} }
func (q *blockQueue) empty() bool { func (q *blockQueue) empty() bool {
// There is a race condition here. We're only mostly sure the queue is empty if the expression below is true. var l uint32
return len(q.queued) == 0 && len(q.inbox) == 0 && len(q.outbox) == 0 atomic.LoadUint32(&l)
return l == 0
} }

View File

@ -108,8 +108,8 @@ func (m *Set) Need(id uint) []scanner.File {
if debug { if debug {
dlog.Printf("Need(%d)", id) dlog.Printf("Need(%d)", id)
} }
var fs = make([]scanner.File, 0, len(m.globalKey)/2) // Just a guess, but avoids too many reallocations
m.Lock() m.Lock()
var fs = make([]scanner.File, 0, len(m.globalKey)/2) // Just a guess, but avoids too many reallocations
rkID := m.remoteKey[id] rkID := m.remoteKey[id]
for gk, gf := range m.files { for gk, gf := range m.files {
if !gf.Global { if !gf.Global {
@ -145,8 +145,8 @@ func (m *Set) Global() []scanner.File {
if debug { if debug {
dlog.Printf("Global()") dlog.Printf("Global()")
} }
var fs = make([]scanner.File, 0, len(m.globalKey))
m.Lock() m.Lock()
var fs = make([]scanner.File, 0, len(m.globalKey))
for _, file := range m.files { for _, file := range m.files {
if file.Global { if file.Global {
fs = append(fs, file.File) fs = append(fs, file.File)