Skip to content

Replace async_channel with event_listener in bevy_tasks #11942

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

Closed
wants to merge 7 commits into from
Closed
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: 2 additions & 2 deletions crates/bevy_tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
multi-threaded = ["dep:async-channel", "dep:concurrent-queue"]
multi-threaded = ["dep:event-listener", "dep:concurrent-queue"]

[dependencies]
futures-lite = "2.0.1"
async-executor = "1.11"
async-channel = { version = "2.2.0", optional = true }
async-io = { version = "2.0.0", optional = true }
concurrent-queue = { version = "2.0.0", optional = true }
event-listener = { version = "5.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"
Expand Down
19 changes: 9 additions & 10 deletions crates/bevy_tasks/src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{

use async_executor::FallibleTask;
use concurrent_queue::ConcurrentQueue;
use event_listener::{Event, listener};
use futures_lite::FutureExt;

use crate::{
Expand Down Expand Up @@ -111,7 +112,7 @@ pub struct TaskPool {

// The inner state of the pool.
threads: Vec<JoinHandle<()>>,
shutdown_tx: async_channel::Sender<()>,
shutdown: Arc<Event>,
}

impl TaskPool {
Expand All @@ -131,8 +132,7 @@ impl TaskPool {
}

fn new_internal(builder: TaskPoolBuilder) -> Self {
let (shutdown_tx, shutdown_rx) = async_channel::unbounded::<()>();

let shutdown = Arc::new(Event::new());
let executor = Arc::new(async_executor::Executor::new());

let num_threads = builder
Expand All @@ -142,7 +142,7 @@ impl TaskPool {
let threads = (0..num_threads)
.map(|i| {
let ex = Arc::clone(&executor);
let shutdown_rx = shutdown_rx.clone();
let shutdown = shutdown.clone();

let thread_name = if let Some(thread_name) = builder.thread_name.as_deref() {
format!("{thread_name} ({i})")
Expand Down Expand Up @@ -173,11 +173,10 @@ impl TaskPool {
local_executor.tick().await;
}
};
block_on(ex.run(tick_forever.or(shutdown_rx.recv())))
listener!(shutdown => shutdown_listener);
block_on(ex.run(tick_forever.or(shutdown_listener)));
});
if let Ok(value) = res {
// Use unwrap_err because we expect a Closed error
value.unwrap_err();
if res.is_ok() {
break;
}
}
Expand All @@ -190,7 +189,7 @@ impl TaskPool {
Self {
executor,
threads,
shutdown_tx,
shutdown,
}
}

Expand Down Expand Up @@ -581,7 +580,7 @@ impl Default for TaskPool {

impl Drop for TaskPool {
fn drop(&mut self) {
self.shutdown_tx.close();
self.shutdown.notify(usize::MAX);

let panicking = thread::panicking();
for join_handle in self.threads.drain(..) {
Expand Down
Loading