summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Dröge <slomo@coaxion.net>2021-02-03 15:18:55 +0000
committerSebastian Dröge <slomo@coaxion.net>2021-02-03 15:18:55 +0000
commitf67b12467b09c9458ccdc7d852bfe0c2653430d9 (patch)
tree9d04bd8c7a9c2102567bd0b31cc981937117ba7e
parent018ae0982c05fcb8d0f8403c18afd5e00f0625c5 (diff)
parent580b415ebd34c69e7e10b5693b2fa808491637c8 (diff)
downloadglib-f67b12467b09c9458ccdc7d852bfe0c2653430d9.tar.gz
Merge branch 'atomic-take2' into 'master'
atomic: Fix type check of g_atomic_pointer_compare_and_exchange() See merge request GNOME/glib!1919
-rw-r--r--glib/gatomic.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/glib/gatomic.h b/glib/gatomic.h
index 12387df26..3b84a5049 100644
--- a/glib/gatomic.h
+++ b/glib/gatomic.h
@@ -182,6 +182,25 @@ G_END_DECLS
(void) (0 ? *(atomic) ^ (val) : 1); \
(guint) __atomic_fetch_xor ((atomic), (val), __ATOMIC_SEQ_CST); \
}))
+
+#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 (*(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) && defined(__cplusplus) && __cplusplus >= 201103L) */
#define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \
@@ -190,6 +209,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; \
}))
+#endif /* defined(glib_typeof) */
#define g_atomic_pointer_add(atomic, val) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \