Skip to content

Commit de866ca

Browse files
Copilotithewei
andcommitted
fix: use InterlockedExchangeAdd for Windows atomic ops to return old value
Replace InterlockedAdd/InterlockedIncrement/InterlockedDecrement with InterlockedExchangeAdd which returns the value before the operation, matching the behavior of __sync_fetch_and_add (GCC) and atomic_fetch_add (C11). Also fix ATOMIC_SUB macro parenthesization: -(n) instead of -n. Co-authored-by: ithewei <26049660+ithewei@users.noreply.github.com>
1 parent dec4b90 commit de866ca

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

base/hatomic.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ static inline bool atomic_flag_test_and_set(atomic_flag* p) {
5252
return InterlockedCompareExchange(&p->_Value, 1, 0);
5353
}
5454

55-
#define ATOMIC_ADD InterlockedAdd
56-
#define ATOMIC_SUB(p, n) InterlockedAdd(p, -n)
57-
#define ATOMIC_INC InterlockedIncrement
58-
#define ATOMIC_DEC InterlockedDecrement
55+
#define ATOMIC_ADD InterlockedExchangeAdd
56+
#define ATOMIC_SUB(p, n) InterlockedExchangeAdd(p, -(n))
57+
#define ATOMIC_INC(p) InterlockedExchangeAdd(p, 1)
58+
#define ATOMIC_DEC(p) InterlockedExchangeAdd(p, -1)
5959

6060
#elif __GNUC_PREREQ(4, 1)
6161

0 commit comments

Comments
 (0)