From 1be5cd452d14c4f581823134d25fcddd97b8a4d8 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Sun, 21 Jul 2024 20:49:41 +0900 Subject: [PATCH] Make all TCS RunContinuationsAsynchronously --- Dalamud/Game/Config/GameConfig.cs | 15 +++++++++++---- Dalamud/Game/Framework.cs | 2 +- .../SingleFontChooserDialog.cs | 2 +- Dalamud/Interface/Internal/InterfaceManager.cs | 8 ++++---- .../Internal/Windows/PluginImageCache.cs | 4 ++-- .../PluginInstaller/PluginInstallerWindow.cs | 4 ++-- .../Internals/FontAtlasFactory.Implementation.cs | 2 +- .../ManagedFontAtlas/Internals/FontHandle.cs | 2 +- .../TextureWraps/Internal/ViewportTextureWrap.cs | 3 ++- .../Utility/Internal/DevTextureSaveMenu.cs | 4 ++-- Dalamud/Service/ServiceManager.cs | 4 +++- Dalamud/Service/Service{T}.cs | 2 +- Dalamud/Utility/AsyncUtils.cs | 2 +- Dalamud/Utility/DynamicPriorityQueueLoader.cs | 2 +- 14 files changed, 33 insertions(+), 23 deletions(-) diff --git a/Dalamud/Game/Config/GameConfig.cs b/Dalamud/Game/Config/GameConfig.cs index 1aeb42488..bfb58fd3c 100644 --- a/Dalamud/Game/Config/GameConfig.cs +++ b/Dalamud/Game/Config/GameConfig.cs @@ -16,10 +16,17 @@ namespace Dalamud.Game.Config; [ServiceManager.EarlyLoadedService] internal sealed class GameConfig : IInternalDisposableService, IGameConfig { - private readonly TaskCompletionSource tcsInitialization = new(); - private readonly TaskCompletionSource tcsSystem = new(); - private readonly TaskCompletionSource tcsUiConfig = new(); - private readonly TaskCompletionSource tcsUiControl = new(); + private readonly TaskCompletionSource tcsInitialization = + new(TaskCreationOptions.RunContinuationsAsynchronously); + + private readonly TaskCompletionSource tcsSystem = + new(TaskCreationOptions.RunContinuationsAsynchronously); + + private readonly TaskCompletionSource tcsUiConfig = + new(TaskCreationOptions.RunContinuationsAsynchronously); + + private readonly TaskCompletionSource tcsUiControl = + new(TaskCreationOptions.RunContinuationsAsynchronously); private readonly GameConfigAddressResolver address = new(); private Hook? configChangeHook; diff --git a/Dalamud/Game/Framework.cs b/Dalamud/Game/Framework.cs index 4f9c8d6c6..07942f780 100644 --- a/Dalamud/Game/Framework.cs +++ b/Dalamud/Game/Framework.cs @@ -139,7 +139,7 @@ internal sealed class Framework : IInternalDisposableService, IFramework if (numTicks <= 0) return Task.CompletedTask; - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.tickDelayedTaskCompletionSources[tcs] = (this.tickCounter + (ulong)numTicks, cancellationToken); return tcs.Task; } diff --git a/Dalamud/Interface/ImGuiFontChooserDialog/SingleFontChooserDialog.cs b/Dalamud/Interface/ImGuiFontChooserDialog/SingleFontChooserDialog.cs index c5c4581e7..f03518ada 100644 --- a/Dalamud/Interface/ImGuiFontChooserDialog/SingleFontChooserDialog.cs +++ b/Dalamud/Interface/ImGuiFontChooserDialog/SingleFontChooserDialog.cs @@ -59,7 +59,7 @@ public sealed class SingleFontChooserDialog : IDisposable private readonly int counter; private readonly byte[] fontPreviewText = new byte[2048]; - private readonly TaskCompletionSource tcs = new(); + private readonly TaskCompletionSource tcs = new(TaskCreationOptions.RunContinuationsAsynchronously); private readonly IFontAtlas atlas; private string popupImGuiName; diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index e2993f911..cbbf63075 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -336,7 +336,7 @@ internal class InterfaceManager : IInternalDisposableService /// A that resolves once is run. public Task RunBeforeImGuiRender(Action action) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.runBeforeImGuiRender.Enqueue( () => { @@ -359,7 +359,7 @@ internal class InterfaceManager : IInternalDisposableService /// A that resolves once is run. public Task RunBeforeImGuiRender(Func func) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.runBeforeImGuiRender.Enqueue( () => { @@ -380,7 +380,7 @@ internal class InterfaceManager : IInternalDisposableService /// A that resolves once is run. public Task RunAfterImGuiRender(Action action) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.runAfterImGuiRender.Enqueue( () => { @@ -403,7 +403,7 @@ internal class InterfaceManager : IInternalDisposableService /// A that resolves once is run. public Task RunAfterImGuiRender(Func func) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.runAfterImGuiRender.Enqueue( () => { diff --git a/Dalamud/Interface/Internal/Windows/PluginImageCache.cs b/Dalamud/Interface/Internal/Windows/PluginImageCache.cs index e67ff3cf5..e95d2e1b8 100644 --- a/Dalamud/Interface/Internal/Windows/PluginImageCache.cs +++ b/Dalamud/Interface/Internal/Windows/PluginImageCache.cs @@ -315,7 +315,7 @@ internal class PluginImageCache : IInternalDisposableService private Task RunInDownloadQueue(Func> func, ulong requestedFrame) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.downloadQueue.Add(Tuple.Create(requestedFrame, async () => { try @@ -332,7 +332,7 @@ internal class PluginImageCache : IInternalDisposableService private Task RunInLoadQueue(Func> func) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.loadQueue.Add(async () => { try diff --git a/Dalamud/Interface/Internal/Windows/PluginInstaller/PluginInstallerWindow.cs b/Dalamud/Interface/Internal/Windows/PluginInstaller/PluginInstallerWindow.cs index 466277a2f..ccf7b8226 100644 --- a/Dalamud/Interface/Internal/Windows/PluginInstaller/PluginInstallerWindow.cs +++ b/Dalamud/Interface/Internal/Windows/PluginInstaller/PluginInstallerWindow.cs @@ -3774,7 +3774,7 @@ internal class PluginInstallerWindow : Window, IDisposable this.errorModalMessage = message; this.errorModalDrawing = true; this.errorModalOnNextFrame = true; - this.errorModalTaskCompletionSource = new TaskCompletionSource(); + this.errorModalTaskCompletionSource = new(TaskCreationOptions.RunContinuationsAsynchronously); return this.errorModalTaskCompletionSource.Task; } @@ -3782,7 +3782,7 @@ internal class PluginInstallerWindow : Window, IDisposable { this.updateModalOnNextFrame = true; this.updateModalPlugin = plugin; - this.updateModalTaskCompletionSource = new TaskCompletionSource(); + this.updateModalTaskCompletionSource = new(TaskCreationOptions.RunContinuationsAsynchronously); return this.updateModalTaskCompletionSource.Task; } diff --git a/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.Implementation.cs b/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.Implementation.cs index ef92ffd65..61ac00faf 100644 --- a/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.Implementation.cs +++ b/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.Implementation.cs @@ -497,7 +497,7 @@ internal sealed partial class FontAtlasFactory $"{nameof(FontAtlasAutoRebuildMode.Async)}."); } - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); try { var rebuildIndex = Interlocked.Increment(ref this.buildIndex); diff --git a/Dalamud/Interface/ManagedFontAtlas/Internals/FontHandle.cs b/Dalamud/Interface/ManagedFontAtlas/Internals/FontHandle.cs index 0e26145f0..b84a857da 100644 --- a/Dalamud/Interface/ManagedFontAtlas/Internals/FontHandle.cs +++ b/Dalamud/Interface/ManagedFontAtlas/Internals/FontHandle.cs @@ -242,7 +242,7 @@ internal abstract class FontHandle : IFontHandle if (this.Available) return Task.FromResult(this); - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.ImFontChanged += OnImFontChanged; this.Disposed += OnDisposed; if (this.Available) diff --git a/Dalamud/Interface/Textures/TextureWraps/Internal/ViewportTextureWrap.cs b/Dalamud/Interface/Textures/TextureWraps/Internal/ViewportTextureWrap.cs index ad3188925..6e21bc0e8 100644 --- a/Dalamud/Interface/Textures/TextureWraps/Internal/ViewportTextureWrap.cs +++ b/Dalamud/Interface/Textures/TextureWraps/Internal/ViewportTextureWrap.cs @@ -24,7 +24,8 @@ internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDispos private readonly string? debugName; private readonly LocalPlugin? ownerPlugin; private readonly CancellationToken cancellationToken; - private readonly TaskCompletionSource firstUpdateTaskCompletionSource = new(); + private readonly TaskCompletionSource firstUpdateTaskCompletionSource = + new(TaskCreationOptions.RunContinuationsAsynchronously); private ImGuiViewportTextureArgs args; private D3D11_TEXTURE2D_DESC desc; diff --git a/Dalamud/Interface/Utility/Internal/DevTextureSaveMenu.cs b/Dalamud/Interface/Utility/Internal/DevTextureSaveMenu.cs index 2ff42bc2a..a6584f9aa 100644 --- a/Dalamud/Interface/Utility/Internal/DevTextureSaveMenu.cs +++ b/Dalamud/Interface/Utility/Internal/DevTextureSaveMenu.cs @@ -59,7 +59,7 @@ internal sealed class DevTextureSaveMenu : IInternalDisposableService { var first = true; var encoders = textureManager.Wic.GetSupportedEncoderInfos().ToList(); - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); Service.Get().Draw += DrawChoices; encoder = await tcs.Task; @@ -108,7 +108,7 @@ internal sealed class DevTextureSaveMenu : IInternalDisposableService string path; { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); this.fileDialogManager.SaveFileDialog( "Save texture...", $"{encoder.Name.Replace(',', '.')}{{{string.Join(',', encoder.Extensions)}}}", diff --git a/Dalamud/Service/ServiceManager.cs b/Dalamud/Service/ServiceManager.cs index 5e22ed0c1..29016bc69 100644 --- a/Dalamud/Service/ServiceManager.cs +++ b/Dalamud/Service/ServiceManager.cs @@ -44,7 +44,9 @@ internal static class ServiceManager private static readonly List LoadedServices = new(); #endif - private static readonly TaskCompletionSource BlockingServicesLoadedTaskCompletionSource = new(); + private static readonly TaskCompletionSource BlockingServicesLoadedTaskCompletionSource = + new(TaskCreationOptions.RunContinuationsAsynchronously); + private static readonly CancellationTokenSource UnloadCancellationTokenSource = new(); private static ManualResetEvent unloadResetEvent = new(false); diff --git a/Dalamud/Service/Service{T}.cs b/Dalamud/Service/Service{T}.cs index 57acd2ccf..b4bfff917 100644 --- a/Dalamud/Service/Service{T}.cs +++ b/Dalamud/Service/Service{T}.cs @@ -332,7 +332,7 @@ internal static class Service where T : IServiceType break; } - instanceTcs = new TaskCompletionSource(); + instanceTcs = new(TaskCreationOptions.RunContinuationsAsynchronously); instanceTcs.SetException(new UnloadedException()); } diff --git a/Dalamud/Utility/AsyncUtils.cs b/Dalamud/Utility/AsyncUtils.cs index 9533f2ab0..4de561275 100644 --- a/Dalamud/Utility/AsyncUtils.cs +++ b/Dalamud/Utility/AsyncUtils.cs @@ -21,7 +21,7 @@ public static class AsyncUtils /// Returns the first task that completes, according to . public static Task FirstSuccessfulTask(ICollection> tasks) { - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var remainingTasks = tasks.Count; foreach (var task in tasks) diff --git a/Dalamud/Utility/DynamicPriorityQueueLoader.cs b/Dalamud/Utility/DynamicPriorityQueueLoader.cs index 8109d2e94..83fd366bb 100644 --- a/Dalamud/Utility/DynamicPriorityQueueLoader.cs +++ b/Dalamud/Utility/DynamicPriorityQueueLoader.cs @@ -238,7 +238,7 @@ internal class DynamicPriorityQueueLoader : IDisposable params IDisposable?[] disposables) : base(basis, cancellationToken, disposables) { - this.taskCompletionSource = new(); + this.taskCompletionSource = new(TaskCreationOptions.RunContinuationsAsynchronously); this.immediateLoadFunction = immediateLoadFunction; }