khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 1 | // Copyright (c) 2016 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | // Package atomic provides simple wrappers around numerics to enforce atomic |
| 22 | // access. |
| 23 | package atomic |
| 24 | |
| 25 | import ( |
| 26 | "math" |
| 27 | "sync/atomic" |
| 28 | ) |
| 29 | |
| 30 | // Int32 is an atomic wrapper around an int32. |
| 31 | type Int32 struct{ v int32 } |
| 32 | |
| 33 | // NewInt32 creates an Int32. |
| 34 | func NewInt32(i int32) *Int32 { |
| 35 | return &Int32{i} |
| 36 | } |
| 37 | |
| 38 | // Load atomically loads the wrapped value. |
| 39 | func (i *Int32) Load() int32 { |
| 40 | return atomic.LoadInt32(&i.v) |
| 41 | } |
| 42 | |
| 43 | // Add atomically adds to the wrapped int32 and returns the new value. |
| 44 | func (i *Int32) Add(n int32) int32 { |
| 45 | return atomic.AddInt32(&i.v, n) |
| 46 | } |
| 47 | |
| 48 | // Sub atomically subtracts from the wrapped int32 and returns the new value. |
| 49 | func (i *Int32) Sub(n int32) int32 { |
| 50 | return atomic.AddInt32(&i.v, -n) |
| 51 | } |
| 52 | |
| 53 | // Inc atomically increments the wrapped int32 and returns the new value. |
| 54 | func (i *Int32) Inc() int32 { |
| 55 | return i.Add(1) |
| 56 | } |
| 57 | |
| 58 | // Dec atomically decrements the wrapped int32 and returns the new value. |
| 59 | func (i *Int32) Dec() int32 { |
| 60 | return i.Sub(1) |
| 61 | } |
| 62 | |
| 63 | // CAS is an atomic compare-and-swap. |
| 64 | func (i *Int32) CAS(old, new int32) bool { |
| 65 | return atomic.CompareAndSwapInt32(&i.v, old, new) |
| 66 | } |
| 67 | |
| 68 | // Store atomically stores the passed value. |
| 69 | func (i *Int32) Store(n int32) { |
| 70 | atomic.StoreInt32(&i.v, n) |
| 71 | } |
| 72 | |
| 73 | // Swap atomically swaps the wrapped int32 and returns the old value. |
| 74 | func (i *Int32) Swap(n int32) int32 { |
| 75 | return atomic.SwapInt32(&i.v, n) |
| 76 | } |
| 77 | |
| 78 | // Int64 is an atomic wrapper around an int64. |
| 79 | type Int64 struct{ v int64 } |
| 80 | |
| 81 | // NewInt64 creates an Int64. |
| 82 | func NewInt64(i int64) *Int64 { |
| 83 | return &Int64{i} |
| 84 | } |
| 85 | |
| 86 | // Load atomically loads the wrapped value. |
| 87 | func (i *Int64) Load() int64 { |
| 88 | return atomic.LoadInt64(&i.v) |
| 89 | } |
| 90 | |
| 91 | // Add atomically adds to the wrapped int64 and returns the new value. |
| 92 | func (i *Int64) Add(n int64) int64 { |
| 93 | return atomic.AddInt64(&i.v, n) |
| 94 | } |
| 95 | |
| 96 | // Sub atomically subtracts from the wrapped int64 and returns the new value. |
| 97 | func (i *Int64) Sub(n int64) int64 { |
| 98 | return atomic.AddInt64(&i.v, -n) |
| 99 | } |
| 100 | |
| 101 | // Inc atomically increments the wrapped int64 and returns the new value. |
| 102 | func (i *Int64) Inc() int64 { |
| 103 | return i.Add(1) |
| 104 | } |
| 105 | |
| 106 | // Dec atomically decrements the wrapped int64 and returns the new value. |
| 107 | func (i *Int64) Dec() int64 { |
| 108 | return i.Sub(1) |
| 109 | } |
| 110 | |
| 111 | // CAS is an atomic compare-and-swap. |
| 112 | func (i *Int64) CAS(old, new int64) bool { |
| 113 | return atomic.CompareAndSwapInt64(&i.v, old, new) |
| 114 | } |
| 115 | |
| 116 | // Store atomically stores the passed value. |
| 117 | func (i *Int64) Store(n int64) { |
| 118 | atomic.StoreInt64(&i.v, n) |
| 119 | } |
| 120 | |
| 121 | // Swap atomically swaps the wrapped int64 and returns the old value. |
| 122 | func (i *Int64) Swap(n int64) int64 { |
| 123 | return atomic.SwapInt64(&i.v, n) |
| 124 | } |
| 125 | |
| 126 | // Uint32 is an atomic wrapper around an uint32. |
| 127 | type Uint32 struct{ v uint32 } |
| 128 | |
| 129 | // NewUint32 creates a Uint32. |
| 130 | func NewUint32(i uint32) *Uint32 { |
| 131 | return &Uint32{i} |
| 132 | } |
| 133 | |
| 134 | // Load atomically loads the wrapped value. |
| 135 | func (i *Uint32) Load() uint32 { |
| 136 | return atomic.LoadUint32(&i.v) |
| 137 | } |
| 138 | |
| 139 | // Add atomically adds to the wrapped uint32 and returns the new value. |
| 140 | func (i *Uint32) Add(n uint32) uint32 { |
| 141 | return atomic.AddUint32(&i.v, n) |
| 142 | } |
| 143 | |
| 144 | // Sub atomically subtracts from the wrapped uint32 and returns the new value. |
| 145 | func (i *Uint32) Sub(n uint32) uint32 { |
| 146 | return atomic.AddUint32(&i.v, ^(n - 1)) |
| 147 | } |
| 148 | |
| 149 | // Inc atomically increments the wrapped uint32 and returns the new value. |
| 150 | func (i *Uint32) Inc() uint32 { |
| 151 | return i.Add(1) |
| 152 | } |
| 153 | |
| 154 | // Dec atomically decrements the wrapped int32 and returns the new value. |
| 155 | func (i *Uint32) Dec() uint32 { |
| 156 | return i.Sub(1) |
| 157 | } |
| 158 | |
| 159 | // CAS is an atomic compare-and-swap. |
| 160 | func (i *Uint32) CAS(old, new uint32) bool { |
| 161 | return atomic.CompareAndSwapUint32(&i.v, old, new) |
| 162 | } |
| 163 | |
| 164 | // Store atomically stores the passed value. |
| 165 | func (i *Uint32) Store(n uint32) { |
| 166 | atomic.StoreUint32(&i.v, n) |
| 167 | } |
| 168 | |
| 169 | // Swap atomically swaps the wrapped uint32 and returns the old value. |
| 170 | func (i *Uint32) Swap(n uint32) uint32 { |
| 171 | return atomic.SwapUint32(&i.v, n) |
| 172 | } |
| 173 | |
| 174 | // Uint64 is an atomic wrapper around a uint64. |
| 175 | type Uint64 struct{ v uint64 } |
| 176 | |
| 177 | // NewUint64 creates a Uint64. |
| 178 | func NewUint64(i uint64) *Uint64 { |
| 179 | return &Uint64{i} |
| 180 | } |
| 181 | |
| 182 | // Load atomically loads the wrapped value. |
| 183 | func (i *Uint64) Load() uint64 { |
| 184 | return atomic.LoadUint64(&i.v) |
| 185 | } |
| 186 | |
| 187 | // Add atomically adds to the wrapped uint64 and returns the new value. |
| 188 | func (i *Uint64) Add(n uint64) uint64 { |
| 189 | return atomic.AddUint64(&i.v, n) |
| 190 | } |
| 191 | |
| 192 | // Sub atomically subtracts from the wrapped uint64 and returns the new value. |
| 193 | func (i *Uint64) Sub(n uint64) uint64 { |
| 194 | return atomic.AddUint64(&i.v, ^(n - 1)) |
| 195 | } |
| 196 | |
| 197 | // Inc atomically increments the wrapped uint64 and returns the new value. |
| 198 | func (i *Uint64) Inc() uint64 { |
| 199 | return i.Add(1) |
| 200 | } |
| 201 | |
| 202 | // Dec atomically decrements the wrapped uint64 and returns the new value. |
| 203 | func (i *Uint64) Dec() uint64 { |
| 204 | return i.Sub(1) |
| 205 | } |
| 206 | |
| 207 | // CAS is an atomic compare-and-swap. |
| 208 | func (i *Uint64) CAS(old, new uint64) bool { |
| 209 | return atomic.CompareAndSwapUint64(&i.v, old, new) |
| 210 | } |
| 211 | |
| 212 | // Store atomically stores the passed value. |
| 213 | func (i *Uint64) Store(n uint64) { |
| 214 | atomic.StoreUint64(&i.v, n) |
| 215 | } |
| 216 | |
| 217 | // Swap atomically swaps the wrapped uint64 and returns the old value. |
| 218 | func (i *Uint64) Swap(n uint64) uint64 { |
| 219 | return atomic.SwapUint64(&i.v, n) |
| 220 | } |
| 221 | |
| 222 | // Bool is an atomic Boolean. |
| 223 | type Bool struct{ v uint32 } |
| 224 | |
| 225 | // NewBool creates a Bool. |
| 226 | func NewBool(initial bool) *Bool { |
| 227 | return &Bool{boolToInt(initial)} |
| 228 | } |
| 229 | |
| 230 | // Load atomically loads the Boolean. |
| 231 | func (b *Bool) Load() bool { |
| 232 | return truthy(atomic.LoadUint32(&b.v)) |
| 233 | } |
| 234 | |
| 235 | // Store atomically stores the passed value. |
| 236 | func (b *Bool) Store(new bool) { |
| 237 | atomic.StoreUint32(&b.v, boolToInt(new)) |
| 238 | } |
| 239 | |
| 240 | // Swap sets the given value and returns the previous value. |
| 241 | func (b *Bool) Swap(new bool) bool { |
| 242 | return truthy(atomic.SwapUint32(&b.v, boolToInt(new))) |
| 243 | } |
| 244 | |
| 245 | // Toggle atomically negates the Boolean and returns the previous value. |
| 246 | func (b *Bool) Toggle() bool { |
| 247 | return truthy(atomic.AddUint32(&b.v, 1) - 1) |
| 248 | } |
| 249 | |
| 250 | func truthy(n uint32) bool { |
| 251 | return n&1 == 1 |
| 252 | } |
| 253 | |
| 254 | func boolToInt(b bool) uint32 { |
| 255 | if b { |
| 256 | return 1 |
| 257 | } |
| 258 | return 0 |
| 259 | } |
| 260 | |
| 261 | // Float64 is an atomic wrapper around float64. |
| 262 | type Float64 struct { |
| 263 | v uint64 |
| 264 | } |
| 265 | |
| 266 | // NewFloat64 creates a Float64. |
| 267 | func NewFloat64(f float64) *Float64 { |
| 268 | return &Float64{math.Float64bits(f)} |
| 269 | } |
| 270 | |
| 271 | // Load atomically loads the wrapped value. |
| 272 | func (f *Float64) Load() float64 { |
| 273 | return math.Float64frombits(atomic.LoadUint64(&f.v)) |
| 274 | } |
| 275 | |
| 276 | // Store atomically stores the passed value. |
| 277 | func (f *Float64) Store(s float64) { |
| 278 | atomic.StoreUint64(&f.v, math.Float64bits(s)) |
| 279 | } |
| 280 | |
| 281 | // Add atomically adds to the wrapped float64 and returns the new value. |
| 282 | func (f *Float64) Add(s float64) float64 { |
| 283 | for { |
| 284 | old := f.Load() |
| 285 | new := old + s |
| 286 | if f.CAS(old, new) { |
| 287 | return new |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Sub atomically subtracts from the wrapped float64 and returns the new value. |
| 293 | func (f *Float64) Sub(s float64) float64 { |
| 294 | return f.Add(-s) |
| 295 | } |
| 296 | |
| 297 | // CAS is an atomic compare-and-swap. |
| 298 | func (f *Float64) CAS(old, new float64) bool { |
| 299 | return atomic.CompareAndSwapUint64(&f.v, math.Float64bits(old), math.Float64bits(new)) |
| 300 | } |
| 301 | |
| 302 | // Value shadows the type of the same name from sync/atomic |
| 303 | // https://godoc.org/sync/atomic#Value |
| 304 | type Value struct{ atomic.Value } |