-
Notifications
You must be signed in to change notification settings - Fork 910
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
On Web, implement Send
and Sync
where appropriate
#2834
Conversation
Send
and `Sync where appropriateSend
and Sync
where appropriate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I've seen that! I was wondering if this thing deserves it's own crate or not ...
# 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.
# 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 implements
Send
andSync
forWindow
andSend
forEventLoopProxy
on Wasm to make them implement the same traits on all platforms.I took some inspiration from
MainThreadSafe
on macOS.It's implemented in a way that if you call this from the
Window
it 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::Sender
to 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
Condvar
and aMutex
is 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.