diff --git a/Include/cpython/pyatomic.h b/Include/cpython/pyatomic.h index bf04b4a63d6f49..bbc0b4c2f6e124 100644 --- a/Include/cpython/pyatomic.h +++ b/Include/cpython/pyatomic.h @@ -1,6 +1,3 @@ -#ifndef Py_ATOMIC_H -#define Py_ATOMIC_H - // This header provides cross-platform low-level atomic operations // similar to C11 atomics. // @@ -10,17 +7,22 @@ // The "_relaxed" suffix for load and store operations indicates the "relaxed" // memory order. They don't provide synchronization, but (roughly speaking) // guarantee somewhat sane behavior for races instead of undefined behavior. -// In practice, they correspond to "normal" hardware load and store instructions, -// so they are almost as inexpensive as plain loads and stores in C. +// In practice, they correspond to "normal" hardware load and store +// instructions, so they are almost as inexpensive as plain loads and stores +// in C. // // Note that atomic read-modify-write operations like _Py_atomic_add_* return // the previous value of the atomic variable, not the new value. // -// See https://en.cppreference.com/w/c/atomic for more information on C11 atomics. +// See https://en.cppreference.com/w/c/atomic for more information on C11 +// atomics. // See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2055r0.pdf // "A Relaxed Guide to memory_order_relaxed" for discussion of and common usage // or relaxed atomics. +#ifndef Py_ATOMIC_H +#define Py_ATOMIC_H + // Atomically adds `value` to `address` and returns the previous value static inline int _Py_atomic_add_int(int *address, int value); diff --git a/Include/cpython/pyatomic_gcc.h b/Include/cpython/pyatomic_gcc.h index cdf3497fd36f83..aff732ecfe61fd 100644 --- a/Include/cpython/pyatomic_gcc.h +++ b/Include/cpython/pyatomic_gcc.h @@ -1,12 +1,12 @@ -#ifndef Py_ATOMIC_GCC_H -# error "this header file must not be included directly" -#endif - // This is the implementation of Python atomic operations using GCC's built-in // functions that match the C+11 memory model. This implementation is preferred // for GCC compatible compilers, such as Clang. These functions are available in // GCC 4.8+ without needing to compile with --std=c11 or --std=gnu11. +#ifndef Py_ATOMIC_GCC_H +# error "this header file must not be included directly" +#endif + static inline int _Py_atomic_add_int(int *address, int value) { diff --git a/Include/cpython/pyatomic_msc.h b/Include/cpython/pyatomic_msc.h index 291952dbe1c3a9..6da4903717ad93 100644 --- a/Include/cpython/pyatomic_msc.h +++ b/Include/cpython/pyatomic_msc.h @@ -1,10 +1,10 @@ +// This is the implementation of Python atomic operations for MSVC if the +// compiler does not support C11 or C++11 atomics. + #ifndef Py_ATOMIC_MSC_H # error "this header file must not be included directly" #endif -// This is the implementation of Python atomic operations for MSVC if the -// compiler does not support C11 or C++11 atomics. - #include @@ -85,7 +85,7 @@ _Py_atomic_add_uint32(uint32_t *address, uint32_t value) static inline uint64_t _Py_atomic_add_uint64(uint64_t *address, uint64_t value) { - return (uint64_t)_Py_atomic_add_int64((volatile int64_t*)address, (int64_t)value); + return (uint64_t)_Py_atomic_add_int64((int64_t*)address, (int64_t)value); } static inline uintptr_t @@ -270,7 +270,7 @@ _Py_atomic_exchange_uint32(uint32_t *address, uint32_t value) static inline uint64_t _Py_atomic_exchange_uint64(uint64_t *address, uint64_t value) { - return (uint64_t)_Py_atomic_exchange_int64((volatile __int64*)address, (__int64)value); + return (uint64_t)_Py_atomic_exchange_int64((int64_t *)address, (int64_t)value); } static inline uintptr_t @@ -693,7 +693,7 @@ _Py_atomic_store_uint32(uint32_t *address, uint32_t value) static inline void _Py_atomic_store_uint64(uint64_t *address, uint64_t value) { - _Py_atomic_exchange_int64((volatile __int64*)address, (__int64)value); + _Py_atomic_exchange_int64((int64_t *)address, (int64_t)value); } static inline void diff --git a/Include/cpython/pyatomic_std.h b/Include/cpython/pyatomic_std.h index 253cbaa3887f92..ae698057795829 100644 --- a/Include/cpython/pyatomic_std.h +++ b/Include/cpython/pyatomic_std.h @@ -1,11 +1,11 @@ -#ifndef Py_ATOMIC_STD_H -# error "this header file must not be included directly" -#endif - // This is the implementation of Python atomic operations using C++11 or C11 // atomics. Note that the pyatomic_gcc.h implementation is preferred for GCC // compatible compilers, even if they support C++11 atomics. +#ifndef Py_ATOMIC_STD_H +# error "this header file must not be included directly" +#endif + #ifdef __cplusplus extern "C++" { #include