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 #2624

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ keywords = ["game", "engine", "gamedev", "graphics", "bevy"]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/bevyengine/bevy"
resolver = "2"

[workspace]
exclude = ["benches"]
Expand Down Expand Up @@ -86,6 +87,10 @@ subpixel_glyph_atlas = ["bevy_internal/subpixel_glyph_atlas"]
# enable systems that allow for automated testing on CI
bevy_ci_testing = ["bevy_internal/bevy_ci_testing"]

# 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.5.0", default-features = false, optional = true }
bevy_internal = { path = "crates/bevy_internal", version = "0.5.0", default-features = false }
Expand All @@ -96,7 +101,7 @@ rand = "0.8.0"
ron = "0.6.2"
serde = { version = "1", features = ["derive"] }
# Needed to poll Task examples
futures-lite = "1.11.3"
futures-lite = "1.12.0"

[[example]]
name = "hello_world"
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 @@ -15,6 +15,8 @@ keywords = ["bevy"]
[features]
default = ["filesystem_watcher"]
filesystem_watcher = ["notify"]
async-io = ["bevy_tasks/async-io"]
tokio = ["bevy_tasks/tokio"]

[dependencies]
# bevy
Expand Down Expand Up @@ -46,5 +48,4 @@ js-sys = "0.3"
ndk-glue = { version = "0.2" }

