Skip to content
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

Non send as thread marker + TLS #6657

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
33 changes: 0 additions & 33 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,29 +707,6 @@ impl App {
self
}

/// Inserts a non-send resource to the app.
///
/// You usually want to use [`insert_resource`](Self::insert_resource),
/// but there are some special cases when a resource cannot be sent across threads.
///
/// # Examples
///
/// ```
/// # use bevy_app::prelude::*;
/// # use bevy_ecs::prelude::*;
/// #
/// struct MyCounter {
/// counter: usize,
/// }
///
/// App::new()
/// .insert_non_send_resource(MyCounter { counter: 0 });
/// ```
pub fn insert_non_send_resource<R: 'static>(&mut self, resource: R) -> &mut Self {
self.world.insert_non_send_resource(resource);
self
}

/// Initialize a [`Resource`] with standard starting values by adding it to the [`World`].
///
/// If the [`Resource`] already exists, nothing happens.
Expand Down Expand Up @@ -765,16 +742,6 @@ impl App {
self
}

/// Initialize a non-send [`Resource`] with standard starting values by adding it to the [`World`].
///
/// The [`Resource`] must implement the [`FromWorld`] trait.
/// If the [`Default`] trait is implemented, the [`FromWorld`] trait will use
/// the [`Default::default`] method to initialize the [`Resource`].
pub fn init_non_send_resource<R: 'static + FromWorld>(&mut self) -> &mut Self {
self.world.init_non_send_resource::<R>();
self
}

/// Sets the function that will be called when the app is run.
///
/// The runner function `run_fn` is called only once by [`App::run`]. If the
Expand Down
50 changes: 29 additions & 21 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,11 @@ impl AddAsset for App {
self.add_system(crate::debug_asset_server::sync_debug_assets::<T>);
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>>();
.resource_mut::<bevy_ecs::system::Tls<crate::debug_asset_server::DebugAssetApp>>();
app.get_mut(|app| {
app.add_asset::<T>()
.init_resource::<crate::debug_asset_server::HandleMap<T>>();
});
}
self
}
Expand All @@ -390,8 +392,10 @@ impl AddAsset for App {
{
let mut app = self
.world
.non_send_resource_mut::<crate::debug_asset_server::DebugAssetApp>();
app.init_asset_loader::<T>();
.resource_mut::<bevy_ecs::system::Tls<crate::debug_asset_server::DebugAssetApp>>();
app.get_mut(|app| {
app.init_asset_loader::<T>();
});
}
self
}
Expand All @@ -416,14 +420,16 @@ 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,
);
.resource_mut::<bevy_ecs::system::Tls<$crate::debug_asset_server::DebugAssetApp>>();
debug_app.get_mut(|debug_app| {
$crate::debug_asset_server::register_handle_with_loader(
$loader,
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 Expand Up @@ -454,14 +460,16 @@ macro_rules! load_internal_binary_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,
);
.resource_mut::<bevy_ecs::system::Tls<$crate::debug_asset_server::DebugAssetApp>>();
debug_app.get_mut(|debug_app| {
$crate::debug_asset_server::register_handle_with_loader(
$loader,
debug_app,
$handle,
file!(),
$path_str,
);
});
}
let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
assets.set_untracked($handle, ($loader)(include_bytes!($path_str).as_ref()));
Expand Down
47 changes: 26 additions & 21 deletions crates/bevy_asset/src/debug_asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_app::{App, Plugin};
use bevy_ecs::{
event::Events,
schedule::SystemLabel,
system::{NonSendMut, Res, ResMut, Resource, SystemState},
system::{MainThread, Res, ResMut, Resource, SystemState, Tls},
};
use bevy_tasks::{IoTaskPool, TaskPoolBuilder};
use bevy_utils::HashMap;
Expand Down Expand Up @@ -78,37 +78,42 @@ impl Plugin for DebugAssetServerPlugin {
asset_folder: "crates".to_string(),
watch_for_changes: true,
});
app.insert_non_send_resource(DebugAssetApp(debug_asset_app));
app.insert_resource(Tls::new(DebugAssetApp(debug_asset_app)));
app.add_system(run_debug_asset_app);
}
}

