Skip to content

runtime/shims: remove function-local static guards in gameinput, dxgi_com and xmem - #7359

Open
tedin7 wants to merge 4 commits into
ItsNotRexed:mainfrom
tedin7:c2-statics
Open

tedin7 wants to merge 4 commits into
ItsNotRexed:mainfrom
tedin7:c2-statics

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What

Replace nine dynamically initialized function-local statics across GameInput, DXGI and XMem. Use explicit one-time initialization for first-use API calls and GameInput objects. Give the four DXGI and XMem tables constant-initialized null pointers. Allocate each table on first use under its existing mutex and retain it for the life of the process.

Why

MSVC can emit a thread-local initialization guard such as _Init_thread_header for these statics. The mapped executable can own TLS slot 0, so the shims need initialization that does not rely on that guard.

The DXGI and XMem containers construct under those locks, which also removes any dependency on namespace-scope constructor order.

Tested

Compared main 8575d36ed with this branch. This change was AI-assisted.

  • gameinput_test — 341 checks and 0 failures on each revision with local clang-cl 19.1.7 and Wine 10.0, using SDL virtual controllers.
  • xmem_test — 116 checks passed on each revision with /root/fast_test_exe.sh /root/c2-statics xmem_test on p2, clang-cl 19.1.1 and Wine 9.0.
  • dxgi_swapchain_test — 1 passing run per revision on Wine 10.0 with WINEDLLOVERRIDES=d3d12,dxgi=, Mesa lavapipe 25.0.7 (llvmpipe, LLVM 19.1.7), and Vulkan validation off. Host comparisons were skipped in these runs. With Wine host libraries enabled, both revisions reported the same 18 mismatches in 45 swapchain descriptions. With only host D3D12 disabled, both hit the same DXGI driver-version mismatch.

Review follow-up 1a4971cd9 on p2 with clang-cl 19.1.1 and Wine 9.0. Ran /root/fast_test_exe.sh /root/c2-statics xmem_test (173 checks passed), then /root/fast_test_exe.sh /root/c2-statics gameinput_test (341 checks, 0 failures) with LEONIDA_SDL3_FETCH=ON in the GameInput CMake configuration. Removed runtime/build/fast afterward.

@ItsNotRexed: an exe PR, as you asked.

Claim: #6910 (comment)

@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

Code read of 3804304 (not run).

The once_flag/pointer changes (now_us, sdl_hid_open, g(), instance(), wait_for_vblank) are constant-initialised and do avoid the guard. The four containers moved to namespace scope don't get the same property: g_seen_stubs, g_reservations, g_seen_attributes and g_seen_transfers are std::set/std::map, and MSVC's STL allocates the tree's sentinel node in the default constructor (_Tree(...) calls _Alloc_sentinel_and_proxy(), ), so they are dynamically initialised at DLL/EXE start-up. That trades the function-local guard for initialisation-order dependence: a call before this TU's dynamic initialisers have run, which is the early-entry case this PR is about, now touches an unconstructed container instead of a guarded one. Using the same pattern as g()/instance() (a zero-initialised pointer allocated under the function's existing mutex) would keep them constant-initialised.

AI-assisted (Claude Code).

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Envyre, you are right: the containers moved to namespace scope are still dynamically initialised (MSVC's std::set/std::map allocate their sentinel in the constructor), so this trades the function-local guard for an init-order dependence in exactly the early-entry case. #7398 (/Zc:threadSafeInit-) removes the guards for the whole build without that trade, so I am closing this one rather than patching it with lazily allocated pointers. The merge with main stays on the branch if anyone wants the constant-initialised parts.

@tedin7 tedin7 closed this Nov 23, 2026
@tedin7 tedin7 reopened this Nov 23, 2026
@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Reopened and fixed in 1a4971c: the four containers now live behind constant-initialised null pointers, allocated on first use under the mutex each one already had (the pattern g()/instance() use), so there is neither a guard nor a dynamic initialiser left. xmem_test 173 checks and gameinput_test 341 checks pass. Thanks @Envyre.

@nullmarsh

Copy link
Copy Markdown

Tested 1a4971cd natively on Windows 11 26200, MSVC 14.44 RelWithDebInfo (thread-safe statics on), RX 9060 XT: pass. gameinput_host, xmem, active_input, dxgi_device, dxgi_swapchain and dxgi_swapchain_count: 6/6. gameinput is disabled in this build (no SDL3), so the GameInput SDL path isn't exercised.

AI-assisted (Claude Code), no game files.

@DeSunShine

Copy link
Copy Markdown
Contributor

Tested 1a4971cd9 merged into main dde82f27e on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass.

  • ctest -R "dxgi|xmem|gameinput|tiny_game" --output-on-failure: 100% passed out of 15 (the dxgi*, xmem and run_tiny_game* tests); gameinput is "Not Run (Disabled)" because this build has no SDL3, so the gameinput.cpp change was only compiled, not exercised.

AI-assisted (Claude Code).

@username634

Copy link
Copy Markdown

Tested 1a4971cd9 and its merge-base 37f215107 side by side, natively on Windows 11 Pro 26300, MSVC 14.44 (VS 2022 Build Tools) RelWithDebInfo with thread-safe statics on, Ninja, with -DLEONIDA_SDL3_FETCH=ON, so gameinput_test is built and run. Both checks above had it disabled. Results are identical on both revisions:

test base 37f215107 this PR
gameinput_test (SDL3 path) 341 checks, 0 failures 341 checks, 0 failures
xmem_test OK (173 checks) OK (173 checks)
dxgi_swapchain_test, RTX 5060 Ti (621.01) PASS, 45/45 descriptions match the host runtime same
dxgi_swapchain_test, Intel UHD 730 (101.7092) PASS, 45/45 descriptions match the host runtime same

Guards that are still emitted. I counted _Init_thread_header references in the objects with dumpbin /symbols, then checked which functions call it with dumpbin /disasm:

  • gameinput.cpp.obj (gta6rt, gameinput_test, gameinput_probe): 1 → 0
  • dxgi_com.cpp.obj (gta6rt_d3d12_com): 1 → 0
  • xmem.cpp.obj (gta6rt and every test that compiles it): 1 → still 1. Three functions in the PR's object still take the guard:
    • note_physical_attributes (runtime/shims/xmem.cpp:477): its static std::set<uint64_t> seen is the same pattern this PR fixed for g_seen_attributes / g_seen_transfers, but this third set was left as a function-local static.
    • placeholder_api() (xmem.cpp:189): static const PlaceholderApi api = [] { ... }() is dynamically initialised.
    • rt::startup_trace::detail::mutex() (runtime/shims/startup_trace.h:43): static auto* m = new std::mutex; is an inline function in a header, so every shim that includes it gets the guard too.

So with thread-safe statics on, xmem.cpp still depends on the TLS guard the PR description wants to avoid. placeholder_api() is reached from the XMem allocation path, not only the trace path. (#7398's /Zc:threadSafeInit- would cover all three.)

Synthetic tests only, no game files. AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants