From 858a09c9c68b6f46bace8f3e2b4efe627a00cacf Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 6 Nov 2019 14:51:53 +1100 Subject: Add WouldBlock --- funcs.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'funcs.go') 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{} -- cgit v1.2.3