feat(libkernel): add missing pthread attribute accessors #8373

Merged
slider merged 1 commit from upstream-pr/8373 into main 2026-10-24 15:29:29 +00:00
slider commented 2026-10-20 11:32:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slider on 2026-10-20T11:32:49Z.


What

Adds 15 missing pthread attribute accessors to libkernel. Most are the scePthread* counterparts of POSIX functions that already exist.

  • Mutex attributes: pthread_mutexattr_setkind_np and pthread_mutexattr_getkind_np, following FreeBSD 11.4 libthr (thr_mutexattr.c): an invalid attribute sets errno to EINVAL and returns -1, and the kind is the mutex type. A kind outside 1 to 4 throws, because pthread_mutex_init here does not reject it with EINVAL as FreeBSD's mutex_check_attr does. Also scePthreadMutexattrGettype, Getprotocol, Setprioceiling, Getprioceiling, Setpshared and Getpshared.
  • scePthreadMutexattrSetprotocol now stores the protocol through pthread_mutexattr_setprotocol, so scePthreadMutexattrGetprotocol reports it. It keeps the 0 to 2 range and the EINVAL results measured on a PS5 (firmware 13.60).
  • Condition variables: pthread_cond_signalto_np over the existing scePthreadCondSignalto; scePthreadCondattrSetpshared and scePthreadCondattrGetpshared.
  • Read-write locks: pthread_rwlockattr_gettype_np and scePthreadRwlockattrGettype, which report what scePthreadRwlockattrSettype stored; scePthreadRwlockattrSetpshared and scePthreadRwlockattrGetpshared.
  • Each SCE variant calls its POSIX function and returns the error as 0x8002xxxx (new PosixThread::ToSce, the inverse of ToErrno), matching how shadPS4 registers these NIDs (ORBIS(posix_...) in src/core/libraries/kernel/threads/{mutex,condvar,rwlock}.cpp).
  • Debt (docs/dev/debt/libkernel.md): scePthreadMutexattrSetprotocol's entry is cut to what is still open, and the remaining gaps are recorded: the setkind_np throw, signalto waking every waiter, and the unapplied rwlock type.

Left out on purpose:

  • pthread_condattr_getclock and scePthreadCondattrGetclock, which boykopovar/AnyPS5#8010 implements.
  • pthread_rwlockattr_settype_np: the lock never applies the type, so it would be a new silent stub.
  • scePthreadMutexattrGetkind/Setkind/Getgen/Setgen: their console behaviour is undocumented.

Tested

Linux (Ubuntu 24.04, GCC 13.3, Release):

  • New cases pass in guest_pthread_mutexattr, guest_pthread_condattr, guest_pthread_rwlock and guest_cond_timedwait. The signalto case has a waiter woken by pthread_cond_signalto_np from another thread.
  • The existing guest_pthread_mutex_errors, guest_kernel_errors (measured scePthreadMutexattrSetprotocol results) and guest_pthread_destroyed_objects pass.
  • Breaking ToSce or the stored value makes the new tests fail.
  • tools/check_conventions.py --base origin/main passes.

