*: use CompareAndSwap instead of CAS for atomics

go.uber.org/atomic deprecated CAS methods in version 1.10 (that introduced
CompareAndSwap), so we need to fix it.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2023-04-27 18:49:19 +03:00
parent 2567c4c672
commit 0a160ee93b
9 changed files with 22 additions and 22 deletions

View file

@ -276,7 +276,7 @@ func (s *service) Name() string {
}
func (s *service) Start() {
if s.started.CAS(false, true) {
if s.started.CompareAndSwap(false, true) {
s.log.Info("starting consensus service")
b, _ := s.Chain.GetBlock(s.Chain.CurrentBlockHash()) // Can't fail, we have some current block!
s.lastTimestamp = b.Timestamp
@ -288,7 +288,7 @@ func (s *service) Start() {
// Shutdown implements the Service interface.
func (s *service) Shutdown() {
if s.started.CAS(true, false) {
if s.started.CompareAndSwap(true, false) {
s.log.Info("stopping consensus service")
close(s.quit)
<-s.finished

View file

@ -153,7 +153,7 @@ func (bq *Queue) LastQueued() (uint32, int) {
// Discard stops the queue and prevents it from accepting more blocks to enqueue.
func (bq *Queue) Discard() {
if bq.discarded.CAS(false, true) {
if bq.discarded.CompareAndSwap(false, true) {
bq.queueLock.Lock()
close(bq.checkBlocks)
// Technically we could bq.queue = nil, but this would cost

View file

@ -541,7 +541,7 @@ func (s *Server) tryStartServices() {
return
}
if s.IsInSync() && s.syncReached.CAS(false, true) {
if s.IsInSync() && s.syncReached.CompareAndSwap(false, true) {
s.log.Info("node reached synchronized state, starting services")
if s.chain.P2PSigExtensionsEnabled() {
s.notaryRequestPool.RunSubscriptions() // WSClient is also a subscriber.
@ -1277,14 +1277,14 @@ func getRequestBlocksPayload(p Peer, currHeight uint32, lastRequestedHeight *ato
old := lastRequestedHeight.Load()
if old <= currHeight {
needHeight = currHeight + 1
if !lastRequestedHeight.CAS(old, needHeight) {
if !lastRequestedHeight.CompareAndSwap(old, needHeight) {
continue
}
} else if old < currHeight+(bqueue.CacheSize-payload.MaxHashesCount) {
needHeight = currHeight + 1
if peerHeight > old+payload.MaxHashesCount {
needHeight = old + payload.MaxHashesCount
if !lastRequestedHeight.CAS(old, needHeight) {
if !lastRequestedHeight.CompareAndSwap(old, needHeight) {
continue
}
}

View file

@ -509,7 +509,7 @@ func NewWS(ctx context.Context, endpoint string, opts WSOptions) (*WSClient, err
// Close closes connection to the remote side rendering this client instance
// unusable.
func (c *WSClient) Close() {
if c.closeCalled.CAS(false, true) {
if c.closeCalled.CompareAndSwap(false, true) {
c.setCloseErr(errConnClosedByUser)
// Closing shutdown channel sends a signal to wsWriter to break out of the
// loop. In doing so it does ws.Close() closing the network connection

View file

@ -35,7 +35,7 @@ func NewService(name string, httpServers []*http.Server, cfg config.BasicService
// Start runs http service with the exposed endpoint on the configured port.
func (ms *Service) Start() error {
if ms.config.Enabled {
if !ms.started.CAS(false, true) {
if !ms.started.CompareAndSwap(false, true) {
ms.log.Info("service already started")
return nil
}
@ -66,7 +66,7 @@ func (ms *Service) ShutDown() {
if !ms.config.Enabled {
return
}
if !ms.started.CAS(true, false) {
if !ms.started.CompareAndSwap(true, false) {
return
}
for _, srv := range ms.http {

View file

@ -171,7 +171,7 @@ func (n *Notary) Name() string {
// Start runs a Notary module in a separate goroutine.
// The Notary only starts once, subsequent calls to Start are no-op.
func (n *Notary) Start() {
if !n.started.CAS(false, true) {
if !n.started.CompareAndSwap(false, true) {
return
}
n.Config.Log.Info("starting notary service")
@ -221,7 +221,7 @@ drainLoop:
// to Shutdown on the same instance are no-op. The instance that was stopped can
// not be started again by calling Start (use a new instance if needed).
func (n *Notary) Shutdown() {
if !n.started.CAS(true, false) {
if !n.started.CompareAndSwap(true, false) {
return
}
n.Config.Log.Info("stopping notary service")

View file

@ -342,7 +342,7 @@ func (s *Server) Start() {
s.log.Info("RPC server is not enabled")
return
}
if !s.started.CAS(false, true) {
if !s.started.CompareAndSwap(false, true) {
s.log.Info("RPC server already started")
return
}
@ -397,7 +397,7 @@ func (s *Server) Start() {
// that was stopped can not be started again by calling Start (use a new
// instance if needed).
func (s *Server) Shutdown() {
if !s.started.CAS(true, false) {
if !s.started.CompareAndSwap(true, false) {
return
}
// Signal to websocket writer routines and handleSubEvents.

View file

@ -156,7 +156,7 @@ func TestSubscriptions(t *testing.T) {
for _, id := range subIDs {
callUnsubscribe(t, c, respMsgs, id)
}
finishedFlag.CAS(false, true)
finishedFlag.CompareAndSwap(false, true)
c.Close()
}
@ -312,7 +312,7 @@ func TestFilteredSubscriptions(t *testing.T) {
callUnsubscribe(t, c, respMsgs, subID)
callUnsubscribe(t, c, respMsgs, blockSubID)
finishedFlag.CAS(false, true)
finishedFlag.CompareAndSwap(false, true)
c.Close()
})
}
@ -408,7 +408,7 @@ func TestFilteredNotaryRequestSubscriptions(t *testing.T) {
callUnsubscribe(t, c, respMsgs, subID)
})
}
finishedFlag.CAS(false, true)
finishedFlag.CompareAndSwap(false, true)
c.Close()
}
@ -448,7 +448,7 @@ func TestFilteredBlockSubscriptions(t *testing.T) {
require.Equal(t, 3, int(primary))
}
callUnsubscribe(t, c, respMsgs, blockSubID)
finishedFlag.CAS(false, true)
finishedFlag.CompareAndSwap(false, true)
c.Close()
}
@ -477,7 +477,7 @@ func TestMaxSubscriptions(t *testing.T) {
}
}
finishedFlag.CAS(false, true)
finishedFlag.CompareAndSwap(false, true)
c.Close()
}
@ -519,7 +519,7 @@ func TestBadSubUnsub(t *testing.T) {
t.Run("subscribe", testF(t, subCases))
t.Run("unsubscribe", testF(t, unsubCases))
finishedFlag.CAS(false, true)
finishedFlag.CompareAndSwap(false, true)
c.Close()
}
@ -614,6 +614,6 @@ func TestSubscriptionOverflow(t *testing.T) {
// `Missed` is the last event and there is nothing afterwards.
require.Equal(t, 0, len(respMsgs))
finishedFlag.CAS(false, true)
finishedFlag.CompareAndSwap(false, true)
c.Close()
}

View file

@ -25,7 +25,7 @@ func (s *service) Name() string {
// Start runs service instance in a separate goroutine.
// The service only starts once, subsequent calls to Start are no-op.
func (s *service) Start() {
if !s.started.CAS(false, true) {
if !s.started.CompareAndSwap(false, true) {
return
}
s.log.Info("starting state validation service")
@ -68,7 +68,7 @@ drainloop:
// to Shutdown on the same instance are no-op. The instance that was stopped can
// not be started again by calling Start (use a new instance if needed).
func (s *service) Shutdown() {
if !s.started.CAS(true, false) {
if !s.started.CompareAndSwap(true, false) {
return
}
s.log.Info("stopping state validation service")