Skip to content

Commit b8f9d82

Browse files
committed
Optimised command loop thread - released v1.1.4 (bench 8431417)
1 parent fc41d02 commit b8f9d82

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

.github/workflows/release-pipeline.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
working-directory: Sapling
2626
id: get_version
2727
run: |
28-
VERSION=1.1.3
28+
VERSION=1.1.4
2929
echo "Application version: $VERSION"
3030
echo "::set-output name=version::$VERSION"
3131

Sapling/Program.cs

+52-32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace Sapling;
77

88
internal class Program
99
{
10+
private static readonly ConcurrentQueue<string> commandQueue = new();
11+
private static readonly ManualResetEventSlim commandAvailable = new(false);
12+
private static bool hasQuit = false;
13+
1014
private static void Main(string[] args)
1115
{
1216
if (args.Length > 0 && args[0] == "--version")
@@ -83,42 +87,14 @@ private static void Main(string[] args)
8387
{
8488
UciEngine engine = new(logWriter);
8589

86-
var commandQueue = new ConcurrentQueue<string>();
87-
88-
var hasQuit = false;
90+
// Start the command reading task
8991
_ = Task.Run(() =>
9092
{
91-
while (true)
92-
{
93-
var command = Console.ReadLine();
94-
if (string.IsNullOrEmpty(command))
95-
{
96-
continue;
97-
}
98-
99-
if (command.Contains("quit"))
100-
{
101-
hasQuit = true;
102-
break;
103-
}
104-
105-
if (command.Contains("stop"))
106-
{
107-
engine.ReceiveCommand(command);
108-
continue;
109-
}
110-
111-
commandQueue.Enqueue(command);
112-
}
93+
ReadCommands(engine);
11394
});
11495

115-
while (!hasQuit)
116-
{
117-
if (commandQueue.TryDequeue(out var command))
118-
{
119-
engine.ReceiveCommand(command);
120-
}
121-
}
96+
// Process commands in the main loop
97+
ProcessCommands(engine);
12298
}
12399
catch (Exception ex)
124100
{
@@ -132,4 +108,48 @@ private static void Main(string[] args)
132108
logWriter.Flush();
133109
}
134110
}
111+
112+
private static void ReadCommands(UciEngine engine)
113+
{
114+
while (true)
115+
{
116+
var command = Console.ReadLine();
117+
if (string.IsNullOrEmpty(command))
118+
{
119+
continue; // Skip empty commands
120+
}
121+
122+
if (command.Contains("quit", StringComparison.OrdinalIgnoreCase))
123+
{
124+
hasQuit = true;
125+
commandQueue.Enqueue(command);
126+
commandAvailable.Set(); // Signal that a command is available
127+
break;
128+
}
129+
130+
if (command.Contains("stop", StringComparison.OrdinalIgnoreCase))
131+
{
132+
// Process the stop command immediately
133+
engine.ReceiveCommand(command);
134+
continue;
135+
}
136+
137+
commandQueue.Enqueue(command);
138+
commandAvailable.Set(); // Signal that a command is available
139+
}
140+
}
141+
142+
private static void ProcessCommands(UciEngine engine)
143+
{
144+
while (!hasQuit)
145+
{
146+
commandAvailable.Wait(); // Wait until a command is available
147+
commandAvailable.Reset(); // Reset the event for the next wait
148+
149+
while (commandQueue.TryDequeue(out var command))
150+
{
151+
engine.ReceiveCommand(command);
152+
}
153+
}
154+
}
135155
}

Sapling/Sapling.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<Nullable>enable</Nullable>
88
<ApplicationIcon>logo.ico</ApplicationIcon>
99
<Title>Sapling</Title>
10-
<AssemblyVersion>1.1.3.0</AssemblyVersion>
11-
<FileVersion>1.1.3.0</FileVersion>
12-
<Version>1.1.3.0</Version>
10+
<AssemblyVersion>1.1.4.0</AssemblyVersion>
11+
<FileVersion>1.1.4.0</FileVersion>
12+
<Version>1.1.4.0</Version>
1313
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1414
</PropertyGroup>
1515

0 commit comments

Comments
 (0)