Skip to content

Commit

Permalink
revert change to executor
Browse files Browse the repository at this point in the history
  • Loading branch information
hymm committed Jun 10, 2022
1 parent b1034e4 commit 0fce9ea
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions crates/bevy_tasks/src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::{
marker::PhantomData,
mem,
sync::Arc,
thread::{self, JoinHandle},
thread::{self, JoinHandle}, pin::Pin,
};

use concurrent_queue::ConcurrentQueue;
use futures_lite::{future, FutureExt};
use futures_lite::{future, pin};

use crate::Task;

Expand Down Expand Up @@ -252,7 +252,9 @@ impl TaskPool {

f(scope_ref);

future::block_on(async move {
if spawned.is_empty() {
Vec::new()
} else {
let get_results = async move {
let mut results = Vec::with_capacity(spawned.len());
while let Ok(task) = spawned.pop() {
Expand All @@ -262,17 +264,33 @@ impl TaskPool {
results
};

let tick_forever = async move {
loop {
self.executor.try_tick();
task_scope_executor.try_tick();

future::yield_now().await;
}
};

get_results.or(tick_forever).await
})
// Pin the futures on the stack.
pin!(get_results);

// SAFETY: This function blocks until all futures complete, so we do not read/write
// the data from futures outside of the 'scope lifetime. However,
// rust has no way of knowing this so we must convert to 'static
// here to appease the compiler as it is unable to validate safety.
let get_results: Pin<&mut (dyn Future<Output = Vec<T>> + 'static + Send)> = get_results;
let get_results: Pin<&'static mut (dyn Future<Output = Vec<T>> + 'static + Send)> =
unsafe { mem::transmute(get_results) };

// The thread that calls scope() will participate in driving tasks in the pool
// forward until the tasks that are spawned by this scope() call
// complete. (If the caller of scope() happens to be a thread in
// this thread pool, and we only have one thread in the pool, then
// simply calling future::block_on(spawned) would deadlock.)
let mut spawned = task_scope_executor.spawn(get_results);

loop {
if let Some(result) = future::block_on(future::poll_once(&mut spawned)) {
break result;
};

self.executor.try_tick();
task_scope_executor.try_tick();
}
}
}

/// Spawns a static future onto the thread pool. The returned Task is a future. It can also be
Expand Down

0 comments on commit 0fce9ea

Please sign in to comment.