Skip to content

Commit ad9eb05

Browse files
std: sysatomics: fix use of atomicCompareExchangeN for MSVC
InterlockedCompareExchange64 (winnt.h) is used instead of gcc atomics when compiling with MSVC on Windows, but the function signatures are InterlockedCompareExchange64(ptr int64, int64, int64) and InterlockedCompareExchange32(ptr int32, int32, int32) as opposed to (ptr T, ptr T, T) for __atomic_compare_exchange_n. Passing a pointer to the expected value instead of the value itself causes the comparison to unconditionally fail, with stalls in threaded code using atomic comparisons. Fix the function signature for MSVC. Signed-off-by: Ryan Walklin <[email protected]>
1 parent 91febf1 commit ad9eb05

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/std/sysatomics.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ elif someVcc:
230230
proc atomicCompareExchangeN*[T: ptr](p, expected: ptr T, desired: T,
231231
weak: bool, success_memmodel: AtomMemModel, failure_memmodel: AtomMemModel): bool =
232232
when sizeof(T) == 8:
233-
interlockedCompareExchange64(p, cast[int64](desired), cast[int64](expected)) ==
234-
cast[int64](expected)
233+
interlockedCompareExchange64(p, cast[int64](desired), cast[int64](expected[])) ==
234+
cast[int64](expected[])
235235
elif sizeof(T) == 4:
236-
interlockedCompareExchange32(p, cast[int32](desired), cast[int32](expected)) ==
237-
cast[int32](expected)
236+
interlockedCompareExchange32(p, cast[int32](desired), cast[int32](expected[])) ==
237+
cast[int32](expected[])
238238

239239
proc atomicExchangeN*[T: ptr](p: ptr T, val: T, mem: AtomMemModel): T =
240240
when sizeof(T) == 8:

0 commit comments

Comments
 (0)