From f5a6322a7261b2ed941a7a5654f98ae4408cd4ba Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Fri, 30 Oct 2026 07:52:02 +0100 Subject: [PATCH] Fix AtomicInt.compareExchange reporting success for failed exchanges If the value changed between the initial load and compare_exchange_strong, the exchange failed and wrote the current value to `expected`, but the function returned the stale `original`, which equals the caller's expected value. Callers therefore saw a successful exchange that never happened. Co-Authored-By: Claude Opus 5.5 --- src/hx/atomic/AtomicInt.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/hx/atomic/AtomicInt.cpp b/src/hx/atomic/AtomicInt.cpp index 30bde4ecf..dfe4dfb4e 100644 --- a/src/hx/atomic/AtomicInt.cpp +++ b/src/hx/atomic/AtomicInt.cpp @@ -74,18 +74,9 @@ int hx::atomic::AtomicInt_obj::compareExchange(int expected, int replacement) { hx::AutoGCFreeZone _; - int original{ impl->atomic.load() }; - while (original == expected) - { - if (impl->atomic.compare_exchange_strong(expected, replacement)) - { - return original; - } - else - { - continue; - } - } + // On failure, compare_exchange_strong stores the current value in `original`. + int original{ expected }; + impl->atomic.compare_exchange_strong(original, replacement); return original; }