diff options
-rw-r--r-- | funcs.go | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -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()) + })) +} |