DragonECS-ClassicThreads/src/ThreadRunner.cs

133 lines
4.3 KiB
C#
Raw Normal View History

2023-06-26 02:58:59 +08:00
using DCFApixels.DragonECS.ClassicThreadsInternal;
2023-06-11 23:24:46 +08:00
using System;
2023-06-12 01:41:21 +08:00
using System.Collections.Generic;
2023-06-11 23:24:46 +08:00
using System.Threading;
2023-06-11 23:18:56 +08:00
namespace DCFApixels.DragonECS
2023-06-11 23:09:46 +08:00
{
2023-06-11 23:18:56 +08:00
internal static class ThreadRunner
{
private readonly static int _maxThreadsCount;
private static ThreadReacord[] _threads;
2023-06-11 23:09:46 +08:00
2023-06-12 14:40:53 +08:00
private static EcsThreadHandler _worker;
private static EcsThreadHandler _nullWorker = delegate { };
2023-06-11 23:18:56 +08:00
private static int[] _entities = new int[64];
2023-06-12 01:33:52 +08:00
private static List<Exception> _catchedExceptions;
2023-06-11 23:09:46 +08:00
2023-06-12 12:52:45 +08:00
private static bool _isRunning = false;
2023-06-11 23:18:56 +08:00
private static void ThreadProc(object obj)
2023-06-11 23:09:46 +08:00
{
2023-06-12 01:33:52 +08:00
int i = (int)obj;
ref ThreadReacord record = ref _threads[i];
2023-06-12 01:16:00 +08:00
while (Thread.CurrentThread.IsAlive)
{
try
2023-06-11 23:18:56 +08:00
{
record.runWork.WaitOne();
record.runWork.Reset();
_worker.Invoke(new ReadOnlySpan<int>(_entities, record.start, record.size));
record.doneWork.Set();
}
2023-06-12 01:33:52 +08:00
catch (Exception e)
2023-06-12 01:16:00 +08:00
{
2023-06-12 01:33:52 +08:00
if (_catchedExceptions == null)
_catchedExceptions = new List<Exception>();
_catchedExceptions.Add(e);
2023-06-12 01:16:00 +08:00
record.doneWork.Set();
}
}
2023-06-11 23:09:46 +08:00
}
2023-06-11 23:18:56 +08:00
static ThreadRunner()
2023-06-11 23:09:46 +08:00
{
2023-06-11 23:18:56 +08:00
_maxThreadsCount = Environment.ProcessorCount;
_threads = new ThreadReacord[_maxThreadsCount];
for (int i = 0; i < _maxThreadsCount; i++)
2023-06-11 23:09:46 +08:00
{
2023-06-11 23:18:56 +08:00
_threads[i] = new ThreadReacord()
{
thread = new Thread(ThreadProc) { IsBackground = true },
runWork = new ManualResetEvent(false),
doneWork = new ManualResetEvent(true),
};
_threads[i].thread.Start(i);
}
2023-06-12 00:59:01 +08:00
_worker = _nullWorker;
2023-06-11 23:09:46 +08:00
}
2023-06-12 14:40:53 +08:00
public static void Run(EcsThreadHandler worker, EcsReadonlyGroup entities, int minSpanSize)
2023-06-11 23:18:56 +08:00
{
2023-06-12 12:52:45 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2023-06-26 01:58:18 +08:00
if (_isRunning) Throw.DoubleParallelIteration();
2023-06-12 12:52:45 +08:00
#endif
_isRunning = true;
2023-06-11 23:18:56 +08:00
_worker = worker;
int entitiesCount = entities.Bake(ref _entities);
2023-06-11 23:09:46 +08:00
2023-06-11 23:18:56 +08:00
int threadsCount = entitiesCount / minSpanSize;
2023-06-12 01:05:19 +08:00
if (entitiesCount % minSpanSize > 0)
threadsCount++;
2023-06-11 23:18:56 +08:00
if (threadsCount > _maxThreadsCount)
threadsCount = _maxThreadsCount;
if (threadsCount > 1)
{
2023-06-12 13:35:54 +08:00
int remainder = entitiesCount % threadsCount;
int quotient = entitiesCount / threadsCount;
for (int i = 0, start = 0; i < threadsCount; i++)
2023-06-11 23:18:56 +08:00
{
ref var thread = ref _threads[i];
2023-06-12 13:35:54 +08:00
thread.start = start;
thread.size = quotient;
if (remainder > 0)
{
thread.size++;
remainder--;
}
start += thread.size;
2023-06-11 23:18:56 +08:00
}
}
else
{
threadsCount = 1;
ref var thread = ref _threads[0];
thread.start = 0;
thread.size = entitiesCount;
}
2023-06-11 23:09:46 +08:00
for (int i = 0; i < threadsCount; i++)
{
ref var thread = ref _threads[i];
2023-06-11 23:56:35 +08:00
thread.doneWork.Reset();
2023-06-11 23:18:56 +08:00
thread.runWork.Set();
}
for (int i = 0; i < threadsCount; i++)
{
_threads[i].doneWork.WaitOne();
2023-06-11 23:09:46 +08:00
}
2023-06-12 12:52:45 +08:00
_isRunning = false;
2023-06-12 00:59:01 +08:00
_worker = _nullWorker;
2023-06-22 14:39:40 +08:00
if (_catchedExceptions != null)
2023-06-12 01:33:52 +08:00
{
var exceptions = _catchedExceptions;
_catchedExceptions = null;
2023-06-26 01:58:18 +08:00
throw new AggregateException(exceptions);
2023-06-12 01:33:52 +08:00
}
2023-06-11 23:09:46 +08:00
}
2023-06-11 23:18:56 +08:00
private struct ThreadReacord
2023-06-11 23:09:46 +08:00
{
2023-06-11 23:18:56 +08:00
public Thread thread;
public ManualResetEvent runWork;
public ManualResetEvent doneWork;
public int start;
public int size;
}
2023-06-11 23:09:46 +08:00
}
2023-06-12 14:40:53 +08:00
public delegate void EcsThreadHandler(ReadOnlySpan<int> entities);
2023-06-11 23:09:46 +08:00
}