File tree 2 files changed +42
-0
lines changed 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -317,6 +317,24 @@ impl TaskPool {
317
317
{
318
318
Task :: new ( TaskPool :: LOCAL_EXECUTOR . with ( |executor| executor. spawn ( future) ) )
319
319
}
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
+ }
320
338
}
321
339
322
340
impl Default for TaskPool {
Original file line number Diff line number Diff line change @@ -109,3 +109,27 @@ impl Deref for IoTaskPool {
109
109
& self . 0
110
110
}
111
111
}
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
+ }
You can’t perform that action at this time.
0 commit comments