From a0f200593ca8343a1ed8bb9868924d1ced13f12b Mon Sep 17 00:00:00 2001 From: Damjan Jovanovic Date: Mon, 22 Aug 2022 17:18:23 +0200 Subject: [PATCH] gatomic: Support atomic compare_and_exchange macros on older C++ standard versions The fix in ad23894c1595482cdc10c17a4070a977e396ca4a only works for __cplusplus >= 201103L, but older C++ standards are not always less strict, and still fail to compile the g_atomic_int_compare_and_exchange() and g_atomic_pointer_compare_and_exchange() macros. Apply that fix to all C++ standard versions. Even if this implies using functions that have been added as part of C++11 specification, this is safe because we wouldn't ever try to use the `__atomic_...()` APIs if `__ATOMIC_SEQ_CST` is not defined, and that's part of the very same API. --- glib/gatomic.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/glib/gatomic.h b/glib/gatomic.h index 11d319edc..e1b5d2391 100644 --- a/glib/gatomic.h +++ b/glib/gatomic.h @@ -170,7 +170,7 @@ G_END_DECLS (void) (0 ? *(atomic) ^ *(atomic) : 1); \ __atomic_fetch_sub ((atomic), 1, __ATOMIC_SEQ_CST) == 1; \ })) -#if defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L +#if defined(glib_typeof) && defined(__cplusplus) /* See comments below about equivalent g_atomic_pointer_compare_and_exchange() * shenanigans for type-safety when compiling in C++ mode. */ #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \ @@ -180,7 +180,7 @@ G_END_DECLS (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \ __atomic_compare_exchange_n ((atomic), &gaicae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \ })) -#else /* if !(defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L) */ +#else /* if !(defined(glib_typeof) && defined(__cplusplus)) */ #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \ (G_GNUC_EXTENSION ({ \ gint gaicae_oldval = (oldval); \ @@ -230,7 +230,7 @@ G_END_DECLS (guint) __atomic_fetch_xor ((atomic), (val), __ATOMIC_SEQ_CST); \ })) -#if defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L +#if defined(glib_typeof) && defined(__cplusplus) /* This is typesafe because we check we can assign oldval to the type of * (*atomic). Unfortunately it can only be done in C++ because gcc/clang warn * when atomic is volatile and not oldval, or when atomic is gsize* and oldval @@ -247,7 +247,7 @@ G_END_DECLS (void) (0 ? (gpointer) *(atomic) : NULL); \ __atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \ })) -#else /* if !(defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L) */ +#else /* if !(defined(glib_typeof) && defined(__cplusplus) */ #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \ (G_GNUC_EXTENSION ({ \ G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \