mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 10:17:22 +01:00
feat: add thread safety helpers
This commit is contained in:
parent
9bebbf2140
commit
8d304dc24b
2 changed files with 51 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
49
Dalamud/Utility/ThreadSafety.cs
Normal file
49
Dalamud/Utility/ThreadSafety.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue