mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 11:45:43 +08:00
Improve allocations for AsyncSignal.
This commit is contained in:
@@ -1 +1 @@
|
|||||||
|
[Core] Fixed a memory leak in _AsyncSignal_ implementation (#1586, thanks to @mario-zelger).
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace MQTTnet.Internal
|
|||||||
_isSignaled = true;
|
_isSignaled = true;
|
||||||
|
|
||||||
Cleanup();
|
Cleanup();
|
||||||
|
|
||||||
// If there is already a waiting task let it run.
|
// If there is already a waiting task let it run.
|
||||||
if (_waiter != null)
|
if (_waiter != null)
|
||||||
{
|
{
|
||||||
@@ -103,6 +103,8 @@ namespace MQTTnet.Internal
|
|||||||
// ReSharper disable once FieldCanBeMadeReadOnly.Local
|
// ReSharper disable once FieldCanBeMadeReadOnly.Local
|
||||||
CancellationTokenRegistration _cancellationTokenRegistration;
|
CancellationTokenRegistration _cancellationTokenRegistration;
|
||||||
|
|
||||||
|
volatile bool _isCompleted;
|
||||||
|
|
||||||
public AsyncSignalWaiter(CancellationToken cancellationToken)
|
public AsyncSignalWaiter(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (cancellationToken.CanBeCanceled)
|
if (cancellationToken.CanBeCanceled)
|
||||||
@@ -115,18 +117,37 @@ namespace MQTTnet.Internal
|
|||||||
|
|
||||||
public void Approve()
|
public void Approve()
|
||||||
{
|
{
|
||||||
|
if (_isCompleted)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isCompleted = true;
|
||||||
_promise.TrySetResult(true);
|
_promise.TrySetResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_cancellationTokenRegistration.Dispose();
|
_cancellationTokenRegistration.Dispose();
|
||||||
|
|
||||||
|
if (_isCompleted)
|
||||||
|
{
|
||||||
|
// Avoid allocation of _ObjectDisposedException_ which may not be used.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isCompleted = true;
|
||||||
_promise.TrySetException(new ObjectDisposedException(nameof(AsyncSignalWaiter)));
|
_promise.TrySetException(new ObjectDisposedException(nameof(AsyncSignalWaiter)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cancel()
|
void Cancel()
|
||||||
{
|
{
|
||||||
|
if (_isCompleted)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isCompleted = true;
|
||||||
_promise.TrySetCanceled();
|
_promise.TrySetCanceled();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace MQTTnet.Server
|
namespace MQTTnet.Server
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user