diff --git a/src/coreclr/gc/env/gcenv.structs.h b/src/coreclr/gc/env/gcenv.structs.h index 9f287ec7bf8c2..e41ba3cfa67c1 100644 --- a/src/coreclr/gc/env/gcenv.structs.h +++ b/src/coreclr/gc/env/gcenv.structs.h @@ -15,8 +15,15 @@ struct GCSystemInfo typedef void * HANDLE; +// Across all platforms, NativeAOT Thread::GetPalThreadIdForLogging assumes that the thread +// ID is stored in the first 8 bytes of this structure. + #ifdef TARGET_UNIX +#ifdef FEATURE_NATIVEAOT +static_assert(sizeof(pthread_t) == sizeof(uint64_t), "EEThreadId layout mismatch"); +#endif + class EEThreadId { pthread_t m_id; diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index d87900ddd79d7..33f58901d4a7d 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -399,9 +399,11 @@ ep_rt_thread_id_t ep_rt_aot_current_thread_get_id (void) { STATIC_CONTRACT_NOTHROW; - #ifdef TARGET_UNIX - return static_cast(PalGetCurrentOSThreadId()); + static __thread uint64_t tid; + if (!tid) + tid = PalGetCurrentOSThreadId(); + return static_cast(tid); #else return static_cast(::GetCurrentThreadId ()); #endif