bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pthread-spin: add optimized fallback for GCC versions >= 4.1, < 4.7


From: Bruno Haible
Subject: pthread-spin: add optimized fallback for GCC versions >= 4.1, < 4.7
Date: Wed, 01 Jul 2020 22:54:36 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-179-generic; KDE/5.18.0; x86_64; ; )

Older GCC versions have another set of built-ins for atomic operations.


2020-07-01  Bruno Haible  <bruno@clisp.org>

        pthread-spin: Add optimized fallback for GCC versions >= 4.1, < 4.7.
        * lib/pthread-spin.c (pthread_spin_init, pthread_spin_lock,
        pthread_spin_trylock, pthread_spin_unlock): For GCC >= 4.1, < 4.7, use
        an implementation based on other GCC built-ins.

diff --git a/lib/pthread-spin.c b/lib/pthread-spin.c
index 1f73f13..c131050 100644
--- a/lib/pthread-spin.c
+++ b/lib/pthread-spin.c
@@ -162,6 +162,52 @@ 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).
+   Documentation:
+   <https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html>  */
+
+int
+pthread_spin_init (pthread_spinlock_t *lock,
+                   int shared_across_processes _GL_UNUSED)
+{
+  * (volatile unsigned int *) lock = 0;
+  __sync_synchronize ();
+  return 0;
+}
+
+int
+pthread_spin_lock (pthread_spinlock_t *lock)
+{
+  /* Wait until *lock becomes 0, then replace it with 1.  */
+  while (__sync_val_compare_and_swap ((unsigned int *) lock, 0, 1) != 0)
+    ;
+  return 0;
+}
+
+int
+pthread_spin_trylock (pthread_spinlock_t *lock)
+{
+  if (__sync_val_compare_and_swap ((unsigned int *) lock, 0, 1) != 0)
+    return EBUSY;
+  return 0;
+}
+
+int
+pthread_spin_unlock (pthread_spinlock_t *lock)
+{
+  /* If *lock is 1, then replace it with 0.  */
+  if (__sync_val_compare_and_swap ((unsigned int *) lock, 1, 0) != 1)
+    abort ();
+  return 0;
+}
+
+int
+pthread_spin_destroy (pthread_spinlock_t *lock)
+{
+  return 0;
+}
+
 # else
 /* Emulate a spin lock through a mutex.  */
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]