Skip to content

Commit

Permalink
Fix performance regression in IPC library from race condition fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparronator9999 committed Dec 31, 2024
1 parent 8b6d26b commit 24a1e87
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions YAMDCC.IPC/NamedPipeConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Threading;
using YAMDCC.IPC.IO;
using YAMDCC.IPC.Threading;

Expand Down Expand Up @@ -57,6 +58,7 @@ public class NamedPipeConnection<TRead, TWrite> : IDisposable

private readonly PipeStreamWrapper<TRead, TWrite> _streamWrapper;

private readonly AutoResetEvent _writeSignal = new(false);
private readonly BlockingCollection<TWrite> _writeQueue = new();

private bool _notifiedSucceeded;
Expand All @@ -82,7 +84,7 @@ internal NamedPipeConnection(int id, string name, PipeStream serverStream)
/// </param>
public bool PushMessage(TWrite message)
{
return _writeQueue.TryAdd(message);
return _writeQueue.TryAdd(message) && _writeSignal.Set();
}

/// <summary>
Expand Down Expand Up @@ -116,6 +118,7 @@ internal void Close()
{
_streamWrapper.Close();
_writeQueue.CompleteAdding();
_writeSignal.Set();
}

/// <summary>
Expand Down Expand Up @@ -169,7 +172,8 @@ private void WritePipe()
{
while (IsConnected && _streamWrapper.CanWrite)
{
if (_writeQueue.TryTake(out TWrite obj))
_writeSignal.WaitOne();
while (_writeQueue.TryTake(out TWrite obj) || _writeQueue.Count > 0)
{
_streamWrapper.WriteObject(obj);
_streamWrapper.WaitForPipeDrain();
Expand All @@ -193,6 +197,7 @@ private void Dispose(bool disposing)
if (disposing)
{
Close();
_writeSignal.Dispose();
_writeQueue.Dispose();
}

Expand Down
2 changes: 1 addition & 1 deletion YAMDCC.IPC/YAMDCC.IPC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RuntimeIdentifiers>win7-x64;win7-x86;win-x64;win-x86</RuntimeIdentifiers>
<TargetFramework>net48</TargetFramework>
<Title>Named Pipe Wrapper library</Title>
<VersionPrefix>2.1.1</VersionPrefix>
<VersionPrefix>2.1.2</VersionPrefix>
<VersionSuffix>release</VersionSuffix>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down

0 comments on commit 24a1e87

Please sign in to comment.