pointer_unsafe_opaque.go raw

   1  // Copyright 2024 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package impl
   6  
   7  import (
   8  	"sync/atomic"
   9  	"unsafe"
  10  )
  11  
  12  func (p pointer) AtomicGetPointer() pointer {
  13  	return pointer{p: atomic.LoadPointer((*unsafe.Pointer)(p.p))}
  14  }
  15  
  16  func (p pointer) AtomicSetPointer(v pointer) {
  17  	atomic.StorePointer((*unsafe.Pointer)(p.p), v.p)
  18  }
  19  
  20  func (p pointer) AtomicSetNilPointer() {
  21  	atomic.StorePointer((*unsafe.Pointer)(p.p), unsafe.Pointer(nil))
  22  }
  23  
  24  func (p pointer) AtomicSetPointerIfNil(v pointer) pointer {
  25  	if atomic.CompareAndSwapPointer((*unsafe.Pointer)(p.p), unsafe.Pointer(nil), v.p) {
  26  		return v
  27  	}
  28  	return pointer{p: atomic.LoadPointer((*unsafe.Pointer)(p.p))}
  29  }
  30  
  31  type atomicV1MessageInfo struct{ p Pointer }
  32  
  33  func (mi *atomicV1MessageInfo) Get() Pointer {
  34  	return Pointer(atomic.LoadPointer((*unsafe.Pointer)(&mi.p)))
  35  }
  36  
  37  func (mi *atomicV1MessageInfo) SetIfNil(p Pointer) Pointer {
  38  	if atomic.CompareAndSwapPointer((*unsafe.Pointer)(&mi.p), nil, unsafe.Pointer(p)) {
  39  		return p
  40  	}
  41  	return mi.Get()
  42  }
  43