Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions rts/System/Platform/ThreadAffinityGuard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "System/Log/ILog.h"
#ifdef _WIN32
#include <windows.h>
#elif defined(__APPLE__)
// macOS has no portable per-thread CPU affinity API; nothing to include.
#else
#include <sched.h>
#include <unistd.h>
Expand All @@ -18,6 +20,8 @@ ThreadAffinityGuard::ThreadAffinityGuard() : affinitySaved(false) {
if (!affinitySaved) {
LOG_L(L_WARNING, "GetThreadAffinityMask failed with error code: %lu", GetLastError());
}
#elif defined(__APPLE__)
// no portable affinity API on macOS; leave affinitySaved == false
#else
tid = syscall(SYS_gettid); // Get thread ID
CPU_ZERO(&savedAffinity);
Expand All @@ -36,6 +40,8 @@ ThreadAffinityGuard::~ThreadAffinityGuard() {
if (!SetThreadAffinityMask(threadHandle, savedAffinity)) {
LOG_L(L_WARNING, "SetThreadAffinityMask failed with error code: %lu", GetLastError());
}
#elif defined(__APPLE__)
// unreachable on macOS: affinitySaved is never set
#else
if (sched_setaffinity(tid, sizeof(cpu_set_t), &savedAffinity) != 0) {
LOG_L(L_WARNING, "Failed to restore thread affinity.");
Expand Down
4 changes: 4 additions & 0 deletions rts/System/Platform/ThreadAffinityGuard.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#ifdef _WIN32
#include <windows.h>
#elif defined(__APPLE__)
// macOS has no portable per-thread CPU affinity API; the guard is a no-op.
#else
#include <sched.h>
#endif
Expand All @@ -12,6 +14,8 @@ class ThreadAffinityGuard {
#ifdef _WIN32
DWORD_PTR savedAffinity;
HANDLE threadHandle;
#elif defined(__APPLE__)
// no affinity state to track on macOS
#else
cpu_set_t savedAffinity;
pid_t tid;
Expand Down
15 changes: 13 additions & 2 deletions rts/System/Platform/Threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include <memory>
#include <numeric>
#include <cinttypes>
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#if defined(__APPLE__)
#include <pthread/qos.h>
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
#elif defined(_WIN32)
#include <windows.h>
#include "System/Platform/Win/DllLib.h"
Expand Down Expand Up @@ -277,7 +279,16 @@ namespace Threading {
if (coreMask == 0)
return (~0);

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#if defined(__APPLE__)
// macOS has no per-thread CPU affinity API. The closest equivalent is a
// QoS hint: QOS_CLASS_USER_INTERACTIVE biases the scheduler toward the
// performance cores (and away from the efficiency cores). We can't honor
// the specific coreMask, so apply the hint and report the affinity as
// "not set" (~0) -- final core placement is left to the scheduler.
pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0);
return (~0);

#elif defined(__FreeBSD__) || defined(__OpenBSD__)
// These platforms don't support thread affinity; return ~0 ("not set")
return (~0);

Expand Down
Loading