diff --git a/llvm/lib/Support/rpmalloc/rpmalloc.c b/llvm/lib/Support/rpmalloc/rpmalloc.c index 6f8b29e31e8ca..fec33f33abae2 100644 --- a/llvm/lib/Support/rpmalloc/rpmalloc.c +++ b/llvm/lib/Support/rpmalloc/rpmalloc.c @@ -969,6 +969,47 @@ static void _rpmalloc_spin(void) { } #if defined(_WIN32) && (!defined(BUILD_DYNAMIC_LINK) || !BUILD_DYNAMIC_LINK) + +static void NTAPI RPMallocTlsOnThreadExit(PVOID module, DWORD reason, + PVOID reserved) { + switch (reason) { + case DLL_PROCESS_ATTACH: + break; + case DLL_PROCESS_DETACH: + rpmalloc_finalize(); + break; + case DLL_THREAD_ATTACH: + break; + case DLL_THREAD_DETACH: + rpmalloc_thread_finalize(1); + break; + } +} + +#ifdef _WIN64 +#pragma comment(linker, "/INCLUDE:_tls_used") +#pragma comment(linker, "/INCLUDE:rpmalloc_tls_thread_exit_callback") + +#pragma const_seg(".CRT$XLY") + +extern const PIMAGE_TLS_CALLBACK rpmalloc_tls_thread_exit_callback; +const PIMAGE_TLS_CALLBACK rpmalloc_tls_thread_exit_callback = + RPMallocTlsOnThreadExit; + +// Reset const section +#pragma const_seg() +#else // _WIN64 +#pragma comment(linker, "/INCLUDE:__tls_used") +#pragma comment(linker, "/INCLUDE:_rpmalloc_tls_thread_exit_callback") + +#pragma data_seg(".CRT$XLY") + +PIMAGE_TLS_CALLBACK rpmalloc_tls_thread_exit_callback = RPMallocTlsOnThreadExit; + +// Reset data section +#pragma data_seg() +#endif // _WIN64 + static void NTAPI _rpmalloc_thread_destructor(void *value) { #if ENABLE_OVERRIDE // If this is called on main thread it means rpmalloc_finalize