aboutsummaryrefslogtreecommitdiff
path: root/funcs.go
diff options
context:
space:
mode:
authorMatt Joiner <anacrolix@gmail.com>2019-11-06 14:51:53 +1100
committerMatt Joiner <anacrolix@gmail.com>2019-11-06 14:51:53 +1100
commit858a09c9c68b6f46bace8f3e2b4efe627a00cacf (patch)
tree838f70d790a086dcb4af8e213af77764bf1bcb7c /funcs.go
parentCache ContextDoneVars (diff)
downloadstm-858a09c9c68b6f46bace8f3e2b4efe627a00cacf.tar.gz
stm-858a09c9c68b6f46bace8f3e2b4efe627a00cacf.tar.xz
Add WouldBlock
Diffstat (limited to 'funcs.go')
-rw-r--r--funcs.go20
1 files changed, 19 insertions, 1 deletions
diff --git a/funcs.go b/funcs.go
index cbd3590..83f8cf2 100644
--- a/funcs.go
+++ b/funcs.go
@@ -26,11 +26,29 @@ func init() {
}
}
+func newTx() *Tx {
+ return txPool.Get().(*Tx)
+}
+
+func WouldBlock(fn func(*Tx)) (block bool) {
+ tx := newTx()
+ tx.reset()
+ defer func() {
+ if r := recover(); r == Retry {
+ block = true
+ } else if _, ok := r.(_return); ok {
+ } else if r != nil {
+ panic(r)
+ }
+ }()
+ return catchRetry(fn, tx)
+}
+
// Atomically executes the atomic function fn.
func Atomically(fn func(*Tx)) interface{} {
expvars.Add("atomically", 1)
// run the transaction
- tx := txPool.Get().(*Tx)
+ tx := newTx()
retry:
tx.reset()
var ret interface{}