Skip to content

Commit

Permalink
Move code documentation around.
Browse files Browse the repository at this point in the history
Fix volatile warning in MSVC.
  • Loading branch information
colesbury committed Aug 23, 2023
1 parent 4daf1a2 commit f932c77
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
14 changes: 8 additions & 6 deletions Include/cpython/pyatomic.h
Original file line number Diff line number Diff line change
@@ -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.
//
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions Include/cpython/pyatomic_gcc.h
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down
12 changes: 6 additions & 6 deletions Include/cpython/pyatomic_msc.h
Original file line number Diff line number Diff line change
@@ -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 <intrin.h>


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Include/cpython/pyatomic_std.h
Original file line number Diff line number Diff line change
@@ -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 <atomic>
Expand Down

0 comments on commit f932c77

Please sign in to comment.