Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/libs/prx/libc/src/Threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,10 @@ int APS5_VABI _Mtx_timedlock_nid_postfix(void** handle, const Xtime* target) {
auto* mutex = ResolveMutex(handle, "_Mtx_timedlock");
const auto self = std::this_thread::get_id();
if (mutex->owner.load(std::memory_order_acquire) == self) {
if ((mutex->type & mutexRecursive) == 0)
return threadBusy;
if ((mutex->type & mutexRecursive) == 0) {
std::this_thread::sleep_until(deadline);
return threadTimedout;
}
if (mutex->count == std::numeric_limits<int>::max())
throw std::overflow_error("_Mtx_timedlock: recursive lock count overflow");
++mutex->count;
Expand Down
13 changes: 12 additions & 1 deletion core/libs/tests/GuestLibcThreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,18 @@ static void TimedMutex() {
void* mutex = CreateMutex(tryable);
Xtime future = RelativeTime(60000);
Require(_Mtx_timedlock_nid_postfix(&mutex, &future) == success);
Require(_Mtx_timedlock_nid_postfix(&mutex, &future) == busy);
const auto start = std::chrono::steady_clock::now();
Xtime shortDeadline = RelativeTime(100);
Require(_Mtx_timedlock_nid_postfix(&mutex, &shortDeadline) == timedout);
Require(std::chrono::steady_clock::now() - start >= std::chrono::milliseconds(90));
void* plainOwned = CreateMutex(plain);
Require(_Mtx_lock_nid_postfix(&plainOwned) == success);
const auto plainStart = std::chrono::steady_clock::now();
Xtime plainDeadline = RelativeTime(100);
Require(_Mtx_timedlock_nid_postfix(&plainOwned, &plainDeadline) == timedout);
Require(std::chrono::steady_clock::now() - plainStart >= std::chrono::milliseconds(90));
Require(_Mtx_unlock_nid_postfix(&plainOwned) == success);
_Mtx_destroy_nid_postfix(&plainOwned);
Require(_Mtx_unlock_nid_postfix(&mutex) == success);

Require(_Mtx_lock_nid_postfix(&mutex) == success);
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/debt/libc.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
- [Dinkumware mutexes and conditions](../../../core/libs/prx/libc/src/Threads.cpp) (libc) use pointer-to-handle arguments, matching the console aerolib signatures reported in [#3079](https://github.com/boykopovar/AnyPS5/pull/3079). The title's `_Mtx_lock` maps `0` to `_Thrd_success` (0), `0x8002000b` to `_Thrd_busy` (3), and other failures to `_Thrd_error` (4); its internal mutex init recognizes recursive flag `0x100`, while every other type uses default attributes. The observed relock failure for a plain mutex suggests those defaults are error-checking, but that has not been confirmed on hardware. The title's `_Mtx_unlock` and `_Cnd_wait` return success regardless of their internal result. The host implementation treats invalid ownership preconditions as no-ops, and throws for invalid handles, host wait errors and destroying a locked mutex as debug guards; those cases are not confirmed against the console.
- [feraiseexcept](../../../core/libs/prx/libc/src/FloatingPoint.cpp) (libSceLibcInternal, implemented in libc, NID `NIfFNcyeCTo`) - console behavior and module/library versions have not been measured. The signature follows the [public PS5 SDK](https://github.com/ps5-payload-dev/sdk/blob/a7685ce8e97ce1f8a4f38253a41d54241c9124f1/include/freebsd/fenv.h); flag updates and x87 trap delivery follow [FreeBSD amd64 fenv.c](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/msun/amd64/fenv.c). All six defined exception bits are supported. Unlike the FreeBSD reference, which does not mask or validate the requested bits, this implementation rejects other bits with `std::invalid_argument` before modifying state; console behavior for those bits is unknown. This is distinct from `_Feraise`, which also modifies errno and adds inexact for range exceptions.
- [stdc_count_zeros_uc, us, ui, ul and ull](../../../core/libs/prx/libc/src/IntegerBuiltins.cpp) (libc) have not been measured on a console. Their unsigned 8/16/32/64/64-bit arguments and zero-bit counts follow PPSA03671 v01.001.005's bundled libc.prx, read from disassembly and run directly on a Windows host; the ul argument remains 64-bit on Windows.
- [_Mtx_timedlock](../../../core/libs/prx/libc/src/Threads.cpp) (libc, NID `hPzYSd5Nasc`) follows the console's `libc.prx` at `0x5d50` (PPSA02530): it takes an absolute xtime and returns `_Thrd_timedout` (2) on expiry (`0x8002003c`) or 5 on other failures. On non-Windows hosts the native mutex and condition are `std::timed_mutex` and `std::condition_variable_any`; Windows keeps the `CRITICAL_SECTION`/`CONDITION_VARIABLE` pair because `std::condition_variable_any` failed `guest_libc_cnd_timed_wait` on the Windows runner, and its `_Mtx_timedlock` polls `TryEnterCriticalSection` until the deadline. Windows timing is not measured on the console.
- [_Mtx_timedlock](../../../core/libs/prx/libc/src/Threads.cpp) (libc, NID `hPzYSd5Nasc`) follows the console's `libc.prx` at `0x5d50` (PPSA02530): it takes an absolute xtime and returns `_Thrd_timedout` (2) on expiry (`0x8002003c`) or 5 on other failures. On non-Windows hosts the native mutex and condition are `std::timed_mutex` and `std::condition_variable_any`; Windows keeps the `CRITICAL_SECTION`/`CONDITION_VARIABLE` pair because `std::condition_variable_any` failed `guest_libc_cnd_timed_wait` on the Windows runner, and its `_Mtx_timedlock` polls `TryEnterCriticalSection` until the deadline. A thread that already owns a plain mutex waits for the deadline and returns `_Thrd_timedout`, as a PS5 payload process (firmware 11.60) did after 100 ms; the title process and Windows timing are not measured on the console.
- [creal, crealf, creall, cimag, cimagf, cimagl, conj, conjf, conjl, cproj, cprojf, cprojl, carg, cargf, cargl, cabs, cabsf and cabsl](../../../core/libs/prx/libc/src/FloatBuiltins.cpp) (libc) - follow FreeBSD 11 msun and C99: `creal`/`cimag` extract components, `conj` conjugates the imaginary sign, `cproj` projects infinities to the Riemann sphere with signed zero imaginary part, `carg` delegates to `atan2*` and `cabs` delegates to `hypot*` (see `atan2l`/`hypotl` for host-libm long double precision); console unverified
- [Floating-point compiler-rt builtins](../../../core/libs/prx/libc/src/FloatBuiltins.cpp) (libc) follow the `libc.prx` bundled with PPSA08427 (disassembly, and that binary run side by side on random inputs) where it differs from current compiler-rt: the soft-float `__add`, `__sub`, `__mul` and `__div` (`sf3`, `df3`) round to nearest, raise no exception flags and ignore MXCSR, here done with SSE under a default MXCSR that is restored afterwards; division flushes a quotient below the smallest normal to a signed zero; `__extendsfdf2` and `__truncdfsf2` copy NaN payloads unshifted; float-to-integer conversions do not saturate but shift the significand by a count masked to the shift width. `__mulsc3`, `__muldc3`, `__divsc3` and `__divdc3` fuse the same products as the console with `fma` and scale through the x87 like its `scalbn`, so their values match, but NaN payloads and exception flags can differ, as the host `fma` and `logb` are not the console's VEX FMA code and libm
- [stdc_leading_zeros_uc, us, ui, ul and ull; stdc_trailing_zeros_uc, us, ui, ul and ull](../../../core/libs/prx/libc/src/IntegerBuiltins.cpp) (libc) have not been measured on a console. Their unsigned 8/16/32/64/64-bit arguments, zero-input results and leading/trailing zero counts follow PPSA03671 v01.001.005's bundled libc.prx, read from disassembly and run directly on a Windows host; ul remains 64-bit on Windows.
Expand Down
Loading