fn run_debug_asset_app(mut debug_asset_app: NonSendMut<DebugAssetApp>) {
debug_asset_app.0.update();
fn run_debug_asset_app(_marker: MainThread, mut debug_asset_app: ResMut<Tls<DebugAssetApp>>) {
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>,
_marker: MainThread,
mut debug_asset_app: ResMut<Tls<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
9 changes: 6 additions & 3 deletions crates/bevy_audio/src/audio_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Audio, AudioSource, Decodable};
use bevy_asset::{Asset, Assets};
use bevy_ecs::system::{NonSend, Res, ResMut};
use bevy_ecs::system::{MainThread, Res, ResMut, Tls};
use bevy_reflect::TypeUuid;
use bevy_utils::tracing::warn;
use rodio::{OutputStream, OutputStreamHandle, Sink, Source};
Expand Down Expand Up @@ -84,13 +84,16 @@ where

/// Plays audio currently queued in the [`Audio`] resource through the [`AudioOutput`] resource
pub fn play_queued_audio_system<Source: Asset + Decodable>(
audio_output: NonSend<AudioOutput<Source>>,
_marker: MainThread,
audio_output: Res<Tls<AudioOutput<Source>>>,
audio_sources: Option<Res<Assets<Source>>>,
mut audio: ResMut<Audio<Source>>,
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
4 changes: 3 additions & 1 deletion crates/bevy_audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub use rodio::Sample;

use bevy_app::prelude::*;
use bevy_asset::AddAsset;
use bevy_ecs::system::Tls;

/// Adds support for audio playback to a Bevy Application
///
Expand All @@ -50,7 +51,8 @@ pub struct AudioPlugin;

impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.init_non_send_resource::<AudioOutput<AudioSource>>()
let audio_output = Tls::new(AudioOutput::<AudioSource>::default());
app.insert_resource(audio_output)
.add_asset::<AudioSource>()
.add_asset::<AudioSink>()
.init_resource::<Audio<AudioSource>>()
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 @@ -23,6 +23,7 @@ bevy_ecs_macros = { path = "macros", version = "0.9.0" }
async-channel = "1.4"
event-listener = "2.5"
thread_local = "1.1.4"
thread-local-object = "0.1.0"
fixedbitset = "0.4"
fxhash = "0.2"
downcast-rs = "1.2"
Expand Down
58 changes: 1 addition & 57 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,38 +285,6 @@ impl<'a, T: Resource> From<ResMut<'a, T>> for Mut<'a, T> {
}
}

/// Unique borrow of a non-[`Send`] resource.
///
/// Only [`Send`] resources may be accessed with the [`ResMut`] [`SystemParam`](crate::system::SystemParam). In case that the
/// resource does not implement `Send`, this `SystemParam` wrapper can be used. This will instruct
/// the scheduler to instead run the system on the main thread so that it doesn't send the resource
/// over to another thread.
///
/// # Panics
///
/// Panics when used as a `SystemParameter` if the resource does not exist.
///
/// Use `Option<NonSendMut<T>>` instead if the resource might not always exist.
pub struct NonSendMut<'a, T: ?Sized + 'static> {
pub(crate) value: &'a mut T,
pub(crate) ticks: Ticks<'a>,
}

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

impl<'a, T: 'static> From<NonSendMut<'a, T>> for Mut<'a, T> {
/// Convert this `NonSendMut` into a `Mut`. This allows keeping the change-detection feature of `Mut`
/// while losing the specificity of `NonSendMut`.
fn from(other: NonSendMut<'a, T>) -> Mut<'a, T> {
Mut {
value: other.value,
ticks: other.ticks,
}
}
}

/// Unique mutable borrow of an entity's component
pub struct Mut<'a, T: ?Sized> {
pub(crate) value: &'a mut T,
Expand Down Expand Up @@ -430,7 +398,7 @@ mod tests {
use crate::{
self as bevy_ecs,
change_detection::{
ComponentTicks, Mut, NonSendMut, ResMut, Ticks, CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE,
ComponentTicks, Mut, ResMut, Ticks, CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE,
},
component::Component,
query::ChangeTrackers,
Expand Down Expand Up @@ -555,30 +523,6 @@ mod tests {
assert_eq!(4, into_mut.ticks.change_tick);
}

#[test]
fn mut_from_non_send_mut() {
let mut component_ticks = ComponentTicks {
added: 1,
changed: 2,
};
let ticks = Ticks {
component_ticks: &mut component_ticks,
last_change_tick: 3,
change_tick: 4,
};
let mut res = R {};
let non_send_mut = NonSendMut {
value: &mut res,
ticks,
};

let into_mut: Mut<R> = non_send_mut.into();
assert_eq!(1, into_mut.ticks.component_ticks.added);
assert_eq!(2, into_mut.ticks.component_ticks.changed);
assert_eq!(3, into_mut.ticks.last_change_tick);
assert_eq!(4, into_mut.ticks.change_tick);
}

#[test]
fn map_mut() {
use super::*;
Expand Down
28 changes: 1 addition & 27 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ use crate::{
};
pub use bevy_ecs_macros::Component;
use bevy_ptr::OwningPtr;
use std::{
alloc::Layout,
any::{Any, TypeId},
borrow::Cow,
mem::needs_drop,
};
use std::{alloc::Layout, any::TypeId, borrow::Cow, mem::needs_drop};

/// A data type that can be used to store data for an [entity].
///
Expand Down Expand Up @@ -335,17 +330,6 @@ impl ComponentDescriptor {
}
}

fn new_non_send<T: Any>(storage_type: StorageType) -> Self {
Self {
name: Cow::Borrowed(std::any::type_name::<T>()),
storage_type,
is_send_and_sync: false,
type_id: Some(TypeId::of::<T>()),
layout: Layout::new::<T>(),
drop: needs_drop::<T>().then_some(Self::drop_ptr::<T> as _),
}
}

#[inline]
pub fn storage_type(&self) -> StorageType {
self.storage_type
Expand Down Expand Up @@ -482,16 +466,6 @@ impl Components {
}
}

#[inline]
pub fn init_non_send<T: Any>(&mut self) -> ComponentId {
// SAFETY: The [`ComponentDescriptor`] matches the [`TypeId`]
unsafe {
self.get_or_insert_resource_with(TypeId::of::<T>(), || {
ComponentDescriptor::new_non_send::<T>(StorageType::default())
})
}
}

/// # Safety
///
/// The [`ComponentDescriptor`] must match the [`TypeId`]
Expand Down
Loading