From 24a1e878396086e16c156d465528a75480e521bf Mon Sep 17 00:00:00 2001 From: Sparronator9999 <86388887+Sparronator9999@users.noreply.github.com> Date: Tue, 31 Dec 2024 14:25:44 +1100 Subject: [PATCH] Fix performance regression in IPC library from race condition fix --- YAMDCC.IPC/NamedPipeConnection.cs | 9 +++++++-- YAMDCC.IPC/YAMDCC.IPC.csproj | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/YAMDCC.IPC/NamedPipeConnection.cs b/YAMDCC.IPC/NamedPipeConnection.cs index 0c77d40..b1a1911 100644 --- a/YAMDCC.IPC/NamedPipeConnection.cs +++ b/YAMDCC.IPC/NamedPipeConnection.cs @@ -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; @@ -57,6 +58,7 @@ public class NamedPipeConnection : IDisposable private readonly PipeStreamWrapper _streamWrapper; + private readonly AutoResetEvent _writeSignal = new(false); private readonly BlockingCollection _writeQueue = new(); private bool _notifiedSucceeded; @@ -82,7 +84,7 @@ internal NamedPipeConnection(int id, string name, PipeStream serverStream) /// public bool PushMessage(TWrite message) { - return _writeQueue.TryAdd(message); + return _writeQueue.TryAdd(message) && _writeSignal.Set(); } /// @@ -116,6 +118,7 @@ internal void Close() { _streamWrapper.Close(); _writeQueue.CompleteAdding(); + _writeSignal.Set(); } /// @@ -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(); @@ -193,6 +197,7 @@ private void Dispose(bool disposing) if (disposing) { Close(); + _writeSignal.Dispose(); _writeQueue.Dispose(); } diff --git a/YAMDCC.IPC/YAMDCC.IPC.csproj b/YAMDCC.IPC/YAMDCC.IPC.csproj index 10e2374..8d9348b 100644 --- a/YAMDCC.IPC/YAMDCC.IPC.csproj +++ b/YAMDCC.IPC/YAMDCC.IPC.csproj @@ -12,7 +12,7 @@ win7-x64;win7-x86;win-x64;win-x86 net48 Named Pipe Wrapper library - 2.1.1 + 2.1.2 release