Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/libs/shared/shared/threading/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@
#include <string>
#include <cstring>

#if defined __APPLE__
#include <TargetConditionals.h>
#endif

#ifdef _WIN32
#include <Windows.h>
#include <cwchar>
#elif defined TARGET_OS_MAC
#include <pthread.h>
#include <mach/mach.h>
#include <mach/thread_policy.h>
#include <signal.h>
#include <atomic>
#elif defined __linux__ || defined __unix__
#include <sched.h>
#include <pthread.h>
Expand Down Expand Up @@ -92,11 +100,36 @@ Result set_name([[maybe_unused]] auto& handle, const char* name) {
FreeLibrary(lib);

#elif defined TARGET_OS_MAC
auto ret = pthread_setname_np(name);
static std::atomic<const char*> desiredThreadName{name};
static std::atomic<kern_return_t> ret{KERN_FAILURE};
static std::atomic<bool> busy{true};

// Define the signal handler
const auto threadNameHandler = +[](int /*signum*/) -> void {
const char* name = desiredThreadName.load(std::memory_order_acquire);
if (name != nullptr) {
ret.store(pthread_setname_np(name), std::memory_order_release);
}
busy.store(false, std::memory_order_release);
};

if(ret) {
throw std::runtime_error("Unable to set thread name, error code" + std::to_string(ret));
// Install the signal handler once for the process.
static std::atomic_flag handlerInstalled = ATOMIC_FLAG_INIT;
if (!handlerInstalled.test_and_set(std::memory_order_acquire)) {
signal(SIGUSR1, threadNameHandler);
}

// Send SIGUSR1 to the target thread so it runs the signal handler.
if (pthread_kill(handle, SIGUSR1) != 0) {
throw std::runtime_error("Unable to send signal handler to thread, error code " + std::to_string(ret));
}

while (busy == true) {std::this_thread::yield();}

if (ret.load(std::memory_order_acquire) != KERN_SUCCESS) {
throw std::runtime_error("Unable to set thread name, error code " + std::to_string(ret));
}

#elif defined __linux__ || defined __unix__
auto ret = pthread_setname_np(handle, name);

Expand All @@ -109,23 +142,13 @@ Result set_name([[maybe_unused]] auto& handle, const char* name) {
}

Result set_name(std::jthread& thread, const char* name) {
#ifndef TARGET_OS_MAC
const auto handle = thread.native_handle();
return set_name(handle, name);
#else
#pragma message WARN("Setting thread names is not implemented for this platform. Implement it, please!")
return Result::unsupported;
#endif
}

Result set_name(std::thread& thread, const char* name) {
#ifndef TARGET_OS_MAC
const auto handle = thread.native_handle();
return set_name(handle, name);
#else
#pragma message WARN("Setting thread names is not implemented for this platform. Implement it, please!")
return Result::unsupported;
#endif
}

Result set_name(const char* name) {
Expand Down Expand Up @@ -228,5 +251,4 @@ unsigned int hardware_concurrency() {
return hardware_concurrency(nullptr);
}


} // thread, ember