This commit is contained in:
zsfelfoldi 2016-06-25 14:49:08 +02:00
parent 6e3dc9806d
commit 12d25262e9
2 changed files with 41 additions and 6 deletions

View file

@ -108,7 +108,8 @@ type ProtocolManager struct {
noMorePeers chan struct{} noMorePeers chan struct{}
syncMu sync.Mutex syncMu sync.Mutex
syncing uint32 syncing bool
syncDone chan struct{}
// wait group is used for graceful shutdowns during downloading // wait group is used for graceful shutdowns during downloading
// and processing // and processing

View file

@ -17,7 +17,6 @@
package les package les
import ( import (
"sync/atomic"
"time" "time"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
@ -76,13 +75,48 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
return return
} }
pm.waitSyncLock()
pm.syncWithLockAcquired(peer)
}
func (pm *ProtocolManager) waitSyncLock() {
for {
chn := pm.getSyncLock(true)
if chn == nil {
break
}
<-chn
}
}
// getSyncLock either acquires the sync lock and returns nil or returns a channel
// which is closed when the lock is free again
func (pm *ProtocolManager) getSyncLock(acquire bool) chan struct{} {
pm.syncMu.Lock() pm.syncMu.Lock()
pm.syncWithoutLock(peer) defer pm.syncMu.Unlock()
if pm.syncing {
if pm.syncDone == nil {
pm.syncDone = make(chan struct{})
}
return pm.syncDone
} else {
pm.syncing = acquire
return nil
}
}
func (pm *ProtocolManager) releaseSyncLock() {
pm.syncMu.Lock()
pm.syncing = false
if pm.syncDone != nil {
close(pm.syncDone)
pm.syncDone = nil
}
pm.syncMu.Unlock() pm.syncMu.Unlock()
} }
func (pm *ProtocolManager) syncWithoutLock(peer *peer) { func (pm *ProtocolManager) syncWithLockAcquired(peer *peer) {
atomic.StoreUint32(&pm.syncing, 1)
pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync) pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync)
atomic.StoreUint32(&pm.syncing, 0) pm.releaseSyncLock()
} }