github.com/hslam/atomic@v1.0.0/uint64.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 // Uint64 represents an uint64. 11 type Uint64 struct { 12 v uint64 13 } 14 15 // NewUint64 returns a new Uint64. 16 func NewUint64(val uint64) *Uint64 { 17 addr := &Uint64{} 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 *Uint64) Swap(new uint64) (old uint64) { 24 return atomic.SwapUint64(&addr.v, new) 25 } 26 27 // CompareAndSwap executes the compare-and-swap operation for an uint64 value. 28 func (addr *Uint64) CompareAndSwap(old, new uint64) (swapped bool) { 29 return atomic.CompareAndSwapUint64(&addr.v, old, new) 30 } 31 32 // Add atomically adds delta to *addr and returns the new value. 33 func (addr *Uint64) Add(delta uint64) (new uint64) { 34 return atomic.AddUint64(&addr.v, delta) 35 } 36 37 // Load atomically loads *addr. 38 func (addr *Uint64) Load() (val uint64) { 39 return atomic.LoadUint64(&addr.v) 40 } 41 42 // Store atomically stores val into *addr. 43 func (addr *Uint64) Store(val uint64) { 44 atomic.StoreUint64(&addr.v, val) 45 }