yuanshen/Yuanshen.Kcp/AsyncAutoResetEvent.cs
wisedev fc2ac1050f First working version of the server
KCP gameserver with packet dispatch, scene streaming, combat, gacha and the
shop, plus a small HTTP server for the login flow and the wish history page.

Game data under assets is pulled out of the client instead of being typed by
hand. The excel tables come from the packed bin container and the spawn points
from the compiled scene scripts.
2026-09-04 02:10:50 +03:00

117 lines
3 KiB
C#

using System.Diagnostics;
using System.Threading.Tasks.Sources;
namespace Yuanshen.Kcp
{
internal class AsyncAutoResetEvent<T> : IValueTaskSource<T>
{
private ManualResetValueTaskSourceCore<T> _rvtsc;
private SpinLock _lock;
private bool _isSet;
private bool _activeWait;
private bool _signaled;
private T? _value;
public AsyncAutoResetEvent()
{
_rvtsc = new ManualResetValueTaskSourceCore<T>()
{
RunContinuationsAsynchronously = true
};
_lock = new SpinLock();
}
T IValueTaskSource<T>.GetResult(short token)
{
try
{
return _rvtsc.GetResult(token);
}
finally
{
_rvtsc.Reset();
bool lockTaken = false;
try
{
_lock.Enter(ref lockTaken);
_activeWait = false;
_signaled = false;
}
finally
{
if (lockTaken)
{
_lock.Exit();
}
}
}
}
ValueTaskSourceStatus IValueTaskSource<T>.GetStatus(short token) => _rvtsc.GetStatus(token);
void IValueTaskSource<T>.OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
=> _rvtsc.OnCompleted(continuation, state, token, flags);
public ValueTask<T> WaitAsync()
{
bool lockTaken = false;
try
{
_lock.Enter(ref lockTaken);
if (_activeWait)
{
return new ValueTask<T>(Task.FromException<T>(new InvalidOperationException("Another thread is already waiting.")));
}
if (_isSet)
{
_isSet = false;
T value = _value!;
_value = default;
return new ValueTask<T>(value);
}
_activeWait = true;
Debug.Assert(!_signaled);
return new ValueTask<T>(this, _rvtsc.Version);
}
finally
{
if (lockTaken)
{
_lock.Exit();
}
}
}
public void Set(T value)
{
bool lockTaken = false;
try
{
_lock.Enter(ref lockTaken);
if (_activeWait && !_signaled)
{
_signaled = true;
_rvtsc.SetResult(value);
return;
}
_isSet = true;
_value = value;
}
finally
{
if (lockTaken)
{
_lock.Exit();
}
}
}
}
}