github.com/go-ng/xatomic@v0.0.0-20230519181013-85c0ec87e55f/pointer.go (about)

     1  package xatomic
     2  
     3  import (
     4  	"sync/atomic"
     5  	"unsafe"
     6  )
     7  
     8  // StorePointer atomically stores a pointer to a value.
     9  // This is just a type-safe wrapper for atomic.LoadPointer.
    10  //
    11  // Consider also using atomic.Pointer, instead of this function.
    12  func StorePointer[T any](dst **T, src *T) {
    13  	atomic.StorePointer(
    14  		(*unsafe.Pointer)(unsafe.Pointer(dst)),
    15  		unsafe.Pointer(src),
    16  	)
    17  }
    18  
    19  // LoadPointer atomically loads a pointer to a value.
    20  // This is just a type-safe wrapper for atomic.LoadPointer.
    21  //
    22  // Consider also using atomic.Pointer, instead of this function.
    23  func LoadPointer[T any](src **T) *T {
    24  	return (*T)(atomic.LoadPointer(
    25  		(*unsafe.Pointer)(unsafe.Pointer(src)),
    26  	))
    27  }