From 3afcf5a0407c69b32603a249e8e7b3d309584a85 Mon Sep 17 00:00:00 2001 From: Olivier Dion Date: Fri, 17 Mar 2023 22:37:49 +0100 Subject: [PATCH] configure: Add --enable-compiler-atomic-builtins option 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 Signed-off-by: Olivier Dion Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- README.md | 4 ++ configure.ac | 27 +++++++++++++ include/urcu/config.h.in | 3 ++ m4/ae_cc_atomic_builtins.m4 | 78 +++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 m4/ae_cc_atomic_builtins.m4 diff --git a/README.md b/README.md index a9f1688..d7966a2 100644 --- 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 ------------ diff --git a/configure.ac b/configure.ac index 978704c..7045cdc 100644 --- a/configure.ac +++ b/configure.ac @@ -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`" diff --git a/include/urcu/config.h.in b/include/urcu/config.h.in index abee4ea..aa1d6c9 100644 --- a/include/urcu/config.h.in +++ b/include/urcu/config.h.in @@ -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 index 0000000..2efbde3 --- /dev/null +++ b/m4/ae_cc_atomic_builtins.m4 @@ -0,0 +1,78 @@ +# SYNOPSIS +# +# AE_CC_ATOMIC_BUILTINS([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) +# +# LICENSE +# +# Copyright (c) 2023 Michael Jeanson +# +# 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 . +# +# 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 -- 2.39.5