Skip to content

gatomic: Fix false positive with Clang+TSAN

Peter Wu requested to merge Lekensteyn/glib:1843-tsan-atomic-clang-fix into master

__atomic_load_8 and friends do not exist under clang. Use the generic __atomic_load variant instead that are documented here: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

These have the additional benefit that the exact size of gint (4 bytes) or gpointer (4 or 8 bytes) no longer have to be checked.

I initially tried __typeof__(*(atomic)) val;, but that caused warnings in Clang (-Wincompatible-pointer-types-discards-qualifiers) when "atomic" points to a volatile variable. Aside from that, it is apparently not supported everywhere, see the g_has_typeof macro. Another reason not to use it are new warnings under Clang, including:

glib/deprecated/gthread-deprecated.c:683:11: warning: incompatible pointer types initializing 'typeof (*(&mutex->mutex.mutex))' (aka 'union _GMutex *') with an expression of type 'GRecMutex *' (aka 'struct _GRecMutex *') [-Wincompatible-pointer-types]
          g_atomic_pointer_set (&mutex->mutex.mutex, result);

Hence, cast the atomic variable to gint/gpointer pointers, the size was already statically asserted so the cast should be safe.

Closes #1843 (closed)


Clang now passes the glib/tests/atomic test which GCC was already able to do. I have not executed a full test suite run yet, hopefully CI can do that for me.

Edited by Peter Wu

Merge request reports