Skip to content

Commit

Permalink
unlock mtx right after we select true indices
Browse files Browse the repository at this point in the history
  • Loading branch information
melekes committed Oct 4, 2018
1 parent 481b5b6 commit 5268d68
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions libs/common/bit_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,16 @@ func (bA *BitArray) PickRandom() (int, bool) {
if bA == nil {
return 0, false
}
bA.mtx.Lock()

bA.mtx.Lock()
trueIndices := bA.getTrueIndices()
// no bits set to true
if len(trueIndices) == 0 {
bA.mtx.Unlock()

if len(trueIndices) == 0 { // no bits set to true
return 0, false
}
index := trueIndices[RandIntn(len(trueIndices))]

bA.mtx.Unlock()
return index, true
return trueIndices[RandIntn(len(trueIndices))], true
}

func (bA *BitArray) getTrueIndices() []int {
Expand Down

0 comments on commit 5268d68

Please sign in to comment.