Skip to content

Commit

Permalink
remove atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo authored and gammazero committed Dec 18, 2024
1 parent ecbf66e commit 5ffdeb5
Showing 1 changed file with 8 additions and 31 deletions.
39 changes: 8 additions & 31 deletions bitswap/network/ipfs_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,9 @@ type impl struct {
receivers []Receiver
}

// interfaceWrapper is concrete type that wraps an interface. Necessary because
// atomic.Value needs the same type and can not Store(nil). This indirection
// allows us to store nil.
type interfaceWrapper[T any] struct {
t T
}
type atomicInterface[T any] struct {
iface atomic.Value
}

func (a *atomicInterface[T]) Load() T {
var v T
x := a.iface.Load()
if x != nil {
return x.(interfaceWrapper[T]).t
}
return v
}

func (a *atomicInterface[T]) Store(v T) {
a.iface.Store(interfaceWrapper[T]{v})
}

type streamMessageSender struct {
to peer.ID
stream atomicInterface[network.Stream]
stream network.Stream
bsnet *impl
opts *MessageSenderOpts
}
Expand All @@ -118,7 +95,7 @@ type HasContext interface {

// Open a stream to the remote peer
func (s *streamMessageSender) Connect(ctx context.Context) (network.Stream, error) {
stream := s.stream.Load()
stream := s.stream
if stream != nil {
return stream, nil
}
Expand All @@ -135,35 +112,35 @@ func (s *streamMessageSender) Connect(ctx context.Context) (network.Stream, erro
return nil, err
}

s.stream.Store(stream)
s.stream = stream
return stream, nil
}

// Reset the stream
func (s *streamMessageSender) Reset() error {
stream := s.stream.Load()
stream := s.stream
if stream != nil {
err := stream.Reset()
s.stream.Store(nil)
s.stream = nil
return err
}
return nil
}

// Close the stream
func (s *streamMessageSender) Close() error {
stream := s.stream.Load()
stream := s.stream
if stream != nil {
err := stream.Close()
s.stream.Store(nil)
s.stream = nil
return err
}
return nil
}

// Indicates whether the peer supports HAVE / DONT_HAVE messages
func (s *streamMessageSender) SupportsHave() bool {
stream := s.stream.Load()
stream := s.stream
if stream == nil {
return false
}
Expand Down

0 comments on commit 5ffdeb5

Please sign in to comment.