Add Windows and macOS CPU affinity support

- Windows: SetThreadAffinityMask to pin main thread to core 0 and
  exclude workers from core 0
- macOS: thread_policy_set with THREAD_AFFINITY_POLICY tags to hint
  scheduler separation (tag 1 for main, tag 2 for workers)
This commit is contained in:
Kelsi 2026-02-25 03:41:18 -08:00
parent 86505ad377
commit 4c8fa9f1fe
2 changed files with 58 additions and 5 deletions

View file

@ -25,6 +25,15 @@
#ifdef __linux__
#include <sched.h>
#include <pthread.h>
#elif defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#elif defined(__APPLE__)
#include <mach/mach.h>
#include <mach/thread_policy.h>
#include <pthread.h>
#endif
namespace wowee {
@ -922,19 +931,33 @@ bool TerrainManager::advanceFinalization(FinalizingTile& ft) {
void TerrainManager::workerLoop() {
// Keep worker threads off core 0 (reserved for main thread)
#ifdef __linux__
{
int numCores = static_cast<int>(std::thread::hardware_concurrency());
if (numCores >= 2) {
#ifdef __linux__
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
for (int i = 1; i < numCores; i++) {
CPU_SET(i, &cpuset);
}
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
#elif defined(_WIN32)
DWORD_PTR mask = 0;
for (int i = 1; i < numCores && i < 64; i++) {
mask |= (static_cast<DWORD_PTR>(1) << i);
}
SetThreadAffinityMask(GetCurrentThread(), mask);
#elif defined(__APPLE__)
// Use affinity tag 2 for workers (separate from main thread tag 1)
thread_affinity_policy_data_t policy = { 2 };
thread_policy_set(
pthread_mach_thread_np(pthread_self()),
THREAD_AFFINITY_POLICY,
reinterpret_cast<thread_policy_t>(&policy),
THREAD_AFFINITY_POLICY_COUNT);
#endif
}
}
#endif
LOG_INFO("Terrain worker thread started");
while (workerRunning.load()) {