Merge branch 'atomic-strict-aliasing-fixes' into 'master'

Strict-aliasing fixes to new atomic built-ins

See merge request GNOME/glib!1155
This commit is contained in:
Sebastian Dröge 2019-10-18 14:00:26 +00:00
commit bab6acff9b
5 changed files with 33 additions and 13 deletions

View File

@ -669,7 +669,7 @@ g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
if (!g_thread_supported ())
return NULL;
result = g_atomic_pointer_get (&mutex->mutex.mutex);
result = (GRecMutex *) g_atomic_pointer_get (&mutex->mutex.mutex);
if (!result)
{
@ -680,7 +680,7 @@ g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
{
result = g_slice_new (GRecMutex);
g_rec_mutex_init (result);
g_atomic_pointer_set (&mutex->mutex.mutex, result);
g_atomic_pointer_set (&mutex->mutex.mutex, (GMutex *) result);
}
G_UNLOCK (g_static_mutex);

View File

@ -103,20 +103,41 @@ G_END_DECLS
__atomic_store ((gint *)(atomic), &gais_temp, __ATOMIC_SEQ_CST); \
}))
#if defined(g_has_typeof)
#define g_atomic_pointer_get(atomic) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
gpointer gapg_temp; \
__atomic_load ((gpointer *)(atomic), &gapg_temp, __ATOMIC_SEQ_CST); \
gapg_temp; \
__typeof__(*(atomic)) gapg_temp_newval; \
__typeof__((atomic)) gapg_temp_atomic = (atomic); \
__atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
gapg_temp_newval; \
}))
#define g_atomic_pointer_set(atomic, newval) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
gpointer gaps_temp = (gpointer)(newval); \
__typeof__((atomic)) gaps_temp_atomic = (atomic); \
__typeof__(*(atomic)) gaps_temp_newval = (newval); \
(void) (0 ? (gpointer) *(atomic) : NULL); \
__atomic_store ((gpointer *)(atomic), &gaps_temp, __ATOMIC_SEQ_CST); \
__atomic_store (gaps_temp_atomic, &gaps_temp_newval, __ATOMIC_SEQ_CST); \
}))
#else /* if !defined(g_has_typeof) */
#define g_atomic_pointer_get(atomic) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
gpointer gapg_temp_newval; \
gpointer *gapg_temp_atomic = (gpointer *)(atomic); \
__atomic_load (gapg_temp_atomic, &gapg_temp_newval, __ATOMIC_SEQ_CST); \
gapg_temp_newval; \
}))
#define g_atomic_pointer_set(atomic, newval) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
gpointer *gaps_temp_atomic = (gpointer *)(atomic); \
gpointer gaps_temp_newval = (gpointer)(newval); \
(void) (0 ? (gpointer) *(atomic) : NULL); \
__atomic_store (gaps_temp_atomic, &gaps_temp_newval, __ATOMIC_SEQ_CST); \
}))
#endif /* !defined(g_has_typeof) */
#else /* defined(__ATOMIC_SEQ_CST) */

View File

@ -675,7 +675,7 @@ g_main_context_new (void)
GMainContext *
g_main_context_default (void)
{
static GMainContext *default_main_context;
static GMainContext *default_main_context = NULL;
if (g_once_init_enter (&default_main_context))
{
@ -690,8 +690,7 @@ g_main_context_default (void)
g_print ("default context=%p\n", context);
#endif
g_once_init_leave ((gsize *) &default_main_context, (gsize) context);
g_once_init_leave (&default_main_context, context);
}
return default_main_context;

View File

@ -682,7 +682,7 @@ gboolean
volatile gsize *value_location = location;
gboolean need_init = FALSE;
g_mutex_lock (&g_once_mutex);
if (g_atomic_pointer_get (value_location) == NULL)
if (g_atomic_pointer_get (value_location) == 0)
{
if (!g_slist_find (g_once_init_list, (void*) value_location))
{
@ -718,7 +718,7 @@ void
{
volatile gsize *value_location = location;
g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
g_return_if_fail (g_atomic_pointer_get (value_location) == 0);
g_return_if_fail (result != 0);
g_atomic_pointer_set (value_location, result);

View File

@ -92,7 +92,7 @@ test_types (void)
g_assert_true (ip == 0);
g_atomic_pointer_set (&gs, 0);
vp2 = g_atomic_pointer_get (&gs);
vp2 = (gpointer) g_atomic_pointer_get (&gs);
gs2 = (gsize) vp2;
g_assert_cmpuint (gs2, ==, 0);
res = g_atomic_pointer_compare_and_exchange (&gs, 0, 0);