From 7bc94dbe3554d10092034c97462c5afc03353d5a Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Tue, 22 Aug 2023 11:45:01 -0700 Subject: [PATCH] fixes --- Include/pyatomic_msc.h | 12 +++++++++++- Modules/_testcapi/pyatomic.c | 16 +++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Include/pyatomic_msc.h b/Include/pyatomic_msc.h index b6c00acd35cba40..cacfdcb4164d95f 100644 --- a/Include/pyatomic_msc.h +++ b/Include/pyatomic_msc.h @@ -200,7 +200,17 @@ _Py_atomic_exchange_int32(volatile int32_t *address, int32_t value) static inline int64_t _Py_atomic_exchange_int64(volatile int64_t *address, int64_t value) { +#if defined(_M_X64) || defined(_M_ARM64) return (int64_t)_InterlockedExchange64((volatile __int64*)address, (__int64)value); +#else + for (;;) { + int64_t old_value = *address; + int64_t new_value = value; + if (_InterlockedCompareExchange64((volatile __int64*)address, (__int64)new_value, (__int64)old_value)) { + return old_value; + } + } +#endif } static inline intptr_t @@ -240,7 +250,7 @@ _Py_atomic_exchange_uintptr(volatile uintptr_t *address, uintptr_t value) } static inline Py_ssize_t -_Py_atomic_exchange_ssize(volatile Py_ssize_t *address, Py_ssize_t value); +_Py_atomic_exchange_ssize(volatile Py_ssize_t *address, Py_ssize_t value) { #if SIZEOF_SIZE_T == 8 return (Py_ssize_t)_InterlockedExchange64((volatile __int64*)address, (__int64)value); diff --git a/Modules/_testcapi/pyatomic.c b/Modules/_testcapi/pyatomic.c index 85f7fb597cf56c5..2bacd982f1dddce 100644 --- a/Modules/_testcapi/pyatomic.c +++ b/Modules/_testcapi/pyatomic.c @@ -36,21 +36,19 @@ static PyObject * \ test_atomic_add_##suffix(PyObject *self, PyObject *obj) { \ dtype x = 0; \ - dtype y = 1; \ - dtype z = 2; \ - assert(_Py_atomic_add_##suffix(&x, y) == 0); \ + assert(_Py_atomic_add_##suffix(&x, 1) == 0); \ assert("a" && x == 1); \ - assert(_Py_atomic_add_##suffix(&x, z) == 1); \ + assert(_Py_atomic_add_##suffix(&x, 2) == 1); \ assert(x == 3); \ - assert(_Py_atomic_add_##suffix(&x, -z) == 3); \ + assert(_Py_atomic_add_##suffix(&x, -2) == 3); \ assert("b" && x == 1); \ - assert(_Py_atomic_add_##suffix(&x, -y) == 1); \ + assert(_Py_atomic_add_##suffix(&x, -1) == 1); \ assert(x == 0); \ - assert(_Py_atomic_add_##suffix(&x, -y) == 0); \ + assert(_Py_atomic_add_##suffix(&x, -1) == 0); \ assert(x == (dtype)-1); \ - assert(_Py_atomic_add_##suffix(&x, -z) == (dtype)-1); \ + assert(_Py_atomic_add_##suffix(&x, -2) == (dtype)-1); \ assert(x == (dtype)-3); \ - assert(_Py_atomic_add_##suffix(&x, z) == (dtype)-3); \ + assert(_Py_atomic_add_##suffix(&x, 2) == (dtype)-3); \ assert(x == (dtype)-1); \ Py_RETURN_NONE; \ }