Skip to content

objc: an AppKit event loop a caller can leave - #12

Merged
tannevaled merged 1 commit into
mainfrom
a-loop-a-caller-can-leave
Aug 31, 2026
Merged

tannevaled merged 1 commit into
mainfrom
a-loop-a-caller-can-leave

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

-[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.

-[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.
@tannevaled
tannevaled merged commit 8f8b590 into main Aug 31, 2026
3 checks passed
@tannevaled
tannevaled deleted the a-loop-a-caller-can-leave branch August 31, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant