Avoid using pthread_self() partially

This commit is contained in:
Elad
2026-07-17 22:04:57 +03:00
parent 6f0e78b6b5
commit 4e49c5319d
2 changed files with 30 additions and 11 deletions
+20 -9
View File
@@ -3560,15 +3560,26 @@ std::pair<void*, usz> thread_ctrl::get_thread_stack()
u64 thread_ctrl::get_tid()
{
#ifdef _WIN32
return GetCurrentThreadId();
#elif defined(ANDROID)
return static_cast<u64>(pthread_self());
#elif defined(__linux__)
return syscall(SYS_gettid);
#else
return reinterpret_cast<u64>(pthread_self());
#endif
static thread_local u64 s_tls_tid = []() -> u64
{
#ifdef _WIN32
return GetCurrentThreadId();
#elif defined(ANDROID)
return pthread_gettid_np(pthread_self());
#elif defined(__linux__)
return syscall(SYS_gettid);
#elif defined(__APPLE__)
u64 tid{};
pthread_threadid_np(nullptr, &tid);
return tid;
#elif defined(__FreeBSD__)
return pthread_getthreadid_np();
#else
return static_cast<u64>(pthread_self());
#endif
}();
return s_tls_tid;
}
bool thread_ctrl::is_main()
+10 -2
View File
@@ -186,9 +186,17 @@ namespace
#ifdef _WIN32
tid = GetCurrentThreadId();
#elif defined(ANDROID)
tid = pthread_self();
tid = pthread_gettid_np(pthread_self());
#elif defined(__linux__)
tid = syscall(SYS_gettid);
#elif defined(__APPLE__)
u64 tid_temp{};
pthread_threadid_np(nullptr, &tid_temp);
tid = tid_temp; // Use a temporary for extra safety
#elif defined(__FreeBSD__)
tid = pthread_getthreadid_np();
#else
tid = reinterpret_cast<u64>(pthread_self());
tid = pthread_self();
#endif
#ifdef USE_STD