Add Bool.CAS for compare-and-swap semantics (#23)

This commit is contained in:
Prashant Varanasi
2017-05-09 12:57:10 -07:00
committed by GitHub
parent 1d7f075bbe
commit 3f020e6d5d
3 changed files with 14 additions and 0 deletions

View File

@@ -232,6 +232,11 @@ func (b *Bool) Load() bool {
return truthy(atomic.LoadUint32(&b.v))
}
// CAS is an atomic compare-and-swap.
func (b *Bool) CAS(old, new bool) bool {
return atomic.CompareAndSwapUint32(&b.v, boolToInt(old), boolToInt(new))
}
// Store atomically stores the passed value.
func (b *Bool) Store(new bool) {
atomic.StoreUint32(&b.v, boolToInt(new))