github.com/number571/tendermint@v0.34.11-gost/libs/sync/atomic_bool.go (about) 1 package sync 2 3 import "sync/atomic" 4 5 // AtomicBool is an atomic Boolean. 6 // Its methods are all atomic, thus safe to be called by multiple goroutines simultaneously. 7 // Note: When embedding into a struct one should always use *AtomicBool to avoid copy. 8 // it's a simple implmentation from https://github.com/tevino/abool 9 type AtomicBool int32 10 11 // NewBool creates an AtomicBool with given default value. 12 func NewBool(ok bool) *AtomicBool { 13 ab := new(AtomicBool) 14 if ok { 15 ab.Set() 16 } 17 return ab 18 } 19 20 // Set sets the Boolean to true. 21 func (ab *AtomicBool) Set() { 22 atomic.StoreInt32((*int32)(ab), 1) 23 } 24 25 // UnSet sets the Boolean to false. 26 func (ab *AtomicBool) UnSet() { 27 atomic.StoreInt32((*int32)(ab), 0) 28 } 29 30 // IsSet returns whether the Boolean is true. 31 func (ab *AtomicBool) IsSet() bool { 32 return atomic.LoadInt32((*int32)(ab))&1 == 1 33 }