Skip to content

Commit 50696ae

Browse files
committed
add a test and update docs
1 parent 7186d73 commit 50696ae

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

crates/bevy_tasks/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ async-channel = "1.4.2"
1515
async-task = "4.2.0"
1616
once_cell = "1.7"
1717
concurrent-queue = "1.2.2"
18-
is_main_thread = "0.1.0"
1918

2019
[target.'cfg(target_arch = "wasm32")'.dependencies]
2120
wasm-bindgen-futures = "0.4"

crates/bevy_tasks/src/main_thread_executor.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ use std::{
77
use async_executor::{Executor, Task};
88
use futures_lite::Future;
99

10-
/// Use to access the global main thread executor. Be aware that the main thread executor
11-
/// only makes progress when it is ticked. This normally happens in `[TaskPool::scope]`.
10+
/// An executor that can only be ticked on the thread it was instantiated on.
1211
#[derive(Debug)]
1312
pub struct ThreadExecutor {
14-
// this is only pub crate for testing purposes, do not contruct otherwise
1513
executor: Arc<Executor<'static>>,
1614
thread_id: ThreadId,
1715
}
@@ -26,13 +24,13 @@ impl Default for ThreadExecutor {
2624
}
2725

2826
impl ThreadExecutor {
29-
/// Initializes the global `[MainThreadExecutor]` instance.
27+
/// createa a new `[ThreadExecutor]`
3028
pub fn new() -> Self {
3129
Self::default()
3230
}
3331

34-
/// Gets the `[MainThreadSpawner]` for the global main thread executor.
35-
/// Use this to spawn tasks on the main thread.
32+
/// Gets the `[MainThreadSpawner]` for the thread executor.
33+
/// Use this to spawn tasks that run on the thread this was instatiated on.
3634
pub fn spawner(&self) -> MainThreadSpawner<'static> {
3735
MainThreadSpawner(self.executor.clone())
3836
}
@@ -69,8 +67,29 @@ pub struct MainThreadTicker {
6967
}
7068
impl MainThreadTicker {
7169
/// Tick the main thread executor.
72-
/// This needs to be called manually on the main thread if a `[TaskPool::scope]` is not active
70+
/// This needs to be called manually on the thread if it is not being used with
71+
/// a `[TaskPool::scope]`.
7372
pub fn tick(&self) -> impl Future<Output = ()> + '_ {
7473
self.executor.tick()
7574
}
7675
}
76+
77+
#[cfg(test)]
78+
mod tests {
79+
use super::*;
80+
use std::sync::Arc;
81+
82+
#[test]
83+
fn test_ticker() {
84+
let executor = Arc::new(ThreadExecutor::new());
85+
let ticker = executor.ticker();
86+
assert!(ticker.is_some());
87+
88+
std::thread::scope(|s| {
89+
s.spawn(|| {
90+
let ticker = executor.ticker();
91+
assert!(ticker.is_none());
92+
});
93+
});
94+
}
95+
}

crates/bevy_tasks/src/task_pool.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ impl TaskPool {
157157
///
158158
/// This is similar to `rayon::scope` and `crossbeam::scope`
159159
///
160+
/// The `thread_executor` optional parameter can be used to pass a `[ThreadExecutor]` to
161+
/// spawn tasks on when calling `spawn_on_scope`. This can be useful for spawning tasks that
162+
/// must run on the main thread. If `None` is passed then `spawn_on_scope` runs tasks on
163+
/// the thread `scope` is run on.
164+
///
160165
/// # Example
161166
///
162167
/// ```

0 commit comments

Comments
 (0)