abool/bool.go

102 lines
2.4 KiB
Go
Raw Permalink Normal View History

2016-06-01 13:38:57 +03:00
// Package abool provides atomic Boolean type for cleaner code and
2016-05-25 07:06:29 +03:00
// better performance.
2016-05-25 06:42:19 +03:00
package abool
import (
"encoding/json"
"sync/atomic"
)
2016-05-25 06:42:19 +03:00
2020-07-16 09:07:17 +03:00
// New creates an AtomicBool with default set to false.
2016-05-25 06:42:19 +03:00
func New() *AtomicBool {
return new(AtomicBool)
}
2020-07-16 09:07:17 +03:00
// NewBool creates an AtomicBool with given default value.
2016-05-25 10:06:49 +03:00
func NewBool(ok bool) *AtomicBool {
ab := New()
if ok {
ab.Set()
}
return ab
}
2020-07-16 09:07:17 +03:00
// 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.
2021-09-02 01:50:36 +03:00
type AtomicBool struct {
boolean int32
}
2016-05-25 06:42:19 +03:00
2020-07-16 09:07:17 +03:00
// Set sets the Boolean to true.
2016-05-25 06:42:19 +03:00
func (ab *AtomicBool) Set() {
2021-09-05 18:56:57 +03:00
atomic.StoreInt32(&ab.boolean, 1)
2016-05-25 06:42:19 +03:00
}
2020-07-16 09:07:17 +03:00
// UnSet sets the Boolean to false.
2016-05-25 06:42:19 +03:00
func (ab *AtomicBool) UnSet() {
2021-09-05 18:56:57 +03:00
atomic.StoreInt32(&ab.boolean, 0)
2016-05-25 06:42:19 +03:00
}
2020-07-16 09:07:17 +03:00
// IsSet returns whether the Boolean is true.
2016-05-25 06:42:19 +03:00
func (ab *AtomicBool) IsSet() bool {
2021-09-05 18:56:57 +03:00
return atomic.LoadInt32(&ab.boolean)&1 == 1
2016-05-25 06:42:19 +03:00
}
2016-05-25 09:57:10 +03:00
2020-07-16 09:46:58 +03:00
// IsNotSet returns whether the Boolean is false.
func (ab *AtomicBool) IsNotSet() bool {
return !ab.IsSet()
}
2019-07-22 08:11:38 +03:00
// SetTo sets the boolean with given Boolean.
2016-05-25 09:57:10 +03:00
func (ab *AtomicBool) SetTo(yes bool) {
if yes {
2021-09-05 18:56:57 +03:00
atomic.StoreInt32(&ab.boolean, 1)
2016-05-25 09:57:10 +03:00
} else {
2021-09-05 18:56:57 +03:00
atomic.StoreInt32(&ab.boolean, 0)
2016-05-25 09:57:10 +03:00
}
}
2016-06-02 06:47:41 +03:00
2021-09-02 01:50:36 +03:00
// Toggle inverts the Boolean then returns the value before inverting.
2021-09-05 18:29:37 +03:00
// Based on: https://github.com/uber-go/atomic/blob/3504dfaa1fa414923b1c8693f45d2f6931daf229/bool_ext.go#L40
2021-09-02 01:50:36 +03:00
func (ab *AtomicBool) Toggle() bool {
2021-09-05 18:29:37 +03:00
var old bool
for {
old = ab.IsSet()
if ab.SetToIf(old, !old) {
return old
}
}
2021-09-02 01:50:36 +03:00
}
2020-07-16 09:07:17 +03:00
// SetToIf sets the Boolean to new only if the Boolean matches the old.
// Returns whether the set was done.
2016-06-02 06:47:41 +03:00
func (ab *AtomicBool) SetToIf(old, new bool) (set bool) {
var o, n int32
if old {
o = 1
}
if new {
n = 1
}
2021-09-05 18:56:57 +03:00
return atomic.CompareAndSwapInt32(&ab.boolean, o, n)
2016-06-02 06:47:41 +03:00
}
2021-05-16 16:13:06 +03:00
// MarshalJSON behaves the same as if the AtomicBool is a builtin.bool.
2021-05-15 14:31:02 +03:00
// NOTE: There's no lock during the process, usually it shouldn't be called with other methods in parallel.
func (ab *AtomicBool) MarshalJSON() ([]byte, error) {
return json.Marshal(ab.IsSet())
}
2021-05-16 16:13:06 +03:00
// UnmarshalJSON behaves the same as if the AtomicBool is a builtin.bool.
2021-05-15 14:31:02 +03:00
// NOTE: There's no lock during the process, usually it shouldn't be called with other methods in parallel.
func (ab *AtomicBool) UnmarshalJSON(b []byte) error {
var v bool
2021-05-15 14:31:34 +03:00
err := json.Unmarshal(b, &v)
2021-05-15 14:31:34 +03:00
if err == nil {
ab.SetTo(v)
}
return err
}