feat(libSceNKWebKit): implement WTF::ParkingLot and the Lock slow paths #14921

Closed
w4rc0n wants to merge 1 commit from upstream-pr/14921 into main
w4rc0n commented 2026-11-02 01:31:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by w4rc0n on 2026-11-02T01:31:54Z.


What

Every WTF locking slow path in libSceNKWebKit was a NotImplemented stub. Guest WebKit code stopped as soon as a WTF::Lock was contended or a WTF::Condition waited, because their inline fast paths fall through to these exports. Eight of them now follow the pinned PS5 WebKit 11.x source:

  • ParkingLot: parkConditionallyImpl, unparkOne, unparkOneImpl, unparkCount and unparkAll.
    • The validation callback and the unparkOneImpl callback run under the queue lock, and beforeSleep runs after the lock is released.
    • Waiters wake in FIFO order, tokens are handed over, and TimeWithDynamicClockType deadlines (past, finite and infinite) use the existing clocks. As in the source, a wall-clock deadline waits on the absolute wall time, so a clock change is seen; monotonic and approximate deadlines wait on the host's steady clock and recheck the same clock.
    • UnparkResult is reported as the source reports it, and timeToBeFair stays false because the pinned source never sets it.
  • Lock::lockSlow, unlockSlow and safepointSlow, using the one-byte protocol of LockAlgorithmInlines.h on the guest's own byte (held bit 1, parked bit 2), so they work alongside the guest's inline fast paths.

The guest's ScopedLambda callbacks are called through their function pointers, and the ParkResult, UnparkResult and ScopedLambda layouts are pinned with static_assert. The parking state is allocated once and never freed, as WebKit's table is, so a thread that wakes during process exit doesn't touch a destroyed object.

ParkingLot::forEachImpl stays NotImplemented because the guest WTF::Thread layout isn't modelled. The debt entry records it, along with the single host queue and the fairness timing, which hasn't been measured on a console.

Adapted source. The code adapted from the pinned source is now in its own file, src/WtfLock.cpp, under a "Derived from" line and the upstream notice for each pinned source file, verbatim apart from trailing whitespace, the form #13766 uses: Apple's BSD notices from LockAlgorithmInlines.h, LockAlgorithm.h and ParkingLot.cpp. Adapted: the lock slow path (LockAlgorithmInlines.h:40-103), the unlock slow path (:106-154), the safepoint wrapper (LockAlgorithm.h:141-145) and the fairness timer (ParkingLot.cpp:129-170). Independent: the park queue and the host glue, which stay in WtfSystem.cpp.

tools/pr_overlap.py on this head, with main at 0eba5db30d: no other open PR changes these exports or adds the same files. For the PRs it couldn't compare because they no longer merge with main, GitHub's file lists show none of them changing these exports either. #13992, #14172 and #15866 change nearby lines in the same files; whichever merges later keeps both sides. The only exports it shares are with #14922, in the same stack.

Tested

