mdbx: fix/refine the use of C11 atomics.

Change-Id: I5d925d4625b06296fd82f4b35ee36be682e7b2d3
This commit is contained in:
Leonid Yuriev
2020-10-11 18:54:07 +03:00
parent 041a4c0aa5
commit 62da4db09a
3 changed files with 83 additions and 43 deletions

View File

@@ -780,12 +780,24 @@ static __always_inline void atomic_yield(void) {
#if MDBX_64BIT_CAS
static __always_inline bool atomic_cas64(volatile uint64_t *p, uint64_t c,
uint64_t v) {
#if defined(ATOMIC_VAR_INIT) || defined(ATOMIC_LLONG_LOCK_FREE)
#if !defined(__STDC_NO_ATOMICS__) && \
(defined(ATOMIC_VAR_INIT) || defined(ATOMIC_LLONG_LOCK_FREE) || \
__has_extension(c_atomic))
STATIC_ASSERT(sizeof(long long) >= sizeof(uint64_t));
#ifndef __COVERITY__
STATIC_ASSERT(atomic_is_lock_free(p));
#endif /* Workaround for Coverity */
return atomic_compare_exchange_strong((_Atomic uint64_t *)p, &c, v);
#ifdef ATOMIC_LLONG_LOCK_FREE
STATIC_ASSERT(ATOMIC_LLONG_LOCK_FREE > 0);
#if ATOMIC_LLONG_LOCK_FREE < 2
assert(atomic_is_lock_free(p));
#endif
#else
assert(atomic_is_lock_free(p));
#endif
#ifdef __clang__
STATIC_ASSERT(sizeof(_Atomic uint64_t) == sizeof(uint64_t));
return atomic_compare_exchange_strong((_Atomic volatile uint64_t *)p, &c, v);
#else
return atomic_compare_exchange_strong(p, &c, v);
#endif
#elif defined(__GNUC__) || defined(__clang__)
return __sync_bool_compare_and_swap(p, c, v);
#elif defined(_MSC_VER)
@@ -801,12 +813,24 @@ static __always_inline bool atomic_cas64(volatile uint64_t *p, uint64_t c,
static __always_inline bool atomic_cas32(volatile uint32_t *p, uint32_t c,
uint32_t v) {
#if defined(ATOMIC_VAR_INIT) || defined(ATOMIC_INT_LOCK_FREE)
#if !defined(__STDC_NO_ATOMICS__) && \
(defined(ATOMIC_VAR_INIT) || defined(ATOMIC_INT_LOCK_FREE) || \
__has_extension(c_atomic))
STATIC_ASSERT(sizeof(int) >= sizeof(uint32_t));
#ifndef __COVERITY__
STATIC_ASSERT(atomic_is_lock_free(p));
#endif /* Workaround for Coverity */
return atomic_compare_exchange_strong((_Atomic uint32_t *)p, &c, v);
#ifdef ATOMIC_INT_LOCK_FREE
STATIC_ASSERT(ATOMIC_INT_LOCK_FREE > 0);
#if ATOMIC_INT_LOCK_FREE < 2
assert(atomic_is_lock_free(p));
#endif
#else
assert(atomic_is_lock_free(p));
#endif
#ifdef __clang__
STATIC_ASSERT(sizeof(_Atomic uint32_t) == sizeof(uint32_t));
return atomic_compare_exchange_strong((_Atomic volatile uint32_t *)p, &c, v);
#else
return atomic_compare_exchange_strong(p, &c, v);
#endif
#elif defined(__GNUC__) || defined(__clang__)
return __sync_bool_compare_and_swap(p, c, v);
#elif defined(_MSC_VER)
@@ -820,12 +844,24 @@ static __always_inline bool atomic_cas32(volatile uint32_t *p, uint32_t c,
}
static __always_inline uint32_t atomic_add32(volatile uint32_t *p, uint32_t v) {
#if defined(ATOMIC_VAR_INIT) || defined(ATOMIC_INT_LOCK_FREE)
#if !defined(__STDC_NO_ATOMICS__) && \
(defined(ATOMIC_VAR_INIT) || defined(ATOMIC_INT_LOCK_FREE) || \
__has_extension(c_atomic))
STATIC_ASSERT(sizeof(int) >= sizeof(uint32_t));
#ifndef __COVERITY__
STATIC_ASSERT(atomic_is_lock_free(p));
#endif /* Workaround for Coverity */
return atomic_fetch_add((_Atomic uint32_t *)p, v);
#ifdef ATOMIC_INT_LOCK_FREE
STATIC_ASSERT(ATOMIC_INT_LOCK_FREE > 0);
#if ATOMIC_INT_LOCK_FREE < 2
assert(atomic_is_lock_free(p));
#endif
#else
assert(atomic_is_lock_free(p));
#endif
#ifdef __clang__
STATIC_ASSERT(sizeof(_Atomic uint32_t) == sizeof(uint32_t));
return atomic_fetch_add((_Atomic volatile uint32_t *)p, v);
#else
return atomic_fetch_add(p, v);
#endif
#elif defined(__GNUC__) || defined(__clang__)
return __sync_fetch_and_add(p, v);
#elif defined(_MSC_VER)