This commit is contained in:
Anon 2026-04-04 00:32:07 +02:00
parent 1ca023be36
commit 3a4d8951d5
20 changed files with 1511 additions and 389 deletions

View file

@ -0,0 +1,45 @@
using System;
using System.Threading;
namespace MinecraftClient
{
internal static class MainThreadExecutionScope
{
private sealed class ScopeNode(object owner, ScopeNode? parent) : IDisposable
{
public object Owner { get; } = owner;
public ScopeNode? Parent { get; } = parent;
public void Dispose()
{
if (!ReferenceEquals(s_currentScope.Value, this))
throw new InvalidOperationException("Main-thread execution scope disposed out of order.");
s_currentScope.Value = Parent;
}
}
private static readonly AsyncLocal<ScopeNode?> s_currentScope = new();
public static IDisposable Enter(object owner)
{
ScopeNode scopeNode = new(owner, s_currentScope.Value);
s_currentScope.Value = scopeNode;
return scopeNode;
}
public static bool IsActive(object owner)
{
ScopeNode? scopeNode = s_currentScope.Value;
while (scopeNode is not null)
{
if (ReferenceEquals(scopeNode.Owner, owner))
return true;
scopeNode = scopeNode.Parent;
}
return false;
}
}
}