atomic.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright 2019 The Vitess Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sync
  14. import (
  15. "math"
  16. "sync"
  17. "sync/atomic"
  18. "time"
  19. )
  20. // AtomicInt32 is a wrapper with a simpler interface around atomic.(Add|Store|Load|CompareAndSwap)Int32 functions.
  21. type AtomicInt32 struct {
  22. int32
  23. }
  24. // NewAtomicInt32 initializes a new AtomicInt32 with a given value.
  25. func NewAtomicInt32(n int32) AtomicInt32 {
  26. return AtomicInt32{n}
  27. }
  28. // Add atomically adds n to the value.
  29. func (i *AtomicInt32) Add(n int32) int32 {
  30. return atomic.AddInt32(&i.int32, n)
  31. }
  32. // Set atomically sets n as new value.
  33. func (i *AtomicInt32) Set(n int32) {
  34. atomic.StoreInt32(&i.int32, n)
  35. }
  36. // Get atomically returns the current value.
  37. func (i *AtomicInt32) Get() int32 {
  38. return atomic.LoadInt32(&i.int32)
  39. }
  40. // CompareAndSwap automatically swaps the old with the new value.
  41. func (i *AtomicInt32) CompareAndSwap(oldval, newval int32) (swapped bool) {
  42. return atomic.CompareAndSwapInt32(&i.int32, oldval, newval)
  43. }
  44. // AtomicInt64 is a wrapper with a simpler interface around atomic.(Add|Store|Load|CompareAndSwap)Int64 functions.
  45. type AtomicInt64 struct {
  46. int64
  47. }
  48. // NewAtomicInt64 initializes a new AtomicInt64 with a given value.
  49. func NewAtomicInt64(n int64) AtomicInt64 {
  50. return AtomicInt64{n}
  51. }
  52. // Add atomically adds n to the value.
  53. func (i *AtomicInt64) Add(n int64) int64 {
  54. return atomic.AddInt64(&i.int64, n)
  55. }
  56. // Set atomically sets n as new value.
  57. func (i *AtomicInt64) Set(n int64) {
  58. atomic.StoreInt64(&i.int64, n)
  59. }
  60. // Get atomically returns the current value.
  61. func (i *AtomicInt64) Get() int64 {
  62. return atomic.LoadInt64(&i.int64)
  63. }
  64. // CompareAndSwap automatically swaps the old with the new value.
  65. func (i *AtomicInt64) CompareAndSwap(oldval, newval int64) (swapped bool) {
  66. return atomic.CompareAndSwapInt64(&i.int64, oldval, newval)
  67. }
  68. // AtomicFloat64 is a wrapper with a simpler interface around atomic.(Add|Store|Load|CompareAndSwap)Flat64 functions.
  69. type AtomicFloat64 struct {
  70. uint64
  71. }
  72. // NewAtomicFloat64 initializes a new AtomicFloat64 with a given value.
  73. func NewAtomicFloat64(n float64) AtomicFloat64 {
  74. return AtomicFloat64{math.Float64bits(n)}
  75. }
  76. // Set atomically sets n as new value.
  77. func (f *AtomicFloat64) Set(n float64) {
  78. atomic.StoreUint64(&f.uint64, math.Float64bits(n))
  79. }
  80. // Get atomically returns the current value.
  81. func (f *AtomicFloat64) Get() float64 {
  82. return math.Float64frombits(atomic.LoadUint64(&f.uint64))
  83. }
  84. // CompareAndSwap automatically swaps the old with the new value.
  85. func (f *AtomicFloat64) CompareAndSwap(oldval, newval float64) (swapped bool) {
  86. return atomic.CompareAndSwapUint64(&f.uint64, math.Float64bits(oldval), math.Float64bits(newval))
  87. }
  88. // AtomicDuration is a wrapper with a simpler interface around atomic.(Add|Store|Load|CompareAndSwap)Int64 functions.
  89. type AtomicDuration struct {
  90. int64
  91. }
  92. // NewAtomicDuration initializes a new AtomicDuration with a given value.
  93. func NewAtomicDuration(duration time.Duration) AtomicDuration {
  94. return AtomicDuration{int64(duration)}
  95. }
  96. // Add atomically adds duration to the value.
  97. func (d *AtomicDuration) Add(duration time.Duration) time.Duration {
  98. return time.Duration(atomic.AddInt64(&d.int64, int64(duration)))
  99. }
  100. // Set atomically sets duration as new value.
  101. func (d *AtomicDuration) Set(duration time.Duration) {
  102. atomic.StoreInt64(&d.int64, int64(duration))
  103. }
  104. // Get atomically returns the current value.
  105. func (d *AtomicDuration) Get() time.Duration {
  106. return time.Duration(atomic.LoadInt64(&d.int64))
  107. }
  108. // CompareAndSwap automatically swaps the old with the new value.
  109. func (d *AtomicDuration) CompareAndSwap(oldval, newval time.Duration) (swapped bool) {
  110. return atomic.CompareAndSwapInt64(&d.int64, int64(oldval), int64(newval))
  111. }
  112. // AtomicBool gives an atomic boolean variable.
  113. type AtomicBool struct {
  114. int32
  115. }
  116. // NewAtomicBool initializes a new AtomicBool with a given value.
  117. func NewAtomicBool(n bool) AtomicBool {
  118. if n {
  119. return AtomicBool{1}
  120. }
  121. return AtomicBool{0}
  122. }
  123. // Set atomically sets n as new value.
  124. func (i *AtomicBool) Set(n bool) {
  125. if n {
  126. atomic.StoreInt32(&i.int32, 1)
  127. } else {
  128. atomic.StoreInt32(&i.int32, 0)
  129. }
  130. }
  131. // Get atomically returns the current value.
  132. func (i *AtomicBool) Get() bool {
  133. return atomic.LoadInt32(&i.int32) != 0
  134. }
  135. // CompareAndSwap automatically swaps the old with the new value.
  136. func (i *AtomicBool) CompareAndSwap(o, n bool) bool {
  137. var old, new int32
  138. if o {
  139. old = 1
  140. }
  141. if n {
  142. new = 1
  143. }
  144. return atomic.CompareAndSwapInt32(&i.int32, old, new)
  145. }
  146. // AtomicString gives you atomic-style APIs for string, but
  147. // it's only a convenience wrapper that uses a mutex. So, it's
  148. // not as efficient as the rest of the atomic types.
  149. type AtomicString struct {
  150. mu sync.Mutex
  151. str string
  152. }
  153. // Set atomically sets str as new value.
  154. func (s *AtomicString) Set(str string) {
  155. s.mu.Lock()
  156. s.str = str
  157. s.mu.Unlock()
  158. }
  159. // Get atomically returns the current value.
  160. func (s *AtomicString) Get() string {
  161. s.mu.Lock()
  162. str := s.str
  163. s.mu.Unlock()
  164. return str
  165. }
  166. // CompareAndSwap automatically swaps the old with the new value.
  167. func (s *AtomicString) CompareAndSwap(oldval, newval string) (swqpped bool) {
  168. s.mu.Lock()
  169. defer s.mu.Unlock()
  170. if s.str == oldval {
  171. s.str = newval
  172. return true
  173. }
  174. return false
  175. }