Skip to content

Commit

Permalink
A blocking collection is already thread safe, AutoResetEvent should n…
Browse files Browse the repository at this point in the history
…ot be required here.
  • Loading branch information
CartBlanche committed Jul 9, 2024
1 parent 21f3e1c commit 205ed23
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private async Task CommandManager()
}
else
{
// If no commands are available, delay a bit to avoid busy waiting
// If no commands to dequeue, delay a bit to avoid busy waiting
await Task.Delay(100);
}
}
Expand Down
11 changes: 6 additions & 5 deletions Source/v2/Meadow.Hcom/Debugging/DebuggingServer.ActiveClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ private class ActiveClient : IDisposable
private readonly ILogger? _logger;
private bool _disposed;
private readonly BlockingCollection<byte[]> _debuggerMessages = new();
private readonly AutoResetEvent _vsDebugDataReady = new(false);
private readonly object _disposeLock = new();

internal ActiveClient(IMeadowConnection connection, TcpClient tcpClient, ILogger? logger, CancellationToken? cancellationToken)
Expand Down Expand Up @@ -47,7 +46,6 @@ private void MeadowConnection_DebuggerMessageReceived(object sender, byte[] e)
if (!_disposed)
{
_debuggerMessages.Add(e);
_vsDebugDataReady.Set();
}
}
}
Expand Down Expand Up @@ -131,9 +129,7 @@ private async Task SendToVisualStudio()
{
if (_networkStream != null && _networkStream.CanWrite)
{
_vsDebugDataReady.WaitOne(1000);

while (_debuggerMessages.TryTake(out var byteData, Timeout.Infinite, _cts.Token))
if (_debuggerMessages.TryTake(out var byteData, Timeout.Infinite, _cts.Token))
{
_logger?.LogTrace("Received {count} bytes from Meadow, will forward to VS", byteData.Length);
if (!_tcpClient.Connected)
Expand All @@ -145,6 +141,11 @@ private async Task SendToVisualStudio()
await _networkStream.WriteAsync(byteData, 0, byteData.Length, _cts.Token);
_logger?.LogTrace("Forwarded {count} bytes to VS", byteData.Length);
}
else
{
// If no _debuggerMessages to Take, delay a bit to avoid busy waiting
await Task.Delay(100);
}
}
else
{
Expand Down

0 comments on commit 205ed23

Please sign in to comment.