Debian 12, GCC 12.2, Linux 6.18, Release, at the head 43e5f65b65b95b14fcc2a5f6a75fe26880525385, no local changes:

  • guest_nkwebkit_dates, guest_nkwebkit_numbers, nkwebkit_base, nkwebkit_config, nkwebkit_constants, nkwebkit_fastbitvector, nkwebkit_jsc, nkwebkit_palsessionid, nkwebkit_preferences, nkwebkit_resourceresponse, nkwebkit_webcoregeometry, nkwebkit_wtf, guest_nkwebkit_urls and public_headers pass. At an earlier commit with the same patch, nkwebkit_wtf also passed 20 repeated runs.
  • The new nkwebkit_wtf cases cover:
    • validation that fails, which returns at once without running beforeSleep;
    • the order of enqueueing, beforeSleep and unlocking;
    • past, finite and infinite deadlines;
    • token handover, and the unparkOneImpl callback with no waiter;
    • FIFO order and the UnparkResult flags;
    • unparkCount and unparkAll, kept separate per address;
    • a counter contended by several threads through the guest's inline fast paths and the slow paths;
    • other bits of the lock byte left untouched, and a stale parked bit cleared;
    • safepointSlow handing the lock straight to a parked thread.
  • tools/progress.py: the eight exports count as implemented (libSceNKWebKit 1320, 1312 on main), and forEachImpl stays unimplemented.
  • tools/check_conventions.py passes.
  • Windows (GitHub-hosted CI in the author's fork): run 41822639868 built feat/nkwebkit-binary-semaphore at its previous head f54a49c07fd92fbc915bd64bd9a8241ad532f2ca, which contains this PR's commit. It is AnyPS5's Build workflow, dispatched in the author's fork with select=true: the branch merged into main at b11c7bf84f5d2f636ff60166cc3371bd071e24d5, built on windows-latest with WinLibs GCC 15.2 (MinGW-w64, UCRT). All 809 selected tests passed, none skipped, including the twelve NKWebKit tests, public_headers and patched_libraries. A fork-internal pull request existed only to give the workflow a number. The rebase onto main at 0eba5db30d8a3d03789e52e5c1a9ff853e4e33e4 after #15911 changed only docs/dev/debt/libSceNKWebKit.md, so git range-diff shows the code and tests under test are the same as at that run.

Not built or run: macOS. A wall-clock jump during a wait was not tested. No title or homebrew was run.

Checklist

  • Based on current main; no other open PR implements the same functions
  • 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: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5113583138 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14921). Originally posted by [w4rc0n](https://github.com/w4rc0n) on 2026-11-02T01:31:54Z. --- ### What Every WTF locking slow path in libSceNKWebKit was a `NotImplemented` stub. Guest WebKit code stopped as soon as a `WTF::Lock` was contended or a `WTF::Condition` waited, because their inline fast paths fall through to these exports. Eight of them now follow the pinned PS5 WebKit 11.x source: - [`ParkingLot`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/ParkingLot.cpp#L561-L791): `parkConditionallyImpl`, `unparkOne`, `unparkOneImpl`, `unparkCount` and `unparkAll`. - The validation callback and the `unparkOneImpl` callback run under the queue lock, and `beforeSleep` runs after the lock is released. - Waiters wake in FIFO order, tokens are handed over, and `TimeWithDynamicClockType` deadlines (past, finite and infinite) use the existing clocks. As in the source, a wall-clock deadline waits on the absolute wall time, so a clock change is seen; monotonic and approximate deadlines wait on the host's steady clock and recheck the same clock. - `UnparkResult` is reported as the source reports it, and `timeToBeFair` stays false because the pinned source never sets it. - [`Lock::lockSlow`, `unlockSlow` and `safepointSlow`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/Lock.cpp#L42-L75), using the one-byte protocol of [`LockAlgorithmInlines.h`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/LockAlgorithmInlines.h) on the guest's own byte (held bit 1, parked bit 2), so they work alongside the guest's inline fast paths. The guest's `ScopedLambda` callbacks are called through their function pointers, and the `ParkResult`, `UnparkResult` and `ScopedLambda` layouts are pinned with `static_assert`. The parking state is allocated once and never freed, as WebKit's table is, so a thread that wakes during process exit doesn't touch a destroyed object. `ParkingLot::forEachImpl` stays `NotImplemented` because the guest `WTF::Thread` layout isn't modelled. The debt entry records it, along with the single host queue and the fairness timing, which hasn't been measured on a console. **Adapted source.** The code adapted from the pinned source is now in its own file, `src/WtfLock.cpp`, under a "Derived from" line and the upstream notice for each pinned source file, verbatim apart from trailing whitespace, the form #13766 uses: Apple's BSD notices from [`LockAlgorithmInlines.h`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/LockAlgorithmInlines.h), [`LockAlgorithm.h`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/LockAlgorithm.h) and [`ParkingLot.cpp`](https://github.com/neonmodder123/PS5-WebKit-11.x/blob/80f77e94c63ee7202eec3d4b5b1a021dd89011ba/Source/WTF/wtf/ParkingLot.cpp). Adapted: the lock slow path (`LockAlgorithmInlines.h:40-103`), the unlock slow path (`:106-154`), the safepoint wrapper (`LockAlgorithm.h:141-145`) and the fairness timer (`ParkingLot.cpp:129-170`). Independent: the park queue and the host glue, which stay in `WtfSystem.cpp`. `tools/pr_overlap.py` on this head, with `main` at `0eba5db30d`: no other open PR changes these exports or adds the same files. For the PRs it couldn't compare because they no longer merge with `main`, GitHub's file lists show none of them changing these exports either. #13992, #14172 and #15866 change nearby lines in the same files; whichever merges later keeps both sides. The only exports it shares are with #14922, in the same stack. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Debian 12, GCC 12.2, Linux 6.18, Release, at the head `43e5f65b65b95b14fcc2a5f6a75fe26880525385`, no local changes: - `guest_nkwebkit_dates`, `guest_nkwebkit_numbers`, `nkwebkit_base`, `nkwebkit_config`, `nkwebkit_constants`, `nkwebkit_fastbitvector`, `nkwebkit_jsc`, `nkwebkit_palsessionid`, `nkwebkit_preferences`, `nkwebkit_resourceresponse`, `nkwebkit_webcoregeometry`, `nkwebkit_wtf`, `guest_nkwebkit_urls` and `public_headers` pass. At an earlier commit with the same patch, `nkwebkit_wtf` also passed 20 repeated runs. - The new `nkwebkit_wtf` cases cover: - validation that fails, which returns at once without running `beforeSleep`; - the order of enqueueing, `beforeSleep` and unlocking; - past, finite and infinite deadlines; - token handover, and the `unparkOneImpl` callback with no waiter; - FIFO order and the `UnparkResult` flags; - `unparkCount` and `unparkAll`, kept separate per address; - a counter contended by several threads through the guest's inline fast paths and the slow paths; - other bits of the lock byte left untouched, and a stale parked bit cleared; - `safepointSlow` handing the lock straight to a parked thread. - `tools/progress.py`: the eight exports count as implemented (libSceNKWebKit 1320, 1312 on `main`), and `forEachImpl` stays unimplemented. - `tools/check_conventions.py` passes. - **Windows (GitHub-hosted CI in the author's fork):** [run 41822639868](https://github.com/w4rc0n/AnyPS5/actions/runs/41822639868) built `feat/nkwebkit-binary-semaphore` at its previous head `f54a49c07fd92fbc915bd64bd9a8241ad532f2ca`, which contains this PR's commit. It is AnyPS5's `Build` workflow, dispatched in the author's fork with `select=true`: the branch merged into `main` at `b11c7bf84f5d2f636ff60166cc3371bd071e24d5`, built on `windows-latest` with WinLibs GCC 15.2 (MinGW-w64, UCRT). All 809 selected tests passed, none skipped, including the twelve NKWebKit tests, `public_headers` and `patched_libraries`. A fork-internal pull request existed only to give the workflow a number. The rebase onto `main` at `0eba5db30d8a3d03789e52e5c1a9ff853e4e33e4` after #15911 changed only `docs/dev/debt/libSceNKWebKit.md`, so `git range-diff` shows the code and tests under test are the same as at that run. Not built or run: macOS. A wall-clock jump during a wait was not tested. No title or homebrew was run. ### 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 - [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: n/a - [x] AI-assisted: yes

Pull request closed

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!14921
No description provided.