Documentation ¶
Overview ¶
Package abool provides atomic Boolean type for cleaner code and better performance.
Index ¶
- type AtomicBool
- func (ab *AtomicBool) IsNotSet() bool
- func (ab *AtomicBool) IsSet() bool
- func (ab *AtomicBool) MarshalJSON() ([]byte, error)
- func (ab *AtomicBool) Set()
- func (ab *AtomicBool) SetTo(yes bool)
- func (ab *AtomicBool) SetToIf(old, new bool) (set bool)
- func (ab *AtomicBool) UnSet()
- func (ab *AtomicBool) UnmarshalJSON(b []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AtomicBool ¶
type AtomicBool int32
AtomicBool is an atomic Boolean. Its methods are all atomic, thus safe to be called by multiple goroutines simultaneously. Note: When embedding into a struct one should always use *AtomicBool to avoid copy.
Example ¶
cond := New() // default to false any := true old := any new := !any cond.Set() // Sets to true cond.IsSet() // Returns true cond.UnSet() // Sets to false cond.IsNotSet() // Returns true cond.SetTo(any) // Sets to whatever you want cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
Output:
func NewBool ¶
func NewBool(ok bool) *AtomicBool
NewBool creates an AtomicBool with given default value.
func (*AtomicBool) IsNotSet ¶
func (ab *AtomicBool) IsNotSet() bool
IsNotSet returns whether the Boolean is false.
func (*AtomicBool) IsSet ¶
func (ab *AtomicBool) IsSet() bool
IsSet returns whether the Boolean is true.
func (*AtomicBool) MarshalJSON ¶
func (ab *AtomicBool) MarshalJSON() ([]byte, error)
MarshalJSON behaves the same as if the AtomicBool is a builtin.bool. NOTE: There's no lock during the process, usually it shouldn't be called with other methods in parallel.
func (*AtomicBool) SetTo ¶
func (ab *AtomicBool) SetTo(yes bool)
SetTo sets the boolean with given Boolean.
func (*AtomicBool) SetToIf ¶
func (ab *AtomicBool) SetToIf(old, new bool) (set bool)
SetToIf sets the Boolean to new only if the Boolean matches the old. Returns whether the set was done.
func (*AtomicBool) UnmarshalJSON ¶
func (ab *AtomicBool) UnmarshalJSON(b []byte) error
UnmarshalJSON behaves the same as if the AtomicBool is a builtin.bool. NOTE: There's no lock during the process, usually it shouldn't be called with other methods in parallel.