diff --git a/src/native/minipal/CMakeLists.txt b/src/native/minipal/CMakeLists.txt index 32c9497f273c13..bd219f66c97e2f 100644 --- a/src/native/minipal/CMakeLists.txt +++ b/src/native/minipal/CMakeLists.txt @@ -29,7 +29,10 @@ if(NOT WIN32 AND NOT HOST_WASM AND NOT (CLR_CMAKE_TARGET_ARCH_WASM AND NOT CLR_C endif() if(CLR_CMAKE_HOST_UNIX) - list(APPEND SOURCES cpucount.c) + list(APPEND SOURCES + cpucount.c + thread.c + ) endif() # Provide an object library for scenarios where we ship static libraries diff --git a/src/native/minipal/thread.c b/src/native/minipal/thread.c new file mode 100644 index 00000000000000..8e86796289d46c --- /dev/null +++ b/src/native/minipal/thread.c @@ -0,0 +1,9 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include +#include + +#if !defined(__wasm) || defined(_REENTRANT) +PLATFORM_THREAD_LOCAL size_t minipal_cached_thread_id; +#endif diff --git a/src/native/minipal/thread.h b/src/native/minipal/thread.h index bc1e8c39144501..1a22d365784d7a 100644 --- a/src/native/minipal/thread.h +++ b/src/native/minipal/thread.h @@ -10,6 +10,7 @@ #include #include #include +#include #if defined(__linux__) #include @@ -79,6 +80,10 @@ static inline size_t minipal_get_current_thread_id_no_cache(void) return tid; } +#if !defined(__wasm) || defined(_REENTRANT) +extern PLATFORM_THREAD_LOCAL size_t minipal_cached_thread_id; +#endif + /** * Get the current thread ID. * @@ -90,20 +95,12 @@ static inline size_t minipal_get_current_thread_id(void) return minipal_get_current_thread_id_no_cache(); #else // !__wasm || _REENTRANT -#if defined(__GNUC__) && !defined(__clang__) && defined(__cplusplus) - // gcc doesn't like _Thread_local when __cplusplus is defined. - // although thread_local is C2x, which other compilers don't allow with C11. - static thread_local size_t tid = 0; -#else - static _Thread_local size_t tid = 0; -#endif - - if (!tid) + if (!minipal_cached_thread_id) { - tid = minipal_get_current_thread_id_no_cache(); + minipal_cached_thread_id = minipal_get_current_thread_id_no_cache(); } - return tid; + return minipal_cached_thread_id; #endif // __wasm && !_REENTRANT }