Skip to content

Commit

Permalink
use atomic.Bool instead of int32 operations
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Feb 13, 2023
1 parent 3a66ff8 commit 110f440
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions p2p/host/eventbus/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ type emitter struct {
n *node
w *wildcardNode
typ reflect.Type
closed int32
closed atomic.Bool
dropper func(reflect.Type)
metricsTracer MetricsTracer
}

func (e *emitter) Emit(evt interface{}) error {
if atomic.LoadInt32(&e.closed) != 0 {
if e.closed.Load() {
return fmt.Errorf("emitter is closed")
}

Expand All @@ -47,7 +47,7 @@ func (e *emitter) Emit(evt interface{}) error {
}

func (e *emitter) Close() error {
if !atomic.CompareAndSwapInt32(&e.closed, 0, 1) {
if !e.closed.CompareAndSwap(false, true) {
return fmt.Errorf("closed an emitter more than once")
}
if atomic.AddInt32(&e.n.nEmitters, -1) == 0 {
Expand Down
10 changes: 5 additions & 5 deletions p2p/net/connmgr/decay.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ type decayingTag struct {
bumpFn connmgr.BumpFn

// closed marks this tag as closed, so that if it's bumped after being
// closed, we can return an error. 0 = false; 1 = true; guarded by atomic.
closed int32
// closed, we can return an error.
closed atomic.Bool
}

var _ connmgr.DecayingTag = (*decayingTag)(nil)
Expand All @@ -307,7 +307,7 @@ func (t *decayingTag) Interval() time.Duration {

// Bump bumps a tag for this peer.
func (t *decayingTag) Bump(p peer.ID, delta int) error {
if atomic.LoadInt32(&t.closed) == 1 {
if t.closed.Load() {
return fmt.Errorf("decaying tag %s had been closed; no further bumps are accepted", t.name)
}

Expand All @@ -324,7 +324,7 @@ func (t *decayingTag) Bump(p peer.ID, delta int) error {
}

func (t *decayingTag) Remove(p peer.ID) error {
if atomic.LoadInt32(&t.closed) == 1 {
if t.closed.Load() {
return fmt.Errorf("decaying tag %s had been closed; no further removals are accepted", t.name)
}

Expand All @@ -341,7 +341,7 @@ func (t *decayingTag) Remove(p peer.ID) error {
}

func (t *decayingTag) Close() error {
if !atomic.CompareAndSwapInt32(&t.closed, 0, 1) {
if !t.closed.CompareAndSwap(false, true) {
log.Warnf("duplicate decaying tag closure: %s; skipping", t.name)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/net/swarm/dial_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestDialSyncAllCancel(t *testing.T) {
}

func TestFailFirst(t *testing.T) {
var count int32
var handledFirst atomic.Bool
dialErr := fmt.Errorf("gophers ate the modem")
f := func(p peer.ID, reqch <-chan dialRequest) {
go func() {
Expand All @@ -172,7 +172,7 @@ func TestFailFirst(t *testing.T) {
return
}

if atomic.CompareAndSwapInt32(&count, 0, 1) {
if handledFirst.CompareAndSwap(false, true) {
req.resch <- dialResponse{err: dialErr}
} else {
req.resch <- dialResponse{conn: new(Conn)}
Expand Down
4 changes: 2 additions & 2 deletions p2p/protocol/circuitv1/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
)

type Relay struct {
closed int32
closed atomic.Bool
ctx context.Context
cancel context.CancelFunc

Expand Down Expand Up @@ -83,7 +83,7 @@ func NewRelay(h host.Host, opts ...Option) (*Relay, error) {
}

func (r *Relay) Close() error {
if atomic.CompareAndSwapInt32(&r.closed, 0, 1) {
if r.closed.CompareAndSwap(false, true) {
r.host.RemoveStreamHandler(ProtoID)
r.scope.Done()
r.cancel()
Expand Down
4 changes: 2 additions & 2 deletions p2p/protocol/circuitv2/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var log = logging.Logger("relay")

// Relay is the (limited) relay service object.
type Relay struct {
closed uint32
closed atomic.Bool
ctx context.Context
cancel func()

Expand Down Expand Up @@ -104,7 +104,7 @@ func New(h host.Host, opts ...Option) (*Relay, error) {
}

func (r *Relay) Close() error {
if atomic.CompareAndSwapUint32(&r.closed, 0, 1) {
if r.closed.CompareAndSwap(false, true) {
r.host.RemoveStreamHandler(proto.ProtoIDv2Hop)
r.scope.Done()
r.cancel()
Expand Down

0 comments on commit 110f440

Please sign in to comment.