mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-09 04:15:49 +01:00
Merge branch '1750-more-atomic-intrinsics' into 'master'
gatomic: Use new __atomic_*() intrinsics for all atomic operations Closes #1750 See merge request GNOME/glib!1123
This commit is contained in:
commit
4dcad56fb2
@ -139,6 +139,92 @@ G_END_DECLS
|
|||||||
}))
|
}))
|
||||||
#endif /* !defined(g_has_typeof) */
|
#endif /* !defined(g_has_typeof) */
|
||||||
|
|
||||||
|
#define g_atomic_int_inc(atomic) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
||||||
|
(void) (0 ? *(atomic) ^ *(atomic) : 1); \
|
||||||
|
(void) __atomic_fetch_add ((atomic), 1, __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
#define g_atomic_int_dec_and_test(atomic) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
||||||
|
(void) (0 ? *(atomic) ^ *(atomic) : 1); \
|
||||||
|
__atomic_fetch_sub ((atomic), 1, __ATOMIC_SEQ_CST) == 1; \
|
||||||
|
}))
|
||||||
|
#define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
gint gaicae_oldval = (oldval); \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
||||||
|
(void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \
|
||||||
|
__atomic_compare_exchange_n ((atomic), &gaicae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
|
||||||
|
}))
|
||||||
|
#define g_atomic_int_add(atomic, val) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
||||||
|
(void) (0 ? *(atomic) ^ (val) : 1); \
|
||||||
|
(gint) __atomic_fetch_add ((atomic), (val), __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
#define g_atomic_int_and(atomic, val) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
||||||
|
(void) (0 ? *(atomic) ^ (val) : 1); \
|
||||||
|
(guint) __atomic_fetch_and ((atomic), (val), __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
#define g_atomic_int_or(atomic, val) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
||||||
|
(void) (0 ? *(atomic) ^ (val) : 1); \
|
||||||
|
(guint) __atomic_fetch_or ((atomic), (val), __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
#define g_atomic_int_xor(atomic, val) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
||||||
|
(void) (0 ? *(atomic) ^ (val) : 1); \
|
||||||
|
(guint) __atomic_fetch_xor ((atomic), (val), __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
|
||||||
|
#define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
__typeof__ ((oldval)) 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; \
|
||||||
|
}))
|
||||||
|
#define g_atomic_pointer_add(atomic, val) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
|
||||||
|
(void) (0 ? (gpointer) *(atomic) : NULL); \
|
||||||
|
(void) (0 ? (val) ^ (val) : 1); \
|
||||||
|
(gssize) __atomic_fetch_add ((atomic), (val), __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
#define g_atomic_pointer_and(atomic, val) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
volatile gsize *gapa_atomic = (volatile gsize *) (atomic); \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gsize)); \
|
||||||
|
(void) (0 ? (gpointer) *(atomic) : NULL); \
|
||||||
|
(void) (0 ? (val) ^ (val) : 1); \
|
||||||
|
(gsize) __atomic_fetch_and (gapa_atomic, (val), __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
#define g_atomic_pointer_or(atomic, val) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
volatile gsize *gapo_atomic = (volatile gsize *) (atomic); \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gsize)); \
|
||||||
|
(void) (0 ? (gpointer) *(atomic) : NULL); \
|
||||||
|
(void) (0 ? (val) ^ (val) : 1); \
|
||||||
|
(gsize) __atomic_fetch_or (gapo_atomic, (val), __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
#define g_atomic_pointer_xor(atomic, val) \
|
||||||
|
(G_GNUC_EXTENSION ({ \
|
||||||
|
volatile gsize *gapx_atomic = (volatile gsize *) (atomic); \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
|
||||||
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gsize)); \
|
||||||
|
(void) (0 ? (gpointer) *(atomic) : NULL); \
|
||||||
|
(void) (0 ? (val) ^ (val) : 1); \
|
||||||
|
(gsize) __atomic_fetch_xor (gapx_atomic, (val), __ATOMIC_SEQ_CST); \
|
||||||
|
}))
|
||||||
|
|
||||||
#else /* defined(__ATOMIC_SEQ_CST) */
|
#else /* defined(__ATOMIC_SEQ_CST) */
|
||||||
|
|
||||||
/* We want to achieve __ATOMIC_SEQ_CST semantics here. See
|
/* We want to achieve __ATOMIC_SEQ_CST semantics here. See
|
||||||
@ -205,8 +291,6 @@ G_END_DECLS
|
|||||||
*(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
|
*(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
|
||||||
}))
|
}))
|
||||||
|
|
||||||
#endif /* !defined(__ATOMIC_SEQ_CST) */
|
|
||||||
|
|
||||||
#define g_atomic_int_inc(atomic) \
|
#define g_atomic_int_inc(atomic) \
|
||||||
(G_GNUC_EXTENSION ({ \
|
(G_GNUC_EXTENSION ({ \
|
||||||
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
|
||||||
@ -285,6 +369,8 @@ G_END_DECLS
|
|||||||
(gsize) __sync_fetch_and_xor ((atomic), (val)); \
|
(gsize) __sync_fetch_and_xor ((atomic), (val)); \
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
#endif /* !defined(__ATOMIC_SEQ_CST) */
|
||||||
|
|
||||||
#else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
|
#else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
|
||||||
|
|
||||||
#define g_atomic_int_get(atomic) \
|
#define g_atomic_int_get(atomic) \
|
||||||
|
@ -80,6 +80,9 @@ test_types (void)
|
|||||||
g_atomic_pointer_set (&vp, 0);
|
g_atomic_pointer_set (&vp, 0);
|
||||||
vp2 = g_atomic_pointer_get (&vp);
|
vp2 = g_atomic_pointer_get (&vp);
|
||||||
g_assert_true (vp2 == 0);
|
g_assert_true (vp2 == 0);
|
||||||
|
res = g_atomic_pointer_compare_and_exchange (&vp, &s, &s);
|
||||||
|
g_assert_false (res);
|
||||||
|
g_assert_true (vp == 0);
|
||||||
res = g_atomic_pointer_compare_and_exchange (&vp, 0, 0);
|
res = g_atomic_pointer_compare_and_exchange (&vp, 0, 0);
|
||||||
g_assert_true (res);
|
g_assert_true (res);
|
||||||
g_assert_true (vp == 0);
|
g_assert_true (vp == 0);
|
||||||
@ -185,6 +188,9 @@ G_GNUC_END_IGNORE_DEPRECATIONS
|
|||||||
g_atomic_pointer_set (&vp, 0);
|
g_atomic_pointer_set (&vp, 0);
|
||||||
vp2 = g_atomic_pointer_get (&vp);
|
vp2 = g_atomic_pointer_get (&vp);
|
||||||
g_assert_true (vp2 == 0);
|
g_assert_true (vp2 == 0);
|
||||||
|
res = g_atomic_pointer_compare_and_exchange (&vp, &s, &s);
|
||||||
|
g_assert_false (res);
|
||||||
|
g_assert_true (vp == 0);
|
||||||
res = g_atomic_pointer_compare_and_exchange (&vp, 0, 0);
|
res = g_atomic_pointer_compare_and_exchange (&vp, 0, 0);
|
||||||
g_assert_true (res);
|
g_assert_true (res);
|
||||||
g_assert_true (vp == 0);
|
g_assert_true (vp == 0);
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
#undef G_DISABLE_ASSERT
|
|
||||||
#undef G_LOG_DOMAIN
|
|
||||||
|
|
||||||
#include <glib.h>
|
|
||||||
|
|
||||||
/* Obviously we can't test that the operations are atomic, but we can
|
|
||||||
* at least test, that they do, what they ought to do */
|
|
||||||
|
|
||||||
int
|
|
||||||
main (int argc,
|
|
||||||
char *argv[])
|
|
||||||
{
|
|
||||||
gint i;
|
|
||||||
gint atomic = -5;
|
|
||||||
gpointer atomic_pointer = NULL;
|
|
||||||
gpointer biggest_pointer = (gpointer)((gsize)atomic_pointer - 1);
|
|
||||||
|
|
||||||
for (i = 0; i < 15; i++)
|
|
||||||
g_atomic_int_inc (&atomic);
|
|
||||||
g_assert (atomic == 10);
|
|
||||||
for (i = 0; i < 9; i++)
|
|
||||||
g_assert (!g_atomic_int_dec_and_test (&atomic));
|
|
||||||
g_assert (g_atomic_int_dec_and_test (&atomic));
|
|
||||||
g_assert (atomic == 0);
|
|
||||||
|
|
||||||
g_assert (g_atomic_int_add (&atomic, 5) == 0);
|
|
||||||
g_assert (atomic == 5);
|
|
||||||
|
|
||||||
g_assert (g_atomic_int_add (&atomic, -10) == 5);
|
|
||||||
g_assert (atomic == -5);
|
|
||||||
|
|
||||||
g_atomic_int_add (&atomic, 20);
|
|
||||||
g_assert (atomic == 15);
|
|
||||||
|
|
||||||
g_atomic_int_add (&atomic, -35);
|
|
||||||
g_assert (atomic == -20);
|
|
||||||
|
|
||||||
g_assert (atomic == g_atomic_int_get (&atomic));
|
|
||||||
|
|
||||||
g_assert (g_atomic_int_compare_and_exchange (&atomic, -20, 20));
|
|
||||||
g_assert (atomic == 20);
|
|
||||||
|
|
||||||
g_assert (!g_atomic_int_compare_and_exchange (&atomic, 42, 12));
|
|
||||||
g_assert (atomic == 20);
|
|
||||||
|
|
||||||
g_assert (g_atomic_int_compare_and_exchange (&atomic, 20, G_MAXINT));
|
|
||||||
g_assert (atomic == G_MAXINT);
|
|
||||||
|
|
||||||
g_assert (g_atomic_int_compare_and_exchange (&atomic, G_MAXINT, G_MININT));
|
|
||||||
g_assert (atomic == G_MININT);
|
|
||||||
|
|
||||||
g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer,
|
|
||||||
NULL, biggest_pointer));
|
|
||||||
g_assert (atomic_pointer == biggest_pointer);
|
|
||||||
|
|
||||||
g_assert (atomic_pointer == g_atomic_pointer_get (&atomic_pointer));
|
|
||||||
|
|
||||||
g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer,
|
|
||||||
biggest_pointer, NULL));
|
|
||||||
g_assert (atomic_pointer == NULL);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -19,7 +19,6 @@ tests = {
|
|||||||
'testglib' : {'tap' : true},
|
'testglib' : {'tap' : true},
|
||||||
'testgdate' : {},
|
'testgdate' : {},
|
||||||
'datetime' : {},
|
'datetime' : {},
|
||||||
'atomic-test' : {},
|
|
||||||
'bit-test' : {},
|
'bit-test' : {},
|
||||||
'child-test' : {},
|
'child-test' : {},
|
||||||
'completion-test' : {},
|
'completion-test' : {},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user