Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tokio / async-io optional task execution engines 2 #6137

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ debug_asset_server = ["bevy_internal/debug_asset_server"]
# Enable animation support, and glTF animation loading
animation = ["bevy_internal/animation"]

# alternative async executors, defaults to futures-lite
async-io = ["bevy_internal/async-io"]
tokio = ["bevy_internal/tokio"]

[dependencies]
bevy_dylib = { path = "crates/bevy_dylib", version = "0.9.0-dev", default-features = false, optional = true }
bevy_internal = { path = "crates/bevy_internal", version = "0.9.0-dev", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ keywords = ["bevy"]
default = []
filesystem_watcher = ["notify"]
debug_asset_server = ["filesystem_watcher"]
async-io = ["bevy_tasks/async-io"]
tokio = ["bevy_tasks/tokio"]

[dependencies]
# bevy
Expand Down Expand Up @@ -43,6 +45,5 @@ js-sys = "0.3"
ndk-glue = { version = "0.5" }

[dev-dependencies]
futures-lite = "1.4.0"
tempfile = "3.2.0"
bevy_core = { path = "../bevy_core", version = "0.9.0-dev" }
12 changes: 4 additions & 8 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,7 @@ mod test {
let path: AssetPath = "file.not-a-real-extension".into();
let handle = asset_server.get_handle_untyped(path.get_id());

let err = futures_lite::future::block_on(asset_server.load_async(path.clone(), true))
.unwrap_err();
let err = bevy_tasks::block_on(asset_server.load_async(path.clone(), true)).unwrap_err();
assert!(match err {
AssetServerError::MissingAssetLoader { extensions } => {
extensions == ["not-a-real-extension"]
Expand All @@ -820,8 +819,7 @@ mod test {
let path: AssetPath = "an/invalid/path.png".into();
let handle = asset_server.get_handle_untyped(path.get_id());

let err = futures_lite::future::block_on(asset_server.load_async(path.clone(), true))
.unwrap_err();
let err = bevy_tasks::block_on(asset_server.load_async(path.clone(), true)).unwrap_err();
assert!(matches!(err, AssetServerError::AssetIoError(_)));

assert_eq!(asset_server.get_load_state(handle), LoadState::Failed);
Expand All @@ -836,8 +834,7 @@ mod test {
let path: AssetPath = "fake.fail".into();
let handle = asset_server.get_handle_untyped(path.get_id());

let err = futures_lite::future::block_on(asset_server.load_async(path.clone(), true))
.unwrap_err();
let err = bevy_tasks::block_on(asset_server.load_async(path.clone(), true)).unwrap_err();
assert!(matches!(err, AssetServerError::AssetLoaderError(_)));

assert_eq!(asset_server.get_load_state(handle), LoadState::Failed);
Expand All @@ -860,8 +857,7 @@ mod test {

fn load_asset(path: AssetPath, world: &World) -> HandleUntyped {
let asset_server = world.resource::<AssetServer>();
let id = futures_lite::future::block_on(asset_server.load_async(path.clone(), true))
.unwrap();
let id = bevy_tasks::block_on(asset_server.load_async(path.clone(), true)).unwrap();
asset_server.get_handle_untyped(id)
}

Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ bevy_ci_testing = ["bevy_app/bevy_ci_testing", "bevy_render/ci_limits"]
# Enable animation support, and glTF animation loading
animation = ["bevy_animation", "bevy_gltf?/bevy_animation"]

# alternative async executors, defaults to futures-lite
async-io = ["bevy_tasks/async-io"]
tokio = ["bevy_tasks/tokio"]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.9.0-dev" }
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ once_cell = "1.4.1" # TODO: replace once_cell with std equivalent if/when this l
downcast-rs = "1.2.0"
thread_local = "1.1"
thiserror = "1.0"
futures-lite = "1.4.0"
futures-lite = "1.12"
anyhow = "1.0"
hex = "0.4.2"
hexasphere = "7.2"
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
async-io = ["async-global-executor/async-io"]
tokio = ["async-global-executor/tokio"]

[dependencies]
futures-lite = "1.4.0"
futures-lite = "1.12.0"
async-global-executor= { version="2.2.0", default-features = false }
async-executor = "1.3.0"
async-channel = "1.4.2"
once_cell = "1.7"
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub use usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool};
mod iter;
pub use iter::ParallelIterator;

// re-export block_on so that consumers don't need to explicitly depend on the async engine being used.
// it uses futures-lite by default, async-io and tokio are optional behind features
pub use async_global_executor::block_on;

#[allow(missing_docs)]
pub mod prelude {
#[doc(hidden)]
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_tasks/src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl TaskPool {
.spawn(move || {
let shutdown_future = ex.run(shutdown_rx.recv());
// Use unwrap_err because we expect a Closed error
future::block_on(shutdown_future).unwrap_err();
crate::block_on(shutdown_future).unwrap_err();
})
.expect("Failed to spawn thread.")
})
Expand Down Expand Up @@ -269,11 +269,11 @@ impl TaskPool {
// 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.)
// simply calling crate::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)) {
if let Some(result) = crate::block_on(future::poll_once(&mut spawned)) {
break result;
};

Expand Down
2 changes: 2 additions & 0 deletions docs/cargo_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@
|subpixel_glyph_atlas|Enable this to cache glyphs using subpixel accuracy. This increases texture memory usage as each position requires a separate sprite in the glyph atlas, but provide more accurate character spacing.|
|bevy_ci_testing|Used for running examples in CI.|
|debug_asset_server|Enabling this turns on "hot reloading" of built in assets, such as shaders.|
|tokio|Use tokio as task execution engine.|
|async-io|Use async-io as task execution engine.|
2 changes: 1 addition & 1 deletion examples/async_tasks/async_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn handle_tasks(
box_material_handle: Res<BoxMaterialHandle>,
) {
for (entity, mut task) in &mut transform_tasks {
if let Some(transform) = future::block_on(future::poll_once(&mut task.0)) {
if let Some(transform) = bevy::tasks::block_on(future::poll_once(&mut task.0)) {
// Add our new PbrBundle of components to our tagged entity
commands.entity(entity).insert(PbrBundle {
mesh: box_mesh_handle.clone(),
Expand Down