Improve allocations for AsyncSignal.

This commit is contained in:
Christian
2022-11-26 14:45:34 +01:00
parent c45cb8b04e
commit 581a024ceb
3 changed files with 24 additions and 2 deletions
+22 -1
View File
@@ -34,7 +34,7 @@ namespace MQTTnet.Internal
_isSignaled = true;
Cleanup();
// If there is already a waiting task let it run.
if (_waiter != null)
{
@@ -103,6 +103,8 @@ namespace MQTTnet.Internal
// ReSharper disable once FieldCanBeMadeReadOnly.Local
CancellationTokenRegistration _cancellationTokenRegistration;
volatile bool _isCompleted;
public AsyncSignalWaiter(CancellationToken cancellationToken)
{
if (cancellationToken.CanBeCanceled)
@@ -115,18 +117,37 @@ namespace MQTTnet.Internal
public void Approve()
{
if (_isCompleted)
{
return;
}
_isCompleted = true;
_promise.TrySetResult(true);
}
public void Dispose()
{
_cancellationTokenRegistration.Dispose();
if (_isCompleted)
{
// Avoid allocation of _ObjectDisposedException_ which may not be used.
return;
}
_isCompleted = true;
_promise.TrySetException(new ObjectDisposedException(nameof(AsyncSignalWaiter)));
}
void Cancel()
{
if (_isCompleted)
{
return;
}
_isCompleted = true;
_promise.TrySetCanceled();
}
}
@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
namespace MQTTnet.Server
{