feat(libkernel): implement the pthread_rwlock try and timed lock functions #344

Merged
DotDebian merged 1 commit from upstream-pr/344 into main 2026-10-04 22:42:30 +00:00
DotDebian commented 2026-10-02 10:17:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-02T10:17:41Z.


What

The POSIX layer of libkernel exported only pthread_rwlock_init, destroy, rdlock, wrlock and unlock. This PR adds the four missing lock functions in core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp, on top of the scePthreadRwlock* functions that already exist:

  • pthread_rwlock_tryrdlock and pthread_rwlock_trywrlock return 0 or EBUSY.
  • pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock take an absolute CLOCK_REALTIME deadline and return 0, ETIMEDOUT, or EINVAL when tv_nsec is outside [0, 1000000000).
  • All four accept the null static initializer, like the existing wrappers.

Behaviour follows POSIX.1-2017 and FreeBSD libthr (thr_rwlock.c): the timed variants try the lock first and only validate abstime when the lock is not free, so an invalid abstime on a free lock still returns 0.

Three choices to review:

  • Null abstime throws. FreeBSD treats it as an unbounded wait. I followed pthread_mutex_timedlock in this repository, which throws. I can make it wait forever instead if you prefer.
  • EDEADLK for the owning writer. A timed lock called by the thread that already holds the write lock returns EDEADLK, because scePthreadRwlockTimedrdlock and scePthreadRwlockTimedwrlock already do. POSIX allows it.
  • Deadline clamp. The deadline is converted with PosixThread::RelativeMicroseconds, so a deadline more than KernelUseconds max (about 71 minutes) away is clamped, as it is for sem_timedwait.

Two changes in the sce layer:

  • Pthread/include/Rwlock.hpp declares scePthreadRwlockTimedrdlock and scePthreadRwlockTimedwrlock, which were defined but not declared.
  • scePthreadRwlockTryrdlock and scePthreadRwlockTrywrlock return EBUSY when the caller already holds the write lock. They used to call try_lock_shared / try_lock on a std::shared_timed_mutex the thread already owns, which is undefined behaviour. This is a guard, not an observed bug: on my toolchain the old code already returned EBUSY.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release build with -DBUILD_TESTING=ON. No title or homebrew was run. Linux was not built or run locally; the change has no platform-specific code and the new test is registered on both platforms, so CI is the first Linux run of it.

New ctest guest_pthread_rwlock (core/libs/tests/GuestPthreadRwlock.cpp):

  • each of the four functions locks a null (statically initialized) rwlock and returns 0; the timed ones are given tv_nsec = 1000000000 and still return 0 because the lock is free;
  • the thread holding the write lock gets EBUSY from both try functions and EDEADLK from both timed functions;
  • with a reader on another thread: tryrdlock returns 0, trywrlock returns EBUSY, timedwrlock with a deadline 20 ms away returns ETIMEDOUT after at least 15 ms, a past deadline returns ETIMEDOUT, tv_nsec = 1000000000 and tv_nsec = -1 return EINVAL;
  • with a writer on another thread: both try functions return EBUSY, and both timed functions go through the same timeout and EINVAL checks;
  • timedwrlock and timedrdlock with a deadline 5 s away return 0 once the holder unlocks, 20 ms later;
  • a null abstime throws std::runtime_error.

