On Web, implement Send and Sync where appropriate#2834
Merged
daxpedda merged 2 commits intorust-windowing:masterfrom Jun 5, 2023
Merged
On Web, implement Send and Sync where appropriate#2834daxpedda merged 2 commits intorust-windowing:masterfrom
Send and Sync where appropriate#2834daxpedda merged 2 commits intorust-windowing:masterfrom
Conversation
Send and `Sync where appropriateSend and Sync where appropriate
madsmtm
reviewed
Jun 1, 2023
Member
madsmtm
left a comment
There was a problem hiding this comment.
I like the clippy lint, seems like a good way to go.
|
|
||
| // Unsafe wrapper type that allows us to use `T` when it's not `Send` from other threads. | ||
| // `value` **must** only be accessed on the main thread. | ||
| pub struct MainThreadSafe<const SYNC: bool, T: 'static, E: 'static> { |
Member
There was a problem hiding this comment.
Since you're referencing MainThreadSafe, I'll note that I've since made MainThreadBound, you may be able to take some inspiration there as well.
Member
Author
There was a problem hiding this comment.
Yeah, I've seen that! I was wondering if this thing deserves it's own crate or not ...
This was referenced Jun 5, 2023
Closed
github-merge-queue bot
pushed a commit
to bevyengine/bevy
that referenced
this pull request
Jan 9, 2024
# Objective In the past `winit::window::Window` was not Send + Sync on web. rust-windowing/winit#2834 made `winit::window::Window` Sync + Send so Bevy's `unsafe impl` is no longer necessary. ## Solution Remove the unsafe impls.
Subserial
pushed a commit
to Subserial/bevy_winit_hook
that referenced
this pull request
Feb 20, 2024
# Objective In the past `winit::window::Window` was not Send + Sync on web. rust-windowing/winit#2834 made `winit::window::Window` Sync + Send so Bevy's `unsafe impl` is no longer necessary. ## Solution Remove the unsafe impls.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This implements
SendandSyncforWindowandSendforEventLoopProxyon Wasm to make them implement the same traits on all platforms.I took some inspiration from
MainThreadSafeon macOS.It's implemented in a way that if you call this from the
Windowit will do nothing special and just call the appropriate method on theWindow, as before. Therefor, except a singleRc<Cell<bool>>toArc<AtomicBool>change, there is no overhead if Wasm is only used single-threaded.If used in a multi-threaded environment from a worker, it will use a
mpsc::Senderto do it's work on the main thread. Currently this is implemented by just sending a boxed function to execute, but it could be rewritten to just send a message and a message handler would then do the right thing.When it's necessary to return a value a
Condvarand aMutexis used to await the completion, which would block, but as it's not in the window, where that would fail, thats fine.Most of the small changes here and there are to disallow calls to
web_sys::window(), as these are the main culprits that will fail if not called from a window. This was necessary because it was quite hard to figure out what needs to be changed, so I added a Clippydisallowed-methods, which should hopefully make maintenance of this feature easy. Let me know what you think of this.Fixes #1518.
Replaces #2294.
Replaces #2737.
Replaces #2740.