Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 1 | From 2668390454bc0efe52a262eb2faa4a2bd5a062e2 Mon Sep 17 00:00:00 2001 |
| 2 | From: Philip Withnall <pwithnall@endlessos.org> |
| 3 | Date: Fri, 1 Apr 2022 13:47:19 +0100 |
| 4 | Subject: [PATCH 2/2] gatomic: Add a C++ variant of |
| 5 | g_atomic_int_compare_and_exchange() |
| 6 | MIME-Version: 1.0 |
| 7 | Content-Type: text/plain; charset=UTF-8 |
| 8 | Content-Transfer-Encoding: 8bit |
| 9 | |
| 10 | The C++ variant implements type safety differently, to avoid warnings |
| 11 | from C++ compilers about: |
| 12 | ``` |
| 13 | ../../../gnome-commander-1.14.2/src/intviewer/searcher.cc:303:5: error: cannot initialize a parameter of type 'gint *' (aka 'int *') with an rvalue of type 'void *' |
| 14 | g_atomic_int_compare_and_exchange ((gint*)&src->priv->progress_value, oldval, (gint)d); |
| 15 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 16 | /mnt/b/yoe/master/build/tmp/work/cortexa72-yoe-linux/gnome-commander/1.14.2-r0/recipe-sysroot/usr/include/glib-2.0/glib/gatomic.h:160:44: note: expanded from macro 'g_atomic_int_compare_and_exchange' |
| 17 | __atomic_compare_exchange_n ((atomic), (void *) (&(gaicae_oldval)), (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \ |
| 18 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 | ``` |
| 20 | |
| 21 | This complements the existing C++ variant for |
| 22 | `g_atomic_pointer_compare_and_exchange()`, and fixes a regression on C++ |
| 23 | from https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2114. |
| 24 | |
| 25 | With the addition of the unit tests in the previous commit, this is |
| 26 | effectively tested by the FreeBSD and macOS CI jobs, as they use |
| 27 | `clang++` in C++ mode. `g++` doesn’t seem to emit a warning about this. |
| 28 | |
| 29 | Signed-off-by: Philip Withnall <pwithnall@endlessos.org> |
| 30 | |
| 31 | Fixes: #2625 |
| 32 | Upstream-Status: Submitted [https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2578] |
| 33 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 34 | --- |
| 35 | glib/gatomic.h | 12 ++++++++++++ |
| 36 | 1 file changed, 12 insertions(+) |
| 37 | |
| 38 | diff --git a/glib/gatomic.h b/glib/gatomic.h |
| 39 | index 5eba1dbc7..8b2b880c8 100644 |
| 40 | --- a/glib/gatomic.h |
| 41 | +++ b/glib/gatomic.h |
| 42 | @@ -152,6 +152,17 @@ G_END_DECLS |
| 43 | (void) (0 ? *(atomic) ^ *(atomic) : 1); \ |
| 44 | __atomic_fetch_sub ((atomic), 1, __ATOMIC_SEQ_CST) == 1; \ |
| 45 | })) |
| 46 | +#if defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L |
| 47 | +/* See comments below about equivalent g_atomic_pointer_compare_and_exchange() |
| 48 | + * shenanigans for type-safety when compiling in C++ mode. */ |
| 49 | +#define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \ |
| 50 | + (G_GNUC_EXTENSION ({ \ |
| 51 | + glib_typeof (*(atomic)) gaicae_oldval = (oldval); \ |
| 52 | + G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \ |
| 53 | + (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \ |
| 54 | + __atomic_compare_exchange_n ((atomic), &gaicae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \ |
| 55 | + })) |
| 56 | +#else /* if !(defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L) */ |
| 57 | #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \ |
| 58 | (G_GNUC_EXTENSION ({ \ |
| 59 | gint gaicae_oldval = (oldval); \ |
| 60 | @@ -159,6 +170,7 @@ G_END_DECLS |
| 61 | (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \ |
| 62 | __atomic_compare_exchange_n ((atomic), (void *) (&(gaicae_oldval)), (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \ |
| 63 | })) |
| 64 | +#endif /* defined(glib_typeof) */ |
| 65 | #define g_atomic_int_add(atomic, val) \ |
| 66 | (G_GNUC_EXTENSION ({ \ |
| 67 | G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \ |
| 68 | -- |
| 69 | 2.35.1 |
| 70 | |