Skip to content

Replace some !Send resources with thread_local! #17730

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

Merged
merged 24 commits into from
Mar 4, 2025

Conversation

joshua-holmes
Copy link
Contributor

@joshua-holmes joshua-holmes commented Feb 7, 2025

Objective

Work for issue #17682

What's in this PR:

  • Removal of some !Send resources that Bevy uses internally
  • Replaces !Send resources with thread_local! static

What this PR does not cover:

  • The ability to create !Send resources still exists
  • Tests that test !Send resources are present (and should not be removed until the ability to create !Send resources is removed)
  • The example log_layers_ecs still uses a !Send resource. In this example, removing the !Send resource results in the system that uses it running on a thread other than the main thread, which doesn't work with lazily initialized thread_local! static data. Removing this !Send resource will need to be deferred until the System API is extended to support configuring which thread the System runs on. Once an issue for this work is created, it will be mentioned in 🐢 Eliminate !Send resources by supporting Send and !Send World in the same binary #17667

Once the System API is extended to allow control of which thread the System runs on, the rest of the !Send resources can be removed in a different PR.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events M-Needs-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 9, 2025
@joshua-holmes joshua-holmes marked this pull request as ready for review February 16, 2025 03:06
@urben1680
Copy link
Contributor

urben1680 commented Feb 18, 2025

Won't this cause multiple worlds to share the same !Send resources?

@joshua-holmes
Copy link
Contributor Author

joshua-holmes commented Feb 19, 2025

Yes, in theory multiple worlds could share these resources. However, in practice in this PR, that is not happening.

Keep in mind, I am not changing the API in any way. I am only changing how a few internal variables are stored.

@joshua-holmes
Copy link
Contributor Author

@alice-i-cecile, just pinging you here for review on GitHub as well, in case that's your preferred workflow

@joshua-holmes joshua-holmes changed the title Replace !Send resources with thread_local! Replace some !Send resources with thread_local! Feb 22, 2025
Copy link
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Feb 23, 2025
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code quality is good, and I'm broadly on board with the direction, but I don't personally have the experience with these primitives required to verify correctness. @maniwani or @hymm , can I defer to you?

Copy link
Contributor

@hymm hymm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are fairly straightforward. I think passing the EventLoop through a closure makes way more sense than smuggling it through a non send resource. My one worry is that nothing is forcing the gilrs systems to run on the main thread anymore other than the fact that wasm is single threaded. Maybe not too big of a deal though as it'll just panic with the thread local not being initialized and should be easy to fix if we do get wasm multithreading.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed M-Needs-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 27, 2025
@aloucks
Copy link
Contributor

aloucks commented Mar 1, 2025

gilrs uses mpsc::{Sender, Receiver} under the hood. From what I can tell, the Receiver is what causes Gilrs to be !Sync. I suspect that if the system is bouncing between threads and hitting different Gilrs instances (i.e. with different pairs of sender/receivers), you're going to have broken gamepad input. I haven't actually tested with a gamepad or Gilrs and I've come to this conclusion only by looking through the Gilrs code.

EDIT1: Was there some reason why you used thread_local! instead of Mutex<Gilrs> ?
EDIT2: I now see that it's also !Send in addition to !Sync on wasm32.

I think passing the EventLoop through a closure makes way more sense than smuggling it through a non send resource.

Agreed. I think this will help. However, the WinitWindows struct is still added via init_non_send_resource, which still prevents the App/World from being Send.

Allowing the App/World to be Send opens up some other options I'd like to explore with bevy_winit. I'm wondering if there's some design work opportunities for some kind of "lower level" systems that do not live inside the World and only executes on the "main" thread. These hypothetical type of systems would live along side the runner and the runner may be responsible for triggering them. I'll try to articulate this in more detail later this weekend.

@joshua-holmes
Copy link
Contributor Author

joshua-holmes commented Mar 3, 2025

@aloucks

Was there some reason why you used thread_local! instead of Mutex ?

I don't recall ever discussing that, so I'm going to say no. Looking at the gilrs code, I see your concern for using Receiver with thread_local! if we were using multiple threads with thread_local!. However, considering we are only using thread_local! with wasm here, I can't see a way that it would break anything.

However, the WinitWindows struct is still added via init_non_send_resource

You are correct. This PR does not completely get rid of !Send resources. We will need one more PR for that.

which still prevents the App/World from being Send.

Correct me if I'm wrong, but I believe the world is Send with the exception of !Send resources. We force the World to be Send here and just tell the user not to access !Send resources in a different thread, or else the program will panic.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 4, 2025
Merged via the queue into bevyengine:main with commit df6e136 Mar 4, 2025
30 checks passed
github-merge-queue bot pushed a commit that referenced this pull request May 6, 2025
# Objective

Remaining work for and closes #17682. First half of work for that issue
was completed in [PR
17730](#17730). However, the rest
of the work ended up getting blocked because we needed a way of forcing
systems to run on the main thread without the use of `!Send` resources.
That was unblocked by [PR
18301](#18301).

This work should finish unblocking the resources-as-components effort.

# Testing

Ran several examples using my Linux machine, just to make sure things
are working as expected and no surprises pop up.

---------

Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com>
Co-authored-by: François Mockers <francois.mockers@vleue.com>
andrewzhurov pushed a commit to andrewzhurov/bevy that referenced this pull request May 17, 2025
# Objective

Remaining work for and closes bevyengine#17682. First half of work for that issue
was completed in [PR
17730](bevyengine#17730). However, the rest
of the work ended up getting blocked because we needed a way of forcing
systems to run on the main thread without the use of `!Send` resources.
That was unblocked by [PR
18301](bevyengine#18301).

This work should finish unblocking the resources-as-components effort.

# Testing

Ran several examples using my Linux machine, just to make sure things
are working as expected and no surprises pop up.

---------

Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com>
Co-authored-by: François Mockers <francois.mockers@vleue.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants