Use bool instead of atomic into to record closed status

This commit is contained in:
Andy Wang 2020-04-13 14:36:32 +01:00
parent c8368bcc7e
commit 242fe28262
2 changed files with 17 additions and 13 deletions

View File

@ -7,7 +7,6 @@ import (
"errors" "errors"
"io" "io"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
@ -20,7 +19,7 @@ type bufferedPipe struct {
// only alloc when on first Read or Write // only alloc when on first Read or Write
buf *bytes.Buffer buf *bytes.Buffer
closed uint32 closed bool
rwCond *sync.Cond rwCond *sync.Cond
rDeadline time.Time rDeadline time.Time
} }
@ -39,7 +38,7 @@ func (p *bufferedPipe) Read(target []byte) (int, error) {
p.buf = new(bytes.Buffer) p.buf = new(bytes.Buffer)
} }
for { for {
if atomic.LoadUint32(&p.closed) == 1 && p.buf.Len() == 0 { if p.closed && p.buf.Len() == 0 {
return 0, io.EOF return 0, io.EOF
} }
if !p.rDeadline.IsZero() { if !p.rDeadline.IsZero() {
@ -67,7 +66,7 @@ func (p *bufferedPipe) WriteTo(w io.Writer) (n int64, err error) {
p.buf = new(bytes.Buffer) p.buf = new(bytes.Buffer)
} }
for { for {
if atomic.LoadUint32(&p.closed) == 1 && p.buf.Len() == 0 { if p.closed && p.buf.Len() == 0 {
return 0, io.EOF return 0, io.EOF
} }
if !p.rDeadline.IsZero() { if !p.rDeadline.IsZero() {
@ -97,7 +96,7 @@ func (p *bufferedPipe) Write(input []byte) (int, error) {
p.buf = new(bytes.Buffer) p.buf = new(bytes.Buffer)
} }
for { for {
if atomic.LoadUint32(&p.closed) == 1 { if p.closed {
return 0, io.ErrClosedPipe return 0, io.ErrClosedPipe
} }
if p.buf.Len() <= BUF_SIZE_LIMIT { if p.buf.Len() <= BUF_SIZE_LIMIT {
@ -113,7 +112,10 @@ func (p *bufferedPipe) Write(input []byte) (int, error) {
} }
func (p *bufferedPipe) Close() error { func (p *bufferedPipe) Close() error {
atomic.StoreUint32(&p.closed, 1) p.rwCond.L.Lock()
defer p.rwCond.L.Unlock()
p.closed = true
p.rwCond.Broadcast() p.rwCond.Broadcast()
return nil return nil
} }

View File

@ -6,7 +6,6 @@ import (
"bytes" "bytes"
"io" "io"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
@ -16,7 +15,7 @@ import (
type datagramBuffer struct { type datagramBuffer struct {
pLens []int pLens []int
buf *bytes.Buffer buf *bytes.Buffer
closed uint32 closed bool
rwCond *sync.Cond rwCond *sync.Cond
rDeadline time.Time rDeadline time.Time
} }
@ -35,7 +34,7 @@ func (d *datagramBuffer) Read(target []byte) (int, error) {
d.buf = new(bytes.Buffer) d.buf = new(bytes.Buffer)
} }
for { for {
if atomic.LoadUint32(&d.closed) == 1 && len(d.pLens) == 0 { if d.closed && len(d.pLens) == 0 {
return 0, io.EOF return 0, io.EOF
} }
@ -70,7 +69,7 @@ func (d *datagramBuffer) WriteTo(w io.Writer) (n int64, err error) {
d.buf = new(bytes.Buffer) d.buf = new(bytes.Buffer)
} }
for { for {
if atomic.LoadUint32(&d.closed) == 1 && len(d.pLens) == 0 { if d.closed && len(d.pLens) == 0 {
return 0, io.EOF return 0, io.EOF
} }
@ -104,7 +103,7 @@ func (d *datagramBuffer) Write(f Frame) (toBeClosed bool, err error) {
d.buf = new(bytes.Buffer) d.buf = new(bytes.Buffer)
} }
for { for {
if atomic.LoadUint32(&d.closed) == 1 { if d.closed {
return true, io.ErrClosedPipe return true, io.ErrClosedPipe
} }
if d.buf.Len() <= BUF_SIZE_LIMIT { if d.buf.Len() <= BUF_SIZE_LIMIT {
@ -115,7 +114,7 @@ func (d *datagramBuffer) Write(f Frame) (toBeClosed bool, err error) {
} }
if f.Closing != C_NOOP { if f.Closing != C_NOOP {
atomic.StoreUint32(&d.closed, 1) d.closed = true
d.rwCond.Broadcast() d.rwCond.Broadcast()
return true, nil return true, nil
} }
@ -129,7 +128,10 @@ func (d *datagramBuffer) Write(f Frame) (toBeClosed bool, err error) {
} }
func (d *datagramBuffer) Close() error { func (d *datagramBuffer) Close() error {
atomic.StoreUint32(&d.closed, 1) d.rwCond.L.Lock()
defer d.rwCond.L.Unlock()
d.closed = true
d.rwCond.Broadcast() d.rwCond.Broadcast()
return nil return nil
} }