mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Refactor code.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user