Skip to content

Commit

Permalink
Optimised command loop thread - released v1.1.4 (bench 8431417)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmoth committed Sep 29, 2024
1 parent fc41d02 commit b8f9d82
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
84 changes: 52 additions & 32 deletions Sapling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace Sapling;

internal class Program
{
private static readonly ConcurrentQueue<string> 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")
Expand Down Expand Up @@ -83,42 +87,14 @@ private static void Main(string[] args)
{
UciEngine engine = new(logWriter);

var commandQueue = new ConcurrentQueue<string>();

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)
{
Expand All @@ -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);
}
}
}
}
6 changes: 3 additions & 3 deletions Sapling/Sapling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<Nullable>enable</Nullable>
<ApplicationIcon>logo.ico</ApplicationIcon>
<Title>Sapling</Title>
<AssemblyVersion>1.1.3.0</AssemblyVersion>
<FileVersion>1.1.3.0</FileVersion>
<Version>1.1.3.0</Version>
<AssemblyVersion>1.1.4.0</AssemblyVersion>
<FileVersion>1.1.4.0</FileVersion>
<Version>1.1.4.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down

0 comments on commit b8f9d82

Please sign in to comment.