Skip to content

Make Win32WindowHandle Send + Sync#209

Open
daxpedda wants to merge 3 commits intorust-windowing:masterfrom
daxpedda:windows-send-sync
Open

Make Win32WindowHandle Send + Sync#209
daxpedda wants to merge 3 commits intorust-windowing:masterfrom
daxpedda:windows-send-sync

Conversation

@daxpedda
Copy link
Member

@daxpedda daxpedda commented Mar 1, 2026

Resolves #196.

@daxpedda daxpedda force-pushed the windows-send-sync branch from b18e787 to 06aa544 Compare March 1, 2026 12:31
/// Raw window handle for Win32.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Win32WindowHandle {
Copy link
Member

Choose a reason for hiding this comment

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

Could we add an example of getting this, and compare it like:

assert_eq!(GetWindowThreadProcessId(hwnd, std::ptr::null_mut()), GetCurrentThreadId());

Before doing anything with the handle? Just to make it clearer what consumers have to do.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done! I guess it would be nice if we actually run this test on Windows with all the function calls working. A problem for another time.

@madsmtm
Copy link
Member

madsmtm commented Mar 1, 2026

Also, would it make sense to wait for #188? Since the safety of this depends on what Win32WindowHandle ends up being. (If we choose option A in that thread, the handle would become !Send + !Sync again).

Then again, I'm also fine with doing this now (it's correct for the handle as currently defined), and then worry it this later when we do get around to changing the semantics of handles.

See also #192, that one has a bit more detailed docs.

Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

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

Please see #192. The Win32 window handle is !Send but Sync. Please remove the Send implementation.

@daxpedda
Copy link
Member Author

daxpedda commented Mar 2, 2026

The Win32 window handle is !Send but Sync.

See #197 (comment), but I expect we will hash this out in the next meeting.

@daxpedda daxpedda force-pushed the windows-send-sync branch from edd480a to fa10f61 Compare March 2, 2026 09:56
Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

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

Can you rebase?

@daxpedda daxpedda force-pushed the windows-send-sync branch from fa10f61 to d304a61 Compare March 9, 2026 09:06
Comment on lines -9 to -16
///
/// ## Thread-Safety
///
/// Overall, even though Win32 windows have [thread affinity], the overall
/// Win32 user API is thread-safe. Therefore this type is `Send` and `Sync`.
/// This means it can be sent to or used from other threads.
///
/// [thread affinity]: https://devblogs.microsoft.com/oldnewthing/20051010-09/?p=33843
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 believe that this doesn't make sense here because it doesn't actually exist on Windows.

@daxpedda daxpedda force-pushed the windows-send-sync branch from d304a61 to b60b974 Compare March 9, 2026 09:09
Comment on lines -115 to -127
///
/// ## 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
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, CoreWindow is something called a "non-agile" type, which literally means it can't be sent or access in other threads. I could mention that instead but it feels unnecessary considering its part of the actual type and not something we artificially enforce.

WinRT has something called "agile reference"s. This allows you to do something like libdispatch on MacOS. We could introduce a new type WinRtAgileWindowHandle or just enforce this right here.

Copy link
Member Author

Choose a reason for hiding this comment

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

See #210.

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.

Making Windows Send + Sync

4 participants