From ab7da86b06233b325406d8b0fb5ff0902adec001 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Tue, 14 Dec 2021 15:14:30 -0800 Subject: [PATCH] Internalize libs/sync. (#7450) Inline the one usage of this library, and remove the lib. --- CHANGELOG_PENDING.md | 1 + internal/blocksync/reactor.go | 28 +++++++++++++++++++++++++--- libs/sync/atomic_bool.go | 33 --------------------------------- libs/sync/atomic_bool_test.go | 27 --------------------------- 4 files changed, 26 insertions(+), 63 deletions(-) delete mode 100644 libs/sync/atomic_bool.go delete mode 100644 libs/sync/atomic_bool_test.go diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 4b3c53a4c..3e54a230b 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -27,6 +27,7 @@ Special thanks to external contributors on this release: - Go API + - [libs/sync] \#7450 Internalize and remove the library. (@creachadair) - [libs/async] \#7449 Move library to internal. (@creachadair) - [pubsub] \#7231 Remove unbuffered subscriptions and rework the Subscription interface. (@creachadair) - [eventbus] \#7231 Move the EventBus type to the internal/eventbus package. (@creachadair) diff --git a/internal/blocksync/reactor.go b/internal/blocksync/reactor.go index 7ff785c81..ee745fb92 100644 --- a/internal/blocksync/reactor.go +++ b/internal/blocksync/reactor.go @@ -6,6 +6,7 @@ import ( "fmt" "runtime/debug" "sync" + "sync/atomic" "time" "github.com/tendermint/tendermint/internal/consensus" @@ -14,7 +15,6 @@ import ( "github.com/tendermint/tendermint/internal/store" "github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/service" - tmsync "github.com/tendermint/tendermint/libs/sync" bcproto "github.com/tendermint/tendermint/proto/tendermint/blocksync" "github.com/tendermint/tendermint/types" ) @@ -75,7 +75,7 @@ type Reactor struct { store *store.BlockStore pool *BlockPool consReactor consensusReactor - blockSync *tmsync.AtomicBool + blockSync *atomicBool blockSyncCh *p2p.Channel // blockSyncOutBridgeCh defines a channel that acts as a bridge between sending Envelope @@ -132,7 +132,7 @@ func NewReactor( store: store, pool: NewBlockPool(logger, startHeight, requestsCh, errorsCh), consReactor: consReactor, - blockSync: tmsync.NewBool(blockSync), + blockSync: newAtomicBool(blockSync), requestsCh: requestsCh, errorsCh: errorsCh, blockSyncCh: blockSyncCh, @@ -625,3 +625,25 @@ func (r *Reactor) GetRemainingSyncTime() time.Duration { return time.Duration(int64(remain * float64(time.Second))) } + +// atomicBool is an atomic Boolean, safe for concurrent use by multiple +// goroutines. +type atomicBool int32 + +// newAtomicBool creates an atomicBool with given initial value. +func newAtomicBool(ok bool) *atomicBool { + ab := new(atomicBool) + if ok { + ab.Set() + } + return ab +} + +// Set sets the Boolean to true. +func (ab *atomicBool) Set() { atomic.StoreInt32((*int32)(ab), 1) } + +// UnSet sets the Boolean to false. +func (ab *atomicBool) UnSet() { atomic.StoreInt32((*int32)(ab), 0) } + +// IsSet returns whether the Boolean is true. +func (ab *atomicBool) IsSet() bool { return atomic.LoadInt32((*int32)(ab))&1 == 1 } diff --git a/libs/sync/atomic_bool.go b/libs/sync/atomic_bool.go deleted file mode 100644 index 1a530b596..000000000 --- a/libs/sync/atomic_bool.go +++ /dev/null @@ -1,33 +0,0 @@ -package sync - -import "sync/atomic" - -// AtomicBool is an atomic Boolean. -// Its methods are all atomic, thus safe to be called by multiple goroutines simultaneously. -// Note: When embedding into a struct one should always use *AtomicBool to avoid copy. -// it's a simple implmentation from https://github.com/tevino/abool -type AtomicBool int32 - -// NewBool creates an AtomicBool with given default value. -func NewBool(ok bool) *AtomicBool { - ab := new(AtomicBool) - if ok { - ab.Set() - } - return ab -} - -// Set sets the Boolean to true. -func (ab *AtomicBool) Set() { - atomic.StoreInt32((*int32)(ab), 1) -} - -// UnSet sets the Boolean to false. -func (ab *AtomicBool) UnSet() { - atomic.StoreInt32((*int32)(ab), 0) -} - -// IsSet returns whether the Boolean is true. -func (ab *AtomicBool) IsSet() bool { - return atomic.LoadInt32((*int32)(ab))&1 == 1 -} diff --git a/libs/sync/atomic_bool_test.go b/libs/sync/atomic_bool_test.go deleted file mode 100644 index 9531815e8..000000000 --- a/libs/sync/atomic_bool_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package sync - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestDefaultValue(t *testing.T) { - t.Parallel() - v := NewBool(false) - assert.False(t, v.IsSet()) - - v = NewBool(true) - assert.True(t, v.IsSet()) -} - -func TestSetUnSet(t *testing.T) { - t.Parallel() - v := NewBool(false) - - v.Set() - assert.True(t, v.IsSet()) - - v.UnSet() - assert.False(t, v.IsSet()) -}