summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Claessens <xavier.claessens@collabora.com>2021-02-02 13:17:33 -0500
committerXavier Claessens <xavier.claessens@collabora.com>2021-02-02 14:13:43 -0500
commited583f161ce7e7265057f390b17dc8dec21bae35 (patch)
tree35e51b4040808bdb2accd971a8d91d70b0d413a0
parent70fc64e4207b984ceeaaed00d73be5bab4484a05 (diff)
downloadglib-wip/atomic-take2.tar.gz
atomic: Fix type check of g_atomic_pointer_compare_and_exchange()wip/atomic-take2
-rw-r--r--glib/gatomic.h14
1 files changed, 11 insertions, 3 deletions
diff --git a/glib/gatomic.h b/glib/gatomic.h
index e6eccfada..800ddb3aa 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 error if 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)); \