feat(libkernel): start the guest off the main thread on macOS #11048

Merged
imellag merged 2 commits from upstream-pr/11048 into main 2026-10-27 07:27:20 +00:00
imellag commented 2026-10-25 15:11:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-25T15:11:16Z.


What

This is mugurc's 8191f76dd6 from his macOS series (ci/macos-series-2, listed on #1), rebased onto main. 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 makes Aps5StartGuest_nid_no_patch the entry of a relinked executable (MacOsImage.cpp), and no library defines it.

  • MainThreadMacOs.cpp (libkernel, under __APPLE__): Aps5StartGuest runs 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 with exit; a guest entry that returns aborts. Aps5RunOnMainThread runs a function on the main thread and waits for it, or in place when the caller is the main thread or Aps5StartGuest was never called.
  • libSceVideoOut sends its SDL calls through 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 Windows OnMainThread calls the function in place, so the order of the calls is the same as before.
  • The flip test starts its tests through Aps5StartGuest on 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 from main 035ac96edd: #10366, #10552, #7631, #7804, #11249, #10669, #10540, #11447, #11047, this PR, #10876 and #9263, plus a local VulkanDevice.cpp patch 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). main alone doesn't build the libraries on macOS, so the tree is main 61fc572cee with #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).

  • Without this PR (with #11047): the guest aborts when the presenter creates its window: DisplayWindow: NSWindow should only be instantiated on the main thread!.
  • With both: Lana boots, loads the save and plays in the village (27 to 30 fps from 85 s in each of 10 runs of 120 s), and the scripted pad presses that start the game arrive through the main thread's event polling.
  • VideoOut tests, ctest -R 'video_out|flip' -V with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_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 has was not found but was requested by env var VK_INSTANCE_LAYERS or any VUID-.

Linux: VideoOutDriver.cpp, Flip.cpp and MainThreadMacOs.cpp pass g++ -std=gnu++20 -fsyntax-only with GCC 14 (gcc:14 image, x86-64, both values of APS5_ENABLE_TIMING_LOG). Not built on Windows.

check_conventions.py --base upstream/main passes. pr_overlap.py reports #7622 (an include next to the new one) and #8984 (the SDL hint at the start of the constructor, now inside OnMainThread); 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 with main, 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 with main and says no merge into main is implied. If either lands first with these functions, this PR is not needed and I will close it.

Checklist

  • Based on current main; no other open PR implements the same functions (except #7201 and #5021, see the end of Tested)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.0.1
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5005924593 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11048). Originally posted by [imellag](https://github.com/imellag) on 2026-10-25T15:11:16Z. --- ### What This is mugurc's `8191f76dd6` from his macOS series (`ci/macos-series-2`, listed on #1), rebased onto `main`. 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 makes `Aps5StartGuest_nid_no_patch` the entry of a relinked executable (`MacOsImage.cpp`), and no library defines it. - `MainThreadMacOs.cpp` (libkernel, under `__APPLE__`): `Aps5StartGuest` runs 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 with `exit`; a guest entry that returns aborts. `Aps5RunOnMainThread` runs a function on the main thread and waits for it, or in place when the caller is the main thread or `Aps5StartGuest` was never called. - libSceVideoOut sends its SDL calls through `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 Windows `OnMainThread` calls the function in place, so the order of the calls is the same as before. - The flip test starts its tests through `Aps5StartGuest` on 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 from `main` `035ac96edd`: #10366, #10552, #7631, #7804, #11249, #10669, #10540, #11447, #11047, this PR, #10876 and #9263, plus a local `VulkanDevice.cpp` patch 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`). `main` alone doesn't build the libraries on macOS, so the tree is `main` `61fc572cee` with #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`). - **Without this PR** (with #11047): the guest aborts when the presenter creates its window: `DisplayWindow: NSWindow should only be instantiated on the main thread!`. - **With both:** Lana boots, loads the save and plays in the village (27 to 30 fps from 85 s in each of 10 runs of 120 s), and the scripted pad presses that start the game arrive through the main thread's event polling. - VideoOut tests, `ctest -R 'video_out|flip' -V` with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_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 has `was not found but was requested by env var VK_INSTANCE_LAYERS` or any `VUID-`. Linux: `VideoOutDriver.cpp`, `Flip.cpp` and `MainThreadMacOs.cpp` pass `g++ -std=gnu++20 -fsyntax-only` with GCC 14 (`gcc:14` image, x86-64, both values of `APS5_ENABLE_TIMING_LOG`). Not built on Windows. `check_conventions.py --base upstream/main` passes. `pr_overlap.py` reports #7622 (an include next to the new one) and #8984 (the SDL hint at the start of the constructor, now inside `OnMainThread`); 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 with `main`, 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 with `main` and says no merge into `main` is implied. If either lands first with these functions, this PR is not needed and I will close it. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions (except #7201 and #5021, see the end of Tested) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.0.1 - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 15:15:25 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643822113 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11048#pullrequestreview-5643822113). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:15:25Z. **Review:** APPROVED --- Reviewed exact head 30cc4ea2e6a74664bd085e281b8ccaf9717a514f. 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.
jotitadev commented 2026-10-27 07:06:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T07:06:07Z.

Review: COMMENTED


Ran head b05ca4e4b on a second Apple Silicon machine. I didn't run video_out_flip: I keep window and present tests off this machine. Instead I checked Aps5StartGuest and Aps5RunOnMainThread directly.

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: main b986c11a34 does not build the libraries on macOS by itself, so the tree is that main plus 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, with git merge --no-commit on top.

The harness is a small x86_64 program linked against the built libkernel.prx, resolving both symbols with dlsym. 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.
  • From the guest thread, Aps5RunOnMainThread runs on the main thread. 4 guest threads × 2000 concurrent calls all run on the main thread (8000/8000).
  • A guest that calls exit with an atexit handler that itself calls Aps5RunOnMainThread (as a static destructor that shuts SDL down would): the handler runs on the main thread, with no deadlock.
  • Without Aps5StartGuest, a call from a non-main thread runs in place.
  • An entry that returns aborts with 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, including video_out_flip_tests, builds.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5663371752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11048#pullrequestreview-5663371752). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T07:06:07Z. **Review:** COMMENTED --- Ran head `b05ca4e4b` on a second Apple Silicon machine. I didn't run `video_out_flip`: I keep window and present tests off this machine. Instead I checked `Aps5StartGuest` and `Aps5RunOnMainThread` directly. **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:** `main` `b986c11a34` does not build the libraries on macOS by itself, so the tree is that `main` plus 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, with `git merge --no-commit` on top. The harness is a small x86_64 program linked against the built `libkernel.prx`, resolving both symbols with `dlsym`. 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. - From the guest thread, `Aps5RunOnMainThread` runs on the main thread. 4 guest threads × 2000 concurrent calls all run on the main thread (8000/8000). - A guest that calls `exit` with an `atexit` handler that itself calls `Aps5RunOnMainThread` (as a static destructor that shuts SDL down would): the handler runs on the main thread, with no deadlock. - Without `Aps5StartGuest`, a call from a non-main thread runs in place. - An entry that returns aborts with `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, including `video_out_flip_tests`, builds. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!11048
No description provided.