The test passed 20 consecutive runs. It also passes without the EBUSY guard on this toolchain, so it does not prove the guard. Full suite: 108/108 tests pass.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4715294600 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/344). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-02T10:17:41Z. --- ### What The POSIX layer of libkernel exported only `pthread_rwlock_init`, `destroy`, `rdlock`, `wrlock` and `unlock`. This PR adds the four missing lock functions in `core/libs/prx/libkernel/Pthread/Posix/Rwlock.cpp`, on top of the `scePthreadRwlock*` functions that already exist: - `pthread_rwlock_tryrdlock` and `pthread_rwlock_trywrlock` return `0` or `EBUSY`. - `pthread_rwlock_timedrdlock` and `pthread_rwlock_timedwrlock` take an absolute `CLOCK_REALTIME` deadline and return `0`, `ETIMEDOUT`, or `EINVAL` when `tv_nsec` is outside `[0, 1000000000)`. - All four accept the null static initializer, like the existing wrappers. Behaviour follows POSIX.1-2017 and FreeBSD libthr (`thr_rwlock.c`): the timed variants try the lock first and only validate `abstime` when the lock is not free, so an invalid `abstime` on a free lock still returns `0`. Three choices to review: - **Null `abstime` throws.** FreeBSD treats it as an unbounded wait. I followed `pthread_mutex_timedlock` in this repository, which throws. I can make it wait forever instead if you prefer. - **`EDEADLK` for the owning writer.** A timed lock called by the thread that already holds the write lock returns `EDEADLK`, because `scePthreadRwlockTimedrdlock` and `scePthreadRwlockTimedwrlock` already do. POSIX allows it. - **Deadline clamp.** The deadline is converted with `PosixThread::RelativeMicroseconds`, so a deadline more than `KernelUseconds` max (about 71 minutes) away is clamped, as it is for `sem_timedwait`. Two changes in the sce layer: - `Pthread/include/Rwlock.hpp` declares `scePthreadRwlockTimedrdlock` and `scePthreadRwlockTimedwrlock`, which were defined but not declared. - `scePthreadRwlockTryrdlock` and `scePthreadRwlockTrywrlock` return `EBUSY` when the caller already holds the write lock. They used to call `try_lock_shared` / `try_lock` on a `std::shared_timed_mutex` the thread already owns, which is undefined behaviour. This is a guard, not an observed bug: on my toolchain the old code already returned `EBUSY`. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release build with `-DBUILD_TESTING=ON`. No title or homebrew was run. Linux was not built or run locally; the change has no platform-specific code and the new test is registered on both platforms, so CI is the first Linux run of it. New ctest `guest_pthread_rwlock` (`core/libs/tests/GuestPthreadRwlock.cpp`): - each of the four functions locks a null (statically initialized) rwlock and returns `0`; the timed ones are given `tv_nsec = 1000000000` and still return `0` because the lock is free; - the thread holding the write lock gets `EBUSY` from both `try` functions and `EDEADLK` from both `timed` functions; - with a reader on another thread: `tryrdlock` returns `0`, `trywrlock` returns `EBUSY`, `timedwrlock` with a deadline 20 ms away returns `ETIMEDOUT` after at least 15 ms, a past deadline returns `ETIMEDOUT`, `tv_nsec = 1000000000` and `tv_nsec = -1` return `EINVAL`; - with a writer on another thread: both `try` functions return `EBUSY`, and both `timed` functions go through the same timeout and `EINVAL` checks; - `timedwrlock` and `timedrdlock` with a deadline 5 s away return `0` once the holder unlocks, 20 ms later; - a null `abstime` throws `std::runtime_error`. The test passed 20 consecutive runs. It also passes without the `EBUSY` guard on this toolchain, so it does not prove the guard. Full suite: 108/108 tests pass. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-04 05:26:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-04T05:26:19Z.


Progress report (2cf901b → f9625f2)

📈 System libraries: 77.38% (+0.03%, +4 implemented functions)

✅ 4 implemented
Library Name
libkernel pthread_rwlock_timedrdlock_nid_postfix
libkernel pthread_rwlock_timedwrlock_nid_postfix
libkernel pthread_rwlock_tryrdlock_nid_postfix
libkernel pthread_rwlock_trywrlock_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5976920509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/344#issuecomment-5976920509). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-04T05:26:19Z. --- <!-- progress-report --> ### Progress report (2cf901b → f9625f2) 📈 **System libraries**: 77.38% (+0.03%, +4 implemented functions) <details> <summary>✅ 4 implemented</summary> | Library | Name | | - | - | | libkernel | `pthread_rwlock_timedrdlock_nid_postfix` | | libkernel | `pthread_rwlock_timedwrlock_nid_postfix` | | libkernel | `pthread_rwlock_tryrdlock_nid_postfix` | | libkernel | `pthread_rwlock_trywrlock_nid_postfix` | </details>
oneandonlydean commented 2026-10-04 05:42:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:42:01Z.


Thanks, this reads right to me against FreeBSD libthr (thr_rwlock.c: try first, then validate tv_nsec), and the OwnsWrite guards remove real undefined behaviour. It now conflicts with main in core/libs/prx/libkernel/CMakeLists.txt; could you rebase it? Then it looks ready from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977015872 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/344#issuecomment-5977015872). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:42:01Z. --- Thanks, this reads right to me against FreeBSD libthr (`thr_rwlock.c`: try first, then validate `tv_nsec`), and the `OwnsWrite` guards remove real undefined behaviour. It now conflicts with main in `core/libs/prx/libkernel/CMakeLists.txt`; could you rebase it? Then it looks ready from my side.
oneandonlydean commented 2026-10-04 22:37:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:54Z.


Thanks for the rebase; the code is unchanged apart from the CMake context, and CI is green now. guest_pthread_rwlock passes here on current main. Ready to merge from my side. #367 adds its test right after yours in libkernel/CMakeLists.txt, so whichever lands second needs a one-line rebase there.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985218373 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/344#issuecomment-5985218373). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:37:54Z. --- Thanks for the rebase; the code is unchanged apart from the CMake context, and CI is green now. `guest_pthread_rwlock` passes here on current main. Ready to merge from my side. #367 adds its test right after yours in `libkernel/CMakeLists.txt`, so whichever lands second needs a one-line rebase there.
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!344
No description provided.