From de38e77cfc11d471d13eb00c6ae209e6bf8abb03 Mon Sep 17 00:00:00 2001 From: "Andrew Jackson (Ajax)" Date: Mon, 11 Dec 2023 11:31:38 -0600 Subject: [PATCH] tx-detector use atomic --- lib/harmony/harmonydb/harmonydb.go | 3 ++- lib/harmony/harmonydb/userfuncs.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/harmony/harmonydb/harmonydb.go b/lib/harmony/harmonydb/harmonydb.go index caa5d1daa..5ec9f5a25 100644 --- a/lib/harmony/harmonydb/harmonydb.go +++ b/lib/harmony/harmonydb/harmonydb.go @@ -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") diff --git a/lib/harmony/harmonydb/userfuncs.go b/lib/harmony/harmonydb/userfuncs.go index 9e2b5a1b6..7fcf76dcd 100644 --- a/lib/harmony/harmonydb/userfuncs.go +++ b/lib/harmony/harmonydb/userfuncs.go @@ -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