[dev-dependencies]
futures-lite = "1.4.0"
tempfile = "3.2.0"
14 changes: 5 additions & 9 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use anyhow::Result;
use bevy_ecs::system::{Res, ResMut};
use bevy_log::warn;
use bevy_tasks::TaskPool;
use bevy_tasks::{self, TaskPool};
use bevy_utils::{HashMap, Uuid};
use crossbeam_channel::TryRecvError;
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -717,8 +717,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 @@ -737,8 +736,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 @@ -753,8 +751,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 Down Expand Up @@ -785,8 +782,7 @@ mod test {

fn load_asset(path: AssetPath, world: &World) -> HandleUntyped {
let asset_server = world.get_resource::<AssetServer>().unwrap();
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 @@ -47,6 +47,10 @@ subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"]
# enable systems that allow for automated testing on CI
bevy_ci_testing = ["bevy_app/bevy_ci_testing"]

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

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.5.0" }
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
async-io = ["async-global-executor/async-io"]
tokio = ["async-global-executor/tokio"]

[dependencies]
futures-lite = "1.4.0"
async-global-executor= { version="2.0.2", default-features = false }
futures-lite = "1.12.0"
event-listener = "2.4.0"
async-executor = "1.3.0"
async-channel = "1.4.2"
Expand Down
11 changes: 5 additions & 6 deletions crates/bevy_tasks/src/countdown_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,22 @@ impl CountdownEvent {
#[cfg(test)]
mod tests {
use super::*;
use crate::block_on;

#[test]
fn countdown_event_ready_after() {
let countdown_event = CountdownEvent::new(2);
countdown_event.decrement();
countdown_event.decrement();
futures_lite::future::block_on(countdown_event.listen());
block_on(countdown_event.listen());
}

#[test]
fn countdown_event_ready() {
let countdown_event = CountdownEvent::new(2);
countdown_event.decrement();
let countdown_event_clone = countdown_event.clone();
let handle = std::thread::spawn(move || {
futures_lite::future::block_on(countdown_event_clone.listen())
});
let handle = std::thread::spawn(move || block_on(countdown_event_clone.listen()));

// Pause to give the new thread time to start blocking (ugly hack)
std::thread::sleep(instant::Duration::from_millis(100));
Expand All @@ -117,7 +116,7 @@ mod tests {
// notify all listeners
let listener1 = event.listen();
event.notify(std::usize::MAX);
futures_lite::future::block_on(listener1);
block_on(listener1);

// If all listeners are notified, the structure should now be cleared. We're free to listen
// again
Expand All @@ -129,6 +128,6 @@ mod tests {

// Notify all and verify the remaining listener is notified
event.notify(std::usize::MAX);
futures_lite::future::block_on(listener3);
block_on(listener3);
}
}
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 countdown_event::CountdownEvent;
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;

pub mod prelude {
#[doc(hidden)]
pub use crate::{
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 @@ -136,7 +136,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 @@ -186,7 +186,7 @@ impl TaskPool {
if scope.spawned.is_empty() {
Vec::default()
} else if scope.spawned.len() == 1 {
vec![future::block_on(&mut scope.spawned[0])]
vec![crate::block_on(&mut scope.spawned[0])]
} else {
let fut = async move {
let mut results = Vec::with_capacity(scope.spawned.len());
Expand Down Expand Up @@ -215,7 +215,7 @@ impl TaskPool {
// simply calling future::block_on(spawned) would deadlock.)
let mut spawned = local_executor.spawn(fut);
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
4 changes: 3 additions & 1 deletion crates/bevy_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ keywords = ["bevy"]
[features]
default = ["bevy_winit"]
trace = ["wgpu/trace"]
async-io = ["async-global-executor/async-io"]
tokio = ["async-global-executor/tokio"]

[dependencies]
# bevy
Expand All @@ -30,7 +32,7 @@ bevy_utils = { path = "../bevy_utils", version = "0.5.0" }

# other
wgpu = "0.9"
futures-lite = "1.4.0"
crossbeam-channel = "0.5.0"
crossbeam-utils = "0.8.1"
parking_lot = "0.11.0"
async-global-executor= { version="2.0.2", default-features = false }
3 changes: 1 addition & 2 deletions crates/bevy_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use bevy_render::{
renderer::{shared_buffers_update_system, RenderResourceContext, SharedBuffers},
RenderStage,
};
use futures_lite::future;
use renderer::WgpuRenderResourceContext;
use std::borrow::Cow;

Expand Down Expand Up @@ -113,7 +112,7 @@ pub fn get_wgpu_render_system(world: &mut World) -> impl FnMut(&mut World) {
.get_resource::<WgpuOptions>()
.cloned()
.unwrap_or_else(WgpuOptions::default);
let mut wgpu_renderer = future::block_on(WgpuRenderer::new(options));
let mut wgpu_renderer = async_global_executor::block_on(WgpuRenderer::new(options));

let resource_context = WgpuRenderResourceContext::new(wgpu_renderer.device.clone());
world.insert_resource::<Box<dyn RenderResourceContext>>(Box::new(resource_context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use bevy_render::{
};
use bevy_utils::tracing::trace;
use bevy_window::{Window, WindowId};
use futures_lite::future;
use std::{
borrow::Cow,
num::{NonZeroU32, NonZeroU64},
Expand Down Expand Up @@ -654,7 +653,7 @@ impl RenderResourceContext for WgpuRenderResourceContext {
};
let data = buffer_slice.map_async(wgpu_mode);
self.device.poll(wgpu::Maintain::Wait);
if future::block_on(data).is_err() {
if async_global_executor::block_on(data).is_err() {
panic!("Failed to map buffer to host.");
}
}
Expand Down
2 changes: 2 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ skip = [
{ name = "core-graphics", version = "0.19" },
{ name = "fixedbitset", version = "0.2" },
{ name = "hashbrown", version = "0.9" },
{ name = "jni", version = "0.18.0" },
{ name = "jni", version = "0.19.0" },
{ name = "libm", version = "0.1" },
{ name = "mach", version = "0.2" },
{ name = "ndk", version = "0.2" },
Expand Down
2 changes: 2 additions & 0 deletions docs/cargo_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
|wayland|Enable this to use Wayland display server protocol other than X11.|
|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.|
|tokio|Use tokio as task execution engine.|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these two features mutually exclusive? If they are, this docs should cover that mutual exclusion.

|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 @@ -81,7 +81,7 @@ fn handle_tasks(
box_material_handle: Res<BoxMaterialHandle>,
) {
for (entity, mut task) in transform_tasks.iter_mut() {
if let Some(transform) = future::block_on(future::poll_once(&mut *task)) {
if let Some(transform) = bevy::tasks::block_on(future::poll_once(&mut *task)) {
// Add our new PbrBundle of components to our tagged entity
commands.entity(entity).insert_bundle(PbrBundle {
mesh: box_mesh_handle.0.clone(),
Expand Down