clang also has the atomic built-ins and the asm support that GCC has.
2020-08-11 Bruno Haible <br...@clisp.org> asyncsafe-spin: Use GCC built-ins also on clang. * lib/asyncsafe-spin.c (asyncsafe_spin_init, do_lock, do_unlock): Use the newer GCC built-ins also on clang. diff --git a/lib/asyncsafe-spin.c b/lib/asyncsafe-spin.c index 6ea5781..98d4fad 100644 --- a/lib/asyncsafe-spin.c +++ b/lib/asyncsafe-spin.c @@ -67,9 +67,11 @@ asyncsafe_spin_destroy (asyncsafe_spinlock_t *lock) We don't use the C11 <stdatomic.h> (available in GCC >= 4.9) because it would require to link with -latomic. */ -# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined __ibmxl__ -/* Use GCC built-ins (available in GCC >= 4.7) that operate on the first byte of - the lock. +# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) \ + || __clang_major > 3 || (__clang_major__ == 3 && __clang_minor__ >= 1)) \ + && !defined __ibmxl__ +/* Use GCC built-ins (available in GCC >= 4.7 and clang >= 3.1) that operate on + the first byte of the lock. Documentation: <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/_005f_005fatomic-Builtins.html> */ @@ -129,8 +131,10 @@ do_unlock (asyncsafe_spinlock_t *lock) # endif -# elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && !defined __ibmxl__ -/* Use GCC built-ins (available in GCC >= 4.1). +# elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) \ + || __clang_major__ >= 3) \ + && !defined __ibmxl__ +/* Use GCC built-ins (available in GCC >= 4.1 and clang >= 3.0). Documentation: <https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html> */ @@ -184,16 +188,17 @@ do_unlock (asyncsafe_spinlock_t *lock) abort (); } -# elif (defined __GNUC__ || defined __SUNPRO_C) && (defined __sparc || defined __i386 || defined __x86_64__) -/* For older versions of GCC, use inline assembly. - GCC and the Oracle Studio C 12 compiler understand GCC's extended asm syntax, - but the plain Oracle Studio C 11 compiler understands only simple asm. */ +# elif (defined __GNUC__ || defined __clang__ || defined __SUNPRO_C) && (defined __sparc || defined __i386 || defined __x86_64__) +/* For older versions of GCC or clang, use inline assembly. + GCC, clang, and the Oracle Studio C 12 compiler understand GCC's extended + asm syntax, but the plain Oracle Studio C 11 compiler understands only + simple asm. */ /* An implementation that verifies the unlocks. */ static void memory_barrier (void) { -# if defined __GNUC__ || __SUNPRO_C >= 0x590 +# if defined __GNUC__ || defined __clang__ || __SUNPRO_C >= 0x590 # if defined __i386 || defined __x86_64__ asm volatile ("mfence"); # endif @@ -216,7 +221,7 @@ static unsigned int atomic_compare_and_swap (volatile unsigned int *vp, unsigned int cmp, unsigned int newval) { -# if defined __GNUC__ || __SUNPRO_C >= 0x590 +# if defined __GNUC__ || defined __clang__ || __SUNPRO_C >= 0x590 unsigned int oldval; # if defined __i386 || defined __x86_64__ asm volatile (" lock\n cmpxchgl %3,(%1)"