From be34a7ad38b649dfb650f469b2a3453a58c25ef8 Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Fri, 2 Sep 2022 18:21:34 -0400 Subject: [PATCH] refactor: replace Mutex with SemaphoreSlim Mutex uses system mutexes and SemaphoreSlim does not. --- ChatTwo/Configuration.cs | 10 +++++----- ChatTwo/Ui/ChatLog.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ChatTwo/Configuration.cs b/ChatTwo/Configuration.cs index 045237c..3228cb9 100755 --- a/ChatTwo/Configuration.cs +++ b/ChatTwo/Configuration.cs @@ -176,7 +176,7 @@ internal class Tab { public uint Unread; [NonSerialized] - public Mutex MessagesMutex = new(); + public SemaphoreSlim MessagesMutex = new(1, 1); [NonSerialized] public List Messages = new(); @@ -195,13 +195,13 @@ internal class Tab { } internal void AddMessage(Message message, bool unread = true) { - this.MessagesMutex.WaitOne(); + this.MessagesMutex.Wait(); this.Messages.Add(message); while (this.Messages.Count > Store.MessagesLimit) { this.Messages.RemoveAt(0); } - this.MessagesMutex.ReleaseMutex(); + this.MessagesMutex.Release(); if (unread) { this.Unread += 1; @@ -209,9 +209,9 @@ internal class Tab { } internal void Clear() { - this.MessagesMutex.WaitOne(); + this.MessagesMutex.Wait(); this.Messages.Clear(); - this.MessagesMutex.ReleaseMutex(); + this.MessagesMutex.Release(); } internal Tab Clone() { diff --git a/ChatTwo/Ui/ChatLog.cs b/ChatTwo/Ui/ChatLog.cs index acb64ef..54a3425 100755 --- a/ChatTwo/Ui/ChatLog.cs +++ b/ChatTwo/Ui/ChatLog.cs @@ -707,7 +707,7 @@ internal sealed class ChatLog : IUiComponent { } try { - tab.MessagesMutex.WaitOne(); + tab.MessagesMutex.Wait(); var reset = false; if (this._lastResize.IsRunning && this._lastResize.Elapsed.TotalSeconds > 0.25) { @@ -841,7 +841,7 @@ internal sealed class ChatLog : IUiComponent { lastPos = ImGui.GetCursorPosY(); } } finally { - tab.MessagesMutex.ReleaseMutex(); + tab.MessagesMutex.Release(); ImGui.PopStyleVar(this.Ui.Plugin.Config.PrettierTimestamps && this.Ui.Plugin.Config.MoreCompactPretty ? 2 : 1); }