Skip to content

Commit

Permalink
Change mutex to atomic uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Warehime committed Dec 2, 2022
1 parent a377e3f commit 844ffd6
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions catchup/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"sync/atomic"
"time"

"github.com/algorand/go-deadlock"

"github.com/algorand/go-algorand/agreement"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
Expand Down Expand Up @@ -69,7 +67,9 @@ type Ledger interface {

// Service represents the catchup service. Once started and until it is stopped, it ensures that the ledger is up to date with network.
type Service struct {
syncStartNS int64 // at top of struct to keep 64 bit aligned for atomic.* ops
syncStartNS int64 // at top of struct to keep 64 bit aligned for atomic.* ops
// SyncRound, provided externally, which the ledger must keep in cache
syncRound uint64
cfg config.Local
ledger Ledger
ctx context.Context
Expand All @@ -81,9 +81,6 @@ type Service struct {
parallelBlocks uint64
deadlineTimeout time.Duration
blockValidationPool execpool.BacklogPool
// SyncRound, provided externally, which the ledger must keep in cache
syncRoundMu deadlock.RWMutex
syncRound uint64

// suspendForCatchpointWriting defines whether we've ran into a state where the ledger is currently busy writing the
// catchpoint file. If so, we want to suspend the catchup process until the catchpoint file writing is complete,
Expand Down Expand Up @@ -160,24 +157,19 @@ func (s *Service) SetSyncRound(rnd uint64) error {
if basics.Round(rnd) < s.ledger.LastRound() {
return ErrSyncRoundInvalid
}
s.syncRoundMu.Lock()
defer s.syncRoundMu.Unlock()
s.syncRound = rnd
atomic.StoreUint64(&s.syncRound, rnd)
return nil
}

// UnsetSyncRound removes any previously set sync round
func (s *Service) UnsetSyncRound() {
s.syncRoundMu.Lock()
defer s.syncRoundMu.Unlock()
atomic.StoreUint64(&s.syncRound, 0)
s.syncRound = 0
}

// GetSyncRound returns the minimum sync round, and an error
func (s *Service) GetSyncRound() uint64 {
s.syncRoundMu.RLock()
defer s.syncRoundMu.RUnlock()
return s.syncRound
return atomic.LoadUint64(&s.syncRound)
}

// SynchronizingTime returns the time we've been performing a catchup operation (0 if not currently catching up)
Expand Down

0 comments on commit 844ffd6

Please sign in to comment.