From 45c211559de69c973e4c751be4ea6cc148c8ee90 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 11 Mar 2021 10:16:25 +1100 Subject: Add AtomicModify --- funcs.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'funcs.go') diff --git a/funcs.go b/funcs.go index 80c25b1..8694eb4 100644 --- a/funcs.go +++ b/funcs.go @@ -2,6 +2,7 @@ package stm import ( "math/rand" + "reflect" "runtime/pprof" "sync" "time" @@ -162,3 +163,15 @@ func VoidOperation(f func(*Tx)) Operation { return nil } } + +func AtomicModify(v *Var, f interface{}) { + r := reflect.ValueOf(f) + Atomically(VoidOperation(func(tx *Tx) { + cur := reflect.ValueOf(tx.Get(v)) + out := r.Call([]reflect.Value{cur}) + if lenOut := len(out); lenOut != 1 { + panic(lenOut) + } + tx.Set(v, out[0].Interface()) + })) +} -- cgit v1.2.3