tx-detector use atomic

This commit is contained in:
Andrew Jackson (Ajax) 2023-12-11 11:31:38 -06:00
parent 96353e63ea
commit de38e77cfc
2 changed files with 4 additions and 3 deletions

View File

@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
logging "github.com/ipfs/go-log/v2"
@ -35,7 +36,7 @@ type DB struct {
schema string
hostnames []string
BTFPOnce sync.Once
BTFP uintptr
BTFP atomic.Uintptr
}
var logger = logging.Logger("harmonydb")

View File

@ -126,7 +126,7 @@ type Tx struct {
func (db *DB) usedInTransaction() bool {
var framePtrs = (&[20]uintptr{})[:] // 20 can be stack-local (no alloc)
framePtrs = framePtrs[:runtime.Callers(3, framePtrs)] // skip past our caller.
return lo.Contains(framePtrs, db.BTFP) // Unsafe read @ beginTx overlap, but 'return false' is correct there.
return lo.Contains(framePtrs, db.BTFP.Load()) // Unsafe read @ beginTx overlap, but 'return false' is correct there.
}
// BeginTransaction is how you can access transactions using this library.
@ -141,7 +141,7 @@ func (db *DB) BeginTransaction(ctx context.Context, f func(*Tx) (commit bool, er
db.BTFPOnce.Do(func() {
fp := make([]uintptr, 20)
runtime.Callers(1, fp)
db.BTFP = fp[0]
db.BTFP.Store(fp[0])
})
if db.usedInTransaction() {
return false, errTx