fix(libkernel): number pthread keys from 1 as FreeBSD libthr does #3623

Merged
milescoviello merged 1 commit from upstream-pr/3623 into main 2026-10-12 07:24:16 +00:00
milescoviello commented 2026-10-11 14:21:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-11T14:21:45Z.


What

Fixes #3118. scePthreadKeyCreate and pthread_key_create handed out the key table's slot index, so the first key in a process was 0. FreeBSD libthr returns slot + 1 (lib/libthr/thread/thr_spec.c, _pthread_key_create: *key = i + 1) and subtracts 1 again in pthread_key_delete, pthread_getspecific and pthread_setspecific, so 0 is never a valid key. Code that keeps 0 as "key not created yet" (if (!key) pthread_key_create(&key, …)) therefore created a new key on every call and lost the value stored under the previous one.

Pthread/src/Tsd.cpp now returns slot + 1 and maps a key back through KeySlot() (unsigned key - 1, as libthr does). 0 and out-of-range keys are still rejected with EINVAL by delete and set, and read as NULL.

Tested

  • Rebased onto main 43bc17f7 after #3753, which also changed Tsd.cpp, and rechecked on Ubuntu 24.04, GCC 13.3 (CI's compiler): guest_pthread_key, guest_pthread_destructor_lifetime (from #3753) and guest_force_tls_destructor pass, guest_pthread_key fails with that main's Tsd.cpp, and all 292 guest_* tests pass.
  • Gentoo Linux, GCC 15.3.0, Release with CI's configure flags, on main a11c8f7f.
  • guest_pthread_key gains checks: the first pthread_key_create key is non-zero, key 0 is EINVAL for set and delete and reads as NULL, and the first scePthreadKeyCreate key is non-zero. They fail with main's Tsd.cpp and pass with this change.
  • guest_pthread_key, guest_pthread_cancel, guest_thread_atexit, guest_force_tls_destructor and host_thread_local: 5/5.
  • Full ctest on an RTX 5090 Laptop GPU: 834/835, with guest_video_out_* left out because the displays are off. The one failure, agc_driver_emulated_color_compare, fails the same way on plain main a11c8f7f.
  • Windows: @hikazey ran this change in #3118 (5/5 with it, 5/5 failing with Tsd.cpp reverted, full suite matching main).

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819734858 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3623). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-11T14:21:45Z. --- ### What Fixes #3118. `scePthreadKeyCreate` and `pthread_key_create` handed out the key table's slot index, so the first key in a process was 0. FreeBSD libthr returns slot + 1 (`lib/libthr/thread/thr_spec.c`, `_pthread_key_create`: `*key = i + 1`) and subtracts 1 again in `pthread_key_delete`, `pthread_getspecific` and `pthread_setspecific`, so 0 is never a valid key. Code that keeps 0 as "key not created yet" (`if (!key) pthread_key_create(&key, …)`) therefore created a new key on every call and lost the value stored under the previous one. `Pthread/src/Tsd.cpp` now returns slot + 1 and maps a key back through `KeySlot()` (unsigned `key - 1`, as libthr does). 0 and out-of-range keys are still rejected with `EINVAL` by delete and set, and read as `NULL`. ### Tested - Rebased onto `main` 43bc17f7 after #3753, which also changed `Tsd.cpp`, and rechecked on Ubuntu 24.04, GCC 13.3 (CI's compiler): `guest_pthread_key`, `guest_pthread_destructor_lifetime` (from #3753) and `guest_force_tls_destructor` pass, `guest_pthread_key` fails with that `main`'s `Tsd.cpp`, and all 292 `guest_*` tests pass. - Gentoo Linux, GCC 15.3.0, Release with CI's configure flags, on `main` a11c8f7f. - `guest_pthread_key` gains checks: the first `pthread_key_create` key is non-zero, key 0 is `EINVAL` for set and delete and reads as `NULL`, and the first `scePthreadKeyCreate` key is non-zero. They fail with `main`'s `Tsd.cpp` and pass with this change. - `guest_pthread_key`, `guest_pthread_cancel`, `guest_thread_atexit`, `guest_force_tls_destructor` and `host_thread_local`: 5/5. - Full `ctest` on an RTX 5090 Laptop GPU: 834/835, with `guest_video_out_*` left out because the displays are off. The one failure, `agc_driver_emulated_color_compare`, fails the same way on plain `main` a11c8f7f. - Windows: @hikazey ran this change in #3118 (5/5 with it, 5/5 failing with `Tsd.cpp` reverted, full suite matching `main`). ### 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 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
blawesom commented 2026-10-11 15:20:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-11T15:20:05Z.


Independent pre-review on Linux (full CI runs only happen on manual trigger, so recording one): CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON.

  • Builds; tools/check_conventions.py --base origin/main passes.
  • Full suite: 832/832 passed (all tests including the Vulkan suites on AMD RX 9070 XT / RADV).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110520386 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3623#issuecomment-6110520386). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-11T15:20:05Z. --- Independent pre-review on **Linux** (full CI runs only happen on manual trigger, so recording one): CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`. - Builds; `tools/check_conventions.py --base origin/main` passes. - Full suite: **832/832 passed** (all tests including the Vulkan suites on AMD RX 9070 XT / RADV).
milescoviello commented 2026-10-11 15:20:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-11T15:20:35Z.


Thanks @blawesom for the full run on RADV. Good to have it on a second compiler and GPU.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110525181 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3623#issuecomment-6110525181). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-11T15:20:35Z. --- Thanks @blawesom for the full run on RADV. Good to have it on a second compiler and GPU.
milescoviello commented 2026-10-11 22:17:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-11T22:17:40Z.


Rebased onto main 43bc17f7 now that #3753 is in, since it also changed Tsd.cpp. With both changes, guest_pthread_key, #3753's guest_pthread_destructor_lifetime and guest_force_tls_destructor pass, and all 292 guest_* tests pass (Ubuntu 24.04, GCC 13.3). @adriadam10, it's ready whenever you get to it. Thanks!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114494173 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3623#issuecomment-6114494173). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-11T22:17:40Z. --- Rebased onto `main` 43bc17f7 now that #3753 is in, since it also changed `Tsd.cpp`. With both changes, `guest_pthread_key`, #3753's `guest_pthread_destructor_lifetime` and `guest_force_tls_destructor` pass, and all 292 `guest_*` tests pass (Ubuntu 24.04, GCC 13.3). @adriadam10, it's ready whenever you get to it. Thanks!
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!3623
No description provided.