7
7
// as LICENSE-MIT. You may also obtain a copy of the Apache License, Version 2.0
8
8
// at https://www.apache.org/licenses/LICENSE-2.0 and a copy of the MIT license
9
9
// at https://opensource.org/licenses/MIT.
10
-
11
- use crate :: chunks:: Chunk ;
12
10
use crate :: connection:: rtt:: Rtt ;
13
11
use crate :: frame:: header:: ACK ;
14
12
use crate :: {
@@ -28,7 +26,7 @@ use futures::{
28
26
ready, SinkExt ,
29
27
} ;
30
28
31
- use parking_lot:: Mutex ;
29
+ use parking_lot:: { Mutex , MutexGuard } ;
32
30
use std:: {
33
31
fmt, io,
34
32
pin:: Pin ,
@@ -179,14 +177,12 @@ impl Stream {
179
177
matches ! ( self . shared. state( ) , State :: Closed )
180
178
}
181
179
182
- /// Whether we are still waiting for the remote to acknowledge this stream.
183
180
pub fn is_pending_ack ( & self ) -> bool {
184
181
self . shared . is_pending_ack ( )
185
182
}
186
183
187
- /// Returns a reference to the `Shared` concurrency wrapper.
188
- pub ( crate ) fn shared ( & self ) -> & Shared {
189
- & self . shared
184
+ pub ( crate ) fn shared_mut ( & mut self ) -> & mut Shared {
185
+ & mut self . shared
190
186
}
191
187
192
188
pub ( crate ) fn clone_shared ( & self ) -> Shared {
@@ -265,7 +261,8 @@ impl futures::stream::Stream for Stream {
265
261
Poll :: Pending => { }
266
262
}
267
263
268
- if let Some ( bytes) = self . shared . pop_buffer ( ) {
264
+ let mut shared = self . shared . lock ( ) ;
265
+ if let Some ( bytes) = shared. buffer . pop ( ) {
269
266
let off = bytes. offset ( ) ;
270
267
let mut vec = bytes. into_vec ( ) ;
271
268
if off != 0 {
@@ -276,23 +273,21 @@ impl futures::stream::Stream for Stream {
276
273
log:: debug!(
277
274
"{}/{}: chunk has been partially consumed" ,
278
275
self . conn,
279
- self . id
276
+ self . id,
280
277
) ;
281
278
vec = vec. split_off ( off)
282
279
}
283
280
return Poll :: Ready ( Some ( Ok ( Packet ( vec) ) ) ) ;
284
281
}
285
-
286
282
// Buffer is empty, let's check if we can expect to read more data.
287
- if !self . shared . state ( ) . can_read ( ) {
283
+ if !shared. state . can_read ( ) {
288
284
log:: debug!( "{}/{}: eof" , self . conn, self . id) ;
289
285
return Poll :: Ready ( None ) ; // stream has been reset
290
286
}
291
287
292
288
// Since we have no more data at this point, we want to be woken up
293
289
// by the connection when more becomes available for us.
294
- self . shared . set_reader_waker ( Some ( cx. waker ( ) . clone ( ) ) ) ;
295
-
290
+ shared. reader = Some ( cx. waker ( ) . clone ( ) ) ;
296
291
Poll :: Pending
297
292
}
298
293
}
@@ -318,7 +313,9 @@ impl AsyncRead for Stream {
318
313
319
314
// Copy data from stream buffer.
320
315
let mut n = 0 ;
321
- let can_read = self . shared . with_mut ( |inner| {
316
+ let conn_id = self . conn ;
317
+ let stream_id = self . id ;
318
+ let poll_state = self . shared . with_mut ( |inner| {
322
319
while let Some ( chunk) = inner. buffer . front_mut ( ) {
323
320
if chunk. is_empty ( ) {
324
321
inner. buffer . pop ( ) ;
@@ -334,31 +331,23 @@ impl AsyncRead for Stream {
334
331
}
335
332
336
333
if n > 0 {
337
- return true ;
334
+ log:: trace!( "{}/{}: read {} bytes" , conn_id, stream_id, n) ;
335
+ return Poll :: Ready ( Ok ( n) ) ;
338
336
}
339
337
340
338
// Buffer is empty, let's check if we can expect to read more data.
341
339
if !inner. state . can_read ( ) {
342
- return false ; // No more data available
340
+ log:: debug!( "{}/{}: eof" , conn_id, stream_id) ;
341
+ return Poll :: Ready ( Ok ( 0 ) ) ; // stream has been reset
343
342
}
344
343
345
344
// Since we have no more data at this point, we want to be woken up
346
345
// by the connection when more becomes available for us.
347
346
inner. reader = Some ( cx. waker ( ) . clone ( ) ) ;
348
- true
347
+ Poll :: Pending
349
348
} ) ;
350
349
351
- if n > 0 {
352
- log:: trace!( "{}/{}: read {} bytes" , self . conn, self . id, n) ;
353
- return Poll :: Ready ( Ok ( n) ) ;
354
- }
355
-
356
- if !can_read {
357
- log:: debug!( "{}/{}: eof" , self . conn, self . id) ;
358
- return Poll :: Ready ( Ok ( 0 ) ) ; // stream has been reset
359
- }
360
-
361
- Poll :: Pending
350
+ poll_state
362
351
}
363
352
}
364
353
@@ -373,18 +362,19 @@ impl AsyncWrite for Stream {
373
362
. poll_ready( cx)
374
363
. map_err( |_| self . write_zero_err( ) ) ?) ;
375
364
376
- let result = self . shared . with_mut ( |inner| {
377
- if !inner. state . can_write ( ) {
365
+ let body = {
366
+ let mut shared = self . shared . lock ( ) ;
367
+ if !shared. state . can_write ( ) {
378
368
log:: debug!( "{}/{}: can no longer write" , self . conn, self . id) ;
379
369
// Return an error
380
- return Err ( self . write_zero_err ( ) ) ;
370
+ return Poll :: Ready ( Err ( self . write_zero_err ( ) ) ) ;
381
371
}
382
372
383
- let window = inner . send_window ( ) ;
373
+ let window = shared . send_window ( ) ;
384
374
if window == 0 {
385
375
log:: trace!( "{}/{}: no more credit left" , self . conn, self . id) ;
386
- inner . writer = Some ( cx. waker ( ) . clone ( ) ) ;
387
- return Ok ( None ) ; // means we are Pending
376
+ shared . writer = Some ( cx. waker ( ) . clone ( ) ) ;
377
+ return Poll :: Pending ;
388
378
}
389
379
390
380
let k = std:: cmp:: min ( window, buf. len ( ) . try_into ( ) . unwrap_or ( u32:: MAX ) ) ;
@@ -394,15 +384,8 @@ impl AsyncWrite for Stream {
394
384
self . config . split_send_size . try_into ( ) . unwrap_or ( u32:: MAX ) ,
395
385
) ;
396
386
397
- inner. consume_send_window ( k) ;
398
- let body = Some ( Vec :: from ( & buf[ ..k as usize ] ) ) ;
399
- Ok ( body)
400
- } ) ;
401
-
402
- let body = match result {
403
- Err ( e) => return Poll :: Ready ( Err ( e) ) , // can't write
404
- Ok ( None ) => return Poll :: Pending , // no credit => Pending
405
- Ok ( Some ( b) ) => b, // we have a body
387
+ shared. consume_send_window ( k) ;
388
+ Vec :: from ( & buf[ ..k as usize ] )
406
389
} ;
407
390
408
391
let n = body. len ( ) ;
@@ -415,9 +398,8 @@ impl AsyncWrite for Stream {
415
398
// a) to be consistent with outbound streams
416
399
// b) to correctly test our behaviour around timing of when ACKs are sent. See `ack_timing.rs` test.
417
400
if frame. header ( ) . flags ( ) . contains ( ACK ) {
418
- self . shared . with_mut ( |inner| {
419
- inner. update_state ( self . conn , self . id , State :: Open { acknowledged : true } ) ;
420
- } ) ;
401
+ self . shared
402
+ . update_state ( self . conn , self . id , State :: Open { acknowledged : true } ) ;
421
403
}
422
404
423
405
let cmd = StreamCommand :: SendFrame ( frame) ;
@@ -452,9 +434,8 @@ impl AsyncWrite for Stream {
452
434
self . sender
453
435
. start_send ( cmd)
454
436
. map_err ( |_| self . write_zero_err ( ) ) ?;
455
- self . shared . with_mut ( |inner| {
456
- inner. update_state ( self . conn , self . id , State :: SendClosed ) ;
457
- } ) ;
437
+ self . shared
438
+ . update_state ( self . conn , self . id , State :: SendClosed ) ;
458
439
Poll :: Ready ( Ok ( ( ) ) )
459
440
}
460
441
}
@@ -487,10 +468,6 @@ impl Shared {
487
468
self . inner . lock ( ) . state
488
469
}
489
470
490
- pub fn pop_buffer ( & self ) -> Option < Chunk > {
491
- self . with_mut ( |inner| inner. buffer . pop ( ) )
492
- }
493
-
494
471
pub fn is_pending_ack ( & self ) -> bool {
495
472
self . inner . lock ( ) . is_pending_ack ( )
496
473
}
@@ -499,17 +476,15 @@ impl Shared {
499
476
self . inner . lock ( ) . next_window_update ( )
500
477
}
501
478
502
- pub fn set_reader_waker ( & self , waker : Option < Waker > ) {
503
- self . with_mut ( |inner| {
504
- inner. reader = waker;
505
- } ) ;
479
+ pub fn update_state ( & self , cid : connection:: Id , sid : StreamId , next : State ) -> State {
480
+ self . inner . lock ( ) . update_state ( cid, sid, next)
506
481
}
507
482
508
- pub fn update_state ( & self , cid : connection :: Id , sid : StreamId , next : State ) -> State {
509
- self . with_mut ( | inner| inner . update_state ( cid , sid , next ) )
483
+ pub fn lock ( & self ) -> MutexGuard < ' _ , SharedInner > {
484
+ self . inner . lock ( )
510
485
}
511
486
512
- pub fn with_mut < F , R > ( & self , f : F ) -> R
487
+ pub fn with_mut < F , R > ( & mut self , f : F ) -> R
513
488
where
514
489
F : FnOnce ( & mut SharedInner ) -> R ,
515
490
{
0 commit comments