winit
winit copied to clipboard
Make `with_parent_window` safe
Alternative to https://github.com/rust-windowing/winit/pull/3136. EDIT: We'd still need some more work to make Fullscreen
Send + Sync
on all platforms.
Builds upon https://github.com/rust-windowing/winit/pull/3126 to remove the unsafe
from WindowBuilder::with_parent_window
, by taking the lifetime inherent in that, and extending it on the platforms where it is relevant.
TODO:
- [ ] Merge the aforementioned PR.
- [ ] Test on all platforms changed
- [ ] Add entry to
CHANGELOG.md
Alternative to https://github.com/rust-windowing/winit/pull/3136.
This is not an alternative since it doesn't achieve what said PR achieves.
However doing something like that is fine.
After looking into it, I don't think it's a good idea as of now to accept owned handler if winit can only provide Raw
handles.
This update should be a part of the winit safe handles work.
That should actually be good to go now, right?
After looking at this a bit more closely, I question how thread safety is achieved here exactly. WindowBuilder
is Send
, but with_parent()
can be called on any thread, but WindowHandle
isn't Send
. So a WindowHandle
that has been passed into with_parent()
that would be valid on a thread that is not the main thread, is suddenly being used in the main thread.
thread safety
I think this works on platforms with a "main-thread only" concept, unsure about platforms where a handle may be bound to a specific thread (Windows?)
My reasoning for main thread only being safe on macOS/iOS:
-
WindowHandle<'_>
is!Send + !Sync
, because it is bound to the main thread. - If we get that in
with_parent_window
, we hence know we're running on the main thread (because of the yet-to-be-documented contract onWindowHandle<'_>
). - This we put it into
OwnedWindowHandle
, which contains aMainThreadBound
. - So now, even though
WindowBuilder
is moved, we're guaranteed to only access the parent window on the main thread.
If the only valid thread is the main thread, I agree there is no problem, because WindowBuilder
only gets consumed in the main thread, which is checked by Winit.
Summarizing remaining problems:
- How to correctly implement
OwnedWindowHandle
on all platforms if at all possible, alternatively we have to discuss adding a lifetime toWindowBuilder
. - Determine thread safety for all platforms. See https://github.com/rust-windowing/raw-window-handle/pull/154 for documenting this in
raw-window-handle
.