github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xsync/xatomic/atomic_integer.go (about) 1 package xatomic 2 3 import ( 4 "sync/atomic" 5 ) 6 7 // AtomicInt32 is an atomic wrapper around an int32. 8 type AtomicInt32 struct{ v int32 } 9 10 // NewAtomicInt32 creates an AtomicInt32. 11 func NewAtomicInt32(i int32) *AtomicInt32 { 12 return &AtomicInt32{i} 13 } 14 15 // Load atomically loads the wrapped value. 16 func (i *AtomicInt32) Load() int32 { 17 return atomic.LoadInt32(&i.v) 18 } 19 20 // Add atomically adds to the wrapped int32 and returns the new value. 21 func (i *AtomicInt32) Add(n int32) int32 { 22 return atomic.AddInt32(&i.v, n) 23 } 24 25 // Sub atomically subtracts from the wrapped int32 and returns the new value. 26 func (i *AtomicInt32) Sub(n int32) int32 { 27 return atomic.AddInt32(&i.v, -n) 28 } 29 30 // Inc atomically increments the wrapped int32 and returns the new value. 31 func (i *AtomicInt32) Inc() int32 { 32 return i.Add(1) 33 } 34 35 // Dec atomically decrements the wrapped int32 and returns the new value. 36 func (i *AtomicInt32) Dec() int32 { 37 return i.Sub(1) 38 } 39 40 // CAS is an atomic compare-and-swap. 41 func (i *AtomicInt32) CAS(old, new int32) bool { 42 return atomic.CompareAndSwapInt32(&i.v, old, new) 43 } 44 45 // Store atomically stores the passed value. 46 func (i *AtomicInt32) Store(n int32) { 47 atomic.StoreInt32(&i.v, n) 48 } 49 50 // Swap atomically swaps the wrapped int32 and returns the old value. 51 func (i *AtomicInt32) Swap(n int32) int32 { 52 return atomic.SwapInt32(&i.v, n) 53 } 54 55 // AtomicInt64 is an atomic wrapper around an int64. 56 type AtomicInt64 struct{ v int64 } 57 58 // NewAtomicInt64 creates an AtomicInt64. 59 func NewAtomicInt64(i int64) *AtomicInt64 { 60 return &AtomicInt64{i} 61 } 62 63 // Load atomically loads the wrapped value. 64 func (i *AtomicInt64) Load() int64 { 65 return atomic.LoadInt64(&i.v) 66 } 67 68 // Add atomically adds to the wrapped int64 and returns the new value. 69 func (i *AtomicInt64) Add(n int64) int64 { 70 return atomic.AddInt64(&i.v, n) 71 } 72 73 // Sub atomically subtracts from the wrapped int64 and returns the new value. 74 func (i *AtomicInt64) Sub(n int64) int64 { 75 return atomic.AddInt64(&i.v, -n) 76 } 77 78 // Inc atomically increments the wrapped int64 and returns the new value. 79 func (i *AtomicInt64) Inc() int64 { 80 return i.Add(1) 81 } 82 83 // Dec atomically decrements the wrapped int64 and returns the new value. 84 func (i *AtomicInt64) Dec() int64 { 85 return i.Sub(1) 86 } 87 88 // CAS is an atomic compare-and-swap. 89 func (i *AtomicInt64) CAS(old, new int64) bool { 90 return atomic.CompareAndSwapInt64(&i.v, old, new) 91 } 92 93 // Store atomically stores the passed value. 94 func (i *AtomicInt64) Store(n int64) { 95 atomic.StoreInt64(&i.v, n) 96 } 97 98 // Swap atomically swaps the wrapped int64 and returns the old value. 99 func (i *AtomicInt64) Swap(n int64) int64 { 100 return atomic.SwapInt64(&i.v, n) 101 } 102 103 // AtomicUint32 is an atomic wrapper around an uint32. 104 type AtomicUint32 struct{ v uint32 } 105 106 // NewAtomicUint32 creates a AtomicUint32. 107 func NewAtomicUint32(i uint32) *AtomicUint32 { 108 return &AtomicUint32{i} 109 } 110 111 // Load atomically loads the wrapped value. 112 func (i *AtomicUint32) Load() uint32 { 113 return atomic.LoadUint32(&i.v) 114 } 115 116 // Add atomically adds to the wrapped uint32 and returns the new value. 117 func (i *AtomicUint32) Add(n uint32) uint32 { 118 return atomic.AddUint32(&i.v, n) 119 } 120 121 // Sub atomically subtracts from the wrapped uint32 and returns the new value. 122 func (i *AtomicUint32) Sub(n uint32) uint32 { 123 return atomic.AddUint32(&i.v, ^(n - 1)) 124 } 125 126 // Inc atomically increments the wrapped uint32 and returns the new value. 127 func (i *AtomicUint32) Inc() uint32 { 128 return i.Add(1) 129 } 130 131 // Dec atomically decrements the wrapped int32 and returns the new value. 132 func (i *AtomicUint32) Dec() uint32 { 133 return i.Sub(1) 134 } 135 136 // CAS is an atomic compare-and-swap. 137 func (i *AtomicUint32) CAS(old, new uint32) bool { 138 return atomic.CompareAndSwapUint32(&i.v, old, new) 139 } 140 141 // Store atomically stores the passed value. 142 func (i *AtomicUint32) Store(n uint32) { 143 atomic.StoreUint32(&i.v, n) 144 } 145 146 // Swap atomically swaps the wrapped uint32 and returns the old value. 147 func (i *AtomicUint32) Swap(n uint32) uint32 { 148 return atomic.SwapUint32(&i.v, n) 149 } 150 151 // AtomicUint64 is an atomic wrapper around a uint64. 152 type AtomicUint64 struct{ v uint64 } 153 154 // NewAtomicUint64 creates a AtomicUint64. 155 func NewAtomicUint64(i uint64) *AtomicUint64 { 156 return &AtomicUint64{i} 157 } 158 159 // Load atomically loads the wrapped value. 160 func (i *AtomicUint64) Load() uint64 { 161 return atomic.LoadUint64(&i.v) 162 } 163 164 // Add atomically adds to the wrapped uint64 and returns the new value. 165 func (i *AtomicUint64) Add(n uint64) uint64 { 166 return atomic.AddUint64(&i.v, n) 167 } 168 169 // Sub atomically subtracts from the wrapped uint64 and returns the new value. 170 func (i *AtomicUint64) Sub(n uint64) uint64 { 171 return atomic.AddUint64(&i.v, ^(n - 1)) 172 } 173 174 // Inc atomically increments the wrapped uint64 and returns the new value. 175 func (i *AtomicUint64) Inc() uint64 { 176 return i.Add(1) 177 } 178 179 // Dec atomically decrements the wrapped uint64 and returns the new value. 180 func (i *AtomicUint64) Dec() uint64 { 181 return i.Sub(1) 182 } 183 184 // CAS is an atomic compare-and-swap. 185 func (i *AtomicUint64) CAS(old, new uint64) bool { 186 return atomic.CompareAndSwapUint64(&i.v, old, new) 187 } 188 189 // Store atomically stores the passed value. 190 func (i *AtomicUint64) Store(n uint64) { 191 atomic.StoreUint64(&i.v, n) 192 } 193 194 // Swap atomically swaps the wrapped uint64 and returns the old value. 195 func (i *AtomicUint64) Swap(n uint64) uint64 { 196 return atomic.SwapUint64(&i.v, n) 197 }