configure: Add --enable-compiler-atomic-builtins option
authorOlivier Dion <odion@efficios.com>
Fri, 17 Mar 2023 21:37:49 +0000 (22:37 +0100)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 14 Aug 2023 19:38:53 +0000 (15:38 -0400)
If the toolchain supports atomic builtins and the user ask for atomic
builtins, use them for the uatomic API. This requires that the
toolchains used to compile the library and the user application supports
such builtins.

The advantage of using these builtins is that they are well known
synchronization primitives by several tools such as TSAN.

However, they may introduce redundant memory barriers, mainly on
strongly ordered architectures.

Change-Id: Ia8e97112681f744f17816dbc4cbbec805a483331
Co-authored-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Olivier Dion <odion@efficios.com>
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
README.md
configure.ac
include/urcu/config.h.in
m4/ae_cc_atomic_builtins.m4 [new file with mode: 0644]

index a9f1688f5d2e1bcbd1dd577bc83f14261bc1c45f..d7966a2c54dbbad4eaf92a5e01b8d99abd7e6451 100644 (file)
--- a/README.md
+++ b/README.md
@@ -440,6 +440,10 @@ still being used to iterate on a hash table.
 This option alters the rculfhash ABI. Make sure to compile both library
 and application with matching configuration.
 
+### Usage of `--enable-compiler-atomic-builtins`
+
+Building liburcu with `--enable-compiler-atomic-builtins` implements the uatomic
+API with the compiler atomic builtins if supported.
 
 Make targets
 ------------
