[PATCH] Configure: Prefer gcc __atomic builtins instead of older __sync builtins

Debayan Ghosh debayang.qdt at qualcommdatacenter.com
Sun Dec 3 15:41:52 UTC 2017


# HG changeset patch
# User Debayan Ghosh <debayang.qdt at qualcommdatacenter.com>
# Date 1511884462 -19800
#      Tue Nov 28 21:24:22 2017 +0530
# Node ID 4576e7c90ed42da3af67d90f624b34dd9285a658
# Parent  fc0d06224edac2c7cfbfd9a4def478f285d9957b
Configure: Prefer gcc __atomic builtins instead of older __sync builtins

It is desirable to use the newer GCC __atomic builtins when using later
versions of GCC , compared to the legacy __sync atomic builtins.
Ref: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

diff -r fc0d06224eda -r 4576e7c90ed4 auto/cc/conf
--- a/auto/cc/conf	Tue Nov 28 13:09:54 2017 +0300
+++ b/auto/cc/conf	Tue Nov 28 21:24:22 2017 +0530
@@ -181,12 +181,28 @@
     if [ "$NGX_CC_NAME" = "sunc" ]; then
         echo "checking for gcc builtin atomic operations ... disabled"
     else
-        ngx_feature="gcc builtin atomic operations"
+        ngx_feature="gcc builtin new atomic operations"
         ngx_feature_name=NGX_HAVE_GCC_ATOMIC
         ngx_feature_run=yes
         ngx_feature_incs=
         ngx_feature_path=
         ngx_feature_libs=
+        ngx_feature_test="long  n = 0, m = 0;
+                          if (!__atomic_compare_exchange_n(&n, &m, 1, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
+                              return 1;
+                          if (__atomic_fetch_add(&n, 1, __ATOMIC_SEQ_CST) != 1)
+                              return 1;
+                          if (n != 2)
+                              return 1;
+                          __atomic_thread_fence(__ATOMIC_ACQUIRE);"
+        . auto/feature
+
+        ngx_feature="gcc builtin sync atomic operations"
+        ngx_feature_name=NGX_HAVE_GCC_SYNC_ATOMIC
+        ngx_feature_run=yes
+        ngx_feature_incs=
+        ngx_feature_path=
+        ngx_feature_libs=
         ngx_feature_test="long  n = 0;
                           if (!__sync_bool_compare_and_swap(&n, 0, 1))
                               return 1;
diff -r fc0d06224eda -r 4576e7c90ed4 src/os/unix/ngx_atomic.h
--- a/src/os/unix/ngx_atomic.h	Tue Nov 28 13:09:54 2017 +0300
+++ b/src/os/unix/ngx_atomic.h	Tue Nov 28 21:24:22 2017 +0530
@@ -88,9 +88,8 @@
 typedef volatile ngx_atomic_uint_t  ngx_atomic_t;
 
 
-#elif (NGX_HAVE_GCC_ATOMIC)
+#elif (NGX_HAVE_GCC_ATOMIC || NGX_HAVE_GCC_SYNC_ATOMIC)
 
-/* GCC 4.1 builtin atomic operations */
 
 #define NGX_HAVE_ATOMIC_OPS  1
 
@@ -105,7 +104,24 @@
 
 typedef volatile ngx_atomic_uint_t  ngx_atomic_t;
 
+#if (NGX_HAVE_GCC_ATOMIC)
+/* Use GCC 5.4 builtin atomic operations */
+static inline ngx_atomic_uint_t
+ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
+    ngx_atomic_uint_t set) {
+    return __atomic_compare_exchange_n(lock, &old, set, 0,
+                                     __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+}
 
+static inline ngx_atomic_int_t
+ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add) {
+    return __atomic_fetch_add(value, add, __ATOMIC_SEQ_CST);
+}
+
+#define ngx_memory_barrier() __atomic_thread_fence(__ATOMIC_ACQUIRE)
+
+#else
+/* Use older gcc sync atomic builtin operations */
 #define ngx_atomic_cmp_set(lock, old, set)                                    \
     __sync_bool_compare_and_swap(lock, old, set)
 
@@ -113,6 +129,8 @@
     __sync_fetch_and_add(value, add)
 
 #define ngx_memory_barrier()        __sync_synchronize()
+#endif
+
 
 #if ( __i386__ || __i386 || __amd64__ || __amd64 )
 #define ngx_cpu_pause()             __asm__ ("pause")


More information about the nginx-devel mailing list