Revert "p2p: use atomic types (#27764)"

This reverts commit f438ff5d47.
This commit is contained in:
devopsbo3 2023-11-10 12:27:53 -06:00 committed by GitHub
parent 01c80dd410
commit ccb9c191cd

View file

@ -156,7 +156,7 @@ func MsgPipe() (*MsgPipeRW, *MsgPipeRW) {
var ( var (
c1, c2 = make(chan Msg), make(chan Msg) c1, c2 = make(chan Msg), make(chan Msg)
closing = make(chan struct{}) closing = make(chan struct{})
closed = new(atomic.Bool) closed = new(int32)
rw1 = &MsgPipeRW{c1, c2, closing, closed} rw1 = &MsgPipeRW{c1, c2, closing, closed}
rw2 = &MsgPipeRW{c2, c1, closing, closed} rw2 = &MsgPipeRW{c2, c1, closing, closed}
) )
@ -172,13 +172,13 @@ type MsgPipeRW struct {
w chan<- Msg w chan<- Msg
r <-chan Msg r <-chan Msg
closing chan struct{} closing chan struct{}
closed *atomic.Bool closed *int32
} }
// WriteMsg sends a message on the pipe. // WriteMsg sends a message on the pipe.
// It blocks until the receiver has consumed the message payload. // It blocks until the receiver has consumed the message payload.
func (p *MsgPipeRW) WriteMsg(msg Msg) error { func (p *MsgPipeRW) WriteMsg(msg Msg) error {
if !p.closed.Load() { if atomic.LoadInt32(p.closed) == 0 {
consumed := make(chan struct{}, 1) consumed := make(chan struct{}, 1)
msg.Payload = &eofSignal{msg.Payload, msg.Size, consumed} msg.Payload = &eofSignal{msg.Payload, msg.Size, consumed}
select { select {
@ -199,7 +199,7 @@ func (p *MsgPipeRW) WriteMsg(msg Msg) error {
// ReadMsg returns a message sent on the other end of the pipe. // ReadMsg returns a message sent on the other end of the pipe.
func (p *MsgPipeRW) ReadMsg() (Msg, error) { func (p *MsgPipeRW) ReadMsg() (Msg, error) {
if !p.closed.Load() { if atomic.LoadInt32(p.closed) == 0 {
select { select {
case msg := <-p.r: case msg := <-p.r:
return msg, nil return msg, nil
@ -213,8 +213,9 @@ func (p *MsgPipeRW) ReadMsg() (Msg, error) {
// of the pipe. They will return ErrPipeClosed. Close also // of the pipe. They will return ErrPipeClosed. Close also
// interrupts any reads from a message payload. // interrupts any reads from a message payload.
func (p *MsgPipeRW) Close() error { func (p *MsgPipeRW) Close() error {
if p.closed.Swap(true) { if atomic.AddInt32(p.closed, 1) != 1 {
// someone else is already closing // someone else is already closing
atomic.StoreInt32(p.closed, 1) // avoid overflow
return nil return nil
} }
close(p.closing) close(p.closing)