Windows: every changed source and test file passes a syntax-only check with MinGW-w64 GCC 13 (not the supported 15.2); not built or 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:4935101893 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8373). Originally posted by [slider](https://github.com/slider) on 2026-10-20T11:32:49Z. --- ### What Adds 15 missing pthread attribute accessors to libkernel. Most are the `scePthread*` counterparts of POSIX functions that already exist. - Mutex attributes: `pthread_mutexattr_setkind_np` and `pthread_mutexattr_getkind_np`, following FreeBSD 11.4 libthr (`thr_mutexattr.c`): an invalid attribute sets errno to `EINVAL` and returns -1, and the kind is the mutex type. A kind outside 1 to 4 throws, because `pthread_mutex_init` here does not reject it with `EINVAL` as FreeBSD's `mutex_check_attr` does. Also `scePthreadMutexattrGettype`, `Getprotocol`, `Setprioceiling`, `Getprioceiling`, `Setpshared` and `Getpshared`. - `scePthreadMutexattrSetprotocol` now stores the protocol through `pthread_mutexattr_setprotocol`, so `scePthreadMutexattrGetprotocol` reports it. It keeps the 0 to 2 range and the `EINVAL` results measured on a PS5 (firmware 13.60). - Condition variables: `pthread_cond_signalto_np` over the existing `scePthreadCondSignalto`; `scePthreadCondattrSetpshared` and `scePthreadCondattrGetpshared`. - Read-write locks: `pthread_rwlockattr_gettype_np` and `scePthreadRwlockattrGettype`, which report what `scePthreadRwlockattrSettype` stored; `scePthreadRwlockattrSetpshared` and `scePthreadRwlockattrGetpshared`. - Each SCE variant calls its POSIX function and returns the error as `0x8002xxxx` (new `PosixThread::ToSce`, the inverse of `ToErrno`), matching how shadPS4 registers these NIDs (`ORBIS(posix_...)` in `src/core/libraries/kernel/threads/{mutex,condvar,rwlock}.cpp`). - Debt (`docs/dev/debt/libkernel.md`): `scePthreadMutexattrSetprotocol`'s entry is cut to what is still open, and the remaining gaps are recorded: the `setkind_np` throw, `signalto` waking every waiter, and the unapplied rwlock type. Left out on purpose: - `pthread_condattr_getclock` and `scePthreadCondattrGetclock`, which boykopovar/AnyPS5#8010 implements. - `pthread_rwlockattr_settype_np`: the lock never applies the type, so it would be a new silent stub. - `scePthreadMutexattrGetkind`/`Setkind`/`Getgen`/`Setgen`: their console behaviour is undocumented. ### Tested Linux (Ubuntu 24.04, GCC 13.3, Release): - New cases pass in `guest_pthread_mutexattr`, `guest_pthread_condattr`, `guest_pthread_rwlock` and `guest_cond_timedwait`. The `signalto` case has a waiter woken by `pthread_cond_signalto_np` from another thread. - The existing `guest_pthread_mutex_errors`, `guest_kernel_errors` (measured `scePthreadMutexattrSetprotocol` results) and `guest_pthread_destroyed_objects` pass. - Breaking `ToSce` or the stored value makes the new tests fail. - `tools/check_conventions.py --base origin/main` passes. Windows: every changed source and test file passes a syntax-only check with MinGW-w64 GCC 13 (not the supported 15.2); not built or 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
harisibrar1 commented 2026-10-24 13:56:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:56:17Z.

Review: APPROVED


Checked against main 522e32e3b6 and FreeBSD 11.4 libthr (thr_mutexattr.c L108-133, thr_mutex.c L207-218):

  • pthread_mutexattr_setkind_np/getkind_np match FreeBSD: errno EINVAL with -1 for a null or uninitialized attribute, getkind by value. The throw for kinds outside 1-4 stands in for mutex_check_attr's later EINVAL and is recorded in libkernel.md, which seems the right call.
  • None of the 15 functions exist on current main, and the branch still merges cleanly into 522e32e3b6. The only file shared with #9156 is libkernel.md (#9156 only adds pthread_attr_* thread-attribute forms), so the two don't overlap.
  • ToSce inverts ToErrno for every value the wrapped POSIX functions return (0 or 22), and scePthreadMutexattrSetprotocol keeps the 13.60-measured 0-2 range and EINVAL results while now storing the protocol.
  • Looks good to me; the #10173 ordering note above covers the only interaction I found.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638495608 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8373#pullrequestreview-5638495608). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:56:17Z. **Review:** APPROVED --- Checked against main 522e32e3b6 and FreeBSD 11.4 libthr (thr_mutexattr.c L108-133, thr_mutex.c L207-218): - `pthread_mutexattr_setkind_np`/`getkind_np` match FreeBSD: errno EINVAL with -1 for a null or uninitialized attribute, getkind by value. The throw for kinds outside 1-4 stands in for `mutex_check_attr`'s later EINVAL and is recorded in libkernel.md, which seems the right call. - None of the 15 functions exist on current main, and the branch still merges cleanly into 522e32e3b6. The only file shared with #9156 is libkernel.md (#9156 only adds `pthread_attr_*` thread-attribute forms), so the two don't overlap. - `ToSce` inverts `ToErrno` for every value the wrapped POSIX functions return (0 or 22), and `scePthreadMutexattrSetprotocol` keeps the 13.60-measured 0-2 range and EINVAL results while now storing the protocol. - Looks good to me; the #10173 ordering note above covers the only interaction I found. Reviewed with AI assistance (Claude Code); the checks above were run locally 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!8373
No description provided.