Browse Source

Switch to ConcurrentQueue for all drivers.

BDisp 8 tháng trước cách đây
mục cha
commit
967cbc205a

+ 5 - 3
Terminal.Gui/ConsoleDrivers/NetDriver/NetMainLoop.cs

@@ -1,5 +1,7 @@
 #nullable enable
 #nullable enable
 
 
+using System.Collections.Concurrent;
+
 namespace Terminal.Gui;
 namespace Terminal.Gui;
 
 
 /// <summary>
 /// <summary>
@@ -18,7 +20,7 @@ internal class NetMainLoop : IMainLoopDriver
     internal readonly ManualResetEventSlim _waitForProbe = new (false);
     internal readonly ManualResetEventSlim _waitForProbe = new (false);
     private readonly CancellationTokenSource _eventReadyTokenSource = new ();
     private readonly CancellationTokenSource _eventReadyTokenSource = new ();
     private readonly CancellationTokenSource _inputHandlerTokenSource = new ();
     private readonly CancellationTokenSource _inputHandlerTokenSource = new ();
-    private readonly Queue<NetEvents.InputResult> _resultQueue = new ();
+    private readonly ConcurrentQueue<NetEvents.InputResult> _resultQueue = new ();
     private MainLoop? _mainLoop;
     private MainLoop? _mainLoop;
     bool IMainLoopDriver._forceRead { get; set; }
     bool IMainLoopDriver._forceRead { get; set; }
     ManualResetEventSlim IMainLoopDriver._waitForInput { get; set; } = new (false);
     ManualResetEventSlim IMainLoopDriver._waitForInput { get; set; } = new (false);
@@ -89,9 +91,9 @@ internal class NetMainLoop : IMainLoopDriver
 
 
     void IMainLoopDriver.Iteration ()
     void IMainLoopDriver.Iteration ()
     {
     {
-        while (_resultQueue.Count > 0)
+        while (_resultQueue.TryDequeue (out NetEvents.InputResult inputRecords))
         {
         {
-            ProcessInput?.Invoke (_resultQueue.Dequeue ());
+            ProcessInput?.Invoke (inputRecords);
         }
         }
     }
     }
 
 

+ 5 - 3
Terminal.Gui/ConsoleDrivers/WindowsDriver/WindowsMainLoop.cs

@@ -1,5 +1,7 @@
 #nullable enable
 #nullable enable
 
 
+using System.Collections.Concurrent;
+
 namespace Terminal.Gui;
 namespace Terminal.Gui;
 
 
 /// <summary>
 /// <summary>
@@ -20,7 +22,7 @@ internal class WindowsMainLoop : IMainLoopDriver
     private readonly ManualResetEventSlim _eventReady = new (false);
     private readonly ManualResetEventSlim _eventReady = new (false);
 
 
     // The records that we keep fetching
     // The records that we keep fetching
-    private readonly Queue<WindowsConsole.InputRecord> _resultQueue = new ();
+    private readonly ConcurrentQueue<WindowsConsole.InputRecord> _resultQueue = new ();
     ManualResetEventSlim IMainLoopDriver._waitForInput { get; set; } = new (false);
     ManualResetEventSlim IMainLoopDriver._waitForInput { get; set; } = new (false);
     private readonly WindowsConsole? _winConsole;
     private readonly WindowsConsole? _winConsole;
     private CancellationTokenSource _eventReadyTokenSource = new ();
     private CancellationTokenSource _eventReadyTokenSource = new ();
@@ -102,9 +104,9 @@ internal class WindowsMainLoop : IMainLoopDriver
 
 
     void IMainLoopDriver.Iteration ()
     void IMainLoopDriver.Iteration ()
     {
     {
-        while (_resultQueue.Count > 0)
+        while (_resultQueue.TryDequeue (out WindowsConsole.InputRecord inputRecords))
         {
         {
-            ((WindowsDriver)_consoleDriver).ProcessInput (_resultQueue.Dequeue ());
+            ((WindowsDriver)_consoleDriver).ProcessInput (inputRecords);
         }
         }
 #if HACK_CHECK_WINCHANGED
 #if HACK_CHECK_WINCHANGED
         if (_winChanged)
         if (_winChanged)