bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* improve clang support (23)
@ 2020-08-11 17:13 Bruno Haible
  2020-08-16 16:43 ` Bruno Haible
  0 siblings, 1 reply; 2+ messages in thread
From: Bruno Haible @ 2020-08-11 17:13 UTC (permalink / raw
  To: bug-gnulib

clang also has the atomic built-ins and the asm support that GCC has.


2020-08-11  Bruno Haible  <bruno@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)"



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: improve clang support (23)
  2020-08-11 17:13 improve clang support (23) Bruno Haible
@ 2020-08-16 16:43 ` Bruno Haible
  0 siblings, 0 replies; 2+ messages in thread
From: Bruno Haible @ 2020-08-16 16:43 UTC (permalink / raw
  To: bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 836 bytes --]

> clang also has the atomic built-ins and the asm support that GCC has.
> 
> 
> 2020-08-11  Bruno Haible  <bruno@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.

The tests need an update for this change. And the same GCC built-ins can
also be used in the module 'pthread-spin'.


2020-08-16  Bruno Haible  <bruno@clisp.org>

	pthread-spin: Use GCC built-ins also on clang.
	* lib/pthread-spin.c (pthread_spin_init, pthread_spin_lock,
	pthread_spin_trylock, pthread_spin_unlock): Use the newer GCC built-ins
	also on clang.

2020-08-16  Bruno Haible  <bruno@clisp.org>

	asyncsafe-spin tests: Update.
	* tests/test-asyncsafe-spin2.c: Update to match the change in
	lib/asyncsafe-spin.c from 2020-08-11.


[-- Attachment #2: 0001-asyncsafe-spin-tests-Update.patch --]
[-- Type: text/x-patch, Size: 1563 bytes --]

From dcc8b493adecff06096bb3516aac26d29012cf1a Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 16 Aug 2020 18:41:57 +0200
Subject: [PATCH 1/2] asyncsafe-spin tests: Update.

* tests/test-asyncsafe-spin2.c: Update to match the change in
lib/asyncsafe-spin.c from 2020-08-11.
---
 ChangeLog                    | 6 ++++++
 tests/test-asyncsafe-spin2.c | 3 ++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index b750f34..0f7ca8e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2020-08-16  Bruno Haible  <bruno@clisp.org>
 
+	asyncsafe-spin tests: Update.
+	* tests/test-asyncsafe-spin2.c: Update to match the change in
+	lib/asyncsafe-spin.c from 2020-08-11.
+
+2020-08-16  Bruno Haible  <bruno@clisp.org>
+
 	setenv: Use tree code also with clang.
 	* lib/setenv.c (USE_TSEARCH): Treat clang like GCC.
 
diff --git a/tests/test-asyncsafe-spin2.c b/tests/test-asyncsafe-spin2.c
index cb0364f..38d9324 100644
--- a/tests/test-asyncsafe-spin2.c
+++ b/tests/test-asyncsafe-spin2.c
@@ -36,7 +36,8 @@
 #define THREAD_COUNT 10
 
 /* Number of operations performed in each thread.  */
-#if !(defined _WIN32 && ! defined __CYGWIN__) && HAVE_PTHREAD_H && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && !defined __ibmxl__
+#if !(defined _WIN32 && ! defined __CYGWIN__) && HAVE_PTHREAD_H && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || __clang_major__ >= 3) && !defined __ibmxl__
+
 /* The GCC built-ins are known to work fine.  */
 # define REPEAT_COUNT 5000
 #else
-- 
2.7.4


[-- Attachment #3: 0002-pthread-spin-Use-GCC-built-ins-also-on-clang.patch --]
[-- Type: text/x-patch, Size: 2424 bytes --]

From bed51ff8cc7bb755b32fa83c589436d8221380c6 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 16 Aug 2020 18:42:02 +0200
Subject: [PATCH 2/2] pthread-spin: Use GCC built-ins also on clang.

* lib/pthread-spin.c (pthread_spin_init, pthread_spin_lock,
pthread_spin_trylock, pthread_spin_unlock): Use the newer GCC built-ins
also on clang.
---
 ChangeLog          |  7 +++++++
 lib/pthread-spin.c | 14 +++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0f7ca8e..b97fcf7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2020-08-16  Bruno Haible  <bruno@clisp.org>
 
+	pthread-spin: Use GCC built-ins also on clang.
+	* lib/pthread-spin.c (pthread_spin_init, pthread_spin_lock,
+	pthread_spin_trylock, pthread_spin_unlock): Use the newer GCC built-ins
+	also on clang.
+
+2020-08-16  Bruno Haible  <bruno@clisp.org>
+
 	asyncsafe-spin tests: Update.
 	* tests/test-asyncsafe-spin2.c: Update to match the change in
 	lib/asyncsafe-spin.c from 2020-08-11.
diff --git a/lib/pthread-spin.c b/lib/pthread-spin.c
index c131050..c343f9d 100644
--- a/lib/pthread-spin.c
+++ b/lib/pthread-spin.c
@@ -68,9 +68,11 @@ pthread_spin_destroy (pthread_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)
-/* Use GCC built-ins (available in GCC >= 4.7) that operate on the first 32-bit
-   word 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>  */
 
@@ -162,8 +164,10 @@ pthread_spin_destroy (pthread_spinlock_t *lock)
   return 0;
 }
 
-# elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
-/* 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>  */
 
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-16 16:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-08-11 17:13 improve clang support (23) Bruno Haible
2020-08-16 16:43 ` Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).