Skip to content

CDRIVER-4215 use legacy atomic built-ins on gcc (>= 4.1, < 4.9) #889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ if [ "$SSL" != "nossl" ]; then
sudo cp -v src/libmongoc/tests/x509gen/ca.pem /usr/share/pki/ca-trust-source/anchors/cdriver.crt || true
if [ -f /usr/share/pki/ca-trust-source/anchors/cdriver.crt ]; then
echo "Copying CA certificate to /usr/share/pki/ca-trust-source/anchors... done."
sudo update-ca-trust enable --verbose
sudo update-ca-trust extract --verbose
else
echo "Copying CA certificate to /usr/share/pki/ca-trust-source/anchors... failed."
Expand Down
83 changes: 78 additions & 5 deletions src/libbson/src/bson/bson-atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,73 @@ enum bson_memory_order {
#define MSVC_MEMORDER_SUFFIX(X)
#endif

#if defined(USE_LEGACY_GCC_ATOMICS) || (!defined(__clang__) && \
__GNUC__ == 4 && __GNUC_MINOR__ >= 1 && __GNUC_MINOR__ < 9)
#define BSON_USE_LEGACY_GCC_ATOMICS
#else
#undef BSON_USE_LEGACY_GCC_ATOMICS
#endif

/* Not all GCC-like compilers support the current __atomic built-ins. Older
* GCC (pre-4.9) used different built-ins named with the __sync prefix. When
* compiling with such older GCC versions, it is necessary to use the applicable
* functions, which requires redefining BSON_IF_GNU_LIKE and defining the
* additional BSON_IF_GNU_LEGACY_ATOMICS macro here. */
#ifdef BSON_USE_LEGACY_GCC_ATOMICS
#undef BSON_IF_GNU_LIKE
#define BSON_IF_GNU_LIKE(...)
#define BSON_IF_GNU_LEGACY_ATOMICS(...) __VA_ARGS__
#else
#define BSON_IF_GNU_LEGACY_ATOMICS(...)
#endif

#define DEF_ATOMIC_OP(MSVC_Intrinsic, GNU_Intrinsic, Order, ...) \
#define DEF_ATOMIC_OP(MSVC_Intrinsic, GNU_Intrinsic, GNU_Legacy_Intrinsic, Order, ...) \
do { \
switch (Order) { \
case bson_memory_order_acq_rel: \
BSON_IF_MSVC (return MSVC_Intrinsic (__VA_ARGS__);) \
BSON_IF_GNU_LIKE ( \
return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_ACQ_REL);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
return GNU_Legacy_Intrinsic (__VA_ARGS__);) \
case bson_memory_order_seq_cst: \
BSON_IF_MSVC (return MSVC_Intrinsic (__VA_ARGS__);) \
BSON_IF_GNU_LIKE ( \
return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_SEQ_CST);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
return GNU_Legacy_Intrinsic (__VA_ARGS__);) \
case bson_memory_order_acquire: \
BSON_IF_MSVC ( \
return BSON_CONCAT (MSVC_Intrinsic, \
MSVC_MEMORDER_SUFFIX (_acq)) (__VA_ARGS__);) \
BSON_IF_GNU_LIKE ( \
return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_ACQUIRE);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
return GNU_Legacy_Intrinsic (__VA_ARGS__);) \
case bson_memory_order_consume: \
BSON_IF_MSVC ( \
return BSON_CONCAT (MSVC_Intrinsic, \
MSVC_MEMORDER_SUFFIX (_acq)) (__VA_ARGS__);) \
BSON_IF_GNU_LIKE ( \
return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_CONSUME);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
return GNU_Legacy_Intrinsic (__VA_ARGS__);) \
case bson_memory_order_release: \
BSON_IF_MSVC ( \
return BSON_CONCAT (MSVC_Intrinsic, \
MSVC_MEMORDER_SUFFIX (_rel)) (__VA_ARGS__);) \
BSON_IF_GNU_LIKE ( \
return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_RELEASE);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
return GNU_Legacy_Intrinsic (__VA_ARGS__);) \
case bson_memory_order_relaxed: \
BSON_IF_MSVC ( \
return BSON_CONCAT (MSVC_Intrinsic, \
MSVC_MEMORDER_SUFFIX (_nf)) (__VA_ARGS__);) \
BSON_IF_GNU_LIKE ( \
return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_RELAXED);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
return GNU_Legacy_Intrinsic (__VA_ARGS__);) \
default: \
BSON_UNREACHABLE ("Invalid bson_memory_order value"); \
} \
Expand All @@ -102,6 +133,12 @@ enum bson_memory_order {
false, /* Not weak */ \
GNU_MemOrder, \
GNU_MemOrder);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
__typeof__ (ExpectActualVar) _val; \
_val = __sync_val_compare_and_swap (Ptr, \
ExpectActualVar, \
NewValue); \
ExpectActualVar = _val;) \
} while (0)


