refactor: Make AtomicGate actually atomic (lockless)

This commit is contained in:
John Colvin committed 2025-12-05 15:56:23 +00:00
1 parent 2cdeab394b
commit 02356cf97c
1 file changed
+4 -12
+4 -12
View File
@@ -16,7 +16,6 @@ public class AtomicGate {
readonly string name;
readonly int id = Interlocked.Increment(ref idCounter);
readonly object syncRoot = new();
int versionIn = 0;
int versionOut = 0;
@@ -55,13 +54,8 @@ public class AtomicGate {
return res;
bool Impl() {
lock (syncRoot) {
if (versionIn == versionOut) {
versionIn++;
return true;
}
}
return false;
int versOut = versionOut;
return Interlocked.CompareExchange(ref versionIn, versOut + 1, versOut) == versOut;
}
}
@@ -140,10 +134,8 @@ public class AtomicGate {
Task.Run(BeginTransaction);
public void Exit() {
lock (syncRoot) {
Log.assert(versionIn == versionOut + 1);
versionOut = versionIn;
}
int versIn = Volatile.Read(ref versionIn);
Log.assert(Interlocked.CompareExchange(ref versionOut, versIn, versIn - 1) == versIn - 1);
}
void sleep(int sleepMillis, int? version = null) {