Skip to content

Commit

Permalink
Use a more performant way to get the current thread id (dotnet#95609)
Browse files Browse the repository at this point in the history
* Fix thread_id perf issue

* add an assert to ensure the current thread is not null

* FB

* Apply suggestions from code review

Co-authored-by: Jan Kotas <[email protected]>

---------

Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
LakshanF and jkotas authored Dec 11, 2023
1 parent e7da682 commit 07b2b50
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/coreclr/gc/env/gcenv.structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ep_rt_thread_id_t>(PalGetCurrentOSThreadId());
static __thread uint64_t tid;
if (!tid)
tid = PalGetCurrentOSThreadId();
return static_cast<ep_rt_thread_id_t>(tid);
#else
return static_cast<ep_rt_thread_id_t>(::GetCurrentThreadId ());
#endif
Expand Down

0 comments on commit 07b2b50

Please sign in to comment.