github.com/haraldrudell/parl@v0.4.176/atomic-64.go (about) 1 /* 2 © 2023-present Harald Rudell <haraldrudell@proton.me> (https://haraldrudell.github.io/haraldrudell/) 3 All rights reserved 4 */ 5 6 package parl 7 8 import ( 9 "sync/atomic" 10 11 "golang.org/x/exp/constraints" 12 ) 13 14 // Atomic64 is a generic 64-bit integer with atomic access 15 // - generic for named types of any underlying integer or any built-in integer type 16 // - generic version of [atomic.Uint64] 17 // - — 18 // - go1.21.5 due to alignment using atomic.align64, Atomic64 must be based on [atomic.Uint64] 19 type Atomic64[T constraints.Integer] struct{ atomic.Uint64 } 20 21 // Load atomically loads and returns the value stored in a. 22 func (a *Atomic64[T]) Load() (value T) { return T(a.Uint64.Load()) } 23 24 // Store atomically stores val into a. 25 func (a *Atomic64[T]) Store(val T) { a.Uint64.Store(uint64(val)) } 26 27 // Swap atomically stores new into x and returns the previous value. 28 func (a *Atomic64[T]) Swap(new T) (old T) { return T(a.Uint64.Swap(uint64(new))) } 29 30 // CompareAndSwap executes the compare-and-swap operation for a. 31 func (a *Atomic64[T]) CompareAndSwap(old, new T) (swapped bool) { 32 return a.Uint64.CompareAndSwap(uint64(old), uint64(new)) 33 } 34 35 // Add atomically adds delta to a and returns the new value. 36 func (a *Atomic64[T]) Add(delta T) (new T) { return T(a.Uint64.Add(uint64(delta))) }