]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/internal/atomic: add Pointer[T] type
authorAustin Clements <austin@google.com>
Thu, 21 Jul 2022 18:54:07 +0000 (14:54 -0400)
committerAustin Clements <austin@google.com>
Mon, 8 Aug 2022 15:33:45 +0000 (15:33 +0000)
Change-Id: If8fcb37f4a8fcc0668af0df12f1cb8c66f2d2eea
Reviewed-on: https://go-review.googlesource.com/c/go/+/418954
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/runtime/internal/atomic/types.go

index d346a76b67c10de95b3d44d8b9f21e2a0991ee53..784acaadc1dd1b1036823423677d7addd63ee730 100644 (file)
@@ -414,6 +414,40 @@ func (u *UnsafePointer) CompareAndSwapNoWB(old, new unsafe.Pointer) bool {
        return Casp1(&u.value, old, new)
 }
 
+// Pointer is an atomic pointer of type *T.
+type Pointer[T any] struct {
+       u UnsafePointer
+}
+
+// Load accesses and returns the value atomically.
+func (p *Pointer[T]) Load() *T {
+       return (*T)(p.u.Load())
+}
+
+// StoreNoWB updates the value atomically.
+//
+// WARNING: As the name implies this operation does *not*
+// perform a write barrier on value, and so this operation may
+// hide pointers from the GC. Use with care and sparingly.
+// It is safe to use with values not found in the Go heap.
+func (p *Pointer[T]) StoreNoWB(value *T) {
+       p.u.StoreNoWB(unsafe.Pointer(value))
+}
+
+// CompareAndSwapNoWB atomically (with respect to other methods)
+// compares u's value with old, and if they're equal,
+// swaps u's value with new.
+//
+// Returns true if the operation succeeded.
+//
+// WARNING: As the name implies this operation does *not*
+// perform a write barrier on value, and so this operation may
+// hide pointers from the GC. Use with care and sparingly.
+// It is safe to use with values not found in the Go heap.
+func (p *Pointer[T]) CompareAndSwapNoWB(old, new *T) bool {
+       return p.u.CompareAndSwapNoWB(unsafe.Pointer(old), unsafe.Pointer(new))
+}
+
 // noCopy may be embedded into structs which must not be copied
 // after the first use.
 //