Skip to content

Move Non Send Resources into thread locals #5135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,10 @@ impl AddAsset for App {
let mut app = self
.world
.non_send_resource_mut::<crate::debug_asset_server::DebugAssetApp>();
app.add_asset::<T>()
.init_resource::<crate::debug_asset_server::HandleMap<T>>();
app.get_mut(|app| {
app.add_asset::<T>()
.init_resource::<crate::debug_asset_server::HandleMap<T>>();
});
}
self
}
Expand All @@ -335,7 +337,9 @@ impl AddAsset for App {
let mut app = self
.world
.non_send_resource_mut::<crate::debug_asset_server::DebugAssetApp>();
app.init_asset_loader::<T>();
app.get_mut(|app| {
app.init_asset_loader::<T>();
});
}
self
}
Expand All @@ -357,13 +361,15 @@ macro_rules! load_internal_asset {
let mut debug_app = $app
.world
.non_send_resource_mut::<$crate::debug_asset_server::DebugAssetApp>();
$crate::debug_asset_server::register_handle_with_loader(
$loader,
&mut debug_app,
$handle,
file!(),
$path_str,
);
debug_app.get_mut(|mut debug_app| {
$crate::debug_asset_server::register_handle_with_loader(
$loader,
&mut debug_app,
$handle,
file!(),
$path_str,
);
});
}
let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
assets.set_untracked($handle, ($loader)(include_str!($path_str)));
Expand Down
38 changes: 21 additions & 17 deletions crates/bevy_asset/src/debug_asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,35 @@ impl Plugin for DebugAssetServerPlugin {
}

fn run_debug_asset_app(mut debug_asset_app: NonSendMut<DebugAssetApp>) {
debug_asset_app.0.update();
debug_asset_app.get_mut(|debug_asset_app| {
debug_asset_app.0.update();
});
}

