Skip to content

Commit

Permalink
optional arc/mutex commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Nov 8, 2020
1 parent d4d8fc4 commit 3893541
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
3 changes: 1 addition & 2 deletions crates/bevy_ecs/src/system/commands.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::SystemId;
use crate::resource::{Resource, Resources};
use bevy_hecs::{Bundle, Component, DynamicBundle, Entity, EntityReserver, World};
use parking_lot::Mutex;
use std::{marker::PhantomData, sync::Arc};
use std::marker::PhantomData;

/// A [World] mutation
pub trait Command: Send + Sync {
Expand Down
34 changes: 20 additions & 14 deletions crates/bevy_ecs/src/system/into_system.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
Commands, Resources, System, SystemId, SystemParam, ThreadLocalExecution,
};
use crate::{Commands, Resources, System, SystemId, SystemParam, ThreadLocalExecution};
use bevy_hecs::{ArchetypeComponent, QueryAccess, TypeAccess, World};
use std::{any::TypeId, borrow::Cow};
use parking_lot::Mutex;
use std::{any::TypeId, borrow::Cow, sync::Arc};

pub struct SystemState {
pub(crate) id: SystemId,
Expand All @@ -14,6 +13,7 @@ pub struct SystemState {
pub(crate) query_accesses: Vec<Vec<QueryAccess>>,
pub(crate) query_type_names: Vec<&'static str>,
pub(crate) commands: Commands,
pub(crate) arc_commands: Option<Arc<Mutex<Commands>>>,
pub(crate) current_query_index: usize,
}

Expand Down Expand Up @@ -151,6 +151,7 @@ macro_rules! impl_into_system {
is_initialized: false,
id: SystemId::new(),
commands: Commands::default(),
arc_commands: Default::default(),
query_archetype_component_accesses: Vec::new(),
query_accesses: Vec::new(),
query_type_names: Vec::new(),
Expand All @@ -166,6 +167,10 @@ macro_rules! impl_into_system {
},
thread_local_func: |state, world, resources| {
state.commands.apply(world, resources);
if let Some(ref commands) = state.arc_commands {
let mut commands = commands.lock();
commands.apply(world, resources);
}
},
init_func: |state, world, resources| {
$($param::init(state, world, resources);)*
Expand Down Expand Up @@ -274,14 +279,12 @@ mod tests {
fn or_query_set_system() {
// Regression test for issue #762
use crate::{Added, Changed, Mutated, Or};
let query_system = move |
mut ran: ResMut<bool>,
set: QuerySet<(
Query<Or<(Changed<A>, Changed<B>)>>,
Query<Or<(Added<A>, Added<B>)>>,
Query<Or<(Mutated<A>, Mutated<B>)>>,
)>,
| {
let query_system = move |mut ran: ResMut<bool>,
set: QuerySet<(
Query<Or<(Changed<A>, Changed<B>)>>,
Query<Or<(Added<A>, Added<B>)>>,
Query<Or<(Mutated<A>, Mutated<B>)>>,
)>| {
let changed = set.q0().iter().count();
let added = set.q1().iter().count();
let mutated = set.q2().iter().count();
Expand Down Expand Up @@ -334,7 +337,10 @@ mod tests {

#[test]
fn changed_resource_or_system() {
fn incr_e_on_flip(_or: Or<(Option<ChangedRes<bool>>, Option<ChangedRes<i32>>)>, mut query: Query<&mut i32>) {
fn incr_e_on_flip(
_or: Or<(Option<ChangedRes<bool>>, Option<ChangedRes<i32>>)>,
mut query: Query<&mut i32>,
) {
for mut i in query.iter_mut() {
*i += 1;
}
Expand Down Expand Up @@ -363,7 +369,7 @@ mod tests {

schedule.run(&mut world, &mut resources);
assert_eq!(*(world.get::<i32>(ent).unwrap()), 2);

*resources.get_mut::<i32>().unwrap() = 20;
schedule.run(&mut world, &mut resources);
assert_eq!(*(world.get::<i32>(ent).unwrap()), 3);
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_ecs/src/system/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ pub enum QueryError {

impl<'a, Q: HecsQuery> Query<'a, Q> {
#[inline]
pub unsafe fn new(world: &'a World, component_access: &'a TypeAccess<ArchetypeComponent>) -> Self {
pub unsafe fn new(
world: &'a World,
component_access: &'a TypeAccess<ArchetypeComponent>,
) -> Self {
Self {
world,
component_access,
Expand Down
22 changes: 21 additions & 1 deletion crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::{
ResourceIndex, Resources, SystemState,
};
use bevy_hecs::{ArchetypeComponent, Fetch, Or, Query as HecsQuery, TypeAccess, World};
use std::any::TypeId;
use parking_lot::Mutex;
use std::{any::TypeId, sync::Arc};

pub trait SystemParam: Sized {
fn init(system_state: &mut SystemState, world: &World, resources: &mut Resources);
Expand Down Expand Up @@ -88,6 +89,25 @@ impl<'a> SystemParam for &'a mut Commands {
}
}

impl SystemParam for Arc<Mutex<Commands>> {
fn init(system_state: &mut SystemState, world: &World, _resources: &mut Resources) {
system_state.arc_commands.get_or_insert_with(|| {
let mut commands = Commands::default();
commands.set_entity_reserver(world.get_entity_reserver());
Arc::new(Mutex::new(commands))
});
}

#[inline]
unsafe fn get_param(
system_state: &mut SystemState,
_world: &World,
_resources: &Resources,
) -> Option<Self> {
Some(system_state.arc_commands.as_ref().unwrap().clone())
}
}

impl<'a, T: Resource> SystemParam for Res<'a, T> {
fn init(system_state: &mut SystemState, _world: &World, _resources: &mut Resources) {
system_state.resource_access.add_read(TypeId::of::<T>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use crate::{
};

use bevy_asset::{Asset, Assets, Handle, HandleId};
use bevy_ecs::{
Commands, Entity, IntoSystem, Local, Query, Res, ResMut, Resources, System, World,
};
use bevy_ecs::{Commands, Entity, IntoSystem, Local, Query, Res, ResMut, Resources, System, World};
use bevy_utils::HashMap;
use renderer::{AssetRenderResourceBindings, BufferId, RenderResourceType, RenderResources};
use std::{hash::Hash, marker::PhantomData, ops::DerefMut};
Expand Down

0 comments on commit 3893541

Please sign in to comment.