@@ -44,7 +44,7 @@ use std::marker::PhantomData;
44
44
use std:: panic:: { RefUnwindSafe , UnwindSafe } ;
45
45
use std:: rc:: Rc ;
46
46
use std:: sync:: atomic:: { AtomicBool , AtomicPtr , Ordering } ;
47
- use std:: sync:: { Arc , Mutex , RwLock , TryLockError } ;
47
+ use std:: sync:: { Arc , Mutex , MutexGuard , RwLock , TryLockError } ;
48
48
use std:: task:: { Poll , Waker } ;
49
49
50
50
use async_task:: { Builder , Runnable } ;
@@ -143,7 +143,7 @@ impl<'a> Executor<'a> {
143
143
/// assert!(ex.is_empty());
144
144
/// ```
145
145
pub fn is_empty ( & self ) -> bool {
146
- self . state ( ) . active . lock ( ) . unwrap ( ) . is_empty ( )
146
+ self . state ( ) . active ( ) . is_empty ( )
147
147
}
148
148
149
149
/// Spawns a task onto the executor.
@@ -160,7 +160,7 @@ impl<'a> Executor<'a> {
160
160
/// });
161
161
/// ```
162
162
pub fn spawn < T : Send + ' a > ( & self , future : impl Future < Output = T > + Send + ' a ) -> Task < T > {
163
- let mut active = self . state ( ) . active . lock ( ) . unwrap ( ) ;
163
+ let mut active = self . state ( ) . active ( ) ;
164
164
165
165
// SAFETY: `T` and the future are `Send`.
166
166
unsafe { self . spawn_inner ( future, & mut active) }
@@ -211,7 +211,7 @@ impl<'a> Executor<'a> {
211
211
futures : impl IntoIterator < Item = F > ,
212
212
handles : & mut impl Extend < Task < F :: Output > > ,
213
213
) {
214
- let mut active = Some ( self . state ( ) . active . lock ( ) . unwrap ( ) ) ;
214
+ let mut active = Some ( self . state ( ) . active ( ) ) ;
215
215
216
216
// Convert the futures into tasks.
217
217
let tasks = futures. into_iter ( ) . enumerate ( ) . map ( move |( i, future) | {
@@ -221,7 +221,7 @@ impl<'a> Executor<'a> {
221
221
// Yield the lock every once in a while to ease contention.
222
222
if i. wrapping_sub ( 1 ) % 500 == 0 {
223
223
drop ( active. take ( ) ) ;
224
- active = Some ( self . state ( ) . active . lock ( ) . unwrap ( ) ) ;
224
+ active = Some ( self . state ( ) . active ( ) ) ;
225
225
}
226
226
227
227
task
@@ -246,7 +246,7 @@ impl<'a> Executor<'a> {
246
246
let index = entry. key ( ) ;
247
247
let state = self . state_as_arc ( ) ;
248
248
let future = async move {
249
- let _guard = CallOnDrop ( move || drop ( state. active . lock ( ) . unwrap ( ) . try_remove ( index) ) ) ;
249
+ let _guard = CallOnDrop ( move || drop ( state. active ( ) . try_remove ( index) ) ) ;
250
250
future. await
251
251
} ;
252
252
@@ -415,7 +415,7 @@ impl Drop for Executor<'_> {
415
415
// via Arc::into_raw in state_ptr.
416
416
let state = unsafe { Arc :: from_raw ( ptr) } ;
417
417
418
- let mut active = state. active . lock ( ) . unwrap_or_else ( |e| e . into_inner ( ) ) ;
418
+ let mut active = state. active ( ) ;
419
419
for w in active. drain ( ) {
420
420
w. wake ( ) ;
421
421
}
@@ -517,7 +517,7 @@ impl<'a> LocalExecutor<'a> {
517
517
/// });
518
518
/// ```
519
519
pub fn spawn < T : ' a > ( & self , future : impl Future < Output = T > + ' a ) -> Task < T > {
520
- let mut active = self . inner ( ) . state ( ) . active . lock ( ) . unwrap ( ) ;
520
+ let mut active = self . inner ( ) . state ( ) . active ( ) ;
521
521
522
522
// SAFETY: This executor is not thread safe, so the future and its result
523
523
// cannot be sent to another thread.
@@ -569,7 +569,7 @@ impl<'a> LocalExecutor<'a> {
569
569
futures : impl IntoIterator < Item = F > ,
570
570
handles : & mut impl Extend < Task < F :: Output > > ,
571
571
) {
572
- let mut active = self . inner ( ) . state ( ) . active . lock ( ) . unwrap ( ) ;
572
+ let mut active = self . inner ( ) . state ( ) . active ( ) ;
573
573
574
574
// Convert all of the futures to tasks.
575
575
let tasks = futures. into_iter ( ) . map ( |future| {
@@ -694,6 +694,11 @@ impl State {
694
694
}
695
695
}
696
696
697
+ /// Returns a reference to currently active tasks.
698
+ fn active ( & self ) -> MutexGuard < ' _ , Slab < Waker > > {
699
+ self . active . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) )
700
+ }
701
+
697
702
/// Notifies a sleeping ticker.
698
703
#[ inline]
699
704
fn notify ( & self ) {
@@ -1099,7 +1104,7 @@ fn debug_state(state: &State, name: &str, f: &mut fmt::Formatter<'_>) -> fmt::Re
1099
1104
match self . 0 . try_lock ( ) {
1100
1105
Ok ( lock) => fmt:: Debug :: fmt ( & lock. len ( ) , f) ,
1101
1106
Err ( TryLockError :: WouldBlock ) => f. write_str ( "<locked>" ) ,
1102
- Err ( TryLockError :: Poisoned ( _ ) ) => f . write_str ( "<poisoned>" ) ,
1107
+ Err ( TryLockError :: Poisoned ( err ) ) => fmt :: Debug :: fmt ( & err . into_inner ( ) . len ( ) , f ) ,
1103
1108
}
1104
1109
}
1105
1110
}
0 commit comments