upgrade lock usage

This commit is contained in:
maskpp 2024-06-16 18:34:16 +08:00
parent fd5078c779
commit e2c50738f8
2 changed files with 10 additions and 11 deletions

View file

@ -525,19 +525,20 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) ([]common.Hash, bool)
dl.lock.RUnlock() dl.lock.RUnlock()
return list, destructed // the cached list can't be nil return list, destructed // the cached list can't be nil
} }
storageMap := dl.storageData[accountHash]
dl.lock.RUnlock() dl.lock.RUnlock()
// No old sorted account list exists, generate a new one
dl.lock.Lock()
defer dl.lock.Unlock()
storageMap := dl.storageData[accountHash]
storageList := make([]common.Hash, 0, len(storageMap)) storageList := make([]common.Hash, 0, len(storageMap))
for k := range storageMap { for k := range storageMap {
storageList = append(storageList, k) storageList = append(storageList, k)
} }
slices.SortFunc(storageList, common.Hash.Cmp) slices.SortFunc(storageList, common.Hash.Cmp)
// No old sorted account list exists, generate a new one
dl.lock.Lock()
dl.storageList[accountHash] = storageList dl.storageList[accountHash] = storageList
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength) dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
dl.lock.Unlock()
return storageList, destructed return storageList, destructed
} }

View file

@ -818,10 +818,12 @@ func (t *Tree) Verify(root common.Hash) error {
// The lock of snapTree is assumed to be held already. // The lock of snapTree is assumed to be held already.
func (t *Tree) disklayer() *diskLayer { func (t *Tree) disklayer() *diskLayer {
var snap snapshot var snap snapshot
t.lock.RLock()
for _, s := range t.layers { for _, s := range t.layers {
snap = s snap = s
break break
} }
t.lock.RUnlock()
if snap == nil { if snap == nil {
return nil return nil
} }
@ -829,6 +831,8 @@ func (t *Tree) disklayer() *diskLayer {
case *diskLayer: case *diskLayer:
return layer return layer
case *diffLayer: case *diffLayer:
layer.lock.RLock()
defer layer.lock.RUnlock()
return layer.origin return layer.origin
default: default:
panic(fmt.Sprintf("%T: undefined layer", snap)) panic(fmt.Sprintf("%T: undefined layer", snap))
@ -848,9 +852,6 @@ func (t *Tree) diskRoot() common.Hash {
// generating is an internal helper function which reports whether the snapshot // generating is an internal helper function which reports whether the snapshot
// is still under the construction. // is still under the construction.
func (t *Tree) generating() (bool, error) { func (t *Tree) generating() (bool, error) {
t.lock.Lock()
defer t.lock.Unlock()
layer := t.disklayer() layer := t.disklayer()
if layer == nil { if layer == nil {
return false, errors.New("disk layer is missing") return false, errors.New("disk layer is missing")
@ -862,9 +863,6 @@ func (t *Tree) generating() (bool, error) {
// DiskRoot is a external helper function to return the disk layer root. // DiskRoot is a external helper function to return the disk layer root.
func (t *Tree) DiskRoot() common.Hash { func (t *Tree) DiskRoot() common.Hash {
t.lock.Lock()
defer t.lock.Unlock()
return t.diskRoot() return t.diskRoot()
} }