upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/tls_host.cpp
diff options
context:
space:
mode:
authorromainveurrier <romain.veurrier@gmail.com>2026-11-22 13:38:24 +0100
committerromainveurrier <romain.veurrier@gmail.com>2026-11-22 13:38:24 +0100
commit83f13ed569af4394786b108b490f61659352c2a5 (patch)
treea009d3b37e23ebd6e9e6faf34e23e0a88f794d24 /runtime/src/tls_host.cpp
parentc2215fc63539747cd8a771eb4183a88149867b27 (diff)
parent41f448261e1de8a410b4d760e69a7e5656cef858 (diff)
Merge remote-tracking branch 'origin/main' into rt/xdxr-gpu-builder-merge
# Conflicts: # runtime/CMakeLists.txt
Diffstat (limited to 'runtime/src/tls_host.cpp')
-rw-r--r--runtime/src/tls_host.cpp28
1 files changed, 27 insertions, 1 deletions
diff --git a/runtime/src/tls_host.cpp b/runtime/src/tls_host.cpp
index 54322be4f..ab234cbd9 100644
--- a/runtime/src/tls_host.cpp
+++ b/runtime/src/tls_host.cpp
@@ -4,12 +4,38 @@
4// every thread's TEB. Loader::initialize_tls reuses that reserved index for the manually mapped image, so 4// every thread's TEB. Loader::initialize_tls reuses that reserved index for the manually mapped image, so
5// __declspec(thread) access in mapped code reads a valid TEB slot. The anchor itself is never used; the loader saves 5// __declspec(thread) access in mapped code reads a valid TEB slot. The anchor itself is never used; the loader saves
6// and restores the helper's own block, so the helper's CRT TLS state is not disturbed. 6// and restores the helper's own block, so the helper's CRT TLS state is not disturbed.
7//
8// The Windows loader also calls this DLL's DllMain with DLL_THREAD_ATTACH on every new thread (after it has built that
9// thread's TLS array) and with DLL_THREAD_DETACH when a thread exits (before it frees the array). The loader registers
10// a callback here so it can give each new thread its own copy of the mapped image's TLS, and put this DLL's block back
11// before the thread exits. DllMain itself must not touch TLS: this DLL's slot is the one being swapped.
7#include <windows.h> 12#include <windows.h>
8 13
9__declspec(thread) unsigned char g_tls_host_anchor = 0; 14__declspec(thread) unsigned char g_tls_host_anchor = 0;
10 15
16namespace {
17
18using ThreadEventFn = void(__cdecl*)(DWORD reason);
19
20// A plain global, not TLS (see above). Written once by the loader, read on every thread event.
21void* volatile g_thread_event = nullptr;
22
23} // namespace
24
11// Never called. The export keeps the TLS variable (and therefore the image's TLS directory) from being dropped by 25// Never called. The export keeps the TLS variable (and therefore the image's TLS directory) from being dropped by
12// the linker without making DllMain touch TLS during thread attach/detach. 26// the linker without making DllMain touch TLS during thread attach/detach.
13extern "C" __declspec(dllexport) void gta6rt_tls_host_keep() { g_tls_host_anchor = 1; } 27extern "C" __declspec(dllexport) void gta6rt_tls_host_keep() { g_tls_host_anchor = 1; }
14 28
15BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID) { return TRUE; } 29// Called by the loader. `fn` runs inside DllMain (under the Windows loader lock) for DLL_THREAD_ATTACH and
30// DLL_THREAD_DETACH; nullptr stops the calls.
31extern "C" __declspec(dllexport) void gta6rt_tls_host_set_thread_event(ThreadEventFn fn) {
32 InterlockedExchangePointer(&g_thread_event, reinterpret_cast<void*>(fn));
33}
34
35BOOL WINAPI DllMain(HINSTANCE, DWORD reason, LPVOID) {
36 if (reason == DLL_THREAD_ATTACH || reason == DLL_THREAD_DETACH) {
37 void* fn = InterlockedCompareExchangePointer(&g_thread_event, nullptr, nullptr);
38 if (fn) reinterpret_cast<ThreadEventFn>(fn)(reason);
39 }
40 return TRUE;
41}