[glib: 1/2] gthread: Port native mutex to Clang




commit f1a1e84ddae4ca66ca1c2bb0910d3265a5ac9ddc
Author: Ole André Vadla Ravnås <oleavr gmail com>
Date:   Fri Jul 21 19:40:48 2017 +0200

    gthread: Port native mutex to Clang
    
    And other toolchains that support stdatomic.

 glib/gthread-posix.c | 23 +++++++++++++++++++++--
 meson.build          |  1 +
 2 files changed, 22 insertions(+), 2 deletions(-)
---
diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c
index f09f58a15..2ec50f774 100644
--- a/glib/gthread-posix.c
+++ b/glib/gthread-posix.c
@@ -72,8 +72,8 @@
 #include <sys/syscall.h>
 #endif
 
-/* clang defines __ATOMIC_SEQ_CST but doesn't support the GCC extension */
-#if defined(HAVE_FUTEX) && defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
+#if defined(HAVE_FUTEX) && \
+    (defined(HAVE_STDATOMIC_H) || defined(__ATOMIC_SEQ_CST))
 #define USE_NATIVE_MUTEX
 #endif
 
@@ -1401,6 +1401,23 @@ g_system_thread_set_name (const gchar *name)
  * purposes...
  */
 
+#ifdef HAVE_STDATOMIC_H
+
+#include <stdatomic.h>
+
+#define exchange_acquire(ptr, new) \
+  atomic_exchange_explicit((atomic_uint *) (ptr), (new), __ATOMIC_ACQUIRE)
+#define compare_exchange_acquire(ptr, old, new) \
+  atomic_compare_exchange_strong_explicit((atomic_uint *) (ptr), (old), (new), \
+                                          __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)
+
+#define exchange_release(ptr, new) \
+  atomic_exchange_explicit((atomic_uint *) (ptr), (new), __ATOMIC_RELEASE)
+#define store_release(ptr, new) \
+  atomic_store_explicit((atomic_uint *) (ptr), (new), __ATOMIC_RELEASE)
+
+#else
+
 #define exchange_acquire(ptr, new) \
   __atomic_exchange_4((ptr), (new), __ATOMIC_ACQUIRE)
 #define compare_exchange_acquire(ptr, old, new) \
@@ -1411,6 +1428,8 @@ g_system_thread_set_name (const gchar *name)
 #define store_release(ptr, new) \
   __atomic_store_4((ptr), (new), __ATOMIC_RELEASE)
 
+#endif
+
 /* Our strategy for the mutex is pretty simple:
  *
  *  0: not in use
diff --git a/meson.build b/meson.build
index 05dfff027..1d910c12c 100644
--- a/meson.build
+++ b/meson.build
@@ -275,6 +275,7 @@ headers = [
   'pwd.h',
   'sched.h',
   'spawn.h',
+  'stdatomic.h',
   'stdint.h',
   'stdlib.h',
   'string.h',


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