Dalamud/Dalamud/Utility/ThreadSafety.cs
goat 448b0d16ea
Add "loading dialog" for service init, unify blocking logic (#1779)
* wip

* hacky fix for overlapping event text in profiler

* move IsResumeGameAfterPluginLoad logic to PluginManager

* fix some warnings

* handle exceptions properly

* remove ability to cancel, rename button to "hide" instead

* undo Dalamud.Service refactor for now

* warnings

* add explainer, show which plugins are still loading

* add some text if loading takes more than 3 minutes

* undo wrong CS merge
2024-04-21 17:28:37 +02:00

60 lines
1.8 KiB
C#

using System.Runtime.CompilerServices;
namespace Dalamud.Utility;
/// <summary>
/// Helpers for working with thread safety.
/// </summary>
public static class ThreadSafety
{
[ThreadStatic]
private static bool threadStaticIsMainThread;
/// <summary>
/// Gets a value indicating whether the current thread is the main thread.
/// </summary>
public static bool IsMainThread => threadStaticIsMainThread;
/// <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>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AssertMainThread()
{
if (!threadStaticIsMainThread)
{
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>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AssertNotMainThread()
{
if (threadStaticIsMainThread)
{
throw new InvalidOperationException("On main thread!");
}
}
/// <summary><see cref="AssertMainThread"/>, but only on debug compilation mode.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DebugAssertMainThread()
{
#if DEBUG
AssertMainThread();
#endif
}
/// <summary>
/// Marks a thread as the main thread.
/// </summary>
internal static void MarkMainThread()
{
threadStaticIsMainThread = true;
}
}