mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-05 13:17:36 +03:00
replace the sync.Cond for stream.Read() by a channel
This commit is contained in:
parent
bd693193ed
commit
5fbd52158f
2 changed files with 21 additions and 14 deletions
25
stream.go
25
stream.go
|
@ -41,7 +41,7 @@ type stream struct {
|
||||||
resetRemotely utils.AtomicBool
|
resetRemotely utils.AtomicBool
|
||||||
|
|
||||||
frameQueue *streamFrameSorter
|
frameQueue *streamFrameSorter
|
||||||
newFrameOrErrCond sync.Cond
|
readChan chan struct{}
|
||||||
|
|
||||||
dataForWriting []byte
|
dataForWriting []byte
|
||||||
finSent utils.AtomicBool
|
finSent utils.AtomicBool
|
||||||
|
@ -62,8 +62,8 @@ func newStream(StreamID protocol.StreamID,
|
||||||
streamID: StreamID,
|
streamID: StreamID,
|
||||||
flowControlManager: flowControlManager,
|
flowControlManager: flowControlManager,
|
||||||
frameQueue: newStreamFrameSorter(),
|
frameQueue: newStreamFrameSorter(),
|
||||||
|
readChan: make(chan struct{}, 1),
|
||||||
}
|
}
|
||||||
s.newFrameOrErrCond.L = &s.mutex
|
|
||||||
s.doneWritingOrErrCond.L = &s.mutex
|
s.doneWritingOrErrCond.L = &s.mutex
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
@ -84,9 +84,9 @@ func (s *stream) Read(p []byte) (int, error) {
|
||||||
for bytesRead < len(p) {
|
for bytesRead < len(p) {
|
||||||
s.mutex.Lock()
|
s.mutex.Lock()
|
||||||
frame := s.frameQueue.Head()
|
frame := s.frameQueue.Head()
|
||||||
|
s.mutex.Unlock()
|
||||||
|
|
||||||
if frame == nil && bytesRead > 0 {
|
if frame == nil && bytesRead > 0 {
|
||||||
s.mutex.Unlock()
|
|
||||||
return bytesRead, s.err
|
return bytesRead, s.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,10 +101,11 @@ func (s *stream) Read(p []byte) (int, error) {
|
||||||
s.readPosInFrame = int(s.readOffset - frame.Offset)
|
s.readPosInFrame = int(s.readOffset - frame.Offset)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
s.newFrameOrErrCond.Wait()
|
<-s.readChan
|
||||||
|
s.mutex.Lock()
|
||||||
frame = s.frameQueue.Head()
|
frame = s.frameQueue.Head()
|
||||||
}
|
|
||||||
s.mutex.Unlock()
|
s.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytesRead, err
|
return bytesRead, err
|
||||||
|
@ -250,10 +251,18 @@ func (s *stream) AddStreamFrame(frame *frames.StreamFrame) error {
|
||||||
if err != nil && err != errDuplicateStreamData {
|
if err != nil && err != errDuplicateStreamData {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.newFrameOrErrCond.Signal()
|
s.signalRead()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// signalRead performs a non-blocking send on the readChan
|
||||||
|
func (s *stream) signalRead() {
|
||||||
|
select {
|
||||||
|
case s.readChan <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CloseRemote makes the stream receive a "virtual" FIN stream frame at a given offset
|
// CloseRemote makes the stream receive a "virtual" FIN stream frame at a given offset
|
||||||
func (s *stream) CloseRemote(offset protocol.ByteCount) {
|
func (s *stream) CloseRemote(offset protocol.ByteCount) {
|
||||||
s.AddStreamFrame(&frames.StreamFrame{FinBit: true, Offset: offset})
|
s.AddStreamFrame(&frames.StreamFrame{FinBit: true, Offset: offset})
|
||||||
|
@ -267,7 +276,7 @@ func (s *stream) Cancel(err error) {
|
||||||
// errors must not be changed!
|
// errors must not be changed!
|
||||||
if s.err == nil {
|
if s.err == nil {
|
||||||
s.err = err
|
s.err = err
|
||||||
s.newFrameOrErrCond.Signal()
|
s.signalRead()
|
||||||
s.doneWritingOrErrCond.Signal()
|
s.doneWritingOrErrCond.Signal()
|
||||||
}
|
}
|
||||||
s.mutex.Unlock()
|
s.mutex.Unlock()
|
||||||
|
@ -283,7 +292,7 @@ func (s *stream) Reset(err error) {
|
||||||
// errors must not be changed!
|
// errors must not be changed!
|
||||||
if s.err == nil {
|
if s.err == nil {
|
||||||
s.err = err
|
s.err = err
|
||||||
s.newFrameOrErrCond.Signal()
|
s.signalRead()
|
||||||
s.doneWritingOrErrCond.Signal()
|
s.doneWritingOrErrCond.Signal()
|
||||||
}
|
}
|
||||||
if s.shouldSendReset() {
|
if s.shouldSendReset() {
|
||||||
|
|
|
@ -135,11 +135,9 @@ var _ = Describe("Stream", func() {
|
||||||
mockFcm.EXPECT().UpdateHighestReceived(streamID, protocol.ByteCount(2))
|
mockFcm.EXPECT().UpdateHighestReceived(streamID, protocol.ByteCount(2))
|
||||||
mockFcm.EXPECT().AddBytesRead(streamID, protocol.ByteCount(2))
|
mockFcm.EXPECT().AddBytesRead(streamID, protocol.ByteCount(2))
|
||||||
go func() {
|
go func() {
|
||||||
frame := frames.StreamFrame{
|
defer GinkgoRecover()
|
||||||
Offset: 0,
|
frame := frames.StreamFrame{Data: []byte{0xDE, 0xAD}}
|
||||||
Data: []byte{0xDE, 0xAD},
|
time.Sleep(10 * time.Millisecond)
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond)
|
|
||||||
err := str.AddStreamFrame(&frame)
|
err := str.AddStreamFrame(&frame)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
}()
|
}()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue