objc: an AppKit event loop a caller can leave - #12
Merged
Merged
Conversation
-[NSApplication run] cannot be left on demand. stop: sets a flag AppKit reads only after it finishes processing an event, so an application with nobody touching it waits for an event that is never coming. StopApp, added an hour ago, wakes the run loop -- and it is still not enough, because waking a run loop is not an event either. Measured: the desk still hung, and the goroutine dump showed the work finished, the waiting goroutine gone, and the main thread still inside run. RunAppLoop is the loop run runs, written out: take the next event, send it, and between the two ask the caller whether to carry on. Waking the main run loop makes the wait return nil, the caller is asked, and one that says stop gets its thread back at once -- with no event, no mouse movement and nobody at the machine. It is NOT a pump. -[NSApplication sendEvent:] is what tracks a menu and drives a window; sampling the run loop instead gives a menu-bar item whose menu never opens, which this fleet has already paid for once. Every argument in it is an integer, a pointer or a bool, and that is not an accident: the obvious way out of run is to post an application-defined event, and +[NSEvent otherEventWithType:location:...] takes an NSPoint and a timestamp -- three doubles, in floating-point registers, through a bridge that passes everything in integer ones. Half of it is tested here and the comment says which half and why: the wake-up reaches the MAIN thread's run loop, and `go test` runs every test on a goroutine of its own, so a test can lock itself to a thread but never to that one. A test written against it hung for ten minutes, which was the correct answer. The other half is verified in the program that needed it.
This was referenced Aug 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
-[NSApplication run]cannot be left on demand.stop:sets a flag AppKit reads only after it finishes processing an event, so an application with nobody touching it waits for an event that is never coming.StopApp, added an hour ago, wakes the run loop — and it is still not enough, because waking a run loop is not an event either. Measured: the desk still hung, and the goroutine dump showed the work finished, the waiting goroutine gone, and the main thread still insiderun.RunAppLoopis the looprunruns, written out: take the next event, send it, and between the two ask the caller whether to carry on. Waking the main run loop makes the wait return nil, the caller is asked, and one that says stop gets its thread back at once — with no event, no mouse movement and nobody at the machine.It is not a pump.
-[NSApplication sendEvent:]is what tracks a menu and drives a window; sampling the run loop instead gives a menu-bar item whose menu never opens, which this fleet has already paid for once.Every argument in it is an integer, a pointer or a bool, and that is not an accident: the obvious way out of
runis to post an application-defined event, and+[NSEvent otherEventWithType:location:…]takes anNSPointand a timestamp — three doubles, in floating-point registers, through a bridge that passes everything in integer ones.Half of it is tested here, and the comment says which half and why. The wake-up reaches the main thread's run loop, and
go testruns every test on a goroutine of its own, so a test can lock itself to a thread but never to that one. A test written against it hung for ten minutes, which was the correct answer. The other half is verified in the program that needed it.