Expand All @@ -118,6 +155,12 @@ enum bson_memory_order {
true, /* Yes weak */ \
GNU_MemOrder, \
GNU_MemOrder);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
__typeof__ (ExpectActualVar) _val; \
_val = __sync_val_compare_and_swap (Ptr, \
ExpectActualVar, \
NewValue); \
ExpectActualVar = _val;) \
} while (0)


Expand All @@ -127,6 +170,7 @@ enum bson_memory_order {
{ \
DEF_ATOMIC_OP (BSON_CONCAT (_InterlockedExchangeAdd, VCIntrinSuffix), \
__atomic_fetch_add, \
__sync_fetch_and_add, \
ord, \
a, \
addend); \
Expand All @@ -139,7 +183,9 @@ enum bson_memory_order {
BSON_IF_MSVC ( \
return bson_atomic_##NamePart##_fetch_add (a, -subtrahend, ord);) \
BSON_IF_GNU_LIKE ( \
DEF_ATOMIC_OP (~, __atomic_fetch_sub, ord, a, subtrahend);) \
DEF_ATOMIC_OP (~, __atomic_fetch_sub, ~, ord, a, subtrahend);) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
DEF_ATOMIC_OP (~, ~, __sync_fetch_and_sub, ord, a, subtrahend);) \
} \
\
static BSON_INLINE Type bson_atomic_##NamePart##_fetch ( \
Expand All @@ -164,13 +210,15 @@ enum bson_memory_order {
default: \
BSON_UNREACHABLE ("Invalid bson_memory_order value"); \
}) \
BSON_IF_GNU_LEGACY_ATOMICS ({ __sync_synchronize (); return *a; }) \
} \
\
static BSON_INLINE Type bson_atomic_##NamePart##_exchange ( \
Type volatile *a, Type value, enum bson_memory_order ord) \
{ \
BSON_IF_MSVC ( \
DEF_ATOMIC_OP (BSON_CONCAT (_InterlockedExchange, VCIntrinSuffix), \
~, \
~, \
ord, \
a, \
Expand All @@ -192,6 +240,8 @@ enum bson_memory_order {
default: \
BSON_UNREACHABLE ("Invalid bson_memory_order value"); \
}) \
BSON_IF_GNU_LEGACY_ATOMICS ( \
return __sync_val_compare_and_swap (a, *a, value);) \
} \
\
static BSON_INLINE Type bson_atomic_##NamePart##_compare_exchange_strong ( \
Expand Down Expand Up @@ -285,7 +335,7 @@ enum bson_memory_order {
#define DECL_ATOMIC_STDINT(Name, VCSuffix) \
DECL_ATOMIC_INTEGRAL (Name, Name##_t, VCSuffix)

#ifdef _MSC_VER
#if defined(_MSC_VER) || defined (BSON_USE_LEGACY_GCC_ATOMICS)
/* MSVC expects precise types for their atomic intrinsics. */
DECL_ATOMIC_INTEGRAL (int8, char, 8);
DECL_ATOMIC_INTEGRAL (int16, short, 16)
Expand Down Expand Up @@ -388,8 +438,23 @@ bson_atomic_ptr_exchange (void *volatile *ptr,
void *new_value,
enum bson_memory_order ord)
{
DEF_ATOMIC_OP (
_InterlockedExchangePointer, __atomic_exchange_n, ord, ptr, new_value);
/* The older __sync_val_compare_and_swap also takes oldval */
#if defined(BSON_USE_LEGACY_GCC_ATOMICS)
DEF_ATOMIC_OP (_InterlockedExchangePointer,
,
__sync_val_compare_and_swap,
ord,
ptr,
*ptr,
new_value);
#else
DEF_ATOMIC_OP (_InterlockedExchangePointer,
__atomic_exchange_n,
,
ord,
ptr,
new_value);
#endif
}

static BSON_INLINE void *
Expand Down Expand Up @@ -500,8 +565,16 @@ bson_atomic_thread_fence ()
{
BSON_IF_MSVC (MemoryBarrier ();)
BSON_IF_GNU_LIKE (__sync_synchronize ();)
BSON_IF_GNU_LEGACY_ATOMICS (__sync_synchronize ();)
}

#ifdef BSON_USE_LEGACY_GCC_ATOMICS
#undef BSON_IF_GNU_LIKE
#define BSON_IF_GNU_LIKE(...) __VA_ARGS__
#endif
#undef BSON_IF_GNU_LEGACY_ATOMICS
#undef BSON_USE_LEGACY_GCC_ATOMICS

BSON_GNUC_DEPRECATED_FOR ("bson_atomic_thread_fence")
BSON_EXPORT (void) bson_memory_barrier (void);

Expand Down
67 changes: 36 additions & 31 deletions src/libbson/tests/test-atomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,42 @@
#define ATOMIC(Kind, Operation) BSON_CONCAT4 (bson_atomic_, Kind, _, Operation)


#define TEST_KIND_WITH_MEMORDER(Kind, TypeName, MemOrder, Assert) \
do { \
TypeName got; \
TypeName value = 0; \
got = ATOMIC (Kind, fetch) (&value, MemOrder); \
Assert (got, ==, 0); \
got = ATOMIC (Kind, fetch_add) (&value, 42, MemOrder); \
Assert (got, ==, 0); \
Assert (value, ==, 42); \
got = ATOMIC (Kind, fetch_sub) (&value, 7, MemOrder); \
Assert (got, ==, 42); \
Assert (value, ==, 35); \
got = ATOMIC (Kind, exchange) (&value, 77, MemOrder); \
Assert (got, ==, 35); \
Assert (value, ==, 77); \
/* Compare-exchange fail: */ \
got = ATOMIC (Kind, compare_exchange_strong) (&value, 4, 9, MemOrder); \
Assert (got, ==, 77); \
Assert (value, ==, 77); \
/* Compare-exchange succeed: */ \
got = ATOMIC (Kind, compare_exchange_strong) (&value, 77, 9, MemOrder); \
Assert (got, ==, 77); \
Assert (value, ==, 9); \
/* Compare-exchange fail: */ \
got = ATOMIC (Kind, compare_exchange_weak) (&value, 8, 12, MemOrder); \
Assert (got, ==, 9); \
Assert (value, ==, 9); \
/* Compare-exchange succeed: */ \
got = ATOMIC (Kind, compare_exchange_weak) (&value, 9, 53, MemOrder); \
Assert (got, ==, 9); \
Assert (value, ==, 53); \
#define TEST_KIND_WITH_MEMORDER(Kind, TypeName, MemOrder, Assert) \
do { \
int i; \
TypeName got; \
TypeName value = 0; \
got = ATOMIC (Kind, fetch) (&value, MemOrder); \
Assert (got, ==, 0); \
got = ATOMIC (Kind, fetch_add) (&value, 42, MemOrder); \
Assert (got, ==, 0); \
Assert (value, ==, 42); \
got = ATOMIC (Kind, fetch_sub) (&value, 7, MemOrder); \
Assert (got, ==, 42); \
Assert (value, ==, 35); \
got = ATOMIC (Kind, exchange) (&value, 77, MemOrder); \
Assert (got, ==, 35); \
Assert (value, ==, 77); \
/* Compare-exchange fail: */ \
got = ATOMIC (Kind, compare_exchange_strong) (&value, 4, 9, MemOrder); \
Assert (got, ==, 77); \
Assert (value, ==, 77); \
/* Compare-exchange succeed: */ \
got = ATOMIC (Kind, compare_exchange_strong) (&value, 77, 9, MemOrder); \
Assert (got, ==, 77); \
Assert (value, ==, 9); \
/* Compare-exchange fail: */ \
got = ATOMIC (Kind, compare_exchange_weak) (&value, 8, 12, MemOrder); \
Assert (got, ==, 9); \
Assert (value, ==, 9); \
/* Compare-exchange-weak succeed: */ \
/* 'weak' may fail spuriously, so it must *eventually* succeed */ \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't argue with that haha.

for (i = 0; i < 10000 && value != 53; ++i) { \
got = ATOMIC (Kind, compare_exchange_weak) (&value, 9, 53, MemOrder); \
Assert (got, ==, 9); \
} \
/* Check that it evenutally succeeded */ \
Assert (value, ==, 53); \
} while (0)

#define TEST_INTEGER_KIND(Kind, TypeName, Assert) \
Expand Down