WorkQueue._tasks swapped to Dictionary

This commit is contained in:
Justin Swanson 2020-01-11 18:36:45 -06:00
parent b9f27a1080
commit 1bb7038af6

View File

@ -31,7 +31,7 @@ namespace Wabbajack.Common
public IObservable<CPUStatus> Status => _Status; public IObservable<CPUStatus> Status => _Status;
private int _nextCpuID = 1; // Start at 1, as 0 is "Unassigned" private int _nextCpuID = 1; // Start at 1, as 0 is "Unassigned"
internal List<(int CpuID, Task Task)> _tasks = new List<(int CpuID, Task Task)>(); internal Dictionary<int, Task> _tasks = new Dictionary<int, Task>();
public int DesiredNumWorkers { get; private set; } = 0; public int DesiredNumWorkers { get; private set; } = 0;
private CancellationTokenSource _shutdown = new CancellationTokenSource(); private CancellationTokenSource _shutdown = new CancellationTokenSource();
@ -98,11 +98,10 @@ namespace Wabbajack.Common
while (DesiredNumWorkers > _tasks.Count) while (DesiredNumWorkers > _tasks.Count)
{ {
var cpuID = _nextCpuID++; var cpuID = _nextCpuID++;
_tasks.Add((cpuID, _tasks[cpuID] = Task.Run(async () =>
Task.Run(async () => {
{ await ThreadBody(cpuID);
await ThreadBody(cpuID); });
})));
} }
_cpuCountSubj.OnNext((_tasks.Count, DesiredNumWorkers)); _cpuCountSubj.OnNext((_tasks.Count, DesiredNumWorkers));
} }
@ -146,21 +145,13 @@ namespace Wabbajack.Common
// Check if another thread shut down before this one and got us back to the desired amount already // Check if another thread shut down before this one and got us back to the desired amount already
if (DesiredNumWorkers >= _tasks.Count) continue; if (DesiredNumWorkers >= _tasks.Count) continue;
Report("Shutting down", 0, false); // Shutdown
// Remove this task from list if (!_tasks.Remove(cpuID))
for (int i = 0; i < _tasks.Count; i++)
{ {
if (_tasks[i].CpuID == cpuID) Utils.Error($"Could not remove thread from workpool with CPU ID {cpuID}");
{
_tasks.RemoveAt(i);
_cpuCountSubj.OnNext((_tasks.Count, DesiredNumWorkers));
// Shutdown thread
Report("Shutting down", 0, false);
return;
}
} }
// Failed to remove, warn and then shutdown anyway Report("Shutting down", 0, false);
Utils.Error($"Could not remove thread from workpool with CPU ID {cpuID}"); _cpuCountSubj.OnNext((_tasks.Count, DesiredNumWorkers));
return; return;
} }
} }