Skip to content

Commit 7186d73

Browse files
committed
change scope to take a thread executor
1 parent d0a6615 commit 7186d73

File tree

13 files changed

+118
-110
lines changed

13 files changed

+118
-110
lines changed

crates/bevy_app/src/app.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use bevy_ecs::{
44
event::{Event, Events},
55
prelude::FromWorld,
66
schedule::{
7-
IntoSystemDescriptor, Schedule, ShouldRun, Stage, StageLabel, State, StateData, SystemSet,
8-
SystemStage,
7+
IntoSystemDescriptor, MainThreadExecutor, Schedule, ShouldRun, Stage, StageLabel, State,
8+
StateData, SystemSet, SystemStage,
99
},
1010
system::Resource,
1111
world::World,
@@ -153,7 +153,11 @@ impl App {
153153
pub fn update(&mut self) {
154154
#[cfg(feature = "trace")]
155155
let _bevy_frame_update_span = info_span!("frame").entered();
156-
ComputeTaskPool::init(TaskPool::default).scope(|scope| {
156+
let thread_executor = self
157+
.world
158+
.get_resource::<MainThreadExecutor>()
159+
.map(|e| e.0.clone());
160+
ComputeTaskPool::init(TaskPool::default).scope(thread_executor, |scope| {
157161
if self.run_once {
158162
for sub_app in self.sub_apps.values_mut() {
159163
(sub_app.extract)(&mut self.world, &mut sub_app.app);
@@ -1001,10 +1005,13 @@ impl App {
10011005
pub fn add_sub_app(
10021006
&mut self,
10031007
label: impl AppLabel,
1004-
app: App,
1008+
mut app: App,
10051009
sub_app_extract: impl Fn(&mut World, &mut App) + 'static + Send + Sync,
10061010
sub_app_runner: impl Fn(&mut App) + 'static + Send + Sync,
10071011
) -> &mut Self {
1012+
if let Some(executor) = self.world.get_resource::<MainThreadExecutor>() {
1013+
app.world.insert_resource(executor.clone());
1014+
}
10081015
self.sub_apps.insert(
10091016
label.as_label(),
10101017
SubApp {

crates/bevy_core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod name;
66
mod serde;
77
mod task_pool_options;
88

9+
use bevy_ecs::schedule::MainThreadExecutor;
910
use bevy_ecs::system::Resource;
1011
pub use bytemuck::{bytes_of, cast_slice, Pod, Zeroable};
1112
pub use name::*;
@@ -40,6 +41,7 @@ impl Plugin for CorePlugin {
4041
fn build(&self, app: &mut App) {
4142
// Setup the default bevy task pools
4243
self.task_pool_options.create_default_pools();
44+
app.insert_resource(MainThreadExecutor::new());
4345

4446
#[cfg(not(target_arch = "wasm32"))]
4547
app.add_system_to_stage(

crates/bevy_core/src/task_pool_options.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use bevy_ecs::prelude::Resource;
2-
use bevy_tasks::{
3-
AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool, MainThreadExecutor, TaskPoolBuilder,
4-
};
2+
use bevy_tasks::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool, TaskPoolBuilder};
53
use bevy_utils::tracing::trace;
64

75
/// Defines a simple way to determine how many threads to use given the number of remaining cores
@@ -151,9 +149,5 @@ impl TaskPoolOptions {
151149
.build()
152150
});
153151
}
154-
155-
{
156-
MainThreadExecutor::init();
157-
}
158152
}
159153
}

crates/bevy_ecs/src/query/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
10071007
) {
10081008
// NOTE: If you are changing query iteration code, remember to update the following places, where relevant:
10091009
// QueryIter, QueryIterationCursor, QueryManyIter, QueryCombinationIter, QueryState::for_each_unchecked_manual, QueryState::par_for_each_unchecked_manual
1010-
ComputeTaskPool::get().scope(|scope| {
1010+
ComputeTaskPool::get().scope(None, |scope| {
10111011
if Q::IS_DENSE && F::IS_DENSE {
10121012
let tables = &world.storages().tables;
10131013
for table_id in &self.matched_table_ids {

crates/bevy_ecs/src/schedule/executor_parallel.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
use std::sync::Arc;
2+
3+
use crate as bevy_ecs;
14
use crate::{
25
archetype::ArchetypeComponentId,
36
query::Access,
47
schedule::{ParallelSystemExecutor, SystemContainer},
8+
system::Resource,
59
world::World,
610
};
711
use async_channel::{Receiver, Sender};
8-
use bevy_tasks::{ComputeTaskPool, Scope, TaskPool};
12+
use bevy_tasks::{ComputeTaskPool, Scope, TaskPool, ThreadExecutor};
913
#[cfg(feature = "trace")]
1014
use bevy_utils::tracing::Instrument;
1115
use event_listener::Event;
@@ -14,6 +18,22 @@ use fixedbitset::FixedBitSet;
1418
#[cfg(test)]
1519
use scheduling_event::*;
1620

21+
///
22+
#[derive(Resource, Default)]
23+
pub struct MainThreadExecutor(pub Arc<ThreadExecutor>);
24+
25+
impl MainThreadExecutor {
26+
pub fn new() -> Self {
27+
MainThreadExecutor(Arc::new(ThreadExecutor::new()))
28+
}
29+
}
30+
31+
impl Clone for MainThreadExecutor {
32+
fn clone(&self) -> Self {
33+
MainThreadExecutor(self.0.clone())
34+
}
35+
}
36+
1737
struct SystemSchedulingMetadata {
1838
/// Used to signal the system's task to start the system.
1939
start: Event,
@@ -124,7 +144,11 @@ impl ParallelSystemExecutor for ParallelExecutor {
124144
}
125145
}
126146

127-
ComputeTaskPool::init(TaskPool::default).scope(|scope| {
147+
let thread_executor = world
148+
.get_resource::<MainThreadExecutor>()
149+
.map(|e| e.0.clone());
150+
151+
ComputeTaskPool::init(TaskPool::default).scope(thread_executor, |scope| {
128152
self.prepare_systems(scope, systems, world);
129153
if self.should_run.count_ones(..) == 0 {
130154
return;
@@ -236,7 +260,7 @@ impl ParallelExecutor {
236260
if system_data.is_send {
237261
scope.spawn(task);
238262
} else {
239-
scope.spawn_on_main(task);
263+
scope.spawn_on_scope(task);
240264
}
241265

242266
#[cfg(test)]
@@ -271,7 +295,7 @@ impl ParallelExecutor {
271295
if system_data.is_send {
272296
scope.spawn(task);
273297
} else {
274-
scope.spawn_on_main(task);
298+
scope.spawn_on_scope(task);
275299
}
276300
}
277301
}

crates/bevy_gltf/src/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ async fn load_gltf<'a, 'b>(
409409
} else {
410410
#[cfg(not(target_arch = "wasm32"))]
411411
IoTaskPool::get()
412-
.scope(|scope| {
412+
.scope(None, |scope| {
413413
gltf.textures().for_each(|gltf_texture| {
414414
let linear_textures = &linear_textures;
415415
let load_context: &LoadContext = load_context;

crates/bevy_tasks/examples/busy_behavior.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
.build();
1212

1313
let t0 = instant::Instant::now();
14-
pool.scope(|s| {
14+
pool.scope(None, |s| {
1515
for i in 0..40 {
1616
s.spawn(async move {
1717
let now = instant::Instant::now();

crates/bevy_tasks/examples/idle_behavior.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
.thread_name("Idle Behavior ThreadPool".to_string())
1010
.build();
1111

12-
pool.scope(|s| {
12+
pool.scope(None, |s| {
1313
for i in 0..1 {
1414
s.spawn(async move {
1515
println!("Blocking for 10 seconds");

crates/bevy_tasks/src/iter/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ where
3434
///
3535
/// See [`Iterator::count()`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.count)
3636
fn count(mut self, pool: &TaskPool) -> usize {
37-
pool.scope(|s| {
37+
pool.scope(None, |s| {
3838
while let Some(batch) = self.next_batch() {
3939
s.spawn(async move { batch.count() });
4040
}
@@ -105,7 +105,7 @@ where
105105
where
106106
F: FnMut(BatchIter::Item) + Send + Clone + Sync,
107107
{
108-
pool.scope(|s| {
108+
pool.scope(None, |s| {
109109
while let Some(batch) = self.next_batch() {
110110
let newf = f.clone();
111111
s.spawn(async move {
@@ -195,7 +195,7 @@ where
195195
C: std::iter::FromIterator<BatchIter::Item>,
196196
BatchIter::Item: Send + 'static,
197197
{
198-
pool.scope(|s| {
198+
pool.scope(None, |s| {
199199
while let Some(batch) = self.next_batch() {
200200
s.spawn(async move { batch.collect::<Vec<_>>() });
201201
}
@@ -216,7 +216,7 @@ where
216216
BatchIter::Item: Send + 'static,
217217
{
218218
let (mut a, mut b) = <(C, C)>::default();
219-
pool.scope(|s| {
219+
pool.scope(None, |s| {
220220
while let Some(batch) = self.next_batch() {
221221
let newf = f.clone();
222222
s.spawn(async move { batch.partition::<Vec<_>, F>(newf) });
@@ -242,7 +242,7 @@ where
242242
F: FnMut(C, BatchIter::Item) -> C + Send + Sync + Clone,
243243
C: Clone + Send + Sync + 'static,
244244
{
245-
pool.scope(|s| {
245+
pool.scope(None, |s| {
246246
while let Some(batch) = self.next_batch() {
247247
let newf = f.clone();
248248
let newi = init.clone();
@@ -260,7 +260,7 @@ where
260260
where
261261
F: FnMut(BatchIter::Item) -> bool + Send + Sync + Clone,
262262
{
263-
pool.scope(|s| {
263+
pool.scope(None, |s| {
264264
while let Some(mut batch) = self.next_batch() {
265265
let newf = f.clone();
266266
s.spawn(async move { batch.all(newf) });
@@ -279,7 +279,7 @@ where
279279
where
280280
F: FnMut(BatchIter::Item) -> bool + Send + Sync + Clone,
281281
{
282-
pool.scope(|s| {
282+
pool.scope(None, |s| {
283283
while let Some(mut batch) = self.next_batch() {
284284
let newf = f.clone();
285285
s.spawn(async move { batch.any(newf) });
@@ -299,7 +299,7 @@ where
299299
where
300300
F: FnMut(BatchIter::Item) -> bool + Send + Sync + Clone,
301301
{
302-
let poses = pool.scope(|s| {
302+
let poses = pool.scope(None, |s| {
303303
while let Some(batch) = self.next_batch() {
304304
let mut newf = f.clone();
305305
s.spawn(async move {
@@ -332,7 +332,7 @@ where
332332
where
333333
BatchIter::Item: Ord + Send + 'static,
334334
{
335-
pool.scope(|s| {
335+
pool.scope(None, |s| {
336336
while let Some(batch) = self.next_batch() {
337337
s.spawn(async move { batch.max() });
338338
}
@@ -349,7 +349,7 @@ where
349349
where
350350
BatchIter::Item: Ord + Send + 'static,
351351
{
352-
pool.scope(|s| {
352+
pool.scope(None, |s| {
353353
while let Some(batch) = self.next_batch() {
354354
s.spawn(async move { batch.min() });
355355
}
@@ -368,7 +368,7 @@ where
368368
F: FnMut(&BatchIter::Item) -> R + Send + Sync + Clone,
369369
BatchIter::Item: Send + 'static,
370370
{
371-
pool.scope(|s| {
371+
pool.scope(None, |s| {
372372
while let Some(batch) = self.next_batch() {
373373
let newf = f.clone();
374374
s.spawn(async move { batch.max_by_key(newf) });
@@ -388,7 +388,7 @@ where
388388
F: FnMut(&BatchIter::Item, &BatchIter::Item) -> std::cmp::Ordering + Send + Sync + Clone,
389389
BatchIter::Item: Send + 'static,
390390
{
391-
pool.scope(|s| {
391+
pool.scope(None, |s| {
392392
while let Some(batch) = self.next_batch() {
393393
let newf = f.clone();
394394
s.spawn(async move { batch.max_by(newf) });
@@ -408,7 +408,7 @@ where
408408
F: FnMut(&BatchIter::Item) -> R + Send + Sync + Clone,
409409
BatchIter::Item: Send + 'static,
410410
{
411-
pool.scope(|s| {
411+
pool.scope(None, |s| {
412412
while let Some(batch) = self.next_batch() {
413413
let newf = f.clone();
414414
s.spawn(async move { batch.min_by_key(newf) });
@@ -428,7 +428,7 @@ where
428428
F: FnMut(&BatchIter::Item, &BatchIter::Item) -> std::cmp::Ordering + Send + Sync + Clone,
429429
BatchIter::Item: Send + 'static,
430430
{
431-
pool.scope(|s| {
431+
pool.scope(None, |s| {
432432
while let Some(batch) = self.next_batch() {
433433
let newf = f.clone();
434434
s.spawn(async move { batch.min_by(newf) });
@@ -482,7 +482,7 @@ where
482482
S: std::iter::Sum<BatchIter::Item> + Send + 'static,
483483
R: std::iter::Sum<S>,
484484
{
485-
pool.scope(|s| {
485+
pool.scope(None, |s| {
486486
while let Some(batch) = self.next_batch() {
487487
s.spawn(async move { batch.sum() });
488488
}
@@ -499,7 +499,7 @@ where
499499
S: std::iter::Product<BatchIter::Item> + Send + 'static,
500500
R: std::iter::Product<S>,
501501
{
502-
pool.scope(|s| {
502+
pool.scope(None, |s| {
503503
while let Some(batch) = self.next_batch() {
504504
s.spawn(async move { batch.product() });
505505
}

crates/bevy_tasks/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use usages::tick_global_task_pools_on_main_thread;
2323
pub use usages::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool};
2424

2525
mod main_thread_executor;
26-
pub use main_thread_executor::MainThreadExecutor;
26+
pub use main_thread_executor::ThreadExecutor;
2727

2828
mod iter;
2929
pub use iter::ParallelIterator;

0 commit comments

Comments
 (0)