| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| #ifndef INSIDE_ATOMICS_H |
| # error "should be included via atomics.h" |
| #endif |
|
|
| |
| |
| |
| |
| #if !defined(pg_read_barrier_impl) |
| # define pg_read_barrier_impl pg_memory_barrier_impl |
| #endif |
| #if !defined(pg_write_barrier_impl) |
| # define pg_write_barrier_impl pg_memory_barrier_impl |
| #endif |
|
|
| #ifndef PG_HAVE_SPIN_DELAY |
| #define PG_HAVE_SPIN_DELAY |
| #define pg_spin_delay_impl() ((void)0) |
| #endif |
|
|
|
|
| |
| #if !defined(PG_HAVE_ATOMIC_FLAG_SUPPORT) && defined(PG_HAVE_ATOMIC_U32_SUPPORT) |
| #define PG_HAVE_ATOMIC_FLAG_SUPPORT |
| typedef pg_atomic_uint32 pg_atomic_flag; |
| #endif |
|
|
| #ifndef PG_HAVE_ATOMIC_READ_U32 |
| #define PG_HAVE_ATOMIC_READ_U32 |
| static inline uint32 |
| pg_atomic_read_u32_impl(volatile pg_atomic_uint32 *ptr) |
| { |
| return ptr->value; |
| } |
| #endif |
|
|
| #ifndef PG_HAVE_ATOMIC_WRITE_U32 |
| #define PG_HAVE_ATOMIC_WRITE_U32 |
| static inline void |
| pg_atomic_write_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val) |
| { |
| ptr->value = val; |
| } |
| #endif |
|
|
| #ifndef PG_HAVE_ATOMIC_UNLOCKED_WRITE_U32 |
| #define PG_HAVE_ATOMIC_UNLOCKED_WRITE_U32 |
| static inline void |
| pg_atomic_unlocked_write_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val) |
| { |
| ptr->value = val; |
| } |
| #endif |
|
|
| |
| |
| |
| #if !defined(PG_HAVE_ATOMIC_TEST_SET_FLAG) && defined(PG_HAVE_ATOMIC_EXCHANGE_U32) |
|
|
| #define PG_HAVE_ATOMIC_INIT_FLAG |
| static inline void |
| pg_atomic_init_flag_impl(volatile pg_atomic_flag *ptr) |
| { |
| pg_atomic_write_u32_impl(ptr, 0); |
| } |
|
|
| #define PG_HAVE_ATOMIC_TEST_SET_FLAG |
| static inline bool |
| pg_atomic_test_set_flag_impl(volatile pg_atomic_flag *ptr) |
| { |
| return pg_atomic_exchange_u32_impl(ptr, 1) == 0; |
| } |
|
|
| #define PG_HAVE_ATOMIC_UNLOCKED_TEST_FLAG |
| static inline bool |
| pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr) |
| { |
| return pg_atomic_read_u32_impl(ptr) == 0; |
| } |
|
|
|
|
| #define PG_HAVE_ATOMIC_CLEAR_FLAG |
| static inline void |
| pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr) |
| { |
| |
| pg_memory_barrier_impl(); |
| pg_atomic_write_u32_impl(ptr, 0); |
| } |
|
|
| |
| |
| |
| |
| #elif !defined(PG_HAVE_ATOMIC_TEST_SET_FLAG) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32) |
|
|
| #define PG_HAVE_ATOMIC_INIT_FLAG |
| static inline void |
| pg_atomic_init_flag_impl(volatile pg_atomic_flag *ptr) |
| { |
| pg_atomic_write_u32_impl(ptr, 0); |
| } |
|
|
| #define PG_HAVE_ATOMIC_TEST_SET_FLAG |
| static inline bool |
| pg_atomic_test_set_flag_impl(volatile pg_atomic_flag *ptr) |
| { |
| uint32 value = 0; |
| return pg_atomic_compare_exchange_u32_impl(ptr, &value, 1); |
| } |
|
|
| #define PG_HAVE_ATOMIC_UNLOCKED_TEST_FLAG |
| static inline bool |
| pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr) |
| { |
| return pg_atomic_read_u32_impl(ptr) == 0; |
| } |
|
|
| #define PG_HAVE_ATOMIC_CLEAR_FLAG |
| static inline void |
| pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr) |
| { |
| |
| |
| |
| |
| |
| #ifndef PG_HAVE_MEMORY_BARRIER_EMULATION |
| |
| pg_memory_barrier_impl(); |
| pg_atomic_write_u32_impl(ptr, 0); |
| #else |
| uint32 value = 1; |
| pg_atomic_compare_exchange_u32_impl(ptr, &value, 0); |
| #endif |
| } |
|
|
| #elif !defined(PG_HAVE_ATOMIC_TEST_SET_FLAG) |
| # error "No pg_atomic_test_and_set provided" |
| #endif |
|
|
|
|
| #ifndef PG_HAVE_ATOMIC_INIT_U32 |
| #define PG_HAVE_ATOMIC_INIT_U32 |
| static inline void |
| pg_atomic_init_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val_) |
| { |
| ptr->value = val_; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_EXCHANGE_U32) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32) |
| #define PG_HAVE_ATOMIC_EXCHANGE_U32 |
| static inline uint32 |
| pg_atomic_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 xchg_) |
| { |
| uint32 old; |
| old = ptr->value; |
| while (!pg_atomic_compare_exchange_u32_impl(ptr, &old, xchg_)) |
| ; |
| return old; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_FETCH_ADD_U32) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32) |
| #define PG_HAVE_ATOMIC_FETCH_ADD_U32 |
| static inline uint32 |
| pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_) |
| { |
| uint32 old; |
| old = ptr->value; |
| while (!pg_atomic_compare_exchange_u32_impl(ptr, &old, old + add_)) |
| ; |
| return old; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_FETCH_SUB_U32) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32) |
| #define PG_HAVE_ATOMIC_FETCH_SUB_U32 |
| static inline uint32 |
| pg_atomic_fetch_sub_u32_impl(volatile pg_atomic_uint32 *ptr, int32 sub_) |
| { |
| return pg_atomic_fetch_add_u32_impl(ptr, -sub_); |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_FETCH_AND_U32) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32) |
| #define PG_HAVE_ATOMIC_FETCH_AND_U32 |
| static inline uint32 |
| pg_atomic_fetch_and_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 and_) |
| { |
| uint32 old; |
| old = ptr->value; |
| while (!pg_atomic_compare_exchange_u32_impl(ptr, &old, old & and_)) |
| ; |
| return old; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_FETCH_OR_U32) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32) |
| #define PG_HAVE_ATOMIC_FETCH_OR_U32 |
| static inline uint32 |
| pg_atomic_fetch_or_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 or_) |
| { |
| uint32 old; |
| old = ptr->value; |
| while (!pg_atomic_compare_exchange_u32_impl(ptr, &old, old | or_)) |
| ; |
| return old; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_ADD_FETCH_U32) && defined(PG_HAVE_ATOMIC_FETCH_ADD_U32) |
| #define PG_HAVE_ATOMIC_ADD_FETCH_U32 |
| static inline uint32 |
| pg_atomic_add_fetch_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_) |
| { |
| return pg_atomic_fetch_add_u32_impl(ptr, add_) + add_; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_SUB_FETCH_U32) && defined(PG_HAVE_ATOMIC_FETCH_SUB_U32) |
| #define PG_HAVE_ATOMIC_SUB_FETCH_U32 |
| static inline uint32 |
| pg_atomic_sub_fetch_u32_impl(volatile pg_atomic_uint32 *ptr, int32 sub_) |
| { |
| return pg_atomic_fetch_sub_u32_impl(ptr, sub_) - sub_; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_EXCHANGE_U64) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64) |
| #define PG_HAVE_ATOMIC_EXCHANGE_U64 |
| static inline uint64 |
| pg_atomic_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 xchg_) |
| { |
| uint64 old; |
| old = ptr->value; |
| while (!pg_atomic_compare_exchange_u64_impl(ptr, &old, xchg_)) |
| ; |
| return old; |
| } |
| #endif |
|
|
| #ifndef PG_HAVE_ATOMIC_WRITE_U64 |
| #define PG_HAVE_ATOMIC_WRITE_U64 |
|
|
| #if defined(PG_HAVE_8BYTE_SINGLE_COPY_ATOMICITY) && \ |
| !defined(PG_HAVE_ATOMIC_U64_SIMULATION) |
|
|
| static inline void |
| pg_atomic_write_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 val) |
| { |
| |
| |
| |
| |
| |
| AssertPointerAlignment(ptr, 8); |
| ptr->value = val; |
| } |
|
|
| #else |
|
|
| static inline void |
| pg_atomic_write_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 val) |
| { |
| |
| |
| |
| |
| pg_atomic_exchange_u64_impl(ptr, val); |
| } |
|
|
| #endif |
| #endif |
|
|
| #ifndef PG_HAVE_ATOMIC_READ_U64 |
| #define PG_HAVE_ATOMIC_READ_U64 |
|
|
| #if defined(PG_HAVE_8BYTE_SINGLE_COPY_ATOMICITY) && \ |
| !defined(PG_HAVE_ATOMIC_U64_SIMULATION) |
|
|
| static inline uint64 |
| pg_atomic_read_u64_impl(volatile pg_atomic_uint64 *ptr) |
| { |
| |
| |
| |
| AssertPointerAlignment(ptr, 8); |
| return ptr->value; |
| } |
|
|
| #else |
|
|
| static inline uint64 |
| pg_atomic_read_u64_impl(volatile pg_atomic_uint64 *ptr) |
| { |
| uint64 old = 0; |
|
|
| |
| |
| |
| |
| |
| |
| pg_atomic_compare_exchange_u64_impl(ptr, &old, 0); |
|
|
| return old; |
| } |
| #endif |
| #endif |
|
|
| #ifndef PG_HAVE_ATOMIC_INIT_U64 |
| #define PG_HAVE_ATOMIC_INIT_U64 |
| static inline void |
| pg_atomic_init_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 val_) |
| { |
| ptr->value = val_; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_FETCH_ADD_U64) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64) |
| #define PG_HAVE_ATOMIC_FETCH_ADD_U64 |
| static inline uint64 |
| pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_) |
| { |
| uint64 old; |
| old = ptr->value; |
| while (!pg_atomic_compare_exchange_u64_impl(ptr, &old, old + add_)) |
| ; |
| return old; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_FETCH_SUB_U64) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64) |
| #define PG_HAVE_ATOMIC_FETCH_SUB_U64 |
| static inline uint64 |
| pg_atomic_fetch_sub_u64_impl(volatile pg_atomic_uint64 *ptr, int64 sub_) |
| { |
| return pg_atomic_fetch_add_u64_impl(ptr, -sub_); |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_FETCH_AND_U64) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64) |
| #define PG_HAVE_ATOMIC_FETCH_AND_U64 |
| static inline uint64 |
| pg_atomic_fetch_and_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 and_) |
| { |
| uint64 old; |
| old = ptr->value; |
| while (!pg_atomic_compare_exchange_u64_impl(ptr, &old, old & and_)) |
| ; |
| return old; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_FETCH_OR_U64) && defined(PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64) |
| #define PG_HAVE_ATOMIC_FETCH_OR_U64 |
| static inline uint64 |
| pg_atomic_fetch_or_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 or_) |
| { |
| uint64 old; |
| old = ptr->value; |
| while (!pg_atomic_compare_exchange_u64_impl(ptr, &old, old | or_)) |
| ; |
| return old; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_ADD_FETCH_U64) && defined(PG_HAVE_ATOMIC_FETCH_ADD_U64) |
| #define PG_HAVE_ATOMIC_ADD_FETCH_U64 |
| static inline uint64 |
| pg_atomic_add_fetch_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_) |
| { |
| return pg_atomic_fetch_add_u64_impl(ptr, add_) + add_; |
| } |
| #endif |
|
|
| #if !defined(PG_HAVE_ATOMIC_SUB_FETCH_U64) && defined(PG_HAVE_ATOMIC_FETCH_SUB_U64) |
| #define PG_HAVE_ATOMIC_SUB_FETCH_U64 |
| static inline uint64 |
| pg_atomic_sub_fetch_u64_impl(volatile pg_atomic_uint64 *ptr, int64 sub_) |
| { |
| return pg_atomic_fetch_sub_u64_impl(ptr, sub_) - sub_; |
| } |
| #endif |
|
|