Skip to content

Commit

Permalink
Make Windows main thread checks more forgiving
Browse files Browse the repository at this point in the history
The check on Windows to enforce that the event loop is created from the main thread is not fully reliable (rust-windowing#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)
  • Loading branch information
PJB3005 committed Nov 21, 2024
1 parent f6b2085 commit d690880
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ impl EventLoop {
) -> Result<Self, EventLoopError> {
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 \
Expand Down Expand Up @@ -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:
/// <https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-160>
#[cfg(debug_assertions)]
fn main_thread_id() -> u32 {
static mut MAIN_THREAD_ID: u32 = 0;

Expand Down

0 comments on commit d690880

Please sign in to comment.