github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/others/sys/atomic.go (about) 1 package sys 2 3 import ( 4 "fmt" 5 "sync/atomic" 6 ) 7 8 type SyncBool struct { 9 val int32 10 } 11 12 func (b *SyncBool) Get() bool { 13 return atomic.LoadInt32(&b.val) != 0 14 } 15 16 func (b *SyncBool) Set() { 17 atomic.StoreInt32(&b.val, 1) 18 } 19 20 func (b *SyncBool) Clr() { 21 atomic.StoreInt32(&b.val, 0) 22 } 23 24 func (b *SyncBool) MarshalText() (text []byte, err error) { 25 return []byte(fmt.Sprint(b.Get())), nil 26 } 27 28 func (b *SyncBool) Store(val bool) { 29 if val { 30 b.Set() 31 } else { 32 b.Clr() 33 } 34 } 35 36 37 type SyncInt struct { 38 val int64 39 } 40 41 func (b *SyncInt) Get() int { 42 return int(atomic.LoadInt64(&b.val)) 43 } 44 45 func (b *SyncInt) Store(val int) { 46 atomic.StoreInt64(&b.val, int64(val)) 47 } 48 49 func (b *SyncInt) Add(val int) { 50 atomic.AddInt64(&b.val, int64(val)) 51 } 52 53 func (b *SyncInt) MarshalText() (text []byte, err error) { 54 return []byte(fmt.Sprint(b.Get())), nil 55 }