Add getThreadId helper using pthread_{gettid,threadid}_np/gettid

This commit is contained in:
2025-11-18 21:52:23 -07:00
parent 19697170f9
commit 2304c05ed0
5 changed files with 32 additions and 18 deletions

View File

@@ -271,6 +271,13 @@ else()
endif() endif()
install(TARGETS wibo DESTINATION bin) install(TARGETS wibo DESTINATION bin)
# Check for features
include(CheckSymbolExists)
set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
set(CMAKE_REQUIRED_LIBRARIES pthread)
check_symbol_exists(pthread_gettid_np "pthread.h" HAVE_PTHREAD_GETTID_NP)
target_compile_definitions(wibo PRIVATE -DHAVE_PTHREAD_GETTID_NP=${HAVE_PTHREAD_GETTID_NP})
find_package(Python3 COMPONENTS Interpreter REQUIRED) find_package(Python3 COMPONENTS Interpreter REQUIRED)
# Track down libclang for trampoline generation. # Track down libclang for trampoline generation.

View File

@@ -182,7 +182,7 @@ BOOL WINAPI IsProcessorFeaturePresent(DWORD ProcessorFeature) {
HANDLE WINAPI GetCurrentProcess() { HANDLE WINAPI GetCurrentProcess() {
HOST_CONTEXT_GUARD(); HOST_CONTEXT_GUARD();
DEBUG_LOG("GetCurrentProcess() -> %p\n", reinterpret_cast<void *>(static_cast<uintptr_t>(-1))); DEBUG_LOG("GetCurrentProcess() -> %d\n", kPseudoCurrentProcessHandleValue);
return kPseudoCurrentProcessHandleValue; return kPseudoCurrentProcessHandleValue;
} }
@@ -195,12 +195,7 @@ DWORD WINAPI GetCurrentProcessId() {
DWORD WINAPI GetCurrentThreadId() { DWORD WINAPI GetCurrentThreadId() {
HOST_CONTEXT_GUARD(); HOST_CONTEXT_GUARD();
pthread_t thread = pthread_self(); DWORD threadId = wibo::getThreadId();
#ifdef __linux__
const auto threadId = static_cast<DWORD>(thread);
#else
const auto threadId = static_cast<DWORD>(reinterpret_cast<uintptr_t>(thread));
#endif
DEBUG_LOG("GetCurrentThreadId() -> %u\n", threadId); DEBUG_LOG("GetCurrentThreadId() -> %u\n", threadId);
return threadId; return threadId;
} }

View File

@@ -36,12 +36,7 @@ void wibo::debug_log(const char *fmt, ...) {
if (wibo::debugEnabled) { if (wibo::debugEnabled) {
for (size_t i = 0; i < wibo::debugIndent; i++) for (size_t i = 0; i < wibo::debugIndent; i++)
fprintf(stderr, "\t"); fprintf(stderr, "\t");
pthread_t threadId = pthread_self(); fprintf(stderr, "[thread %x] ", getThreadId());
#ifdef __APPLE__
fprintf(stderr, "[thread %p] ", threadId);
#else
fprintf(stderr, "[thread %lu] ", threadId);
#endif
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
fflush(stderr); fflush(stderr);
} }

View File

@@ -55,4 +55,6 @@ int spawnWithArgv(const std::string &applicationName, const std::vector<std::str
Pin<kernel32::ProcessObject> &pinOut); Pin<kernel32::ProcessObject> &pinOut);
std::vector<std::string> splitCommandLine(const char *commandLine); std::vector<std::string> splitCommandLine(const char *commandLine);
DWORD getThreadId();
} // namespace wibo } // namespace wibo

View File

@@ -16,7 +16,11 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <pthread.h>
#ifdef __APPLE__
extern char **environ; extern char **environ;
#endif
using kernel32::ProcessObject; using kernel32::ProcessObject;
@@ -46,9 +50,7 @@ bool ProcessManager::addProcess(Pin<ProcessObject> po) {
return mImpl->addProcess(std::move(po)); return mImpl->addProcess(std::move(po));
} }
bool ProcessManager::running() const { bool ProcessManager::running() const { return mImpl && mImpl->running(); }
return mImpl && mImpl->running();
}
ProcessManager &processes() { ProcessManager &processes() {
static ProcessManager mgr; static ProcessManager mgr;
@@ -350,5 +352,18 @@ std::vector<std::string> splitCommandLine(const char *commandLine) {
return result; return result;
} }
} // namespace wibo DWORD getThreadId() {
#if HAVE_PTHREAD_GETTID_NP
pid_t threadId = pthread_gettid_np(pthread_self());
#elif defined(__linux__)
pid_t threadId = gettid();
#elif defined(__APPLE__)
uint64_t threadId = 0;
pthread_threadid_np(nullptr, &threadId);
#else
#error "Unknown platform"
#endif
return static_cast<DWORD>(threadId);
}
} // namespace wibo