feat: add thread safety helpers

This commit is contained in:
goaaats 2022-05-27 14:43:56 +02:00
parent 9bebbf2140
commit 8d304dc24b
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B
2 changed files with 51 additions and 1 deletions

View file

@ -99,6 +99,8 @@ namespace Dalamud
{
try
{
ThreadSafety.MarkMainThread();
SerilogEventSink.Instance.LogLine += SerilogOnLogLine;
Service<ServiceContainer>.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

View file

@ -0,0 +1,49 @@
using System;
namespace Dalamud.Utility;
/// <summary>
/// Helpers for working with thread safety.
/// </summary>
public static class ThreadSafety
{
[ThreadStatic]
private static bool isMainThread;
/// <summary>
/// Gets a value indicating whether the current thread is the main thread.
/// </summary>
public static bool IsMainThread => isMainThread;
/// <summary>
/// Throws an exception when the current thread is not the main thread.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the current thread is not the main thread.</exception>
public static void AssertMainThread()
{
if (!isMainThread)
{
throw new InvalidOperationException("Not on main thread!");
}
}
/// <summary>
/// Throws an exception when the current thread is the main thread.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the current thread is the main thread.</exception>
public static void AssertNotMainThread()
{
if (isMainThread)
{
throw new InvalidOperationException("On main thread!");
}
}
/// <summary>
/// Marks a thread as the main thread.
/// </summary>
internal static void MarkMainThread()
{
isMainThread = true;
}
}