Add SetThreadAffinityMask and SetThreadPriority and thread name for Apple platforms

This commit is contained in:
Wojtek Figat
2023-11-04 14:08:53 +01:00
parent 236e5772ce
commit 50bcbf980e
3 changed files with 25 additions and 6 deletions

View File

@@ -19,6 +19,7 @@
#include "Engine/Platform/StringUtils.h"
#include "Engine/Platform/WindowsManager.h"
#include "Engine/Platform/Clipboard.h"
#include "Engine/Platform/Thread.h"
#include "Engine/Platform/IGuiData.h"
#include "Engine/Platform/Base/PlatformUtils.h"
#include "Engine/Utilities/StringConverter.h"
@@ -176,12 +177,19 @@ uint64 ApplePlatform::GetCurrentThreadID()
void ApplePlatform::SetThreadPriority(ThreadPriority priority)
{
// TODO: impl this
struct sched_param sched;
Platform::MemoryClear(&sched, sizeof(struct sched_param));
int32 policy = SCHED_RR;
pthread_getschedparam(pthread_self(), &policy, &sched);
sched.sched_priority = AppleThread::GetAppleThreadPriority(priority);
pthread_setschedparam(pthread_self(), policy, &sched);
}
void ApplePlatform::SetThreadAffinityMask(uint64 affinityMask)
{
// TODO: impl this
thread_affinity_policy policy;
policy.affinity_tag = affinityMask;
thread_policy_set(pthread_mach_thread_np(pthread_self()), THREAD_AFFINITY_POLICY, (integer_t*)&policy, THREAD_AFFINITY_POLICY_COUNT);
}
void ApplePlatform::Sleep(int32 milliseconds)

View File

@@ -40,10 +40,7 @@ public:
return (AppleThread*)Setup(New<AppleThread>(runnable, name, priority), stackSize);
}
protected:
// [UnixThread]
int32 GetThreadPriority(ThreadPriority priority) override
static int32 GetAppleThreadPriority(ThreadPriority priority)
{
switch (priority)
{
@@ -60,6 +57,14 @@ protected:
}
return 31;
}
protected:
// [UnixThread]
int32 GetThreadPriority(ThreadPriority priority) override
{
return GetAppleThreadPriority(priority);
}
int32 Start(pthread_attr_t& attr) override
{
return pthread_create(&_thread, &attr, ThreadProc, this);

View File

@@ -26,6 +26,12 @@ int32 UnixThread::Start(pthread_attr_t& attr)
void* UnixThread::ThreadProc(void* pThis)
{
auto thread = (UnixThread*)pThis;
#if PLATFORM_APPLE_FAMILY
// Apple doesn't support creating named thread so assign name here
{
pthread_setname_np(StringAnsi(thread->GetName()).Get());
}
#endif
const int32 exitCode = thread->Run();
return (void*)(uintptr)exitCode;
}