github.com/rajveermalviya/gamen@v0.1.2-0.20220930195403-9be15877c1aa/internal/common/atomicx/pointer.go (about)

     1  // TODO: remove when we update go.mod to go1.19
     2  package atomicx
     3  
     4  import (
     5  	"sync/atomic"
     6  	"unsafe"
     7  )
     8  
     9  type Pointer[T any] struct {
    10  	_ noCopy
    11  	v unsafe.Pointer
    12  }
    13  
    14  func (x *Pointer[T]) Load() *T {
    15  	return (*T)(atomic.LoadPointer(&x.v))
    16  }
    17  func (x *Pointer[T]) Store(val *T) {
    18  	atomic.StorePointer(&x.v, unsafe.Pointer(val))
    19  }
    20  func (x *Pointer[T]) Swap(new *T) (old *T) {
    21  	return (*T)(atomic.SwapPointer(&x.v, unsafe.Pointer(new)))
    22  }
    23  func (x *Pointer[T]) CompareAndSwap(old, new *T) (swapped bool) {
    24  	return atomic.CompareAndSwapPointer(&x.v, unsafe.Pointer(old), unsafe.Pointer(new))
    25  }