Fix taskTracker collection modified errors

This commit is contained in:
Raymond 2021-10-17 22:01:03 -04:00
parent e8f395b8cc
commit 13b7a116b8

View file

@ -1,11 +1,11 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dalamud.Game; using Dalamud.Game;
using MonoMod.RuntimeDetour;
using Serilog; using Serilog;
namespace Dalamud.Logging.Internal namespace Dalamud.Logging.Internal
@ -16,8 +16,10 @@ namespace Dalamud.Logging.Internal
internal class TaskTracker : IDisposable internal class TaskTracker : IDisposable
{ {
private static readonly List<TaskInfo> TrackedTasksInternal = new(); private static readonly List<TaskInfo> TrackedTasksInternal = new();
private static readonly ConcurrentQueue<TaskInfo> NewlyCreatedTasks = new();
private static bool clearRequested = false;
private Hook? scheduleAndStartHook; private MonoMod.RuntimeDetour.Hook? scheduleAndStartHook;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TaskTracker"/> class. /// Initializes a new instance of the <see cref="TaskTracker"/> class.
@ -38,15 +40,27 @@ namespace Dalamud.Logging.Internal
/// <summary> /// <summary>
/// Clear the list of tracked tasks. /// Clear the list of tracked tasks.
/// </summary> /// </summary>
public static void Clear() => TrackedTasksInternal.Clear(); public static void Clear() => clearRequested = true;
/// <summary> /// <summary>
/// Update the tracked data. /// Update the tracked data.
/// </summary> /// </summary>
public static void UpdateData() public static void UpdateData()
{ {
foreach (var taskInfo in TrackedTasksInternal) if (clearRequested)
{ {
TrackedTasksInternal.Clear();
clearRequested = false;
}
while (NewlyCreatedTasks.TryDequeue(out var newTask))
{
TrackedTasksInternal.Add(newTask);
}
for (var i = 0; i < TrackedTasksInternal.Count; i++)
{
var taskInfo = TrackedTasksInternal[i];
if (taskInfo.Task == null) if (taskInfo.Task == null)
continue; continue;
@ -81,7 +95,7 @@ namespace Dalamud.Logging.Internal
orig(self); orig(self);
var trace = new StackTrace(); var trace = new StackTrace();
TrackedTasksInternal.Add(new TaskInfo NewlyCreatedTasks.Enqueue(new TaskInfo
{ {
Task = self, Task = self,
Id = self.Id, Id = self.Id,
@ -114,7 +128,7 @@ namespace Dalamud.Logging.Internal
if (patchMethod == null) if (patchMethod == null)
Log.Error("PatchMethod null!"); Log.Error("PatchMethod null!");
this.scheduleAndStartHook = new Hook(targetMethod, patchMethod); this.scheduleAndStartHook = new MonoMod.RuntimeDetour.Hook(targetMethod, patchMethod);
Log.Information("AddToActiveTasks Hooked!"); Log.Information("AddToActiveTasks Hooked!");
} }