index 978704cfbf307457fb33e5ce0793f0350cbae4f6..7045cdc066660ed78fe93385fbbef89821fcbcd2 100644 (file)
@@ -71,6 +71,10 @@ AS_IF([test "$ac_cv_prog_cc_c99" = "no"], [AC_MSG_ERROR([The compiler does not s
 AC_USE_SYSTEM_EXTENSIONS
 AC_SYS_LARGEFILE
 
+# Check if the selected C compiler supports atomic builtins
+AE_CC_ATOMIC_BUILTINS
+
+
 ##                     ##
 ## C++ compiler checks ##
 ##                     ##
@@ -230,6 +234,11 @@ AE_FEATURE([rcu-debug], [Enable internal debugging self-checks. Introduces a per
 AE_FEATURE_DEFAULT_DISABLE
 AE_FEATURE([cds-lfht-iter-debug], [Enable extra debugging checks for lock-free hash table iterator traversal. Alters the rculfhash ABI. Make sure to compile both library and application with matching configuration.])
 
+# Use compiler atomic builtins, when disabled use our legacy uatomic implementation.
+# Disabled by default
+AE_FEATURE_DEFAULT_DISABLE
+AE_FEATURE([compiler-atomic-builtins], [Enable the use of compiler atomic builtins.])
+
 # When given, add -Werror to WARN_CFLAGS and WARN_CXXFLAGS.
 # Disabled by default
 AE_FEATURE_DEFAULT_DISABLE
@@ -259,6 +268,9 @@ AE_IF_FEATURE_ENABLED([cds-lfht-iter-debug], [
   AC_DEFINE([CONFIG_CDS_LFHT_ITER_DEBUG], [1], [Enable extra debugging checks for lock-free hash table iterator traversal. Alters the rculfhash ABI. Make sure to compile both library and application with matching configuration.])
 ])
 
+AE_IF_FEATURE_ENABLED([compiler-atomic-builtins], [
+  AC_DEFINE([CONFIG_RCU_USE_ATOMIC_BUILTINS], [1], [Use compiler atomic builtins.])
+])
 
 ##                                                                          ##
 ## Set automake variables for optional feature conditionnals in Makefile.am ##
@@ -268,6 +280,17 @@ AE_IF_FEATURE_ENABLED([cds-lfht-iter-debug], [
 AM_CONDITIONAL([ENABLE_EXAMPLES], AE_IS_FEATURE_ENABLED([shared]))
 
 
+##                                          ##
+## Check for optional features dependencies ##
+##                                          ##
+
+
+AE_IF_FEATURE_ENABLED([compiler-atomic-builtins], [
+  AS_IF([test "x$ae_cv_cc_atomic_builtins" != xyes], [
+     AC_MSG_ERROR([The compiler does not support atomic builtins.])
+  ])
+])
+
 ##                                             ##
 ## Substitute variables for use in Makefile.am ##
 ##                                             ##
@@ -363,6 +386,10 @@ AE_PPRINT_PROP_BOOL([Lock-free HT iterator debugging], $value)
 
 AE_PPRINT_PROP_BOOL([Multi-flavor support], 1)
 
+# atomic builtins enabled/disabled
+AE_IS_FEATURE_ENABLED([compiler-atomic-builtins]) && value=1 || value=0
+AE_PPRINT_PROP_BOOL([Use compiler atomic builtins], $value)
+
 report_bindir="`eval eval echo $bindir`"
 report_libdir="`eval eval echo $libdir`"
 
index abee4ea301d5c30ad4d44b8597c436873346264d..aa1d6c92b24cb2bff2d97751aaac5cad0c4f7da1 100644 (file)
@@ -23,6 +23,9 @@
    Introduces a performance penalty. */
 #undef CONFIG_RCU_DEBUG
 
+/* Uatomic API uses atomic builtins. */
+#undef CONFIG_RCU_USE_ATOMIC_BUILTINS
+
 /* Expose multi-flavor support */
 #define CONFIG_RCU_HAVE_MULTIFLAVOR 1
 
diff --git a/m4/ae_cc_atomic_builtins.m4 b/m4/ae_cc_atomic_builtins.m4
new file mode 100644 (file)
index 0000000..2efbde3
--- /dev/null
@@ -0,0 +1,78 @@
+# SYNOPSIS
+#
+#   AE_CC_ATOMIC_BUILTINS([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
+#
+# LICENSE
+#
+#   Copyright (c) 2023 Michael Jeanson <mjeanson@efficios.com>
+#
+#   This program is free software; you can redistribute it and/or modify it
+#   under the terms of the GNU General Public License as published by the
+#   Free Software Foundation; either version 2 of the License, or (at your
+#   option) any later version.
+#
+#   This program is distributed in the hope that it will be useful, but
+#   WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+#   Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License along
+#   with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+#   As a special exception, the respective Autoconf Macro's copyright owner
+#   gives unlimited permission to copy, distribute and modify the configure
+#   scripts that are the output of Autoconf when processing the Macro. You
+#   need not follow the terms of the GNU General Public License when using
+#   or distributing such scripts, even though portions of the text of the
+#   Macro appear in them. The GNU General Public License (GPL) does govern
+#   all other use of the material that constitutes the Autoconf Macro.
+#
+#   This special exception to the GPL applies to versions of the Autoconf
+#   Macro released by the Autoconf Archive. When you make and distribute a
+#   modified version of the Autoconf Macro, you may extend this special
+#   exception to the GPL to apply to your modified version as well.
+
+#serial 1
+
+AC_DEFUN([AE_CC_ATOMIC_BUILTINS], [
+AC_REQUIRE([AC_PROG_CC])
+AC_LANG_PUSH([C])
+
+AC_CACHE_CHECK(
+  [whether $CC supports atomic builtins],
+  [ae_cv_cc_atomic_builtins],
+  [
+    AC_LINK_IFELSE([
+      AC_LANG_PROGRAM([
+        int x, y;
+      ],[
+        __atomic_store_n(&x, 0, __ATOMIC_RELAXED);
+        __atomic_load_n(&x, __ATOMIC_RELAXED);
+        y = __atomic_exchange_n(&x, 1, __ATOMIC_RELAXED);
+        __atomic_compare_exchange_n(&x, &y, 0, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
+        __atomic_add_fetch(&x, 1, __ATOMIC_RELAXED);
+        __atomic_sub_fetch(&x, 1, __ATOMIC_RELAXED);
+        __atomic_and_fetch(&x, 0x01, __ATOMIC_RELAXED);
+        __atomic_or_fetch(&x, 0x01, __ATOMIC_RELAXED);
+        __atomic_thread_fence(__ATOMIC_RELAXED);
+        __atomic_signal_fence(__ATOMIC_RELAXED);
+      ])
+    ],[
+      ae_cv_cc_atomic_builtins=yes
+    ],[
+      ae_cv_cc_atomic_builtins=no
+    ])
+  ]
+)
+
+# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
+if test "x$ae_cv_cc_atomic_builtins" = "xyes"; then
+  $1
+  :
+else
+  $2
+  :
+fi
+
+AC_LANG_POP
+])dnl AE_CC_ATOMIC_BUILTINS
This page took 0.028915 seconds and 4 git commands to generate.