pub(crate) fn sync_debug_assets<T: Asset + Clone>(
mut debug_asset_app: NonSendMut<DebugAssetApp>,
mut assets: ResMut<Assets<T>>,
) {
let world = &mut debug_asset_app.0.world;
let mut state = SystemState::<(
Res<Events<AssetEvent<T>>>,
Res<HandleMap<T>>,
Res<Assets<T>>,
)>::new(world);
let (changed_shaders, handle_map, debug_assets) = state.get_mut(world);
for changed in changed_shaders.iter_current_update_events() {
let debug_handle = match changed {
AssetEvent::Created { handle } | AssetEvent::Modified { handle } => handle,
AssetEvent::Removed { .. } => continue,
};
if let Some(handle) = handle_map.handles.get(debug_handle) {
if let Some(debug_asset) = debug_assets.get(debug_handle) {
assets.set_untracked(handle, debug_asset.clone());
debug_asset_app.get_mut(|debug_asset_app| {
let world = &mut debug_asset_app.0.world;
let mut state = SystemState::<(
Res<Events<AssetEvent<T>>>,
Res<HandleMap<T>>,
Res<Assets<T>>,
)>::new(world);
let (changed_shaders, handle_map, debug_assets) = state.get_mut(world);
for changed in changed_shaders.iter_current_update_events() {
let debug_handle = match changed {
AssetEvent::Created { handle } | AssetEvent::Modified { handle } => handle,
AssetEvent::Removed { .. } => continue,
};
if let Some(handle) = handle_map.handles.get(debug_handle) {
if let Some(debug_asset) = debug_assets.get(debug_handle) {
assets.set_untracked(handle, debug_asset.clone());
}
}
}
}
});
}

/// Uses the return type of the given loader to register the given handle with the appropriate type
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_audio/src/audio_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ pub fn play_queued_audio_system<Source: Asset + Decodable>(
mut sinks: ResMut<Assets<AudioSink>>,
) {
if let Some(audio_sources) = audio_sources {
audio_output.try_play_queued(&*audio_sources, &mut *audio, &mut *sinks);
audio_output.get(|audio_output| {
audio_output.try_play_queued(&*audio_sources, &mut *audio, &mut *sinks);
});
};
}

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bevy_ecs_macros = { path = "macros", version = "0.8.0-dev" }

async-channel = "1.4"
thread_local = "1.1.4"
thread-local-object = "0.1.0"
fixedbitset = "0.4"
fxhash = "0.2"
downcast-rs = "1.2"
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Types that detect when their internal data mutate.

use crate::{component::ComponentTicks, ptr::PtrMut, system::Resource};
use crate::{component::ComponentTicks, ptr::PtrMut, system::Resource, world::ThreadLocalResource};
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -198,12 +198,12 @@ impl_debug!(ResMut<'a, T>, Resource);
///
/// Use `Option<NonSendMut<T>>` instead if the resource might not always exist.
pub struct NonSendMut<'a, T: 'static> {
pub(crate) value: &'a mut T,
pub(crate) value: &'a mut ThreadLocalResource<T>,
pub(crate) ticks: Ticks<'a>,
}

change_detection_impl!(NonSendMut<'a, T>, T,);
impl_into_inner!(NonSendMut<'a, T>, T,);
change_detection_impl!(NonSendMut<'a, T>, ThreadLocalResource<T>,);
impl_into_inner!(NonSendMut<'a, T>, ThreadLocalResource<T>,);
impl_debug!(NonSendMut<'a, T>,);

/// Unique mutable borrow of an entity's component
Expand Down
11 changes: 8 additions & 3 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,13 @@ mod tests {
let mut world = World::default();
world.insert_non_send_resource(123i32);
world.insert_non_send_resource(456i64);
assert_eq!(*world.non_send_resource::<i32>(), 123);
assert_eq!(*world.non_send_resource_mut::<i64>(), 456);
world.non_send_resource::<i32>().get(|res| {
assert_eq!(*res, 123);
});

world.non_send_resource_mut::<i64>().get(|res| {
assert_eq!(*res, 456);
});
}

#[test]
Expand All @@ -1199,7 +1204,7 @@ mod tests {
let mut world = World::default();
world.insert_non_send_resource(0i32);
std::thread::spawn(move || {
let _ = world.non_send_resource_mut::<i32>();
world.non_send_resource_mut::<i32>().get(|_| {});
})
.join()
.unwrap();
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_ecs/src/schedule/executor_parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,9 @@ mod tests {
let mut world = World::new();
world.insert_non_send_resource(thread::current().id());
fn non_send(thread_id: NonSend<thread::ThreadId>) {
assert_eq!(thread::current().id(), *thread_id);
thread_id.get(|thread_id| {
assert_eq!(thread::current().id(), *thread_id);
});
}
fn empty() {}
let mut stage = SystemStage::parallel()
Expand Down
14 changes: 13 additions & 1 deletion crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,19 @@ mod tests {
world.insert_non_send_resource(NotSend1(std::rc::Rc::new(1)));
world.insert_non_send_resource(NotSend2(std::rc::Rc::new(2)));

fn sys(_op: NonSend<NotSend1>, mut _op2: NonSendMut<NotSend2>, mut run: ResMut<bool>) {
fn sys(
non_send: NonSend<NotSend1>,
mut non_send_mut: NonSendMut<NotSend2>,
mut run: ResMut<bool>,
) {
non_send.get(|non_send| {
assert_eq!(*non_send.0, 1);
});

non_send_mut.get_mut(|non_send_mut| {
assert_eq!(*non_send_mut.0, 2);
});

*run = true;
}

Expand Down
26 changes: 15 additions & 11 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
Access, FilteredAccess, FilteredAccessSet, QueryState, ReadOnlyWorldQuery, WorldQuery,
},
system::{CommandQueue, Commands, Query, SystemMeta},
world::ThreadLocalResource,
world::{FromWorld, World},
};
pub use bevy_ecs_macros::SystemParam;
Expand Down Expand Up @@ -797,7 +798,7 @@ impl<'w, 's, T: Component> SystemParamFetch<'w, 's> for RemovedComponentsState<T
///
/// Use `Option<NonSend<T>>` instead if the resource might not always exist.
pub struct NonSend<'w, T: 'static> {
pub(crate) value: &'w T,
pub(crate) value: &'w ThreadLocalResource<T>,
ticks: ComponentTicks,
last_change_tick: u32,
change_tick: u32,
Expand All @@ -811,7 +812,8 @@ where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("NonSend").field(&self.value).finish()
self.value
.get(|value| f.debug_tuple("NonSend").field(value).finish())
}
}

Expand All @@ -829,7 +831,7 @@ impl<'w, T: 'static> NonSend<'w, T> {
}

impl<'w, T> Deref for NonSend<'w, T> {
type Target = T;
type Target = ThreadLocalResource<T>;

fn deref(&self) -> &Self::Target {
self.value
Expand Down Expand Up @@ -897,7 +899,6 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for NonSendState<T> {
world: &'w World,
change_tick: u32,
) -> Self::Item {
world.validate_non_send_access::<T>();
let column = world
.get_populated_resource_column(state.component_id)
.unwrap_or_else(|| {
Expand All @@ -909,7 +910,7 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for NonSendState<T> {
});

NonSend {
value: column.get_data_ptr().deref::<T>(),
value: column.get_data_ptr().deref::<ThreadLocalResource<T>>(),
ticks: column.get_ticks_unchecked(0).read(),
last_change_tick: system_meta.last_change_tick,
change_tick,
Expand Down Expand Up @@ -945,11 +946,10 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for OptionNonSendState<T> {
world: &'w World,
change_tick: u32,
) -> Self::Item {
world.validate_non_send_access::<T>();
world
.get_populated_resource_column(state.0.component_id)
.map(|column| NonSend {
value: column.get_data_ptr().deref::<T>(),
value: column.get_data_ptr().deref::<ThreadLocalResource<T>>(),
ticks: column.get_ticks_unchecked(0).read(),
last_change_tick: system_meta.last_change_tick,
change_tick,
Expand Down Expand Up @@ -1011,7 +1011,6 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for NonSendMutState<T> {
world: &'w World,
change_tick: u32,
) -> Self::Item {
world.validate_non_send_access::<T>();
let column = world
.get_populated_resource_column(state.component_id)
.unwrap_or_else(|| {
Expand All @@ -1022,7 +1021,10 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for NonSendMutState<T> {
)
});
NonSendMut {
value: column.get_data_ptr().assert_unique().deref_mut::<T>(),
value: column
.get_data_ptr()
.assert_unique()
.deref_mut::<ThreadLocalResource<T>>(),
ticks: Ticks {
component_ticks: column.get_ticks_unchecked(0).deref_mut(),
last_change_tick: system_meta.last_change_tick,
Expand Down Expand Up @@ -1057,11 +1059,13 @@ impl<'w, 's, T: 'static> SystemParamFetch<'w, 's> for OptionNonSendMutState<T> {
world: &'w World,
change_tick: u32,
) -> Self::Item {
world.validate_non_send_access::<T>();
world
.get_populated_resource_column(state.0.component_id)
.map(|column| NonSendMut {
value: column.get_data_ptr().assert_unique().deref_mut::<T>(),
value: column
.get_data_ptr()
.assert_unique()
.deref_mut::<ThreadLocalResource<T>>(),
ticks: Ticks {
component_ticks: column.get_ticks_unchecked(0).deref_mut(),
last_change_tick: system_meta.last_change_tick,
Expand Down
Loading