mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-04-27 16:15:47 +02:00
29 lines
627 B
C#
29 lines
627 B
C#
namespace Phantom.Utils.Threading;
|
|
|
|
public sealed class ThreadSafeStructRef<T> : IDisposable where T : struct {
|
|
private T? value;
|
|
private readonly SemaphoreSlim semaphore = new (1, 1);
|
|
|
|
public async Task<T?> Get(CancellationToken cancellationToken) {
|
|
await semaphore.WaitAsync(cancellationToken);
|
|
try {
|
|
return value;
|
|
} finally {
|
|
semaphore.Release();
|
|
}
|
|
}
|
|
|
|
public async Task Set(T? value, CancellationToken cancellationToken) {
|
|
await semaphore.WaitAsync(cancellationToken);
|
|
try {
|
|
this.value = value;
|
|
} finally {
|
|
semaphore.Release();
|
|
}
|
|
}
|
|
|
|
public void Dispose() {
|
|
semaphore.Dispose();
|
|
}
|
|
}
|