rx: add process utility

This commit is contained in:
DH 2025-10-16 11:29:16 +03:00
parent 715a7dfc5e
commit e219bc4504
4 changed files with 87 additions and 1 deletions

View file

@ -8,6 +8,7 @@ add_library(${PROJECT_NAME} OBJECT
src/FileLock.cpp
src/hexdump.cpp
src/mem.cpp
src/Process.cpp
src/SharedAtomic.cpp
src/SharedCV.cpp
src/SharedMutex.cpp
@ -22,6 +23,10 @@ PRIVATE
include/${PROJECT_NAME}
)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_libraries(${PROJECT_NAME} PUBLIC synchronization kernel32 onecore)
endif()
target_link_libraries(${PROJECT_NAME} PUBLIC fmt::fmt)
if (Git_FOUND)

30
rx/include/rx/Process.hpp Normal file
View file

@ -0,0 +1,30 @@
#pragma once
#include <cstdint>
namespace rx {
using ProcessId = std::uint32_t;
#ifdef __APPLE__
using ThreadId = std::uint64_t;
#else
using ThreadId = ProcessId;
#endif
#ifdef _WIN32
using ProcessHandle = void *;
#else
using ProcessHandle = ProcessId;
#endif
#if defined(_WIN32) || defined(__APPLE__)
using ThreadHandle = void *;
#else
using ThreadHandle = ThreadId;
#endif
ProcessId getCurrentProcessId();
ThreadId getCurrentThreadId();
ProcessHandle getCurrentProcessHandle();
ThreadHandle getCurrentThreadHandle();
} // namespace rx

50
rx/src/Process.cpp Normal file
View file

@ -0,0 +1,50 @@
#include <rx/Process.hpp>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#endif
#ifdef __APPLE__
#include <pthread.h>
#endif
rx::ProcessId rx::getCurrentProcessId() {
#ifdef _WIN32
return ::GetCurrentProcessId();
#else
return ::getpid();
#endif
}
rx::ThreadId rx::getCurrentThreadId() {
#ifdef _WIN32
return ::GetCurrentProcessId();
#elif defined(__APPLE__)
uint64_t tid = 0;
pthread_threadid_np(nullptr, &tid);
return tid;
#else
return ::gettid();
#endif
}
rx::ProcessHandle rx::getCurrentProcessHandle() {
#ifdef _WIN32
return ::GetCurrentProcess();
#else
return getCurrentProcessId();
#endif
}
rx::ThreadHandle rx::getCurrentThreadHandle() {
#ifdef _WIN32
return ::GetCurrentThread();
#elif defined(__APPLE__)
return pthread_self();
#else
return getCurrentThreadId();
#endif
}

View file

@ -1,4 +1,5 @@
#include "debug.hpp"
#include "Process.hpp"
#include "print.hpp"
#include <fstream>
#include <list>
@ -73,7 +74,7 @@ void rx::waitForDebugger() {
return;
}
rx::println(stderr, "waiting for debugger, pid {}", ::getpid());
rx::println(stderr, "waiting for debugger, pid {}", getCurrentProcessId());
while (!isDebuggerPresent()) {
std::this_thread::sleep_for(std::chrono::seconds(1));