light: use atomic type (#27169)

* light: use atomic type

* light: use a suitable name for the stopped switch in LightChain
This commit is contained in:
Stephen Guo 2023-04-26 16:21:47 +08:00 committed by GitHub
parent 25f9977f2d
commit 306d17749c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -70,9 +70,9 @@ type LightChain struct {
wg sync.WaitGroup
// Atomic boolean switches:
running int32 // whether LightChain is running or stopped
procInterrupt int32 // interrupts chain insert
disableCheckFreq int32 // disables header verification
stopped atomic.Bool // whether LightChain is stopped or running
procInterrupt atomic.Bool // interrupts chain insert
disableCheckFreq atomic.Bool // disables header verification
}
// NewLightChain returns a fully initialised light chain using information
@ -114,7 +114,7 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
}
func (lc *LightChain) getProcInterrupt() bool {
return atomic.LoadInt32(&lc.procInterrupt) == 1
return lc.procInterrupt.Load()
}
// Odr returns the ODR backend of the chain
@ -302,7 +302,7 @@ func (lc *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*typ
// Stop stops the blockchain service. If any imports are currently in progress
// it will abort them using the procInterrupt.
func (lc *LightChain) Stop() {
if !atomic.CompareAndSwapInt32(&lc.running, 0, 1) {
if !lc.stopped.CompareAndSwap(false, true) {
return
}
close(lc.quit)
@ -315,7 +315,7 @@ func (lc *LightChain) Stop() {
// errInsertionInterrupted as soon as possible. Insertion is permanently disabled after
// calling this method.
func (lc *LightChain) StopInsert() {
atomic.StoreInt32(&lc.procInterrupt, 1)
lc.procInterrupt.Store(true)
}
// Rollback is designed to remove a chain of links from the database that aren't
@ -393,7 +393,7 @@ func (lc *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
if len(chain) == 0 {
return 0, nil
}
if atomic.LoadInt32(&lc.disableCheckFreq) == 1 {
if lc.disableCheckFreq.Load() {
checkFreq = 0
}
start := time.Now()
@ -541,10 +541,10 @@ func (lc *LightChain) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent)
// DisableCheckFreq disables header validation. This is used for ultralight mode.
func (lc *LightChain) DisableCheckFreq() {
atomic.StoreInt32(&lc.disableCheckFreq, 1)
lc.disableCheckFreq.Store(true)
}
// EnableCheckFreq enables header validation.
func (lc *LightChain) EnableCheckFreq() {
atomic.StoreInt32(&lc.disableCheckFreq, 0)
lc.disableCheckFreq.Store(false)
}