github.com/go-eden/common@v0.1.15-0.20210617133546-059099253264/esync/sync_atomic_int.go (about) 1 package esync 2 3 import ( 4 "fmt" 5 "sync/atomic" 6 ) 7 8 type AtomicInt struct { 9 value int64 10 } 11 12 func (t *AtomicInt) Inc() int { 13 return t.Add(1) 14 } 15 16 func (t *AtomicInt) Add(v int) int { 17 return int(atomic.AddInt64(&t.value, int64(v))) 18 } 19 20 func (t *AtomicInt) Set(v int) { 21 atomic.StoreInt64(&t.value, int64(v)) 22 } 23 24 func (t *AtomicInt) Get() int { 25 return int(atomic.LoadInt64(&t.value)) 26 } 27 28 func (t *AtomicInt) Swap(v int) int { 29 return int(atomic.SwapInt64(&t.value, int64(v))) 30 } 31 32 func (t *AtomicInt) String() string { 33 return fmt.Sprint(t.Get()) 34 }