fix(libkernel): number pthread keys from 1 as FreeBSD libthr does #3623
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3623"
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 milescoviello on 2026-10-11T14:21:45Z.
What
Fixes #3118.
scePthreadKeyCreateandpthread_key_createhanded 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 inpthread_key_delete,pthread_getspecificandpthread_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.cppnow returns slot + 1 and maps a key back throughKeySlot()(unsignedkey - 1, as libthr does). 0 and out-of-range keys are still rejected withEINVALby delete and set, and read asNULL.Tested
main43bc17f7after #3753, which also changedTsd.cpp, and rechecked on Ubuntu 24.04, GCC 13.3 (CI's compiler):guest_pthread_key,guest_pthread_destructor_lifetime(from #3753) andguest_force_tls_destructorpass,guest_pthread_keyfails with thatmain'sTsd.cpp, and all 292guest_*tests pass.maina11c8f7f.guest_pthread_keygains checks: the firstpthread_key_createkey is non-zero, key 0 isEINVALfor set and delete and reads asNULL, and the firstscePthreadKeyCreatekey is non-zero. They fail withmain'sTsd.cppand pass with this change.guest_pthread_key,guest_pthread_cancel,guest_thread_atexit,guest_force_tls_destructorandhost_thread_local: 5/5.cteston an RTX 5090 Laptop GPU: 834/835, withguest_video_out_*left out because the displays are off. The one failure,agc_driver_emulated_color_compare, fails the same way on plainmaina11c8f7f.Tsd.cppreverted, full suite matchingmain).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 repository (attach them to this PR)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.tools/check_conventions.py --base origin/mainpasses.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.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-11T22:17:40Z.
Rebased onto
main43bc17f7now that #3753 is in, since it also changedTsd.cpp. With both changes,guest_pthread_key, #3753'sguest_pthread_destructor_lifetimeandguest_force_tls_destructorpass, and all 292guest_*tests pass (Ubuntu 24.04, GCC 13.3). @adriadam10, it's ready whenever you get to it. Thanks!