diff options
author | Matt Joiner <anacrolix@gmail.com> | 2019-11-06 14:51:53 +1100 |
---|---|---|
committer | Matt Joiner <anacrolix@gmail.com> | 2019-11-06 14:51:53 +1100 |
commit | 858a09c9c68b6f46bace8f3e2b4efe627a00cacf (patch) | |
tree | 838f70d790a086dcb4af8e213af77764bf1bcb7c /funcs.go | |
parent | Cache ContextDoneVars (diff) | |
download | stm-858a09c9c68b6f46bace8f3e2b4efe627a00cacf.tar.gz stm-858a09c9c68b6f46bace8f3e2b4efe627a00cacf.tar.xz |
Add WouldBlock
Diffstat (limited to 'funcs.go')
-rw-r--r-- | funcs.go | 20 |
1 files changed, 19 insertions, 1 deletions
@@ -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{} |