github.com/hslam/atomic@v1.0.0/uintptr.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 5 6 import ( 7 "sync/atomic" 8 ) 9 10 // Uintptr represents an uintptr. 11 type Uintptr struct { 12 v uintptr 13 } 14 15 // NewUintptr returns a new Uintptr. 16 func NewUintptr(val uintptr) *Uintptr { 17 addr := &Uintptr{} 18 addr.Store(val) 19 return addr 20 } 21 22 // Swap atomically stores new into *addr and returns the previous *addr value. 23 func (addr *Uintptr) Swap(new uintptr) (old uintptr) { 24 return atomic.SwapUintptr(&addr.v, new) 25 } 26 27 // CompareAndSwap executes the compare-and-swap operation for an uintptr value. 28 func (addr *Uintptr) CompareAndSwap(old, new uintptr) (swapped bool) { 29 return atomic.CompareAndSwapUintptr(&addr.v, old, new) 30 } 31 32 // Add atomically adds delta to *addr and returns the new value. 33 func (addr *Uintptr) Add(delta uintptr) (new uintptr) { 34 return atomic.AddUintptr(&addr.v, delta) 35 } 36 37 // Load atomically loads *addr. 38 func (addr *Uintptr) Load() (val uintptr) { 39 return atomic.LoadUintptr(&addr.v) 40 } 41 42 // Store atomically stores val into *addr. 43 func (addr *Uintptr) Store(val uintptr) { 44 atomic.StoreUintptr(&addr.v, val) 45 }