fix: Avoid potential race bug in c# OrderPool

This commit is contained in:
John Colvin committed 2026-01-11 11:20:40 -05:00
1 parent 2aa25d4085
commit cbddb3ae28
3 files changed
+20 -6

No files matched your search

@@ -95,7 +95,7 @@ namespace bts.BlueOrb{
new_state.addRow(r);
}
}
DataPacket dp = null;
DataPacket? dp;
if (myLastValue == null) {
dp = new DataPacket(new_state, new_state, new BlueTable(_bbo_schema));
} else {
@@ -168,7 +168,9 @@ namespace bts.BlueOrb {
}
private Source _sourse = Source.SQL; // get from config
private readonly string _location = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\BlueOrb_Data\\":Path.Combine(Environment.GetEnvironmentVariable("HOME") , "BlueOrb_Data");
readonly string _location = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "C:\\BlueOrb_Data\\"
: Path.Combine(Environment.GetEnvironmentVariable("HOME") ?? string.Empty, "BlueOrb_Data");
private readonly string _db_name = "stream_data.db";
// the symbol is used ot retreive the correct table, the data flows as-is from the table
// with some name / type changes
+16 -4
View File
@@ -32,10 +32,22 @@ class OrderPool {
}
public void Replenish() {
int nUsed = Math.Min(cursor, orderPool.Length);
for (int i = 0; i < nUsed; i++) {
orderPool[i] = factory();
if (this.cursor == 0) {
return;
}
Volatile.Write(ref cursor, 0);
int cursor;
int nUsed;
int nReplenished = 0;
while (nReplenished < orderPool.Length) {
cursor = Volatile.Read(ref this.cursor);
nUsed = Math.Min(cursor, orderPool.Length);
for (; nReplenished < nUsed; nReplenished++) {
orderPool[nReplenished] = factory();
}
if (Interlocked.CompareExchange(ref this.cursor, 0, cursor) == cursor) {
return;
}
}
Volatile.Write(ref this.cursor, 0);
}
}