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
}
@@ -317,47 +312,36 @@ impl AsyncRead for Stream {
317
312
}
318
313
319
314
// Copy data from stream buffer.
315
+ let mut shared = self . shared . lock ( ) ;
320
316
let mut n = 0 ;
321
- let can_read = self . shared . with_mut ( |inner| {
322
- while let Some ( chunk) = inner. buffer . front_mut ( ) {
323
- if chunk. is_empty ( ) {
324
- inner. buffer . pop ( ) ;
325
- continue ;
326
- }
327
- let k = std:: cmp:: min ( chunk. len ( ) , buf. len ( ) - n) ;
328
- buf[ n..n + k] . copy_from_slice ( & chunk. as_ref ( ) [ ..k] ) ;
329
- n += k;
330
- chunk. advance ( k) ;
331
- if n == buf. len ( ) {
332
- break ;
333
- }
317
+ while let Some ( chunk) = shared. buffer . front_mut ( ) {
318
+ if chunk. is_empty ( ) {
319
+ shared. buffer . pop ( ) ;
320
+ continue ;
334
321
}
335
-
336
- if n > 0 {
337
- return true ;
338
- }
339
-
340
- // Buffer is empty, let's check if we can expect to read more data.
341
- if !inner. state . can_read ( ) {
342
- return false ; // No more data available
322
+ let k = std:: cmp:: min ( chunk. len ( ) , buf. len ( ) - n) ;
323
+ buf[ n..n + k] . copy_from_slice ( & chunk. as_ref ( ) [ ..k] ) ;
324
+ n += k;
325
+ chunk. advance ( k) ;
326
+ if n == buf. len ( ) {
327
+ break ;
343
328
}
344
-
345
- // Since we have no more data at this point, we want to be woken up
346
- // by the connection when more becomes available for us.
347
- inner. reader = Some ( cx. waker ( ) . clone ( ) ) ;
348
- true
349
- } ) ;
329
+ }
350
330
351
331
if n > 0 {
352
332
log:: trace!( "{}/{}: read {} bytes" , self . conn, self . id, n) ;
353
333
return Poll :: Ready ( Ok ( n) ) ;
354
334
}
355
335
356
- if !can_read {
336
+ // Buffer is empty, let's check if we can expect to read more data.
337
+ if !shared. state . can_read ( ) {
357
338
log:: debug!( "{}/{}: eof" , self . conn, self . id) ;
358
339
return Poll :: Ready ( Ok ( 0 ) ) ; // stream has been reset
359
340
}
360
341
342
+ // Since we have no more data at this point, we want to be woken up
343
+ // by the connection when more becomes available for us.
344
+ shared. reader = Some ( cx. waker ( ) . clone ( ) ) ;
361
345
Poll :: Pending
362
346
}
363
347
}
@@ -373,18 +357,19 @@ impl AsyncWrite for Stream {
373
357
. poll_ready( cx)
374
358
. map_err( |_| self . write_zero_err( ) ) ?) ;
375
359
376
- let result = self . shared . with_mut ( |inner| {
377
- if !inner. state . can_write ( ) {
360
+ let body = {
361
+ let mut shared = self . shared . lock ( ) ;
362
+ if !shared. state . can_write ( ) {
378
363
log:: debug!( "{}/{}: can no longer write" , self . conn, self . id) ;
379
364
// Return an error
380
- return Err ( self . write_zero_err ( ) ) ;
365
+ return Poll :: Ready ( Err ( self . write_zero_err ( ) ) ) ;
381
366
}
382
367
383
- let window = inner . send_window ( ) ;
368
+ let window = shared . send_window ( ) ;
384
369
if window == 0 {
385
370
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
371
+ shared . writer = Some ( cx. waker ( ) . clone ( ) ) ;
372
+ return Poll :: Pending ;
388
373
}
389
374
390
375
let k = std:: cmp:: min ( window, buf. len ( ) . try_into ( ) . unwrap_or ( u32:: MAX ) ) ;
@@ -394,15 +379,8 @@ impl AsyncWrite for Stream {
394
379
self . config . split_send_size . try_into ( ) . unwrap_or ( u32:: MAX ) ,
395
380
) ;
396
381
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
382
+ shared. consume_send_window ( k) ;
383
+ Vec :: from ( & buf[ ..k as usize ] )
406
384
} ;
407
385
408
386
let n = body. len ( ) ;
@@ -415,9 +393,8 @@ impl AsyncWrite for Stream {
415
393
// a) to be consistent with outbound streams
416
394
// b) to correctly test our behaviour around timing of when ACKs are sent. See `ack_timing.rs` test.
417
395
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
- } ) ;
396
+ self . shared
397
+ . update_state ( self . conn , self . id , State :: Open { acknowledged : true } ) ;
421
398
}
422
399
423
400
let cmd = StreamCommand :: SendFrame ( frame) ;
@@ -452,9 +429,8 @@ impl AsyncWrite for Stream {
452
429
self . sender
453
430
. start_send ( cmd)
454
431
. map_err ( |_| self . write_zero_err ( ) ) ?;
455
- self . shared . with_mut ( |inner| {
456
- inner. update_state ( self . conn , self . id , State :: SendClosed ) ;
457
- } ) ;
432
+ self . shared
433
+ . update_state ( self . conn , self . id , State :: SendClosed ) ;
458
434
Poll :: Ready ( Ok ( ( ) ) )
459
435
}
460
436
}
@@ -487,10 +463,6 @@ impl Shared {
487
463
self . inner . lock ( ) . state
488
464
}
489
465
490
- pub fn pop_buffer ( & self ) -> Option < Chunk > {
491
- self . with_mut ( |inner| inner. buffer . pop ( ) )
492
- }
493
-
494
466
pub fn is_pending_ack ( & self ) -> bool {
495
467
self . inner . lock ( ) . is_pending_ack ( )
496
468
}
@@ -499,17 +471,15 @@ impl Shared {
499
471
self . inner . lock ( ) . next_window_update ( )
500
472
}
501
473
502
- pub fn set_reader_waker ( & self , waker : Option < Waker > ) {
503
- self . with_mut ( |inner| {
504
- inner. reader = waker;
505
- } ) ;
474
+ pub fn update_state ( & self , cid : connection:: Id , sid : StreamId , next : State ) -> State {
475
+ self . inner . lock ( ) . update_state ( cid, sid, next)
506
476
}
507
477
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 ) )
478
+ pub fn lock ( & self ) -> MutexGuard < ' _ , SharedInner > {
479
+ self . inner . lock ( )
510
480
}
511
481
512
- pub fn with_mut < F , R > ( & self , f : F ) -> R
482
+ pub fn with_mut < F , R > ( & mut self , f : F ) -> R
513
483
where
514
484
F : FnOnce ( & mut SharedInner ) -> R ,
515
485
{
0 commit comments