Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Mar 1, 2023
1 parent 832d224 commit 4b24853
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions immutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ImmutableTree struct {
ndb *nodeDB
version int64
skipFastStorageUpgrade bool
mtx sync.RWMutex
mtx *sync.RWMutex
}

// NewImmutableTree creates both in-memory and persistent instances
Expand Down Expand Up @@ -259,7 +259,7 @@ func (t *ImmutableTree) Iterator(start, end []byte, ascending bool) (dbm.Iterato
}
}
t.mtx.Lock()
return NewIterator(start, end, ascending, t, &t.mtx), nil
return NewIterator(start, end, ascending, t, t.mtx), nil
}

// IterateRange makes a callback for all nodes with key between start and end non-inclusive.
Expand Down
18 changes: 11 additions & 7 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,21 @@ type Iterator struct {

t *traversal

mtx *sync.RWMutex
locked bool
mtx *sync.RWMutex
locked bool
unlockMtx *sync.Mutex
}

var _ dbm.Iterator = (*Iterator)(nil)

// Returns a new iterator over the immutable tree. If the tree is nil, the iterator will be invalid.
func NewIterator(start, end []byte, ascending bool, tree *ImmutableTree, mtx *sync.RWMutex) dbm.Iterator {
iter := &Iterator{
start: start,
end: end,
mtx: mtx,
locked: true,
start: start,
end: end,
mtx: mtx,
locked: true,
unlockMtx: &sync.Mutex{},
}

if tree == nil {
Expand Down Expand Up @@ -270,12 +272,14 @@ func (iter *Iterator) IsFast() bool {
}

func (iter *Iterator) unlock() {
iter.unlockMtx.Lock()
defer iter.unlockMtx.Unlock()
if iter.mtx == nil {
return
}
if !iter.locked {
return
}
defer iter.mtx.Unlock()
iter.mtx.Unlock()
iter.locked = false
}
3 changes: 2 additions & 1 deletion mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"fmt"
"sort"
"sync"

"github.com/pkg/errors"
dbm "github.com/tendermint/tm-db"
Expand Down Expand Up @@ -46,7 +47,7 @@ func NewMutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool) (*Mut
// NewMutableTreeWithOpts returns a new tree with the specified options.
func NewMutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options, skipFastStorageUpgrade bool) (*MutableTree, error) {
ndb := newNodeDB(db, cacheSize, opts)
head := &ImmutableTree{ndb: ndb, skipFastStorageUpgrade: skipFastStorageUpgrade}
head := &ImmutableTree{ndb: ndb, skipFastStorageUpgrade: skipFastStorageUpgrade, mtx: &sync.RWMutex{}}

return &MutableTree{
ImmutableTree: head,
Expand Down

0 comments on commit 4b24853

Please sign in to comment.