From 91ba9466596bd94e1e6a868f293abbb32cb6e99d Mon Sep 17 00:00:00 2001 From: Laszlo Paillat Date: Fri, 3 Dec 2021 22:24:18 +0100 Subject: [PATCH] Revert "changed DefaultParallelRunner implementation to rely on Thread for targets above netstandard1.1 (may help with #145)" This reverts commit 6e539f24ab0eca9e1d6fd5ff97d38a2782082d94. --- .../Internal/Helper/DefaultThread.cs | 40 ------------------- .../Threading/DefaultParallelRunner.cs | 26 ++++++------ 2 files changed, 13 insertions(+), 53 deletions(-) delete mode 100644 source/DefaultEcs/Internal/Helper/DefaultThread.cs diff --git a/source/DefaultEcs/Internal/Helper/DefaultThread.cs b/source/DefaultEcs/Internal/Helper/DefaultThread.cs deleted file mode 100644 index e3f11d81..00000000 --- a/source/DefaultEcs/Internal/Helper/DefaultThread.cs +++ /dev/null @@ -1,40 +0,0 @@ -#if NETSTANDARD1_1 -using System; -using System.Threading.Tasks; -#else -using System.Threading; -#endif - -namespace DefaultEcs.Internal.Helper -{ - internal sealed class DefaultThread - { -#if NETSTANDARD1_1 - private readonly Task _task; - - public DefaultThread(Action action, object state) - { - _task = new Task(action, state, TaskCreationOptions.LongRunning); - } - - public void Start() => _task.Start(TaskScheduler.Default); - - public void Wait() => _task.Wait(); -#else - private readonly Thread _thread; - private readonly object _state; - - public DefaultThread(ParameterizedThreadStart action, object state) - { - _thread = new Thread(action); - _state = state; - - _thread.Name = $"{nameof(DefaultThread)} {state}"; - } - - public void Start() => _thread.Start(_state); - - public void Wait() => _thread.Join(); -#endif - } -} diff --git a/source/DefaultEcs/Threading/DefaultParallelRunner.cs b/source/DefaultEcs/Threading/DefaultParallelRunner.cs index 38031192..592e3f4e 100644 --- a/source/DefaultEcs/Threading/DefaultParallelRunner.cs +++ b/source/DefaultEcs/Threading/DefaultParallelRunner.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using DefaultEcs.Internal.Helper; using DefaultEcs.Internal.Threading; namespace DefaultEcs.Threading @@ -19,7 +18,7 @@ public sealed class DefaultParallelRunner : IParallelRunner private readonly CancellationTokenSource _disposeHandle; private readonly WorkerBarrier _barrier; - private readonly DefaultThread[] _threads; + private readonly Task[] _tasks; private IParallelRunnable _currentRunnable; @@ -37,12 +36,12 @@ public DefaultParallelRunner(int degreeOfParallelism) IEnumerable indices = degreeOfParallelism >= 1 ? Enumerable.Range(0, degreeOfParallelism - 1) : throw new ArgumentException("Argument cannot be inferior to one", nameof(degreeOfParallelism)); _disposeHandle = new CancellationTokenSource(); - _threads = indices.Select(index => new DefaultThread(Update, index)).ToArray(); - _barrier = degreeOfParallelism > 1 ? new WorkerBarrier(_threads.Length) : null; + _tasks = indices.Select(index => new Task(Update, index, TaskCreationOptions.LongRunning)).ToArray(); + _barrier = degreeOfParallelism > 1 ? new WorkerBarrier(_tasks.Length) : null; - foreach (DefaultThread thread in _threads) + foreach (Task task in _tasks) { - thread.Start(); + task.Start(TaskScheduler.Default); } } @@ -54,10 +53,14 @@ private void Update(object state) { int index = (int)state; +#if !NETSTANDARD1_1 + Thread.CurrentThread.Name = $"{nameof(DefaultParallelRunner)} worker {index + 1}"; +#endif + goto Start; Work: - Volatile.Read(ref _currentRunnable).Run(index, _threads.Length); + Volatile.Read(ref _currentRunnable).Run(index, _tasks.Length); _barrier.Signal(); @@ -77,7 +80,7 @@ private void Update(object state) /// /// Gets the degree of parallelism used to run an . /// - public int DegreeOfParallelism => _threads.Length + 1; + public int DegreeOfParallelism => _tasks.Length + 1; /// /// Runs the provided . @@ -89,7 +92,7 @@ public void Run(IParallelRunnable runnable) _barrier?.StartWorkers(); - runnable.Run(_threads.Length, _threads.Length); + runnable.Run(_tasks.Length, _tasks.Length); _barrier?.WaitForWorkers(); } @@ -107,10 +110,7 @@ public void Dispose() _barrier?.StartWorkers(); - foreach (DefaultThread thread in _threads) - { - thread.Wait(); - } + Task.WaitAll(_tasks); _barrier?.Dispose(); _disposeHandle.Dispose();