Skip to content

breaking: Change thread safety guarantees for window types#192

Open
notgull wants to merge 1 commit intomasterfrom
notgull/thread-safety
Open

breaking: Change thread safety guarantees for window types#192
notgull wants to merge 1 commit intomasterfrom
notgull/thread-safety

Conversation

@notgull
Copy link
Member

@notgull notgull commented Feb 22, 2026

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.

@notgull
Copy link
Member Author

notgull commented Feb 22, 2026

Poke:

I did some research and I think the conclusions I came to are correct, but I'd appreciate confirmation.

src/unix.rs Outdated
Comment on lines +360 to +365
/// ## 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.
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this depends on the backend? Either way, I'd say we leave it as Send for now.

@notgull notgull force-pushed the notgull/thread-safety branch from 738e54b to 6bbce6b Compare February 28, 2026 01:10
/// # }
/// ```
///
/// ## Thread Safety
Copy link
Member

Choose a reason for hiding this comment

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

Nit: This is already discussed a bit above the example, maybe merge these texts?

Same for UiKitWindowHandle.

Copy link
Member Author

Choose a reason for hiding this comment

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

Where would these texts be merged?

Copy link
Member

Choose a reason for hiding this comment

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

Either before or after the example, I don't mind either way.

Copy link
Member

Choose a reason for hiding this comment

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

Still not resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

Please double check, I modified the AppkitDisplayHandle's docs to point to AppkitWindowHandle's docs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh right, there's a paragraph I missed. Let me fix that.

@notgull notgull force-pushed the notgull/thread-safety branch from 6bbce6b to d3e4910 Compare March 1, 2026 20:26
@notgull notgull requested a review from madsmtm March 1, 2026 20:29
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've looked at the backends I could, generally think this PR is a good change. We might want to refine the Windows stuff, but let's discuss that in #209.

Comment on lines +69 to +71
/// 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.
Copy link
Member

Choose a reason for hiding this comment

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

CC @daxpedda on this.

Comment on lines +115 to 133
///
/// ## 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>,
}
Copy link
Member

Choose a reason for hiding this comment

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

Do you have docs that this really applies to WinRT too? I'd be worried that it might be more restrictive than Win32.

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

@notgull notgull force-pushed the notgull/thread-safety branch from d3e4910 to 2ff7b7d Compare March 1, 2026 22:09
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>
@notgull notgull force-pushed the notgull/thread-safety branch from 2ff7b7d to ce693c8 Compare March 3, 2026 05:04
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.

4 participants