Refactor code.

This commit is contained in:
Christian
2022-09-15 09:09:08 +02:00
parent d23bbf7bf3
commit 7e686d9aa5
3 changed files with 29 additions and 35 deletions
+13 -16
View File
@@ -10,8 +10,8 @@ namespace MQTTnet.Internal
{
public sealed class AsyncLock : IDisposable
{
readonly object _syncRoot = new object();
readonly Task<IDisposable> _releaser;
readonly object _syncRoot = new object();
SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
@@ -20,6 +20,15 @@ namespace MQTTnet.Internal
_releaser = Task.FromResult((IDisposable)new Releaser(this));
}
public void Dispose()
{
lock (_syncRoot)
{
_semaphore?.Dispose();
_semaphore = null;
}
}
public Task<IDisposable> WaitAsync(CancellationToken cancellationToken)
{
Task task;
@@ -45,22 +54,10 @@ namespace MQTTnet.Internal
}
// Wait for the _WaitAsync_ method and return the releaser afterwards.
return task.ContinueWith(
(_, state) => (IDisposable)state,
_releaser.Result,
cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
return task.ContinueWith((_, state) => (IDisposable)state, _releaser.Result, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
public void Dispose()
{
lock (_syncRoot)
{
_semaphore?.Dispose();
_semaphore = null;
}
}
internal void Release()
void Release()
{
lock (_syncRoot)
{
@@ -83,4 +80,4 @@ namespace MQTTnet.Internal
}
}
}
}
}