Skip to content

Commit

Permalink
Merge pull request #63 from apocelipes/master
Browse files Browse the repository at this point in the history
fix data race
  • Loading branch information
dbaggerman authored Mar 25, 2019
2 parents ccee1a5 + 2fd03c6 commit b92cfa5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
14 changes: 5 additions & 9 deletions processor/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime/debug"
"strings"
"sync"
"sync/atomic"
)

// Used as quick lookup for files with the same name to avoid some processing
Expand Down Expand Up @@ -71,8 +72,7 @@ func walkDirectoryParallel(root string, output chan *FileJob) {
extensionLookup = wlExtensionLookup
}

var mutex = &sync.Mutex{}
totalCount := 0
var totalCount int64 = 0

var wg sync.WaitGroup

Expand Down Expand Up @@ -136,12 +136,10 @@ func walkDirectoryParallel(root string, output chan *FileJob) {
output <- &filejobs[i]
}

mutex.Lock()
totalCount += len(filejobs)
mutex.Unlock()
atomic.AddInt64(&totalCount, int64(len(filejobs)))

// Turn GC back to what it was before if we have parsed enough files
if !resetGc && totalCount >= GcFileCount {
if !resetGc && atomic.LoadInt64(&totalCount) >= int64(GcFileCount) {
debug.SetGCPercent(gcPercent)
resetGc = true
}
Expand Down Expand Up @@ -183,9 +181,7 @@ func walkDirectoryParallel(root string, output chan *FileJob) {
}

if ok {
mutex.Lock()
totalCount++
mutex.Unlock()
atomic.AddInt64(&totalCount, 1)

for _, l := range language {
LoadLanguageFeature(l)
Expand Down
9 changes: 3 additions & 6 deletions processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
)

// The below are used as identifiers for the code state machine
Expand Down Expand Up @@ -474,9 +475,7 @@ func fileReaderWorker(input chan *FileJob, output chan *FileJob) {
wg.Add(1)
go func() {
for res := range input {
if startTime == 0 {
startTime = makeTimestampMilli()
}
atomic.CompareAndSwapInt64(&startTime, 0, makeTimestampMilli())

fileStartTime := makeTimestampNano()
content, err := ioutil.ReadFile(res.Location)
Expand Down Expand Up @@ -521,9 +520,7 @@ func fileProcessorWorker(input chan *FileJob, output chan *FileJob) {
wg.Add(1)
go func() {
for res := range input {
if startTime == 0 {
startTime = makeTimestampMilli()
}
atomic.CompareAndSwapInt64(&startTime, 0, makeTimestampMilli())

fileStartTime := makeTimestampNano()
CountStats(res)
Expand Down

0 comments on commit b92cfa5

Please sign in to comment.