Use gettid() on Linux, don't use std:🧵:id

pthread_self() returns a large opaque pointer which is harder to use.
This commit is contained in:
Nekotekina 2022-07-09 14:42:03 +03:00 committed by Ivan
parent 0c6df39a45
commit 786510a937
5 changed files with 12 additions and 20 deletions

View file

@ -8,7 +8,6 @@
#include "Thread.h" #include "Thread.h"
#include "Utilities/JIT.h" #include "Utilities/JIT.h"
#include <thread> #include <thread>
#include <sstream>
#include <cfenv> #include <cfenv>
#ifdef _WIN32 #ifdef _WIN32
@ -17,6 +16,7 @@
#include <process.h> #include <process.h>
#include <sysinfoapi.h> #include <sysinfoapi.h>
#else #else
#define _GNU_SOURCE
#ifdef __APPLE__ #ifdef __APPLE__
#define _XOPEN_SOURCE #define _XOPEN_SOURCE
#define __USE_GNU #define __USE_GNU
@ -96,14 +96,6 @@ extern thread_local std::string(*g_tls_log_prefix)();
// Report error and call std::abort(), defined in main.cpp // Report error and call std::abort(), defined in main.cpp
[[noreturn]] void report_fatal_error(std::string_view); [[noreturn]] void report_fatal_error(std::string_view);
template <>
void fmt_class_string<std::thread::id>::format(std::string& out, u64 arg)
{
std::ostringstream ss;
ss << get_object(arg);
out += ss.str();
}
std::string dump_useful_thread_info() std::string dump_useful_thread_info()
{ {
thread_local volatile bool guard = false; thread_local volatile bool guard = false;
@ -1644,7 +1636,7 @@ static void append_thread_name(std::string& msg)
} }
else else
{ {
fmt::append(msg, "Thread id = %s.\n", std::this_thread::get_id()); fmt::append(msg, "Thread id = %u.\n", thread_ctrl::get_tid());
} }
} }
@ -3139,6 +3131,8 @@ u64 thread_ctrl::get_tid()
{ {
#ifdef _WIN32 #ifdef _WIN32
return GetCurrentThreadId(); return GetCurrentThreadId();
#elif defined(__linux__)
return syscall(SYS_gettid);
#else #else
return reinterpret_cast<u64>(pthread_self()); return reinterpret_cast<u64>(pthread_self());
#endif #endif

View file

@ -101,7 +101,7 @@ LOG_CHANNEL(q_debug, "QDEBUG");
buf = std::string(_text); buf = std::string(_text);
// Always print thread id // Always print thread id
fmt::append(buf, "\n\nThread id = %s.", std::this_thread::get_id()); fmt::append(buf, "\n\nThread id = %u.", thread_ctrl::get_tid());
} }
if (!g_tls_serialize_name.empty()) if (!g_tls_serialize_name.empty())

View file

@ -33,7 +33,7 @@ static std::string default_string()
return {}; return {};
} }
return fmt::format("TID: %s", std::this_thread::get_id()); return fmt::format("TID: %u", thread_ctrl::get_tid());
} }
// Thread-specific log prefix provider // Thread-specific log prefix provider

View file

@ -652,12 +652,8 @@ u32 utils::get_cpu_model()
namespace utils namespace utils
{ {
extern const u64 main_tid = []() -> u64 u64 _get_main_tid()
{ {
#ifdef _WIN32 return thread_ctrl::get_tid();
return GetCurrentThreadId(); }
#else
return reinterpret_cast<u64>(pthread_self());
#endif
}();
} }

View file

@ -66,7 +66,9 @@ namespace utils
// A threshold of 0xFFFFFFFF means that the rep movsb is expected to be slow on this platform // A threshold of 0xFFFFFFFF means that the rep movsb is expected to be slow on this platform
u32 get_rep_movsb_threshold(); u32 get_rep_movsb_threshold();
extern const u64 main_tid; u64 _get_main_tid();
inline const u64 main_tid = _get_main_tid();
#ifdef LLVM_AVAILABLE #ifdef LLVM_AVAILABLE