atomic.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. // Package atomic provides simple wrappers around numerics to enforce atomic
  21. // access.
  22. package atomic
  23. import (
  24. "math"
  25. "sync/atomic"
  26. "time"
  27. )
  28. // Int32 is an atomic wrapper around an int32.
  29. type Int32 struct{ v int32 }
  30. // NewInt32 creates an Int32.
  31. func NewInt32(i int32) *Int32 {
  32. return &Int32{i}
  33. }
  34. // Load atomically loads the wrapped value.
  35. func (i *Int32) Load() int32 {
  36. return atomic.LoadInt32(&i.v)
  37. }
  38. // Add atomically adds to the wrapped int32 and returns the new value.
  39. func (i *Int32) Add(n int32) int32 {
  40. return atomic.AddInt32(&i.v, n)
  41. }
  42. // Sub atomically subtracts from the wrapped int32 and returns the new value.
  43. func (i *Int32) Sub(n int32) int32 {
  44. return atomic.AddInt32(&i.v, -n)
  45. }
  46. // Inc atomically increments the wrapped int32 and returns the new value.
  47. func (i *Int32) Inc() int32 {
  48. return i.Add(1)
  49. }
  50. // Dec atomically decrements the wrapped int32 and returns the new value.
  51. func (i *Int32) Dec() int32 {
  52. return i.Sub(1)
  53. }
  54. // CAS is an atomic compare-and-swap.
  55. func (i *Int32) CAS(old, new int32) bool {
  56. return atomic.CompareAndSwapInt32(&i.v, old, new)
  57. }
  58. // Store atomically stores the passed value.
  59. func (i *Int32) Store(n int32) {
  60. atomic.StoreInt32(&i.v, n)
  61. }
  62. // Swap atomically swaps the wrapped int32 and returns the old value.
  63. func (i *Int32) Swap(n int32) int32 {
  64. return atomic.SwapInt32(&i.v, n)
  65. }
  66. // Int64 is an atomic wrapper around an int64.
  67. type Int64 struct{ v int64 }
  68. // NewInt64 creates an Int64.
  69. func NewInt64(i int64) *Int64 {
  70. return &Int64{i}
  71. }
  72. // Load atomically loads the wrapped value.
  73. func (i *Int64) Load() int64 {
  74. return atomic.LoadInt64(&i.v)
  75. }
  76. // Add atomically adds to the wrapped int64 and returns the new value.
  77. func (i *Int64) Add(n int64) int64 {
  78. return atomic.AddInt64(&i.v, n)
  79. }
  80. // Sub atomically subtracts from the wrapped int64 and returns the new value.
  81. func (i *Int64) Sub(n int64) int64 {
  82. return atomic.AddInt64(&i.v, -n)
  83. }
  84. // Inc atomically increments the wrapped int64 and returns the new value.
  85. func (i *Int64) Inc() int64 {
  86. return i.Add(1)
  87. }
  88. // Dec atomically decrements the wrapped int64 and returns the new value.
  89. func (i *Int64) Dec() int64 {
  90. return i.Sub(1)
  91. }
  92. // CAS is an atomic compare-and-swap.
  93. func (i *Int64) CAS(old, new int64) bool {
  94. return atomic.CompareAndSwapInt64(&i.v, old, new)
  95. }
  96. // Store atomically stores the passed value.
  97. func (i *Int64) Store(n int64) {
  98. atomic.StoreInt64(&i.v, n)
  99. }
  100. // Swap atomically swaps the wrapped int64 and returns the old value.
  101. func (i *Int64) Swap(n int64) int64 {
  102. return atomic.SwapInt64(&i.v, n)
  103. }
  104. // Uint32 is an atomic wrapper around an uint32.
  105. type Uint32 struct{ v uint32 }
  106. // NewUint32 creates a Uint32.
  107. func NewUint32(i uint32) *Uint32 {
  108. return &Uint32{i}
  109. }
  110. // Load atomically loads the wrapped value.
  111. func (i *Uint32) Load() uint32 {
  112. return atomic.LoadUint32(&i.v)
  113. }
  114. // Add atomically adds to the wrapped uint32 and returns the new value.
  115. func (i *Uint32) Add(n uint32) uint32 {
  116. return atomic.AddUint32(&i.v, n)
  117. }
  118. // Sub atomically subtracts from the wrapped uint32 and returns the new value.
  119. func (i *Uint32) Sub(n uint32) uint32 {
  120. return atomic.AddUint32(&i.v, ^(n - 1))
  121. }
  122. // Inc atomically increments the wrapped uint32 and returns the new value.
  123. func (i *Uint32) Inc() uint32 {
  124. return i.Add(1)
  125. }
  126. // Dec atomically decrements the wrapped int32 and returns the new value.
  127. func (i *Uint32) Dec() uint32 {
  128. return i.Sub(1)
  129. }
  130. // CAS is an atomic compare-and-swap.
  131. func (i *Uint32) CAS(old, new uint32) bool {
  132. return atomic.CompareAndSwapUint32(&i.v, old, new)
  133. }
  134. // Store atomically stores the passed value.
  135. func (i *Uint32) Store(n uint32) {
  136. atomic.StoreUint32(&i.v, n)
  137. }
  138. // Swap atomically swaps the wrapped uint32 and returns the old value.
  139. func (i *Uint32) Swap(n uint32) uint32 {
  140. return atomic.SwapUint32(&i.v, n)
  141. }
  142. // Uint64 is an atomic wrapper around a uint64.
  143. type Uint64 struct{ v uint64 }
  144. // NewUint64 creates a Uint64.
  145. func NewUint64(i uint64) *Uint64 {
  146. return &Uint64{i}
  147. }
  148. // Load atomically loads the wrapped value.
  149. func (i *Uint64) Load() uint64 {
  150. return atomic.LoadUint64(&i.v)
  151. }
  152. // Add atomically adds to the wrapped uint64 and returns the new value.
  153. func (i *Uint64) Add(n uint64) uint64 {
  154. return atomic.AddUint64(&i.v, n)
  155. }
  156. // Sub atomically subtracts from the wrapped uint64 and returns the new value.
  157. func (i *Uint64) Sub(n uint64) uint64 {
  158. return atomic.AddUint64(&i.v, ^(n - 1))
  159. }
  160. // Inc atomically increments the wrapped uint64 and returns the new value.
  161. func (i *Uint64) Inc() uint64 {
  162. return i.Add(1)
  163. }
  164. // Dec atomically decrements the wrapped uint64 and returns the new value.
  165. func (i *Uint64) Dec() uint64 {
  166. return i.Sub(1)
  167. }
  168. // CAS is an atomic compare-and-swap.
  169. func (i *Uint64) CAS(old, new uint64) bool {
  170. return atomic.CompareAndSwapUint64(&i.v, old, new)
  171. }
  172. // Store atomically stores the passed value.
  173. func (i *Uint64) Store(n uint64) {
  174. atomic.StoreUint64(&i.v, n)
  175. }
  176. // Swap atomically swaps the wrapped uint64 and returns the old value.
  177. func (i *Uint64) Swap(n uint64) uint64 {
  178. return atomic.SwapUint64(&i.v, n)
  179. }
  180. // Bool is an atomic Boolean.
  181. type Bool struct{ v uint32 }
  182. // NewBool creates a Bool.
  183. func NewBool(initial bool) *Bool {
  184. return &Bool{boolToInt(initial)}
  185. }
  186. // Load atomically loads the Boolean.
  187. func (b *Bool) Load() bool {
  188. return truthy(atomic.LoadUint32(&b.v))
  189. }
  190. // CAS is an atomic compare-and-swap.
  191. func (b *Bool) CAS(old, new bool) bool {
  192. return atomic.CompareAndSwapUint32(&b.v, boolToInt(old), boolToInt(new))
  193. }
  194. // Store atomically stores the passed value.
  195. func (b *Bool) Store(new bool) {
  196. atomic.StoreUint32(&b.v, boolToInt(new))
  197. }
  198. // Swap sets the given value and returns the previous value.
  199. func (b *Bool) Swap(new bool) bool {
  200. return truthy(atomic.SwapUint32(&b.v, boolToInt(new)))
  201. }
  202. // Toggle atomically negates the Boolean and returns the previous value.
  203. func (b *Bool) Toggle() bool {
  204. return truthy(atomic.AddUint32(&b.v, 1) - 1)
  205. }
  206. func truthy(n uint32) bool {
  207. return n&1 == 1
  208. }
  209. func boolToInt(b bool) uint32 {
  210. if b {
  211. return 1
  212. }
  213. return 0
  214. }
  215. // Float64 is an atomic wrapper around float64.
  216. type Float64 struct {
  217. v uint64
  218. }
  219. // NewFloat64 creates a Float64.
  220. func NewFloat64(f float64) *Float64 {
  221. return &Float64{math.Float64bits(f)}
  222. }
  223. // Load atomically loads the wrapped value.
  224. func (f *Float64) Load() float64 {
  225. return math.Float64frombits(atomic.LoadUint64(&f.v))
  226. }
  227. // Store atomically stores the passed value.
  228. func (f *Float64) Store(s float64) {
  229. atomic.StoreUint64(&f.v, math.Float64bits(s))
  230. }
  231. // Add atomically adds to the wrapped float64 and returns the new value.
  232. func (f *Float64) Add(s float64) float64 {
  233. for {
  234. old := f.Load()
  235. new := old + s
  236. if f.CAS(old, new) {
  237. return new
  238. }
  239. }
  240. }
  241. // Sub atomically subtracts from the wrapped float64 and returns the new value.
  242. func (f *Float64) Sub(s float64) float64 {
  243. return f.Add(-s)
  244. }
  245. // CAS is an atomic compare-and-swap.
  246. func (f *Float64) CAS(old, new float64) bool {
  247. return atomic.CompareAndSwapUint64(&f.v, math.Float64bits(old), math.Float64bits(new))
  248. }
  249. // Duration is an atomic wrapper around time.Duration
  250. // https://godoc.org/time#Duration
  251. type Duration struct {
  252. v Int64
  253. }
  254. // NewDuration creates a Duration.
  255. func NewDuration(d time.Duration) *Duration {
  256. return &Duration{v: *NewInt64(int64(d))}
  257. }
  258. // Load atomically loads the wrapped value.
  259. func (d *Duration) Load() time.Duration {
  260. return time.Duration(d.v.Load())
  261. }
  262. // Store atomically stores the passed value.
  263. func (d *Duration) Store(n time.Duration) {
  264. d.v.Store(int64(n))
  265. }
  266. // Add atomically adds to the wrapped time.Duration and returns the new value.
  267. func (d *Duration) Add(n time.Duration) time.Duration {
  268. return time.Duration(d.v.Add(int64(n)))
  269. }
  270. // Sub atomically subtracts from the wrapped time.Duration and returns the new value.
  271. func (d *Duration) Sub(n time.Duration) time.Duration {
  272. return time.Duration(d.v.Sub(int64(n)))
  273. }
  274. // Swap atomically swaps the wrapped time.Duration and returns the old value.
  275. func (d *Duration) Swap(n time.Duration) time.Duration {
  276. return time.Duration(d.v.Swap(int64(n)))
  277. }
  278. // CAS is an atomic compare-and-swap.
  279. func (d *Duration) CAS(old, new time.Duration) bool {
  280. return d.v.CAS(int64(old), int64(new))
  281. }
  282. // Value shadows the type of the same name from sync/atomic
  283. // https://godoc.org/sync/atomic#Value
  284. type Value struct{ atomic.Value }