feat(libSceUlt): implement sceUltMutexDestroy and sceUltWaitingQueueResourcePoolDestroy #5422

Merged
mariusyo07 merged 1 commit from upstream-pr/5422 into main 2026-10-14 14:25:28 +00:00
mariusyo07 commented 2026-10-13 19:25:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mariusyo07 on 2026-10-13T19:25:45Z.


What

Implements sceUltMutexDestroy and sceUltWaitingQueueResourcePoolDestroy in libSceUlt.

METAL GEAR SOLID: Peace Walker - Master Collection Version (PPSA18216) stops on main when relinking mgspw.elf (#5268):

FAIL: unresolved ELF import jW+HnafeS3Y from libSceUlt.prx
FAIL: unresolved ELF import or55417wcDk from libSceUlt.prx
  • sceUltMutexDestroy validates the mutex pointer, returns ULT_ERROR_BUSY if currently locked, and removes the mutex from the registered map.
  • sceUltWaitingQueueResourcePoolDestroy validates the pool pointer, verifies that no registered mutexes, semaphores, or queue data pools are still referencing the waiting queue pool (returning ULT_ERROR_BUSY if so), and erases the pool.
  • UltMutexState and UltSemaphoreState now track their associated _waitingPool so that pool destruction safety is properly enforced.

Tested

  • Linux (GCC 16.1.1): Export.cpp and GuestUltThread.cpp compile cleanly.
  • guest_ult_thread_underscoredcreate in core/libs/tests/GuestUltThread.cpp exercises sceUltMutexDestroy and sceUltWaitingQueueResourcePoolDestroy (null handling, unknown pool, busy checks when resources are active, successful destruction, and subsequent state error on double destroy). The test fails to link on main without the exports.
  • python3 tools/check_conventions.py --base upstream/main: all checks passed.
  • git diff --check: passes with no whitespace or style warnings.
  • python3 tools/pr_overlap.py --branch feat/ult-mutex-pool-destroy: no overlap with open pull requests.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA18216 #5268
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4848350008 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5422). Originally posted by [mariusyo07](https://github.com/mariusyo07) on 2026-10-13T19:25:45Z. --- ### What Implements `sceUltMutexDestroy` and `sceUltWaitingQueueResourcePoolDestroy` in libSceUlt. METAL GEAR SOLID: Peace Walker - Master Collection Version (PPSA18216) stops on `main` when relinking `mgspw.elf` (#5268): ``` FAIL: unresolved ELF import jW+HnafeS3Y from libSceUlt.prx FAIL: unresolved ELF import or55417wcDk from libSceUlt.prx ``` - `sceUltMutexDestroy` validates the mutex pointer, returns `ULT_ERROR_BUSY` if currently locked, and removes the mutex from the registered map. - `sceUltWaitingQueueResourcePoolDestroy` validates the pool pointer, verifies that no registered mutexes, semaphores, or queue data pools are still referencing the waiting queue pool (returning `ULT_ERROR_BUSY` if so), and erases the pool. - `UltMutexState` and `UltSemaphoreState` now track their associated `_waitingPool` so that pool destruction safety is properly enforced. ### Tested - Linux (GCC 16.1.1): `Export.cpp` and `GuestUltThread.cpp` compile cleanly. - `guest_ult_thread_underscoredcreate` in `core/libs/tests/GuestUltThread.cpp` exercises `sceUltMutexDestroy` and `sceUltWaitingQueueResourcePoolDestroy` (null handling, unknown pool, busy checks when resources are active, successful destruction, and subsequent state error on double destroy). The test fails to link on `main` without the exports. - `python3 tools/check_conventions.py --base upstream/main`: all checks passed. - `git diff --check`: passes with no whitespace or style warnings. - `python3 tools/pr_overlap.py --branch feat/ult-mutex-pool-destroy`: no overlap with open pull requests. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA18216 #5268 - [x] GPUs tested: n/a - [x] AI-assisted: yes
copilot-pull-request-reviewer[bot] commented 2026-10-13 19:25:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-10-13T19:25:51Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5508841709 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5422#pullrequestreview-5508841709). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-10-13T19:25:51Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
github-actions[bot] commented 2026-10-13 19:26:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T19:26:22Z.


Progress report (63fb0cd → ca9f4e4)

📈 System libraries: 88.37% (+0.01%, +2 implemented functions)

✅ 2 implemented
Library Name
libSceUlt sceUltMutexDestroy
libSceUlt sceUltWaitingQueueResourcePoolDestroy
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6153435216 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5422#issuecomment-6153435216). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T19:26:22Z. --- <!-- progress-report --> ### Progress report (63fb0cd → ca9f4e4) 📈 **System libraries**: 88.37% (+0.01%, +2 implemented functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libSceUlt | `sceUltMutexDestroy` | | libSceUlt | `sceUltWaitingQueueResourcePoolDestroy` | </details>
CnRJay commented 2026-10-14 01:40:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:40:17Z.

Review: COMMENTED


sceUltWaitingQueueResourcePoolDestroy refusing a pool still referenced by a mutex, semaphore or queue pool looks right. sceUltMutexDestroy's busy check has two problems:

  • try_lock on a mutex the calling thread already owns is undefined for std::mutex. A ULT program that destroys a mutex while it holds it (a bug, but exactly what the BUSY answer is for) gets undefined behaviour instead of ULT_ERROR_BUSY. The same applies when the owner is another ULT scheduled on the same worker thread, which is the normal case for user-level threads.
  • It misses waiters. sceUltMutexLock looks the state up under gMutex, releases gMutex, then blocks in state->_mutex.lock(). A thread can be in that window, or blocked in lock() just after the owner unlocked. try_lock in Destroy then succeeds, the mutex is erased, and the waiter still acquires the orphaned std::mutex afterwards. Its sceUltMutexUnlock returns ULT_ERROR_STATE and leaves the mutex locked when the last shared_ptr destroys it, which is undefined behaviour too.

Tracking ownership and waiters explicitly avoids both. For example, keep owner (thread or ULT id) and waiters counters in UltMutexState, updated under its own lock in Lock/Unlock, and have Destroy return BUSY when either is set. The std::mutex can then be a condition-variable-based lock, so Destroy never has to probe it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5512079388 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5422#pullrequestreview-5512079388). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T01:40:17Z. **Review:** COMMENTED --- `sceUltWaitingQueueResourcePoolDestroy` refusing a pool still referenced by a mutex, semaphore or queue pool looks right. `sceUltMutexDestroy`'s busy check has two problems: - **`try_lock` on a mutex the calling thread already owns is undefined for `std::mutex`.** A ULT program that destroys a mutex while it holds it (a bug, but exactly what the `BUSY` answer is for) gets undefined behaviour instead of `ULT_ERROR_BUSY`. The same applies when the owner is another ULT scheduled on the same worker thread, which is the normal case for user-level threads. - **It misses waiters.** `sceUltMutexLock` looks the state up under `gMutex`, releases `gMutex`, then blocks in `state->_mutex.lock()`. A thread can be in that window, or blocked in `lock()` just after the owner unlocked. `try_lock` in `Destroy` then succeeds, the mutex is erased, and the waiter still acquires the orphaned `std::mutex` afterwards. Its `sceUltMutexUnlock` returns `ULT_ERROR_STATE` and leaves the mutex locked when the last `shared_ptr` destroys it, which is undefined behaviour too. Tracking ownership and waiters explicitly avoids both. For example, keep `owner` (thread or ULT id) and `waiters` counters in `UltMutexState`, updated under its own lock in `Lock`/`Unlock`, and have `Destroy` return `BUSY` when either is set. The `std::mutex` can then be a condition-variable-based lock, so `Destroy` never has to probe it.
adriadam10 commented 2026-10-14 11:42:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T11:42:07Z.

Review: CHANGES_REQUESTED


Thanks, built and ran guest_ult_thread_* on the PR merged onto main: green. Points to fix:

  • sceUltWaitingQueueResourcePoolDestroy (Export.cpp:258) returns ULT_ERROR_BUSY while any idle mutex/semaphore/queue pool is bound to the pool. The PS5 reference we have (prosper hle_ult.cpp, from libSceUlt or55417wcDk disassembly, busy return at 0x473a) refuses only while a thread is currently waiting on a pool-backed object; bound but idle objects do not block destroy on hardware. A title that destroys the pool before its mutexes would get BUSY here and OK on a console. Please drop the bound-object check, or limit it to what can be observed as waiting (and note the rest under TechnicalDebt).
  • sceUltMutexDestroy (Export.cpp:195): _mutex is a std::recursive_mutex, so try_lock from the thread that holds it succeeds and Destroy returns OK instead of BUSY; a thread blocked in sceUltMutexLock after it released gMutex is also missed (see CnRJay's review). Track owner/waiter counts in UltMutexState instead of probing the lock. Note prosper only logs a destroy of a held mutex and returns OK, so check which result you want and say which reference it follows.
  • Tests: the busy path of sceUltMutexDestroy is not exercised (the test only destroys an unlocked mutex). Add a case with a held mutex and a waiter.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5517312231 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5422#pullrequestreview-5517312231). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T11:42:07Z. **Review:** CHANGES_REQUESTED --- Thanks, built and ran `guest_ult_thread_*` on the PR merged onto main: green. Points to fix: - `sceUltWaitingQueueResourcePoolDestroy` (Export.cpp:258) returns `ULT_ERROR_BUSY` while any idle mutex/semaphore/queue pool is bound to the pool. The PS5 reference we have (prosper `hle_ult.cpp`, from libSceUlt `or55417wcDk` disassembly, busy return at 0x473a) refuses only while a thread is currently waiting on a pool-backed object; bound but idle objects do not block destroy on hardware. A title that destroys the pool before its mutexes would get BUSY here and OK on a console. Please drop the bound-object check, or limit it to what can be observed as waiting (and note the rest under TechnicalDebt). - `sceUltMutexDestroy` (Export.cpp:195): `_mutex` is a `std::recursive_mutex`, so `try_lock` from the thread that holds it succeeds and Destroy returns OK instead of BUSY; a thread blocked in `sceUltMutexLock` after it released `gMutex` is also missed (see CnRJay's review). Track owner/waiter counts in `UltMutexState` instead of probing the lock. Note prosper only logs a destroy of a held mutex and returns OK, so check which result you want and say which reference it follows. - Tests: the busy path of `sceUltMutexDestroy` is not exercised (the test only destroys an unlocked mutex). Add a case with a held mutex and a waiter.
adriadam10 commented 2026-10-14 13:16:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T13:16:50Z.

Review: APPROVED


Re-checked the new head: pool destroy refuses only with active waiters, the mutex tracks owner/lock count/waiters (no try_lock), the test covers a held mutex plus a waiter, and TechnicalDebt notes the difference from prosper. Built and ran guest_ult_thread_* on the PR merged onto main (green; the test fails to link without the change); merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5518497132 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5422#pullrequestreview-5518497132). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T13:16:50Z. **Review:** APPROVED --- Re-checked the new head: pool destroy refuses only with active waiters, the mutex tracks owner/lock count/waiters (no `try_lock`), the test covers a held mutex plus a waiter, and TechnicalDebt notes the difference from prosper. Built and ran `guest_ult_thread_*` on the PR merged onto main (green; the test fails to link without the change); merging.
orishamir commented 2026-10-14 13:56:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-14T13:56:40Z.


Windows run: Windows 11, WinLibs GCC 15.2.0 r7, Release, this head (ca9f4e47b) merged onto main f3a826ba5.

  • The 11 guest_ult* tests pass 5 of 5, and the full ctest has 0 failures out of 1184.
  • With only core/libs/prx/libSceUlt/Export.cpp taken back to main, guest_ult_thread_tests no longer links (undefined reference to sceUltMutexDestroy and sceUltWaitingQueueResourcePoolDestroy), so its cases can't pass without the change.

AI-assisted: yes (Claude Code Opus 5.5)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6170059579 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5422#issuecomment-6170059579). Originally posted by [orishamir](https://github.com/orishamir) on 2026-10-14T13:56:40Z. --- Windows run: Windows 11, WinLibs GCC 15.2.0 r7, Release, this head (ca9f4e47b) merged onto `main` f3a826ba5. - The 11 `guest_ult*` tests pass 5 of 5, and the full `ctest` has 0 failures out of 1184. - With only `core/libs/prx/libSceUlt/Export.cpp` taken back to `main`, `guest_ult_thread_tests` no longer links (`undefined reference to sceUltMutexDestroy` and `sceUltWaitingQueueResourcePoolDestroy`), so its cases can't pass without the change. AI-assisted: yes (Claude Code Opus 5.5)
adriadam10 commented 2026-10-14 14:01:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T14:01:47Z.


Re-checked the new head: pool destroy refuses only with active waiters, the mutex tracks owner/lock count/waiters (no try_lock), the test covers a held mutex plus a waiter, and TechnicalDebt notes the difference from prosper. Built and ran guest_ult_thread_* on the PR merged onto main (green; the test fails to link without the change); merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6170167311 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5422#issuecomment-6170167311). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T14:01:47Z. --- Re-checked the new head: pool destroy refuses only with active waiters, the mutex tracks owner/lock count/waiters (no `try_lock`), the test covers a held mutex plus a waiter, and TechnicalDebt notes the difference from prosper. Built and ran `guest_ult_thread_*` on the PR merged onto main (green; the test fails to link without the change); merging.
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!5422
No description provided.