Skip to content

[macOS] Register the Cocoa main thread at an earlier point - #456

Merged
francesca64 merged 1 commit into
rust-windowing:masterfrom
yvt:patch-macos-main-thread
Apr 16, 2018
Merged

francesca64 merged 1 commit into
rust-windowing:masterfrom
yvt:patch-macos-main-thread

Conversation

@yvt

@yvt yvt commented Apr 10, 2018 •

Copy link
Copy Markdown
Contributor

Solves the issues caused by calling EventsLoopProxy::wakeup() too early from a worker thread.

A global singleton instance of NSApplication is created when [NSApplication sharedApplication] is called for the first time. When this happens, the calling thread is registered as the main thread. This means that the main thread in a Cocoa application is determined when the application calls it for the first time.

EventsLoopProxy::wakeup calls it internally. As a result, if you call EventsLoopProxy::wakeup very early, a wrong thread will be registered as the main thread, causing the application to misbehave. The framework detects such situations and generates the following log output:

2018-04-10 13:34:43.495 ${ApplicationName}[90843:3858828] !!! BUG: The current event queue and the main event queue are not the same. Events will not be handled correctly. This is probably because _TSGetMainThread was called for the first time off the main thread.
2018-04-10 13:34:43.499 ${ApplicationName}[90843:3858828] pid(90843)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2018-04-10 13:34:43.513 ${ApplicationName}[90843:3858828] pid(90843)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!

screen shot 2018-04-10 at 14 02 28

This PR fixes this issue by calling [NSApplication sharedApplication] from EventsLoop::new().

Solves the issues caused by calling `EventsLoopProxy::wakeup()` too
early from a worker thread.
@francesca64

Copy link
Copy Markdown
Member

Good catch! Thanks.

@francesca64
francesca64 merged commit 1068891 into rust-windowing:master Apr 16, 2018
@mitchmindtree

Copy link
Copy Markdown
Contributor

Oh wow, great find! I think remember getting stung by this in the past, thank you for submitting this PR!

francesca64 added a commit that referenced this pull request Apr 18, 2018
yvt added a commit to yvt/ngspades that referenced this pull request Apr 25, 2020
Implements a work-around for the issue caused by calling
`EventsLoopProxy::wakeup()` too early from a background thread.

See: rust-windowing/winit#456
tmfink pushed a commit to tmfink/winit that referenced this pull request Jan 5, 2022
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.

3 participants