diff --git a/glib/gatomic.h b/glib/gatomic.h index e6eccfada..3b84a5049 100644 --- a/glib/gatomic.h +++ b/glib/gatomic.h @@ -183,16 +183,24 @@ G_END_DECLS (guint) __atomic_fetch_xor ((atomic), (val), __ATOMIC_SEQ_CST); \ })) -#if defined(glib_typeof) +#if defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L +/* 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 + * is NULL. Note that clang++ force us to be typesafe because it is an error if the 2nd + * argument of __atomic_compare_exchange_n() has a different type than the + * first. + * https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1919 + * https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1715#note_1024120. */ #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \ (G_GNUC_EXTENSION ({ \ G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \ - glib_typeof ((oldval)) gapcae_oldval = (oldval); \ + glib_typeof (*(atomic)) gapcae_oldval = (oldval); \ G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \ (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) */ +#else /* if !(defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L) */ #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \ (G_GNUC_EXTENSION ({ \ G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \