breaking: Change thread safety guarantees for window types#192
breaking: Change thread safety guarantees for window types#192
Conversation
|
Poke:
I did some research and I think the conclusions I came to are correct, but I'd appreciate confirmation. |
src/unix.rs
Outdated
| /// ## Thread-Safety | ||
| /// | ||
| /// GBM surfaces are not bound to a single thread; however, they are not | ||
| /// internally secured by mutexes and cannot be used by multiple threads at | ||
| /// once. Therefore this type is `Send` but not `Sync`. This means it can be | ||
| /// sent to other threads but not used from other threads. |
There was a problem hiding this comment.
The gbm.rs crate defines them as Sync as well, but they wrap the pointers into Arc in the first place, which probably makes it Sync semantically? But Send is true.
There was a problem hiding this comment.
I think this depends on the backend? Either way, I'd say we leave it as Send for now.
738e54b to
6bbce6b
Compare
| /// # } | ||
| /// ``` | ||
| /// | ||
| /// ## Thread Safety |
There was a problem hiding this comment.
Nit: This is already discussed a bit above the example, maybe merge these texts?
Same for UiKitWindowHandle.
There was a problem hiding this comment.
Where would these texts be merged?
There was a problem hiding this comment.
Either before or after the example, I don't mind either way.
There was a problem hiding this comment.
Please double check, I modified the AppkitDisplayHandle's docs to point to AppkitWindowHandle's docs.
There was a problem hiding this comment.
Oh right, there's a paragraph I missed. Let me fix that.
6bbce6b to
d3e4910
Compare
| /// WASM objects are usually bound to the main UI "thread" belonging to the | ||
| /// top-level webpage. Therefore this type is `!Send` and `!Sync`. It cannot be | ||
| /// sent to or used from other threads. |
| /// | ||
| /// ## Thread-Safety | ||
| /// | ||
| /// Window handles have [thread affinity]. This means that they are `!Send`, as | ||
| /// they must be dropped on the same thread that created them. However, some | ||
| /// functions on the window can be called from other threads. This means that | ||
| /// the window is `Sync`. | ||
| /// | ||
| /// Note that not all functions of the Win32 handle are thread-safe (modifying | ||
| /// functions especially), so care should be taken to not call these functions | ||
| /// from other threads. When in doubt, only run the function on the main thread. | ||
| /// | ||
| /// [thread affinity]: https://devblogs.microsoft.com/oldnewthing/20051010-09/?p=33843 | ||
| #[non_exhaustive] | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub struct WinRtWindowHandle { | ||
| /// A WinRT `CoreWindow` handle. | ||
| pub core_window: NonNull<c_void>, | ||
| } |
There was a problem hiding this comment.
Do you have docs that this really applies to WinRT too? I'd be worried that it might be more restrictive than Win32.
There was a problem hiding this comment.
I think so? This document mentions that UI elements have "thread affinity". In addition I also recall reading somewhere that WinRT wraps around Win32 primitives internally.
d3e4910 to
2ff7b7d
Compare
In this commit, thread safety documentation is added for all window handle types. In addition, following some additional research, some window types have their thread safety changed. Most notably: - OHOS windows are definitely thread-unsafe. - Redox and Haiku windows are actually thread safe. - GBM types are Send but not Sync. - Win32 windows are Sync but not Send. - Display handles are changed to match their windows. Signed-off-by: John Nunley <dev@notgull.net>
2ff7b7d to
ce693c8
Compare
In this commit, thread safety documentation is added for all window
handle types. In addition, following some additional research, some
window types have their thread safety changed. Most notably: