mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-10 22:19:31 +01:00
Merge pull request #278 from slingamn/bitset_again
simplify CAS logic a bit
This commit is contained in:
commit
1a5db02236
@ -37,17 +37,15 @@ func BitsetSet(set []uint64, position uint, on bool) (changed bool) {
|
|||||||
mask = 1 << bit
|
mask = 1 << bit
|
||||||
for {
|
for {
|
||||||
current := atomic.LoadUint64(addr)
|
current := atomic.LoadUint64(addr)
|
||||||
previouslyOn := (current & mask) != 0
|
|
||||||
if on == previouslyOn {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
var desired uint64
|
var desired uint64
|
||||||
if on {
|
if on {
|
||||||
desired = current | mask
|
desired = current | mask
|
||||||
} else {
|
} else {
|
||||||
desired = current & (^mask)
|
desired = current & (^mask)
|
||||||
}
|
}
|
||||||
if atomic.CompareAndSwapUint64(addr, current, desired) {
|
if current == desired {
|
||||||
|
return false
|
||||||
|
} else if atomic.CompareAndSwapUint64(addr, current, desired) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user