initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// ReSharper disable ParameterHidesMember
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace GodotTools.IdeMessaging.Utils
|
||||
{
|
||||
public class NotifyAwaiter<T> : INotifyCompletion
|
||||
{
|
||||
private Action? continuation;
|
||||
private Exception? exception;
|
||||
private T? result;
|
||||
|
||||
public bool IsCompleted { get; private set; }
|
||||
|
||||
public T GetResult()
|
||||
{
|
||||
if (exception != null)
|
||||
throw exception;
|
||||
return result!;
|
||||
}
|
||||
|
||||
public void OnCompleted(Action continuation)
|
||||
{
|
||||
if (this.continuation != null)
|
||||
throw new InvalidOperationException("This awaiter already has a continuation.");
|
||||
this.continuation = continuation;
|
||||
}
|
||||
|
||||
public void SetResult(T result)
|
||||
{
|
||||
if (IsCompleted)
|
||||
throw new InvalidOperationException("This awaiter is already completed.");
|
||||
|
||||
IsCompleted = true;
|
||||
this.result = result;
|
||||
|
||||
continuation?.Invoke();
|
||||
}
|
||||
|
||||
public void SetException(Exception exception)
|
||||
{
|
||||
if (IsCompleted)
|
||||
throw new InvalidOperationException("This awaiter is already completed.");
|
||||
|
||||
IsCompleted = true;
|
||||
this.exception = exception;
|
||||
|
||||
continuation?.Invoke();
|
||||
}
|
||||
|
||||
public NotifyAwaiter<T> Reset()
|
||||
{
|
||||
continuation = null;
|
||||
exception = null;
|
||||
result = default(T);
|
||||
IsCompleted = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
public NotifyAwaiter<T> GetAwaiter()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GodotTools.IdeMessaging.Utils
|
||||
{
|
||||
public static class SemaphoreExtensions
|
||||
{
|
||||
public static ConfiguredTaskAwaitable<IDisposable> UseAsync(this SemaphoreSlim semaphoreSlim, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
var wrapper = new SemaphoreSlimWaitReleaseWrapper(semaphoreSlim, out Task waitAsyncTask, cancellationToken);
|
||||
return waitAsyncTask.ContinueWith<IDisposable>(t => wrapper, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private readonly struct SemaphoreSlimWaitReleaseWrapper : IDisposable
|
||||
{
|
||||
private readonly SemaphoreSlim semaphoreSlim;
|
||||
|
||||
public SemaphoreSlimWaitReleaseWrapper(SemaphoreSlim semaphoreSlim, out Task waitAsyncTask, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
this.semaphoreSlim = semaphoreSlim;
|
||||
waitAsyncTask = this.semaphoreSlim.WaitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
semaphoreSlim.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user