From d690880dea1b85c6da9d08c99c6c964e6288c0f5 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Thu, 21 Nov 2024 01:37:16 +0100 Subject: [PATCH] Make Windows main thread checks more forgiving The check on Windows to enforce that the event loop is created from the main thread is not fully reliable (https://github.com/rust-windowing/winit/issues/3999). Given that this check is not necessary on Windows and only exists more as an assert to enforce platform consistency in development, I have made it more lax: * It now produces a tracing::warn!() message instead of an outright panic. * The check is only compiled in with cfg(debug_assertions) --- src/platform_impl/windows/event_loop.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 874ae06a8c..b5da31b359 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -186,8 +186,9 @@ impl EventLoop { ) -> Result { let thread_id = unsafe { GetCurrentThreadId() }; + #[cfg(debug_assertions)] if !attributes.any_thread && thread_id != main_thread_id() { - panic!( + tracing::warn!( "Initializing the event loop outside of the main thread is a significant \ cross-platform compatibility hazard. If you absolutely need to create an \ EventLoop on a different thread, you can use the \ @@ -556,10 +557,12 @@ impl rwh_06::HasDisplayHandle for OwnedDisplayHandle { /// to setup global state within a program. The OS will call a list of function pointers which /// assign values to a static variable. To have get a hold of the main thread id, we need to place /// our function pointer inside of the `.CRT$XCU` section so it is called before the main -/// entrypoint. +/// entrypoint. Note that when compiled into a dylib, this is not guaranteed to be ran from the "main" +/// thread, so this is not foolproof. /// /// Full details of CRT initialization can be found here: /// +#[cfg(debug_assertions)] fn main_thread_id() -> u32 { static mut MAIN_THREAD_ID: u32 = 0;