Refine comments

This commit is contained in:
Tevin Zhang 2020-07-16 14:07:17 +08:00
parent e76b19742c
commit 3a33536c80
No known key found for this signature in database
GPG Key ID: EE7DA2A50F0960FB
1 changed files with 11 additions and 13 deletions

24
bool.go
View File

@ -4,12 +4,12 @@ package abool
import "sync/atomic" import "sync/atomic"
// New creates an AtomicBool with default to false // New creates an AtomicBool with default set to false.
func New() *AtomicBool { func New() *AtomicBool {
return new(AtomicBool) return new(AtomicBool)
} }
// NewBool creates an AtomicBool with given default value // NewBool creates an AtomicBool with given default value.
func NewBool(ok bool) *AtomicBool { func NewBool(ok bool) *AtomicBool {
ab := New() ab := New()
if ok { if ok {
@ -18,24 +18,22 @@ func NewBool(ok bool) *AtomicBool {
return ab return ab
} }
// AtomicBool is an atomic Boolean // AtomicBool is an atomic Boolean.
// Its methods are all atomic, thus safe to be called by // Its methods are all atomic, thus safe to be called by multiple goroutines simultaneously.
// multiple goroutines simultaneously // Note: When embedding into a struct one should always use *AtomicBool to avoid copy.
// Note: When embedding into a struct, one should always use
// *AtomicBool to avoid copy
type AtomicBool int32 type AtomicBool int32
// Set sets the Boolean to true // Set sets the Boolean to true.
func (ab *AtomicBool) Set() { func (ab *AtomicBool) Set() {
atomic.StoreInt32((*int32)(ab), 1) atomic.StoreInt32((*int32)(ab), 1)
} }
// UnSet sets the Boolean to false // UnSet sets the Boolean to false.
func (ab *AtomicBool) UnSet() { func (ab *AtomicBool) UnSet() {
atomic.StoreInt32((*int32)(ab), 0) atomic.StoreInt32((*int32)(ab), 0)
} }
// IsSet returns whether the Boolean is true // IsSet returns whether the Boolean is true.
func (ab *AtomicBool) IsSet() bool { func (ab *AtomicBool) IsSet() bool {
return atomic.LoadInt32((*int32)(ab))&1 == 1 return atomic.LoadInt32((*int32)(ab))&1 == 1
} }
@ -49,13 +47,13 @@ func (ab *AtomicBool) SetTo(yes bool) {
} }
} }
// Toggle inverts the boolean then returns the value before inverting. // Toggle inverts the Boolean then returns the value before inverting.
func (ab *AtomicBool) Toggle() bool { func (ab *AtomicBool) Toggle() bool {
return atomic.AddInt32((*int32)(ab), 1)&1 == 0 return atomic.AddInt32((*int32)(ab), 1)&1 == 0
} }
// SetToIf sets the Boolean to new only if the Boolean matches the old // SetToIf sets the Boolean to new only if the Boolean matches the old.
// Returns whether the set was done // Returns whether the set was done.
func (ab *AtomicBool) SetToIf(old, new bool) (set bool) { func (ab *AtomicBool) SetToIf(old, new bool) (set bool) {
var o, n int32 var o, n int32
if old { if old {