Skip to content

Commit

Permalink
txn.Id() to return uint64 instead of uintptr (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Sep 15, 2021
1 parent 3b55079 commit 1847f25
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
22 changes: 11 additions & 11 deletions exp/mdbxpool/txnpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type TxnPool struct {
// used concurrently.
UpdateHandling UpdateHandling

lastid uintptr
idleGuard uintptr
lastid uint64
idleGuard uint64
}

// NewTxnPool initializes returns a new TxnPool.
Expand Down Expand Up @@ -187,16 +187,16 @@ func (p *TxnPool) handleReadonly(txn *mdbx.Txn, condition UpdateHandling) (renew
// getLastID safely retrieves the value of p.lastid so routines operating on
// the sync.Pool know if a transaction can continue to be used without bloating
// the database.
func (p *TxnPool) getLastID() uintptr {
return atomic.LoadUintptr(&p.lastid)
func (p *TxnPool) getLastID() uint64 {
return atomic.LoadUint64(&p.lastid)
}

// CommitID sets the TxnPool's last-known transaction id to invalidate
// previously created mdbx.Readonly transactions and prevent their reuse.
//
// CommitID should only be called if p is not used to create/commit update
// transactions.
func (p *TxnPool) CommitID(id uintptr) {
func (p *TxnPool) CommitID(id uint64) {
if !p.handlesUpdates() {
return
}
Expand All @@ -205,13 +205,13 @@ func (p *TxnPool) CommitID(id uintptr) {

// As long as we think we are holding a newer id than lastid we keep trying
// to CAS until we see a newer id or the CAS succeeds.
lastid := atomic.LoadUintptr(&p.lastid)
lastid := atomic.LoadUint64(&p.lastid)
for lastid < id {
if atomic.CompareAndSwapUintptr(&p.lastid, lastid, id) {
if atomic.CompareAndSwapUint64(&p.lastid, lastid, id) {
updated = true
break
}
lastid = atomic.LoadUintptr(&p.lastid)
lastid = atomic.LoadUint64(&p.lastid)
}

if updated && p.UpdateHandling&HandleIdle != 0 {
Expand Down Expand Up @@ -239,12 +239,12 @@ func (p *TxnPool) handleIdle() {
// one will probably do the work of the waiting one. So we just attempt to
// CAS a guarding value and continue if the we succeeded (ensuring that we
// reset the value with defer).
if !atomic.CompareAndSwapUintptr(&p.idleGuard, 0, 1) {
if !atomic.CompareAndSwapUint64(&p.idleGuard, 0, 1) {
return
}
// Don't CAS when we reset. Just reset. It will make sure that handleIdle
// can run again.
defer atomic.StoreUintptr(&p.idleGuard, 0)
defer atomic.StoreUint64(&p.idleGuard, 0)

var txnPutBack *mdbx.Txn
for {
Expand Down Expand Up @@ -301,7 +301,7 @@ func (p *TxnPool) Abort(txn *mdbx.Txn) {

// Update is analogous to the Update method on mdbx.Env.
func (p *TxnPool) Update(fn mdbx.TxnOp) error {
var id uintptr
var id uint64
err := p.env.Update(func(txn *mdbx.Txn) (err error) {
err = fn(txn)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions mdbx/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Txn struct {
// The value of Txn.ID() is cached so that the cost of cgo does not have to
// be paid. The id of a Txn cannot change over its life, even if it is
// reset/renewed
id uintptr
id uint64
}

// beginTxn does not lock the OS thread which is a prerequisite for creating a
Expand Down Expand Up @@ -118,7 +118,7 @@ func beginTxn(env *Env, parent *Txn, flags uint) (*Txn, error) {
// view transactions.
//
// See mdbx_txn_id.
func (txn *Txn) ID() uintptr {
func (txn *Txn) ID() uint64 {
// It is possible for a txn to legitimately have ID 0 if it a readonly txn
// created before any updates. In practice this does not really happen
// because an application typically must do an initial update to initialize
Expand All @@ -131,8 +131,8 @@ func (txn *Txn) ID() uintptr {
return txn.id
}

func (txn *Txn) getID() uintptr {
return uintptr(C.mdbx_txn_id(txn._txn))
func (txn *Txn) getID() uint64 {
return uint64(C.mdbx_txn_id(txn._txn))
}

// RunOp executes fn with txn as an argument. During the execution of fn no
Expand Down
2 changes: 1 addition & 1 deletion mdbx/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestTxn_ID(t *testing.T) {
env := setup(t)

var id0, id1, id2, id3 uintptr
var id0, id1, id2, id3 uint64
var txnInvalid *Txn
err := env.View(func(txn *Txn) (err error) {
id0 = txn.ID()
Expand Down

0 comments on commit 1847f25

Please sign in to comment.