Add customizable per-platform affinity for content and pool threads

This commit is contained in:
Wojtek Figat
2025-07-29 10:41:41 +02:00
parent 17c0892ff1
commit 99323c1d2f
4 changed files with 27 additions and 10 deletions

View File

@@ -28,6 +28,9 @@ namespace ThreadPoolImpl
ConcurrentTaskQueue<ThreadPoolTask> Jobs; // Hello Steve!
ConditionVariable JobsSignal;
CriticalSection JobsMutex;
#ifdef THREAD_POOL_AFFINITY_MASK
volatile int64 ThreadIndex = 0;
#endif
}
String ThreadPoolTask::ToString() const
@@ -63,11 +66,12 @@ bool ThreadPoolService::Init()
PROFILE_MEM(EngineThreading);
// Spawn threads
const int32 numThreads = Math::Clamp<int32>(Platform::GetCPUInfo().ProcessorCoreCount - 1, 2, PLATFORM_THREADS_LIMIT / 2);
LOG(Info, "Spawning {0} Thread Pool workers", numThreads);
for (int32 i = ThreadPoolImpl::Threads.Count(); i < numThreads; i++)
const CPUInfo cpuInfo = Platform::GetCPUInfo();
const int32 count = Math::Clamp<int32>(cpuInfo.ProcessorCoreCount - 1, 2, PLATFORM_THREADS_LIMIT / 2);
LOG(Info, "Spawning {0} Thread Pool workers", count);
ThreadPoolImpl::Threads.Resize(count);
for (int32 i = 0; i < count; i++)
{
// Create tread
auto runnable = New<SimpleRunnable>(true);
runnable->OnWork.Bind(ThreadPool::ThreadProc);
auto thread = Thread::Create(runnable, String::Format(TEXT("Thread Pool {0}"), i));
@@ -76,9 +80,7 @@ bool ThreadPoolService::Init()
LOG(Error, "Failed to spawn {0} thread in the Thread Pool", i + 1);
return true;
}
// Add to the list
ThreadPoolImpl::Threads.Add(thread);
ThreadPoolImpl::Threads[i] = thread;
}
return false;
@@ -110,6 +112,10 @@ void ThreadPoolService::Dispose()
int32 ThreadPool::ThreadProc()
{
#ifdef THREAD_POOL_AFFINITY_MASK
const int64 index = Platform::InterlockedIncrement(&ThreadPoolImpl::ThreadIndex) - 1;
Platform::SetThreadAffinityMask(THREAD_POOL_AFFINITY_MASK((int32)index));
#endif
ThreadPoolTask* task;
// Work until end