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

Make default WindowBuilder Send and Sync #3136

Merged

Conversation

kchibisov
Copy link
Member

The default window builder is neither Send nor Sync due to use of RawWindowHandle internally. While we could ensure to make the handle Send + Sync within our use, we can't guarantee that the same will hold true in the future.

Thus add a generic to split the builder into MT-safe and not MT-safe versions.

This could also help with the lifetimes, since default could be bound by static lifetime.

  • Tested on all platforms changed
  • Added an entry to CHANGELOG.md if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality
  • Updated feature matrix, if new features were added or implemented

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 disagree that this is the right way to solve this issue, window handles are thread safe on macOS/iOS, since we guarantee to never use them outside the main thread.

Rather, we should wrap the window handle in something that implements Send + Sync (or maybe we should figure it out in rust-windowing/raw-window-handle#85 instead).

@kchibisov
Copy link
Member Author

kchibisov commented Oct 10, 2023

@madsmtm the issue is not only a window handle as it was said. You can see the CI logs and discover, that all the Web can't be Send, what should we do about it? As well as monitors on macOS.

@kchibisov
Copy link
Member Author

Like, surely, we could keep the WindowAttributes as Send + Sync, but I don't think we can easily guarantee the same about the platform stuff. Also, it's not clear how we should deal with the lifetime, but I don't think it really matters as long as we can convert to `'static' bound?

@madsmtm
Copy link
Member

madsmtm commented Oct 10, 2023

I believe the solution is the same on the web: Since the window handle can only be created and accessed from the main thread, then it doesn't matter if the handle was sent between threads (or service workers) in between those accesses. @daxpedda?

As well as monitors on macOS.

I think it's the same thing here, monitors can be Send + Sync on macOS if I just refactor them a bit.

@kchibisov kchibisov added this to the Version 0.29.0 milestone Oct 10, 2023
Copy link
Member

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

I'm against introducing this generic, we already have solutions in place for this kind of issue in MacOS and Web, we just need to apply them here as well as @madsmtm already suggested.

For Web, we just need to wrap the innards into MainThreadSafe, that's it. I'm happy to figure it out once NotSendSyncWindowAttributes is removed.

@kchibisov kchibisov force-pushed the default-builder-send-sync branch 2 times, most recently from 38f7b29 to fa3aabe Compare October 12, 2023 22:06
Copy link
Member Author

@kchibisov kchibisov left a comment

Choose a reason for hiding this comment

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

I'm only not sure what to do with the video mode stuff, Like it's not send/sync in general, but given that macOS will allow creating it only on the main thread, and then winit will use it on the main thread, I think just marking Fullscreen in builder should be fine.

There probably better solutions, but searching for macOS docs on what is MT-safe and what is not is really hard.

src/lib.rs Outdated
/// mean thread if the platform demands it. Thus marking such objects as `Send + Sync` is safe.
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct SendSyncWrapper<T>(pub T);
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure whether this object should be public at all, the only reason it's public is because we allow access to WindowAttributes and also building them with struct builder.

We could make it crate private and just provide a get/set for parent` on WindowAttributes?

src/window.rs Outdated Show resolved Hide resolved
src/window.rs Outdated Show resolved Hide resolved
src/window.rs Outdated Show resolved Hide resolved
Window builder is always accessed by winit on the thread event loop
is on, thus it's safe to mark the data it gets as `Send + Sync`.
Each unsafe object is marked individually as `Send + Sync` instead
of just implementing `Send` and `Sync` for the whole builder.
@kchibisov kchibisov merged commit 801fddb into rust-windowing:master Oct 17, 2023
50 checks passed
@kchibisov kchibisov deleted the default-builder-send-sync branch October 17, 2023 00:54
@daxpedda
Copy link
Member

daxpedda commented Oct 17, 2023

This is broken on Web and shouldn't be Send + Sync there.

EDIT: False alarm, I forgot WindowBuilder::build() can only be called on the MT anyway.

@kchibisov
Copy link
Member Author

Exactly, we discussed that, it's always used on the thread event loop is on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants