From 093884c5fdd71d9f96e7bd1f70eef61368ad949a Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sun, 9 Feb 2025 17:27:29 -0300 Subject: src/stm.go: Add Atom() alias to NewVar() fn --- src/stm.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/stm.go b/src/stm.go index 362c1dd..ef9e512 100644 --- a/src/stm.go +++ b/src/stm.go @@ -350,6 +350,11 @@ func Deref[T any](v *Var[T]) T { return v.value.Load().Get().(T) } +// AtomicGet is a helper function that atomically reads a value. +func (ref *AtomT[T]) Deref() T { + return ref.value.Load().Get().(T) +} + // AtomicSet is a helper function that atomically writes a value. func AtomicSet[T any](v *Var[T], val T) { v.mu.Lock() @@ -739,6 +744,14 @@ func NewVar[T any](val T) *Var[T] { return v } +type AtomT[T any] struct{ + *Var[T] +} + +func Atom[T any](x T) *AtomT[T] { + return &AtomT[T]{NewVar(x)} +} + func NewCustomVar[T any](val T, changed func(T, T) bool) *Var[T] { v := &Var[T]{} v.value.Store(customVarValue[T]{ -- cgit v1.2.3