Skip to content

Commit bf883a1

Browse files
committed
add function to tick local executors
1 parent 2553464 commit bf883a1

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

crates/bevy_tasks/src/task_pool.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,24 @@ impl TaskPool {
317317
{
318318
Task::new(TaskPool::LOCAL_EXECUTOR.with(|executor| executor.spawn(future)))
319319
}
320+
321+
/// Runs a function with the local executor. Typically used to tick
322+
/// the local executor on the main thread as it needs to share time with
323+
/// other things.
324+
///
325+
/// ```rust
326+
/// use bevy_tasks::TaskPool;
327+
///
328+
/// TaskPool::new().with_local_executor(|local_executor| {
329+
/// local_executor.try_tick();
330+
/// });
331+
/// ```
332+
pub fn with_local_executor<F, R>(&self, f: F) -> R
333+
where
334+
F: FnOnce(&async_executor::LocalExecutor) -> R,
335+
{
336+
Self::LOCAL_EXECUTOR.with(f)
337+
}
320338
}
321339

322340
impl Default for TaskPool {

crates/bevy_tasks/src/usages.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,27 @@ impl Deref for IoTaskPool {
109109
&self.0
110110
}
111111
}
112+
113+
/// Used by `bevy_app` to tick the global tasks pools on the main thread.
114+
pub fn tick_global_task_pools_on_main_thread() {
115+
COMPUTE_TASK_POOL
116+
.get()
117+
.unwrap()
118+
.with_local_executor(|compute_local_executor| {
119+
ASYNC_COMPUTE_TASK_POOL
120+
.get()
121+
.unwrap()
122+
.with_local_executor(|async_local_executor| {
123+
IO_TASK_POOL
124+
.get()
125+
.unwrap()
126+
.with_local_executor(|io_local_executor| {
127+
for _ in 0..20 {
128+
compute_local_executor.try_tick();
129+
async_local_executor.try_tick();
130+
io_local_executor.try_tick();
131+
}
132+
});
133+
});
134+
});
135+
}

0 commit comments

Comments
 (0)