github.com/hslam/atomic@v1.0.0/atomic.go (about) 1 // Copyright (c) 2020 Meng Huang (mhboy@outlook.com) 2 // This package is licensed under a MIT license that can be found in the LICENSE file. 3 4 // Package atomic provides low-level atomic memory primitives 5 // useful for implementing synchronization algorithms. 6 package atomic 7 8 import ( 9 "sync/atomic" 10 "unsafe" 11 ) 12 13 // SwapInt32 atomically stores new into *addr and returns the previous *addr value. 14 func SwapInt32(addr *int32, new int32) (old int32) { 15 return atomic.SwapInt32(addr, new) 16 } 17 18 // SwapInt64 atomically stores new into *addr and returns the previous *addr value. 19 func SwapInt64(addr *int64, new int64) (old int64) { 20 return atomic.SwapInt64(addr, new) 21 } 22 23 // SwapUint32 atomically stores new into *addr and returns the previous *addr value. 24 func SwapUint32(addr *uint32, new uint32) (old uint32) { 25 return atomic.SwapUint32(addr, new) 26 } 27 28 // SwapUint64 atomically stores new into *addr and returns the previous *addr value. 29 func SwapUint64(addr *uint64, new uint64) (old uint64) { 30 return atomic.SwapUint64(addr, new) 31 } 32 33 // SwapUintptr atomically stores new into *addr and returns the previous *addr value. 34 func SwapUintptr(addr *uintptr, new uintptr) (old uintptr) { 35 return atomic.SwapUintptr(addr, new) 36 } 37 38 // SwapPointer atomically stores new into *addr and returns the previous *addr value. 39 func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer) { 40 return atomic.SwapPointer(addr, new) 41 } 42 43 // CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value. 44 func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool) { 45 return atomic.CompareAndSwapInt32(addr, old, new) 46 } 47 48 // CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value. 49 func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool) { 50 return atomic.CompareAndSwapInt64(addr, old, new) 51 } 52 53 // CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value. 54 func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool) { 55 return atomic.CompareAndSwapUint32(addr, old, new) 56 } 57 58 // CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value. 59 func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool) { 60 return atomic.CompareAndSwapUint64(addr, old, new) 61 } 62 63 // CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value. 64 func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool) { 65 return atomic.CompareAndSwapUintptr(addr, old, new) 66 } 67 68 // CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value. 69 func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool) { 70 return atomic.CompareAndSwapPointer(addr, old, new) 71 } 72 73 // AddInt32 atomically adds delta to *addr and returns the new value. 74 func AddInt32(addr *int32, delta int32) (new int32) { 75 return atomic.AddInt32(addr, delta) 76 } 77 78 // AddUint32 atomically adds delta to *addr and returns the new value. 79 // To subtract a signed positive constant value c from x, do AddUint32(&x, ^uint32(c-1)). 80 // In particular, to decrement x, do AddUint32(&x, ^uint32(0)). 81 func AddUint32(addr *uint32, delta uint32) (new uint32) { 82 return atomic.AddUint32(addr, delta) 83 } 84 85 // AddInt64 atomically adds delta to *addr and returns the new value. 86 func AddInt64(addr *int64, delta int64) (new int64) { 87 return atomic.AddInt64(addr, delta) 88 } 89 90 // AddUint64 atomically adds delta to *addr and returns the new value. 91 // To subtract a signed positive constant value c from x, do AddUint64(&x, ^uint64(c-1)). 92 // In particular, to decrement x, do AddUint64(&x, ^uint64(0)). 93 func AddUint64(addr *uint64, delta uint64) (new uint64) { 94 return atomic.AddUint64(addr, delta) 95 } 96 97 // AddUintptr atomically adds delta to *addr and returns the new value. 98 func AddUintptr(addr *uintptr, delta uintptr) (new uintptr) { 99 return atomic.AddUintptr(addr, delta) 100 } 101 102 // LoadInt32 atomically loads *addr. 103 func LoadInt32(addr *int32) (val int32) { 104 return atomic.LoadInt32(addr) 105 } 106 107 // LoadInt64 atomically loads *addr. 108 func LoadInt64(addr *int64) (val int64) { 109 return atomic.LoadInt64(addr) 110 } 111 112 // LoadUint32 atomically loads *addr. 113 func LoadUint32(addr *uint32) (val uint32) { 114 return atomic.LoadUint32(addr) 115 } 116 117 // LoadUint64 atomically loads *addr. 118 func LoadUint64(addr *uint64) (val uint64) { 119 return atomic.LoadUint64(addr) 120 } 121 122 // LoadUintptr atomically loads *addr. 123 func LoadUintptr(addr *uintptr) (val uintptr) { 124 return atomic.LoadUintptr(addr) 125 } 126 127 // LoadPointer atomically loads *addr. 128 func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer) { 129 return atomic.LoadPointer(addr) 130 } 131 132 // StoreInt32 atomically stores val into *addr. 133 func StoreInt32(addr *int32, val int32) { 134 atomic.StoreInt32(addr, val) 135 } 136 137 // StoreInt64 atomically stores val into *addr. 138 func StoreInt64(addr *int64, val int64) { 139 atomic.StoreInt64(addr, val) 140 } 141 142 // StoreUint32 atomically stores val into *addr. 143 func StoreUint32(addr *uint32, val uint32) { 144 atomic.StoreUint32(addr, val) 145 } 146 147 // StoreUint64 atomically stores val into *addr. 148 func StoreUint64(addr *uint64, val uint64) { 149 atomic.StoreUint64(addr, val) 150 } 151 152 // StoreUintptr atomically stores val into *addr. 153 func StoreUintptr(addr *uintptr, val uintptr) { 154 atomic.StoreUintptr(addr, val) 155 } 156 157 // StorePointer atomically stores val into *addr. 158 func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer) { 159 atomic.StorePointer(addr, val) 160 }