aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2025-02-09 17:27:29 -0300
committerEuAndreh <eu@euandre.org>2025-02-09 17:39:09 -0300
commit093884c5fdd71d9f96e7bd1f70eef61368ad949a (patch)
tree4b7cd05e1f44cbff552e4a2adb1ce045e5862c98
parentMove benchmarks from tests/stm.go to tests/benchmarks/* (diff)
downloadstm-093884c5fdd71d9f96e7bd1f70eef61368ad949a.tar.gz
stm-093884c5fdd71d9f96e7bd1f70eef61368ad949a.tar.xz
src/stm.go: Add Atom() alias to NewVar() fn
-rw-r--r--src/stm.go13
1 files changed, 13 insertions, 0 deletions
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]{