Add PLATFORM_THREADS_LIMIT for maximum concurrency limiting

This commit is contained in:
Wojtek Figat
2021-07-08 00:25:49 +02:00
parent 29be6b0068
commit 446458d20c
12 changed files with 16 additions and 15 deletions

View File

@@ -6,7 +6,7 @@
#include "Engine/Graphics/Models/SkeletonData.h"
#include "Engine/Scripting/Scripting.h"
ThreadLocal<AnimGraphContext, 64> AnimGraphExecutor::Context;
ThreadLocal<AnimGraphContext> AnimGraphExecutor::Context;
RootMotionData RootMotionData::Identity = { Vector3(0.0f), Quaternion(0.0f, 0.0f, 0.0f, 1.0f) };

View File

@@ -790,7 +790,7 @@ private:
int32 _skeletonNodesCount = 0;
// Per-thread context to allow async execution
static ThreadLocal<AnimGraphContext, 64> Context;
static ThreadLocal<AnimGraphContext> Context;
public:

View File

@@ -30,7 +30,7 @@ namespace
VisualScripting::StackFrame* Stack;
};
ThreadLocal<VisualScriptThread, 32> ThreadStacks;
ThreadLocal<VisualScriptThread> ThreadStacks;
VisualScriptingBinaryModule VisualScriptingModule;
VisualScriptExecutor VisualScriptingExecutor;

View File

@@ -4,7 +4,7 @@
#include "Engine/Threading/ThreadLocal.h"
// Use a cached storage for the sorting (one per thread to reduce locking)
ThreadLocal<Sorting::SortingStack, 64> SortingStacks;
ThreadLocal<Sorting::SortingStack> SortingStacks;
Sorting::SortingStack& Sorting::SortingStack::Get()
{

View File

@@ -7,7 +7,7 @@
#include "Engine/Particles/ParticleEffect.h"
#include "Engine/Engine/Time.h"
ThreadLocal<ParticleEmitterGraphCPUContext, 64> ParticleEmitterGraphCPUExecutor::Context;
ThreadLocal<ParticleEmitterGraphCPUContext> ParticleEmitterGraphCPUExecutor::Context;
namespace
{

View File

@@ -134,7 +134,7 @@ private:
ParticleEmitterGraphCPU& _graph;
// Per-thread context to allow async execution
static ThreadLocal<ParticleEmitterGraphCPUContext, 64> Context;
static ThreadLocal<ParticleEmitterGraphCPUContext> Context;
public:

View File

@@ -168,6 +168,9 @@ API_ENUM() enum class ArchitectureType
#ifndef PLATFORM_LINE_TERMINATOR
#define PLATFORM_LINE_TERMINATOR "\n"
#endif
#ifndef PLATFORM_THREADS_LIMIT
#define PLATFORM_THREADS_LIMIT 64
#endif
#define PLATFORM_32BITS (!PLATFORM_64BITS)
// Platform family defines

View File

@@ -112,7 +112,7 @@ Action Scripting::ScriptsLoaded;
Action Scripting::ScriptsUnload;
Action Scripting::ScriptsReloading;
Action Scripting::ScriptsReloaded;
ThreadLocal<Scripting::IdsMappingTable*, 32, true> Scripting::ObjectsLookupIdMapping;
ThreadLocal<Scripting::IdsMappingTable*, PLATFORM_THREADS_LIMIT, true> Scripting::ObjectsLookupIdMapping;
ScriptingService ScriptingServiceInstance;
bool initFlaxEngine();

View File

@@ -116,7 +116,7 @@ public:
/// <summary>
/// The objects lookup identifier mapping used to override the object ids on FindObject call (used by the object references deserialization).
/// </summary>
static ThreadLocal<IdsMappingTable*, 32, true> ObjectsLookupIdMapping;
static ThreadLocal<IdsMappingTable*, PLATFORM_THREADS_LIMIT, true> ObjectsLookupIdMapping;
/// <summary>
/// Finds the object by the given identifier. Searches registered scene objects and optionally assets. Logs warning if fails.

View File

@@ -85,7 +85,7 @@ public:
namespace
{
JobSystemService JobSystemInstance;
Thread* Threads[32] = {};
Thread* Threads[PLATFORM_THREADS_LIMIT] = {};
int32 ThreadsCount = 0;
bool JobStartingOnDispatch = true;
volatile int64 ExitFlag = 0;

View File

@@ -5,16 +5,14 @@
#include "Engine/Core/Types/BaseTypes.h"
#include "Engine/Core/Collections/Array.h"
#include "Threading.h"
// Maximum amount of threads with an access to the thread local variable (we use static limit due to engine threading design)
#define THREAD_LOCAL_MAX_CAPACITY 16
#include "Engine/Platform/Platform.h"
/// <summary>
/// Per-thread local variable storage.
/// Implemented using atomic with per-thread storage indexed via thread id hashing.
/// ForConsider using 'THREADLOCAL' define before the variable instead.
/// </summary>
template<typename T, int32 MaxThreads = THREAD_LOCAL_MAX_CAPACITY, bool ClearMemory = true>
template<typename T, int32 MaxThreads = PLATFORM_THREADS_LIMIT, bool ClearMemory = true>
class ThreadLocal
{
protected:
@@ -104,7 +102,7 @@ protected:
/// <summary>
/// Per thread local object
/// </summary>
template<typename T, int32 MaxThreads = THREAD_LOCAL_MAX_CAPACITY>
template<typename T, int32 MaxThreads = PLATFORM_THREADS_LIMIT>
class ThreadLocalObject : public ThreadLocal<T*, MaxThreads>
{
public:

View File

@@ -51,7 +51,7 @@ ThreadPoolService ThreadPoolServiceInstance;
bool ThreadPoolService::Init()
{
// Spawn threads
const int32 numThreads = Math::Max<int32>(2, Platform::GetCPUInfo().ProcessorCoreCount - 1);
const int32 numThreads = Math::Clamp<int32>(Platform::GetCPUInfo().ProcessorCoreCount - 1, 2, PLATFORM_THREADS_LIMIT);
LOG(Info, "Spawning {0} Thread Pool workers", numThreads);
for (int32 i = ThreadPoolImpl::Threads.Count(); i < numThreads; i++)
{