Refine flip method, may be not atomic yet

This commit is contained in:
barryz 2018-09-08 20:31:55 +08:00
parent 5a88366efc
commit a8aae64969
1 changed files with 5 additions and 1 deletions

View File

@ -51,7 +51,11 @@ func (ab *AtomicBool) SetTo(yes bool) {
// Flip toggles the Boolean (replaces with its opposite value).
func (ab *AtomicBool) Flip() {
atomic.StoreInt32((*int32)(ab), atomic.LoadInt32((*int32)(ab))^1)
var o, n int32
o, n = 0, 1
if !atomic.CompareAndSwapInt32((*int32)(ab), o, n) {
atomic.CompareAndSwapInt32((*int32)(ab), n, o)
}
}
// SetToIf sets the Boolean to new only if the Boolean matches the old