Merge pull request #1015 from Soreepeong/fix/ticktask-racecon

This commit is contained in:
goat 2022-10-03 19:53:15 +02:00 committed by GitHub
commit 22ea76c45c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -27,12 +27,15 @@ namespace Dalamud.Game
{ {
private static Stopwatch statsStopwatch = new(); private static Stopwatch statsStopwatch = new();
private readonly List<RunOnNextTickTaskBase> runOnNextTickTaskList = new();
private readonly Stopwatch updateStopwatch = new(); private readonly Stopwatch updateStopwatch = new();
private readonly Hook<OnUpdateDetour> updateHook; private readonly Hook<OnUpdateDetour> updateHook;
private readonly Hook<OnRealDestroyDelegate> destroyHook; private readonly Hook<OnRealDestroyDelegate> destroyHook;
private readonly object runOnNextTickTaskListSync = new();
private List<RunOnNextTickTaskBase> runOnNextTickTaskList = new();
private List<RunOnNextTickTaskBase> runOnNextTickTaskList2 = new();
private Thread? frameworkUpdateThread; private Thread? frameworkUpdateThread;
[ServiceManager.ServiceConstructor] [ServiceManager.ServiceConstructor]
@ -193,6 +196,8 @@ namespace Dalamud.Game
} }
var tcs = new TaskCompletionSource<T>(); var tcs = new TaskCompletionSource<T>();
lock (this.runOnNextTickTaskListSync)
{
this.runOnNextTickTaskList.Add(new RunOnNextTickTaskFunc<T>() this.runOnNextTickTaskList.Add(new RunOnNextTickTaskFunc<T>()
{ {
RemainingTicks = delayTicks, RemainingTicks = delayTicks,
@ -201,6 +206,8 @@ namespace Dalamud.Game
TaskCompletionSource = tcs, TaskCompletionSource = tcs,
Func = func, Func = func,
}); });
}
return tcs.Task; return tcs.Task;
} }
@ -225,6 +232,8 @@ namespace Dalamud.Game
} }
var tcs = new TaskCompletionSource(); var tcs = new TaskCompletionSource();
lock (this.runOnNextTickTaskListSync)
{
this.runOnNextTickTaskList.Add(new RunOnNextTickTaskAction() this.runOnNextTickTaskList.Add(new RunOnNextTickTaskAction()
{ {
RemainingTicks = delayTicks, RemainingTicks = delayTicks,
@ -233,6 +242,8 @@ namespace Dalamud.Game
TaskCompletionSource = tcs, TaskCompletionSource = tcs,
Action = action, Action = action,
}); });
}
return tcs.Task; return tcs.Task;
} }
@ -258,6 +269,8 @@ namespace Dalamud.Game
} }
var tcs = new TaskCompletionSource<Task<T>>(); var tcs = new TaskCompletionSource<Task<T>>();
lock (this.runOnNextTickTaskListSync)
{
this.runOnNextTickTaskList.Add(new RunOnNextTickTaskFunc<Task<T>>() this.runOnNextTickTaskList.Add(new RunOnNextTickTaskFunc<Task<T>>()
{ {
RemainingTicks = delayTicks, RemainingTicks = delayTicks,
@ -266,6 +279,8 @@ namespace Dalamud.Game
TaskCompletionSource = tcs, TaskCompletionSource = tcs,
Func = func, Func = func,
}); });
}
return tcs.Task.ContinueWith(x => x.Result, cancellationToken).Unwrap(); return tcs.Task.ContinueWith(x => x.Result, cancellationToken).Unwrap();
} }
@ -291,6 +306,8 @@ namespace Dalamud.Game
} }
var tcs = new TaskCompletionSource<Task>(); var tcs = new TaskCompletionSource<Task>();
lock (this.runOnNextTickTaskListSync)
{
this.runOnNextTickTaskList.Add(new RunOnNextTickTaskFunc<Task>() this.runOnNextTickTaskList.Add(new RunOnNextTickTaskFunc<Task>()
{ {
RemainingTicks = delayTicks, RemainingTicks = delayTicks,
@ -299,6 +316,8 @@ namespace Dalamud.Game
TaskCompletionSource = tcs, TaskCompletionSource = tcs,
Func = func, Func = func,
}); });
}
return tcs.Task.ContinueWith(x => x.Result, cancellationToken).Unwrap(); return tcs.Task.ContinueWith(x => x.Result, cancellationToken).Unwrap();
} }
@ -330,6 +349,20 @@ namespace Dalamud.Game
this.destroyHook.Enable(); this.destroyHook.Enable();
} }
private void RunPendingTickTasks()
{
if (this.runOnNextTickTaskList.Count == 0 && this.runOnNextTickTaskList2.Count == 0)
return;
for (var i = 0; i < 2; i++)
{
lock (this.runOnNextTickTaskListSync)
(this.runOnNextTickTaskList, this.runOnNextTickTaskList2) = (this.runOnNextTickTaskList2, this.runOnNextTickTaskList);
this.runOnNextTickTaskList2.RemoveAll(x => x.Run());
}
}
private bool HandleFrameworkUpdate(IntPtr framework) private bool HandleFrameworkUpdate(IntPtr framework)
{ {
this.frameworkUpdateThread ??= Thread.CurrentThread; this.frameworkUpdateThread ??= Thread.CurrentThread;
@ -362,7 +395,7 @@ namespace Dalamud.Game
this.LastUpdate = DateTime.Now; this.LastUpdate = DateTime.Now;
this.LastUpdateUTC = DateTime.UtcNow; this.LastUpdateUTC = DateTime.UtcNow;
this.runOnNextTickTaskList.RemoveAll(x => x.Run()); this.RunPendingTickTasks();
if (StatsEnabled && this.Update != null) if (StatsEnabled && this.Update != null)
{ {
@ -430,7 +463,7 @@ namespace Dalamud.Game
Log.Information("Framework::Destroy!"); Log.Information("Framework::Destroy!");
Service<Dalamud>.Get().Unload(); Service<Dalamud>.Get().Unload();
this.runOnNextTickTaskList.RemoveAll(x => x.Run()); this.RunPendingTickTasks();
ServiceManager.UnloadAllServices(); ServiceManager.UnloadAllServices();
Log.Information("Framework::Destroy OK!"); Log.Information("Framework::Destroy OK!");