diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 46e439a10..4389f270e 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -99,6 +99,8 @@ namespace Dalamud { try { + ThreadSafety.MarkMainThread(); + SerilogEventSink.Instance.LogLine += SerilogOnLogLine; Service.Set(); @@ -115,7 +117,6 @@ namespace Dalamud // Signal the main game thread to continue NativeFunctions.SetEvent(this.mainThreadContinueEvent); - Log.Information("[T1] Game thread continued!"); // Initialize FFXIVClientStructs function resolver diff --git a/Dalamud/Utility/ThreadSafety.cs b/Dalamud/Utility/ThreadSafety.cs new file mode 100644 index 000000000..b7cdebb40 --- /dev/null +++ b/Dalamud/Utility/ThreadSafety.cs @@ -0,0 +1,49 @@ +using System; + +namespace Dalamud.Utility; + +/// +/// Helpers for working with thread safety. +/// +public static class ThreadSafety +{ + [ThreadStatic] + private static bool isMainThread; + + /// + /// Gets a value indicating whether the current thread is the main thread. + /// + public static bool IsMainThread => isMainThread; + + /// + /// Throws an exception when the current thread is not the main thread. + /// + /// Thrown when the current thread is not the main thread. + public static void AssertMainThread() + { + if (!isMainThread) + { + throw new InvalidOperationException("Not on main thread!"); + } + } + + /// + /// Throws an exception when the current thread is the main thread. + /// + /// Thrown when the current thread is the main thread. + public static void AssertNotMainThread() + { + if (isMainThread) + { + throw new InvalidOperationException("On main thread!"); + } + } + + /// + /// Marks a thread as the main thread. + /// + internal static void MarkMainThread() + { + isMainThread = true; + } +}