@@ -2,13 +2,12 @@ use std::{
2
2
future:: Future ,
3
3
marker:: PhantomData ,
4
4
mem,
5
- pin:: Pin ,
6
5
sync:: Arc ,
7
6
thread:: { self , JoinHandle } ,
8
7
} ;
9
8
10
9
use concurrent_queue:: ConcurrentQueue ;
11
- use futures_lite:: { future, pin } ;
10
+ use futures_lite:: { future, FutureExt } ;
12
11
13
12
use crate :: Task ;
14
13
@@ -235,40 +234,28 @@ impl TaskPool {
235
234
236
235
f ( scope_ref) ;
237
236
238
- let fut = async move {
239
- let mut results = Vec :: with_capacity ( spawned. len ( ) ) ;
240
- while let Ok ( task) = spawned. pop ( ) {
241
- results. push ( task. await ) ;
242
- }
237
+ future:: block_on ( async move {
238
+ let fut = async move {
239
+ let mut results = Vec :: with_capacity ( spawned. len ( ) ) ;
240
+ while let Ok ( task) = spawned. pop ( ) {
241
+ results. push ( task. await ) ;
242
+ }
243
243
244
- results
245
- } ;
244
+ results
245
+ } ;
246
246
247
- // Pin the futures on the stack.
248
- pin ! ( fut) ;
249
-
250
- // SAFETY: This function blocks until all futures complete, so we do not read/write
251
- // the data from futures outside of the 'scope lifetime. However,
252
- // rust has no way of knowing this so we must convert to 'static
253
- // here to appease the compiler as it is unable to validate safety.
254
- let fut: Pin < & mut ( dyn Future < Output = Vec < T > > + ' static + Send ) > = fut;
255
- let fut: Pin < & ' static mut ( dyn Future < Output = Vec < T > > + ' static + Send ) > =
256
- unsafe { mem:: transmute ( fut) } ;
257
-
258
- // The thread that calls scope() will participate in driving tasks in the pool
259
- // forward until the tasks that are spawned by this scope() call
260
- // complete. (If the caller of scope() happens to be a thread in
261
- // this thread pool, and we only have one thread in the pool, then
262
- // simply calling future::block_on(spawned) would deadlock.)
263
- let mut spawned = task_scope_executor. spawn ( fut) ;
264
- loop {
265
- if let Some ( result) = future:: block_on ( future:: poll_once ( & mut spawned) ) {
266
- break result;
247
+ let tick_forever = async move {
248
+ loop {
249
+ // only yield every 200 ticks
250
+ self . executor . try_tick ( ) ;
251
+ task_scope_executor. try_tick ( ) ;
252
+
253
+ future:: yield_now ( ) . await
254
+ }
267
255
} ;
268
256
269
- self . executor . try_tick ( ) ;
270
- task_scope_executor. try_tick ( ) ;
271
- }
257
+ fut. or ( tick_forever) . await
258
+ } )
272
259
}
273
260
274
261
/// Spawns a static future onto the thread pool. The returned Task is a future. It can also be
0 commit comments