aboutsummaryrefslogtreecommitdiff
path: root/funcs.go
diff options
context:
space:
mode:
Diffstat (limited to 'funcs.go')
-rw-r--r--funcs.go13
1 files changed, 13 insertions, 0 deletions
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())
+ }))
+}