fix(libkernel): allocate the Windows thread-local slot on first use #6956
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6956"
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 jradic on 2026-10-16T21:10:10Z.
What
Setting
APS5_TRACE_TIMERS=1crashed every title at startup on Windows withCannot bind native thread state.The timer self-test in
Time.cppruns during libkernel's static init.WindowsThreadLocal<T>::slotis a template static, so its init order isn't guaranteed, and here it hadn't run yet. The slot was still 0, soFlsSetValuefailed.The slot is now a function-local static, so it gets allocated on first use regardless of init order. Also added
guest_nanosleep_timer_self_test, which rerunsguest_nanosleep_testswithAPS5_TRACE_TIMERS=1(Windows only). #6658 adds a test right after the sameguest_nanosleepblock; no functional overlap.Tested
Windows 11, WinLibs GCC 15.2.0, Release.
alignasbuild error fixed in #6452/#6829, and a symlink-permission failure inselect_testson my machine.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_01KwsvKtVsZDLefysUM9m2QS
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T21:27:28Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by jradic on 2026-10-16T21:47:10Z.
Checked these by hand: none change the same lines. #6658 adds a test right after the same
guest_nanosleepblock, so whichever lands second needs a trivial rebase.Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-17T03:52:27Z.
Review: COMMENTED
Windows check of head
75d29f5b3merged ontomaine3a8e4430(clean merge). Windows 11 Pro (26300), Ryzen 5 3600, WinLibs GCC 15.2.0 r7, Ninja Release.guest_nanosleepand the newguest_nanosleep_timer_self_testpass,--repeat until-fail:3(about 7 s each).main'sWindowsThreadLocal.hppand the new test kept,guest_nanosleep_timer_self_testfails withwhat(): Cannot bind native thread state(0xc0000409), the same error as in the description.Code: a function-local static is the right fix. Dynamic initialization of a class template's
inline staticmember is unordered, so any static initializer in libkernel that reachesGet()can see a zero slot.libc'sHostThreadLocal.hppalready uses the same pattern (static const DWORD slot = FlsAlloc(...)inside the function), so the two thread-local helpers now match. The otherWindowsThreadLocalusers (TimedWait.cpp, twice,Tsd.cpp,Thread.cpp) only callGet(), so they all benefit and none depends on the old member.DestroycallingslotIndex()is safe: the slot is necessarily initialized by the time FLS calls it. I have no requests.AI-assisted: yes. I ran the build, the tests and the negative control above.
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T10:05:48Z.
Cross-validation on Windows 11 Pro 26220, NVIDIA RTX 4070 (596.21), MinGW GCC 15.2, PR head
75d29f5b3, built together with #7005 (disjoint files) on main5f61879a9(clean merge). Before-runs used a build of3b10c7ef0; the libkernel Pthread/Time sources are identical between the two.libs: pass.mainbuild:APS5_TRACE_TIMERS=1withguest_nanosleepfails 3 of 3, exit0xc0000409,terminate called after throwing an instance of 'std::runtime_error'/what(): Cannot bind native thread state, after the first[timers]line; without the variable it passes. No source-revert rebuild was done, themainbuild served as the control.guest_nanosleep_timer_self_test,--repeat until-fail:3: 3 of 3 pass on an idle host (7.2 s each). On a busy host (other builds running) it took 21 s and 25 s and then hit its own 30 s limit twice, so the limit is tight under load; a largerTIMEOUTwould avoid that.guest_nanosleep3 of 3.APS5_TRACE_TIMERS=1, relinked, 60 s limit:mainaborts at 12.0 s with exit0xC0000409(terminate called after throwing an instance of 'std::runtime_error',what(): Cannot bind native thread state,FATAL: abort() (SIGABRT)) after one[timers]line. With this PR it runs until the kill (3323 frames, no stop line) and prints all 7[timers]lines.mainaborts at 10.7 s with the same message and exit code. With this PR it runs until the kill (2 frames presented, as in our earlier runs without the variable) and prints all 7[timers]lines.ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip': 855 of 877 pass, 2 skipped, 20 timeouts on a host with other builds running; all 20 pass on serial reruns (conventions/tooling tests and four libkernel/relinker tests, 0.6 to 55 s each). The new test passed in the suite.tools/check_conventions.py --base origin/mainandgit diff --check: pass.Not checked: Linux/macOS (the change is Windows-only), a revert-and-rebuild of the fix, the other open PRs that touch
libkernel/CMakeLists.txt.Written with Claude Code assistance (validation by a subagent, reviewed in this session).