feat(libc): implement Dinkumware mutex and condition exports #3079
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3079"
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 MarcZX10 on 2026-10-10T21:50:59Z.
What
Carry the Dinkumware mutex and condition-variable implementation offered from screamm's branch in issue #3068. The source branch's single commit (
56b03751) records its author asDavid <[email protected]>and Claude as co-author; this PR preserves that commit metadata. The issue links the branch to screamm, but the commit metadata does not identify David with that account, so attribution has not been rewritten.The eight exports use the console's pointer-to-handle ABI. The title
libc.prxdisassembly reported in this review shows_Mtx_lockmapping success to_Thrd_success(0),0x8002000bto_Thrd_busy(3), and other failures to_Thrd_error(4). Its internal mutex initialization checks recursive flag0x100; other types use default attributes._Mtx_unlockand_Cnd_waitreturn success regardless of their internal result. The host implementation models these results, while guarding invalid handles and destroying a locked mutex with exceptions. The default mutex attribute and those guard cases remain unverified on console hardware and are recorded inTechnicalDebt.md.Tested
libcandguest_libc_threads_testson the rebased head;guest_libc_threadspassed 20 consecutive runs.tools/check_conventions.py --base upstream/mainandgit diff --checkpass.guest_libc_threads3/3 on the source commit, before the carried follow-up changes in this PR.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T21:51:27Z.
Progress report (
3330144→b603d19)📈 System libraries: 85.15% (+0.24%, +8 implemented functions)
✅ 8 implemented
_Cnd_broadcast_nid_postfix_Cnd_destroy_nid_postfix_Cnd_init_nid_postfix_Cnd_wait_nid_postfix_Mtx_destroy_nid_postfix_Mtx_init_nid_postfix_Mtx_lock_nid_postfix_Mtx_unlock_nid_postfixImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T21:53:26Z.
Review: CHANGES_REQUESTED
Thanks for carrying this. The build and
guest_libc_threadspass here on Linux (20/20 repeats), but the ABI doesn't match the console, so I can't merge it yet:core/libs/prx/libc/src/Threads.cpp:39-55,65-155: the handle is taken by value (microsoft/STL contract), but the console's exports take a pointer to the handle. The aerolib names for the PS5 exports are_Mtx_init(pthread_mutex**, int),_Mtx_lock(pthread_mutex**)and_Cnd_wait(pthread_cond**, pthread_mutex**), and the disassembly of_Mtx_lockin a title'slibc.prx(vaddr0x5a30, 33 bytes) forwardsrdiuntouched to an internal lock call and maps its result (0→0,0x8002000b→_Thrd_busy, anything else →_Thrd_error(4)). A real title passes the address of the slot that_Mtx_initfilled, soResolveMutexreads the wrong word and throws. Please takevoid**for lock, unlock, destroy and_Cnd_wait/_Cnd_broadcast/_Cnd_destroy(dereference once) and check the result mapping against that disassembly (_Mtx_unlock,_Cnd_wait,_Mtx_destroytoo).core/libs/tests/GuestLibcThreads.cpp: the tests only pin the by-value signature, so they pass with the wrong ABI. Update them to pass&handle, and keep a case where the dereferenced slot is not a handle created by_Mtx_init.Threads.cpp:57-59:IsOwnedByCallerreads the plainint countbefore checkingowner, which is a data race when another thread unlocks. Checkownerfirst.docs/dev/TechnicalDebt.md:490: the "unverified whether it takes the handle by value" part will need rewriting once the above is done; the line is also the last of its section, where every PR appends and conflicts. Please put it a few lines up.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T22:03:40Z.
Addressed in commits
e5920758,6d3e9384andcdc86c35.void**and dereference the handle slot once. The test declarations and calls now use&handle, including empty/invalid slots._Mtx_lockmaps deadlock to_Thrd_busy(3) and other hostsystem_errors to_Thrd_error(4); invalid unlocks and waits return_Thrd_error._Mtx_destroyhas no status result and still throws when asked to destroy a locked mutex.Validation on the current head: Release
libcandguest_libc_threads_testsbuild;guest_libc_threadspasses 20 consecutive runs; local and CI conventions pass. The automatic register-context, write-watch and Linux/macOS/Windows relinker jobs also passed on the preceding update. Full Linux and Windows builds were skipped by the repository's PR workflow policy.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T22:06:04Z.
Follow-up to the overlap notice for #3047: I moved this PR's
TechnicalDebt.mdentry beside the existing libc thread-runtime note, away from the line where #3047 adds itspthread_killentry. The two implementations do not depend on each other; this docs-only commit avoids the insertion conflict.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T22:09:50Z.
The rebase onto current
mainis complete. I preserved the upstream changes and kept the TechnicalDebt entry besidestd_execute_once, which also avoids the insertion overlap with #3047. On the rebased head, Releaselibcandguest_libc_threads_testsbuild,guest_libc_threadspassed 20 consecutive runs, and conventions plusgit diff --checkpass. CI has passed eligibility and conventions; the platform test matrix is still running. The ABI, data race, and result-mapping changes requested in the review are included in the rebased commits. Please re-review when the checks finish.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:14:44Z.
Review: CHANGES_REQUESTED
Thanks for the changes: the
void**handles, the owner-before-count check and the moved TechnicalDebt line look right, and the build andguest_libc_threadspass here on Linux. The disassembly of a title'slibc.prxstill disagrees with what the tests pin:Threads.cpp:118-127,:150-172:_Mtx_unlock(vaddr0x5a20) and_Cnd_wait(0x4ea0) just call the internal unlock/wait on their arguments and return 0, whatever the internal call returned. Returning_Thrd_errorfor an unowned unlock or an invalid wait is not what the console does. Please return 0 there, or throw for the cases whose real outcome depends on pthread internals;GuestLibcThreads.cpp:52,54,146,149need to follow.Threads.cpp:74,100-102: the internal init at0x5900only tests bit0x100(recursive:settype(2)); every other type uses the default attributes, and no type is rejected. The default mutex relocked by its owner fails with0x8002000b, which_Mtx_lockmaps to_Thrd_busy(3). So a plain (0x01) mutex should return busy on relock like the others (GuestLibcThreads.cpp:63-64pins success), and the type validation throws where the console accepts.docs/dev/TechnicalDebt.md:162: the unlock/wait/relock mappings are now known from the disassembly; rewrite the line to say what is still unverified (e.g. that the default attribute is error-checking, inferred from the0x8002000bmapping).Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:21:21Z.
Review: CHANGES_REQUESTED
The new head only rebases onto
main;Threads.cppandGuestLibcThreads.cppare unchanged, so the points from the previous review still apply:Threads.cpp:118-127,:150-172:_Mtx_unlockand_Cnd_waitreturn 0 on the console whatever the internal call returned. Return 0 there (or throw where the real outcome depends on pthread internals);GuestLibcThreads.cpp:52,54,146,149need to follow.Threads.cpp:74,100-102: the internal init only tests bit0x100, every other type gets the default attributes and none is rejected. Relocking a plain (0x01) mutex returns busy (3) like the others (GuestLibcThreads.cpp:63-64pins success), and the type validation should not throw.docs/dev/TechnicalDebt.md:162: rewrite it to say only what is still unverified (e.g. that the default attribute is error-checking, inferred from the0x8002000bmapping).Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T22:22:58Z.
Follow-up: I rebased again onto current
mainafter #3078 merged. The full diff remains unchanged, and the current-head CI matrix has now passed eligibility, conventions, report, register-context, write-watch and all three relinker jobs. Full Linux and Windows builds remain skipped under the PR workflow policy. The local Release build and 20 consecutiveguest_libc_threadsruns also passed. Please re-review this current head when convenient.Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-10T22:30:33Z.
One more thing on aa00494a, built on Linux (GCC 16.2.1):
_Mtx_destroyfrees the object but leaves the slot pointing at it. In a small probe against the exports, after_Mtx_init(&m, 2)and_Mtx_destroy(&m)the slot still holds the old handle, and a following_Mtx_lock(&m)returns 0 instead of throwing, so it is working on freed memory._Cnd_destroylooks the same from the code but I only ran the mutex case.Clearing the slot (or storing a destroyed marker) in both destroy functions would make a use after destroy fail loudly.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-10T22:35:05Z.
Review: COMMENTED
The stubs-to-implementation move is clean, and what stands out first is that the normal paths follow the contract exactly:
_Mtx_lockreturns busy on a second plain lock,_Mtx_unlockreturns error when called from the wrong thread, and the test pins both of those alongside destroy-while-locked and an invalid handle. Those are the cases a Dinkumware implementation is judged on, and defining the_Thrd_*result set instead of returning bare numbers keeps them readable.The invalid-handle paths are the one place I would ask about the contract. They throw —
invalid_argumentfor a null or foreign handle,logic_errorfor destroying a locked mutex — and the reference you cite does not throw at all: in microsoft/STL'sxthreads.hthese_Mtx_*and_Cnd_*entries are declarednoexceptand report through_Thrd_result, while their public C++ face,std::mutex::lock, is documented to throwstd::system_errorand nothing else.invalid_argumentandlogic_errorappear in neither set, so a guest catching what the contract allows would not catch them and would not even be able to reason about them from the cited headers. If the throws are a deliberate debug aid, saying so is enough; if the console's behaviour is what is being modelled, the_Thrd_*codes are already sitting in the file.And the question this tree always asks of a libc implementation: which title reaches these exports, and which log line changes from
NotImplementedto real behaviour? The description does not claim one, which is honest — but that line turning over is exactly the evidence that would make this landable, and if no title has reached them yet, saying so is just as useful to whoever tests next.Two things worth keeping exactly as they are. The cited microsoft/STL contract is the right reference for these functions, and recording the console ABI uncertainty in
TechnicalDebt.mdrather than asserting it is the correct call — the entry names the headers it checked and says plainly that the console build is unverified.One thing that does not match the description, if it matters to anyone reading later. The body says the original commit author is preserved, but the commits on this PR are authored by a local
[email protected]identity and by you; none is authored by screamm. The code may well be theirs — issue #3068 points at their branch — but the authorship record is not, and in a project that has been careful about exactly this, the discrepancy is worth a look rather than silence. The other carried change in this batch does keep its author, so this reads like an accident rather than a policy.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T22:36:39Z.
The latest probe found that
_Mtx_destroyand_Cnd_destroyfreed their objects but left the guest handle slots pointing at the freed memory. Both destroy functions now clear the caller's slot after deletion. The tests assert that the slots become null and that subsequent lock/broadcast calls reject them safely.Validation on this head: Release
libcandguest_libc_threads_testsbuild;guest_libc_threadspassed 20 consecutive runs; conventions andgit diff --checkpassed. Please re-review the updated head.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:39:14Z.
Review: CHANGES_REQUESTED
Thanks for the destroy fix (slots cleared, tests assert it); the build and
guest_libc_threadspass here on Linux. The new head doesn't touch the points from the last two reviews, which still apply:core/libs/prx/libc/src/Threads.cpp:119-127,:152-173:_Mtx_unlock(vaddr0x5a20) and_Cnd_wait(0x4ea0) in a title'slibc.prxcall the internal unlock/wait and return 0 whatever it returned. Return 0 there (or throw where the real outcome depends on pthread internals);core/libs/tests/GuestLibcThreads.cpp:52,54,148,151need to follow.Threads.cpp:74-75,100-103: the internal init at0x5900only tests bit0x100(recursive); every other type gets the default attributes and none is rejected. The default mutex relocked by its owner fails with0x8002000b, which_Mtx_lockmaps to busy (3), so a plain (0x01) mutex must return busy on relock (GuestLibcThreads.cpp:64-66pins success), and the type validation shouldn't throw.docs/dev/TechnicalDebt.md:162: once the above is done, rewrite it to list only what is still unverified (e.g. that the default attribute is error-checking, inferred from the0x8002000bmapping).Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T22:48:12Z.
Thanks for the detailed follow-up. I updated the implementation and tests on the rebased head:
_Mtx_initno longer rejects type values. The host model treats only0x100as recursive, uses the default behavior for other values, and returns_Thrd_busywhen the owner relocks a non-recursive mutex._Mtx_unlockand_Cnd_waitnow return success for the unowned or recursive-count cases that the console wrapper reports as success. The host model leaves those invalid operations without changing lock state. A hostcondition_variableerror still propagates as an exception after restoring the mutex bookkeeping.The throws for null or foreign handles and destroying a locked mutex are deliberate host-side diagnostic guards. I have not found console evidence for those invalid inputs, so I am not presenting those exceptions as Dinkumware or console behavior. No title-level run or log line for these exports is available yet.
I also checked the source attribution: issue #3068 links the source branch to screamm, while the source commit itself records
David <[email protected]>and Claude as co-author. I preserved that metadata and corrected the PR description to state the discrepancy instead of changing authorship without evidence.Validation on this head: Linux Release
libcandguest_libc_threads_testsbuild;guest_libc_threadspasses 20 consecutive runs; conventions andgit diff --checkpass. Please re-review this rebased head. The current PR workflow skips the full Linux and Windows builds under repository policy.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:52:46Z.
Review: APPROVED
Checked the build and
guest_libc_threadson Linux (partial ctest 63/63), and that all points from the previous reviews are in this head (void**handles, owner-before-count,_Mtx_unlock/_Cnd_waitreturning success,0x100-only recursive type, plain relock returning busy, destroy clearing the slot, TechnicalDebt rewritten). Merging once CI finishes.