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

On Web, implement Send and Sync where appropriate #2834

Merged
merged 2 commits into from
Jun 5, 2023

Conversation

daxpedda
Copy link
Member

@daxpedda daxpedda commented Jun 1, 2023

This implements Send and Sync for Window and Send for EventLoopProxy 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 the Window, as before. Therefor, except a single Rc<Cell<bool>> to Arc<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 a Mutex 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 Clippy disallowed-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.

@daxpedda daxpedda changed the title On Web, implement Send and `Sync where appropriate On Web, implement Send and Sync where appropriate Jun 1, 2023
Copy link
Member

@madsmtm madsmtm left a 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> {
Copy link
Member

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.

Copy link
Member Author

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 ...

src/platform/web.rs Show resolved Hide resolved
@daxpedda daxpedda merged commit 8f7f3ef into rust-windowing:master Jun 5, 2023
This was referenced Jun 5, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

Web worker support
2 participants