From b8f9d8202976b47d5ed632a26ddad2ed377dd584 Mon Sep 17 00:00:00 2001 From: timmoth Date: Sun, 29 Sep 2024 20:23:10 +0100 Subject: [PATCH] Optimised command loop thread - released v1.1.4 (bench 8431417) --- .github/workflows/release-pipeline.yaml | 2 +- Sapling/Program.cs | 84 +++++++++++++++---------- Sapling/Sapling.csproj | 6 +- 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/.github/workflows/release-pipeline.yaml b/.github/workflows/release-pipeline.yaml index 604233c..cb8a5f5 100644 --- a/.github/workflows/release-pipeline.yaml +++ b/.github/workflows/release-pipeline.yaml @@ -25,7 +25,7 @@ jobs: working-directory: Sapling id: get_version run: | - VERSION=1.1.3 + VERSION=1.1.4 echo "Application version: $VERSION" echo "::set-output name=version::$VERSION" diff --git a/Sapling/Program.cs b/Sapling/Program.cs index 5580d64..4e63d2a 100644 --- a/Sapling/Program.cs +++ b/Sapling/Program.cs @@ -7,6 +7,10 @@ namespace Sapling; internal class Program { + private static readonly ConcurrentQueue commandQueue = new(); + private static readonly ManualResetEventSlim commandAvailable = new(false); + private static bool hasQuit = false; + private static void Main(string[] args) { if (args.Length > 0 && args[0] == "--version") @@ -83,42 +87,14 @@ private static void Main(string[] args) { UciEngine engine = new(logWriter); - var commandQueue = new ConcurrentQueue(); - - var hasQuit = false; + // Start the command reading task _ = Task.Run(() => { - while (true) - { - var command = Console.ReadLine(); - if (string.IsNullOrEmpty(command)) - { - continue; - } - - if (command.Contains("quit")) - { - hasQuit = true; - break; - } - - if (command.Contains("stop")) - { - engine.ReceiveCommand(command); - continue; - } - - commandQueue.Enqueue(command); - } + ReadCommands(engine); }); - while (!hasQuit) - { - if (commandQueue.TryDequeue(out var command)) - { - engine.ReceiveCommand(command); - } - } + // Process commands in the main loop + ProcessCommands(engine); } catch (Exception ex) { @@ -132,4 +108,48 @@ private static void Main(string[] args) logWriter.Flush(); } } + + private static void ReadCommands(UciEngine engine) + { + while (true) + { + var command = Console.ReadLine(); + if (string.IsNullOrEmpty(command)) + { + continue; // Skip empty commands + } + + if (command.Contains("quit", StringComparison.OrdinalIgnoreCase)) + { + hasQuit = true; + commandQueue.Enqueue(command); + commandAvailable.Set(); // Signal that a command is available + break; + } + + if (command.Contains("stop", StringComparison.OrdinalIgnoreCase)) + { + // Process the stop command immediately + engine.ReceiveCommand(command); + continue; + } + + commandQueue.Enqueue(command); + commandAvailable.Set(); // Signal that a command is available + } + } + + private static void ProcessCommands(UciEngine engine) + { + while (!hasQuit) + { + commandAvailable.Wait(); // Wait until a command is available + commandAvailable.Reset(); // Reset the event for the next wait + + while (commandQueue.TryDequeue(out var command)) + { + engine.ReceiveCommand(command); + } + } + } } \ No newline at end of file diff --git a/Sapling/Sapling.csproj b/Sapling/Sapling.csproj index b91b0e4..f29ffe2 100644 --- a/Sapling/Sapling.csproj +++ b/Sapling/Sapling.csproj @@ -7,9 +7,9 @@ enable logo.ico Sapling - 1.1.3.0 - 1.1.3.0 - 1.1.3.0 + 1.1.4.0 + 1.1.4.0 + 1.1.4.0 true