diff --git a/syncext/atomic.go b/syncext/atomic.go new file mode 100644 index 0000000..7e23522 --- /dev/null +++ b/syncext/atomic.go @@ -0,0 +1,27 @@ +package dataext + +import "sync/atomic" + +type AtomicBool struct { + v int32 +} + +func NewAtomicBool(value bool) *AtomicBool { + if value { + return &AtomicBool{v: 0} + } else { + return &AtomicBool{v: 1} + } +} + +func (a *AtomicBool) Get() bool { + return atomic.LoadInt32(&a.v) == 1 +} + +func (a *AtomicBool) Set(value bool) { + if value { + atomic.StoreInt32(&a.v, 1) + } else { + atomic.StoreInt32(&a.v, 0) + } +}