Merge pull request #278 from slingamn/bitset_again

simplify CAS logic a bit
This commit is contained in:
Daniel Oaks 2018-07-12 20:05:26 +10:00 committed by GitHub
commit 1a5db02236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 5 deletions

View File

@ -37,17 +37,15 @@ func BitsetSet(set []uint64, position uint, on bool) (changed bool) {
mask = 1 << bit
for {
current := atomic.LoadUint64(addr)
previouslyOn := (current & mask) != 0
if on == previouslyOn {
return false
}
var desired uint64
if on {
desired = current | mask
} else {
desired = current & (^mask)
}
if atomic.CompareAndSwapUint64(addr, current, desired) {
if current == desired {
return false
} else if atomic.CompareAndSwapUint64(addr, current, desired) {
return true
}
}