From 19cceceb780ab13104d5a4e44c373472ac5f430d Mon Sep 17 00:00:00 2001 From: Robie Basak Date: Fri, 13 Dec 2013 15:52:07 +0000 Subject: [PATCH] SERVER-12064 SERVER-12283 Use gcc atomic builtins if available Switch to using gcc atomic builtins for atomic operations if using a new enough gcc and clang and support is available. Otherwise, fall back to the old behaviour of existing non-portable inline assembly to continue to support builds on older versions of gcc. Signed-off-by: Benety Goh --- SConstruct | 24 ++++++++++++++++++++++++ src/mongo/platform/atomic_intrinsics.h | 6 ++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/SConstruct b/SConstruct index 2d7e9a2b4a5..5c7c664bd5e 100644 --- a/SConstruct +++ b/SConstruct @@ -1431,6 +1431,30 @@ def doConfigure(myenv): if haveUUThread: myenv.Append(CPPDEFINES=['MONGO_HAVE___THREAD']) + if using_gcc() or using_clang(): + def CheckGCCAtomicBuiltins(context): + test_body = """ + int main(int argc, char **argv) { + int a = 0; + int b = 0; + int c = 0; + + __atomic_compare_exchange(&a, &b, &c, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); + return 0; + } + """ + context.Message('Checking for gcc atomic builtins... ') + ret = context.TryLink(textwrap.dedent(test_body), '.cpp') + context.Result(ret) + return ret + conf = Configure(myenv, help=False, custom_tests = { + 'CheckGCCAtomicBuiltins': CheckGCCAtomicBuiltins, + }) + haveGCCAtomicBuiltins = conf.CheckGCCAtomicBuiltins() + conf.Finish() + if haveGCCAtomicBuiltins: + conf.env.Append(CPPDEFINES=["HAVE_GCC_ATOMIC_BUILTINS"]) + conf = Configure(myenv) libdeps.setup_conftests(conf) diff --git a/src/mongo/platform/atomic_intrinsics.h b/src/mongo/platform/atomic_intrinsics.h index 23b7b6bce46..ec3da125e69 100644 --- a/src/mongo/platform/atomic_intrinsics.h +++ b/src/mongo/platform/atomic_intrinsics.h @@ -43,10 +43,12 @@ #if defined(_WIN32) #include "mongo/platform/atomic_intrinsics_win32.h" #elif defined(__GNUC__) -#if defined(__i386__) || defined(__x86_64__) +#if defined(HAVE_GCC_ATOMIC_BUILTINS) +#include "mongo/platform/atomic_intrinsics_gcc_generic.h" +#elif defined(__i386__) || defined(__x86_64__) #include "mongo/platform/atomic_intrinsics_gcc_intel.h" #else -#include "mongo/platform/atomic_intrinsics_gcc_generic.h" +#error "Unsupported platform: no gcc atomic builtins or port available" #endif #else #error "Unsupported os/compiler family"