feat(libkernel): start the guest off the main thread on macOS #11048
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11048"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-25T15:11:16Z.
What
This is mugurc's
8191f76dd6from his macOS series (ci/macos-series-2, listed on #1), rebased ontomain. mugurc is co-author of the commit.AppKit, which SDL uses for windows, Metal surfaces, event pumping and game controllers, works only on the main thread, and a guest never hands its thread back.
main's macOS relinker makesAps5StartGuest_nid_no_patchthe entry of a relinked executable (MacOsImage.cpp), and no library defines it.MainThreadMacOs.cpp(libkernel, under__APPLE__):Aps5StartGuestruns the guest entry on a new thread with the main thread's stack size (at least 8 MiB) and serves the main dispatch queue from the main thread's run loop. The guest ends the process withexit; a guest entry that returns aborts.Aps5RunOnMainThreadruns a function on the main thread and waits for it, or in place when the caller is the main thread orAps5StartGuestwas never called.OnMainThread(MainThread.hpp), which carries exceptions back to the caller: SDL init and shutdown, the window, its Vulkan surface and drawable size, event polling, and the pad, mouse, keyboard and IME input, which are now created and destroyed there (std::optional). On Linux and WindowsOnMainThreadcalls the function in place, so the order of the calls is the same as before.Aps5StartGueston macOS, as a relinked guest does.The libkernel debt file gets an entry for the macOS guest thread.
Tested
Current
main(2026-10-26): I looked for the smallest set of open PRs that boots titles on macOS frommain035ac96edd: #10366, #10552, #7631, #7804, #11249, #10669, #10540, #11447, #11047, this PR, #10876 and #9263, plus a localVulkanDevice.cpppatch that no open PR covers yet. With it, Planet of Lana and God of War Sons of Sparta, relinked with that tree, start from New Game and run without errors (Lana in gameplay for 120 s, GoW to the Cyclops fight in 145 s). Without this PR, the relinked eboot stops in dyld:symbol not found in flat namespace '_Aps5StartGuest_nid_no_patch'.Apple M4 Pro, macOS 27.0.1, Apple clang 21, x86-64 Release under Rosetta 2 (
-DCMAKE_OSX_ARCHITECTURES=x86_64, Ninja,BUILD_TESTING=ON).mainalone doesn't build the libraries on macOS, so the tree ismain61fc572ceewith #10423, the open macOS build PRs (#10366, #10419, #10552, #7631, #7804, #4630, #10540, #10659, #10660, #10669, #10676), #10889, #10898 and the parts of mugurc's series that #7201 also carries. Planet of Lana (PPSA19450) is relinked with that tree's relinker (--macos --to-rosetta).DisplayWindow: NSWindow should only be instantiated on the main thread!.ctest -R 'video_out|flip' -VwithANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, without and with this change on the same tree: 34 of 34 pass in both, with the same skips (the flip tests run on SDL's offscreen driver, which has no Vulkan on macOS). Neither log haswas not found but was requested by env var VK_INSTANCE_LAYERSor anyVUID-.Linux:
VideoOutDriver.cpp,Flip.cppandMainThreadMacOs.cpppassg++ -std=gnu++20 -fsyntax-onlywith GCC 14 (gcc:14image, x86-64, both values ofAPS5_ENABLE_TIMING_LOG). Not built on Windows.check_conventions.py --base upstream/mainpasses.pr_overlap.pyreports #7622 (an include next to the new one) and #8984 (the SDL hint at the start of the constructor, now insideOnMainThread); whichever lands second takes the other's line into the moved block. It also lists #5021, which adds files with the same names.#7201 (the whole macOS port) and #5021 (the native ARM64 AOT draft) carry their own versions of these functions. #7201's head (
6687cbac29, 2026-10-16) conflicts withmain, needs a rebase anyway, and is being split into smaller PRs such as #10552 and #10419. #5021 is a draft of more than 43,000 lines that conflicts withmainand says no merge intomainis implied. If either lands first with these functions, this PR is not needed and I will close it.Checklist
main; no other open PR implements the same functions (except #7201 and #5021, see the end of Tested)NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:15:25Z.
Review: APPROVED
Reviewed exact head
30cc4ea2e6. The guest entry moves to a pthread with an adequate stack while the original main thread runs the CFRunLoop. Aps5RunOnMainThread executes inline on the main thread and synchronously dispatches from the guest thread, carrying exceptions back. VideoOut SDL and AppKit operations and input-object lifecycle are routed through that helper; the remaining SDL_GetWindowSize reads SDL cached dimensions. The author reports 34/34 focused VideoOut tests passing with matching validation logs on macOS and a 10-run Planet of Lana session with #11047. Hosted platform product jobs are skipped by path policy; I could not independently run the Apple build or runtime here.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T07:06:07Z.
Review: COMMENTED
Ran head
b05ca4e4bon a second Apple Silicon machine. I didn't runvideo_out_flip: I keep window and present tests off this machine. Instead I checkedAps5StartGuestandAps5RunOnMainThreaddirectly.Machine: Apple M2 Max (32 GB), macOS 27.2 (26B5112a), Apple clang 21.0.0, x86_64 Release under Rosetta 2 (
-DCMAKE_OSX_ARCHITECTURES=x86_64, Ninja,BUILD_TESTING=ON, Python 3.9 from Xcode).Base:
mainb986c11a34does not build the libraries on macOS by itself, so the tree is thatmainplus the open macOS build PRs (#10366, #7631, #10659, #10660, #11249, #11358, #10669, #10552, #7804, #10676, #11447, #11437) and #11554, plus #11705 (applied locally). Only this PR is toggled, withgit merge --no-commiton top.The harness is a small x86_64 program linked against the built
libkernel.prx, resolving both symbols withdlsym. Results, 3/3 each with this PR:Aps5StartGuest(entry, block): the entry gets(block, nullptr), runs off the main thread (pthread_main_np() == 0), and has a stack of at least 8 MiB.Aps5RunOnMainThreadruns on the main thread. 4 guest threads × 2000 concurrent calls all run on the main thread (8000/8000).exitwith anatexithandler that itself callsAps5RunOnMainThread(as a static destructor that shuts SDL down would): the handler runs on the main thread, with no deadlock.Aps5StartGuest, a call from a non-main thread runs in place.guest entry returned(rc 134), as described.Without this PR, the symbols are missing from
libkernel.prx.Non-GPU suite (
ctest -E "^agc|gpu|vulkan|video_out", 1372 tests): 78 fail on the base and the same 78 fail with this PR. The full tree, includingvideo_out_flip_tests, builds.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.