Refactor async/await and ConcurrentDictionary usage.

This commit is contained in:
Christian Kratky
2018-06-20 20:07:25 +02:00
parent bc20850fba
commit 0322660561
20 changed files with 409 additions and 245 deletions
+16 -17
View File
@@ -11,8 +11,10 @@ namespace MQTTnet.Internal
private readonly LinkedList<TaskCompletionSource<bool>> _waiters = new LinkedList<TaskCompletionSource<bool>>();
private bool _isSignaled;
public AsyncAutoResetEvent() : this(false)
{ }
public AsyncAutoResetEvent()
: this(false)
{
}
public AsyncAutoResetEvent(bool signaled)
{
@@ -58,27 +60,24 @@ namespace MQTTnet.Internal
}
var winner = await Task.WhenAny(tcs.Task, Task.Delay(timeout, cancellationToken)).ConfigureAwait(false);
if (winner == tcs.Task)
var taskWasSignaled = winner == tcs.Task;
if (taskWasSignaled)
{
// The task was signaled.
return true;
}
else
// We timed-out; remove our reference to the task.
// This is an O(n) operation since waiters is a LinkedList<T>.
lock (_waiters)
{
// We timed-out; remove our reference to the task.
// This is an O(n) operation since waiters is a LinkedList<T>.
lock (_waiters)
_waiters.Remove(tcs);
if (winner.Status == TaskStatus.Canceled)
{
_waiters.Remove(tcs);
if (winner.Status == TaskStatus.Canceled)
{
throw new OperationCanceledException(cancellationToken);
}
else
{
throw new TimeoutException();
}
throw new OperationCanceledException(cancellationToken);
}
throw new TimeoutException();
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ namespace MQTTnet.Internal
public Task<IDisposable> LockAsync(CancellationToken cancellationToken)
{
Task wait = _semaphore.WaitAsync(cancellationToken);
var wait = _semaphore.WaitAsync(cancellationToken);
return wait.IsCompleted ?
_releaser :
wait.ContinueWith((_, state) => (IDisposable)state,