wp-go/safety/vars.go

48 lines
726 B
Go
Raw Permalink Normal View History

2022-10-14 09:15:43 +00:00
package safety
import (
"sync/atomic"
"unsafe"
)
type Var[T any] struct {
val T
p unsafe.Pointer
}
func NewVar[T any](val T) *Var[T] {
return &Var[T]{val: val, p: unsafe.Pointer(&val)}
2022-10-14 09:15:43 +00:00
}
func (r *Var[T]) Load() T {
return *(*T)(atomic.LoadPointer(&r.p))
}
func (r *Var[T]) Delete() {
for {
px := atomic.LoadPointer(&r.p)
if atomic.CompareAndSwapPointer(&r.p, px, nil) {
return
}
}
}
func (r *Var[T]) Store(v T) {
for {
px := atomic.LoadPointer(&r.p)
if atomic.CompareAndSwapPointer(&r.p, px, unsafe.Pointer(&v)) {
return
}
}
}
2023-02-15 16:32:02 +00:00
func (r *Var[T]) Flush() {
for {
px := atomic.LoadPointer(&r.p)
var v T
if atomic.CompareAndSwapPointer(&r.p, px, unsafe.Pointer(&v)) {
return
}
}
}