diff options
author | Andrew Morrow <acm@mongodb.com> | 2017-06-19 12:27:08 -0400 |
---|---|---|
committer | Andrew Morrow <acm@mongodb.com> | 2017-06-20 09:47:22 -0400 |
commit | e05068da490e900c68d5254a7336d6f7b9354ad6 (patch) | |
tree | 3ecac75a41fa59fe9f69408629556001fc10eb56 | |
parent | 3d25846ea31d91468475086035f4794ad126ed0f (diff) | |
download | mongo-e05068da490e900c68d5254a7336d6f7b9354ad6.tar.gz |
SERVER-29712 Add a polyfill for std::hardware_constructive_interference_size
-rw-r--r-- | SConstruct | 44 | ||||
-rw-r--r-- | src/mongo/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/config.h.in | 3 | ||||
-rw-r--r-- | src/mongo/stdx/new.h | 58 |
4 files changed, 106 insertions, 0 deletions
diff --git a/SConstruct b/SConstruct index d76ccfab807..041967fc278 100644 --- a/SConstruct +++ b/SConstruct @@ -2957,6 +2957,50 @@ def doConfigure(myenv): if not check_all_atomics(' with libatomic'): myenv.ConfError("The toolchain does not support std::atomic, cannot continue") + def CheckExtendedAlignment(context, size): + test_body = """ + #include <atomic> + #include <mutex> + #include <cstddef> + + static_assert(alignof(std::max_align_t) < {0}, "whatever"); + + alignas({0}) std::mutex aligned_mutex; + alignas({0}) std::atomic<int> aligned_atomic; + + struct alignas({0}) aligned_struct_mutex {{ + std::mutex m; + }}; + + struct alignas({0}) aligned_struct_atomic {{ + std::atomic<int> m; + }}; + + struct holds_aligned_mutexes {{ + alignas({0}) std::mutex m1; + alignas({0}) std::mutex m2; + }} hm; + + struct holds_aligned_atomics {{ + alignas({0}) std::atomic<int> a1; + alignas({0}) std::atomic<int> a2; + }} ha; + """.format(size) + + context.Message('Checking for extended alignment {0} for concurrency types... '.format(size)) + ret = context.TryCompile(textwrap.dedent(test_body), ".cpp") + context.Result(ret) + return ret + + conf.AddTest('CheckExtendedAlignment', CheckExtendedAlignment) + + # We are aware of no architecture with a cache line bigger than + # 256. If we had a way to get the cache line size of TARGET_ARCH + # that would be better, but we don't seem to have that. + for size in (64, 128, 256): + if conf.CheckExtendedAlignment(size): + conf.env.SetConfigHeaderDefine("MONGO_CONFIG_MAX_EXTENDED_ALIGNMENT", size) + # ask each module to configure itself and the build environment. moduleconfig.configure_modules(mongo_modules, conf) diff --git a/src/mongo/SConscript b/src/mongo/SConscript index 25bc8155ee6..e0a230e17fb 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -255,6 +255,7 @@ config_header_substs = ( ('@mongo_config_have_std_make_unique@', 'MONGO_CONFIG_HAVE_STD_MAKE_UNIQUE'), ('@mongo_config_have_strnlen@', 'MONGO_CONFIG_HAVE_STRNLEN'), ('@mongo_config_have_thread_local@', 'MONGO_CONFIG_HAVE_THREAD_LOCAL'), + ('@mongo_config_max_extended_alignment@', 'MONGO_CONFIG_MAX_EXTENDED_ALIGNMENT'), ('@mongo_config_optimized_build@', 'MONGO_CONFIG_OPTIMIZED_BUILD'), ('@mongo_config_ssl@', 'MONGO_CONFIG_SSL'), ('@mongo_config_ssl_has_asn1_any_definitions@', 'MONGO_CONFIG_HAVE_ASN1_ANY_DEFINITIONS'), diff --git a/src/mongo/config.h.in b/src/mongo/config.h.in index a47b644300d..02b8d94d566 100644 --- a/src/mongo/config.h.in +++ b/src/mongo/config.h.in @@ -67,6 +67,9 @@ // Defined if thread_local storage class is available @mongo_config_have_thread_local@ +// A number, if we have some extended alignment ability +@mongo_config_max_extended_alignment@ + // Defined if building an optimized build @mongo_config_optimized_build@ diff --git a/src/mongo/stdx/new.h b/src/mongo/stdx/new.h new file mode 100644 index 00000000000..158223d35d2 --- /dev/null +++ b/src/mongo/stdx/new.h @@ -0,0 +1,58 @@ +/** + * Copyright (C) 2017 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +#include "mongo/config.h" + +#include <cstddef> +#include <new> + +namespace mongo { +namespace stdx { + +#if __cplusplus < 201703L + +#if defined(MONGO_CONFIG_MAX_EXTENDED_ALIGNMENT) +static_assert(MONGO_CONFIG_MAX_EXTENDED_ALIGNMENT >= sizeof(uint64_t), "Bad extended alignment"); +constexpr std::size_t hardware_destructive_interference_size = MONGO_CONFIG_MAX_EXTENDED_ALIGNMENT; +#else +constexpr std::size_t hardware_destructive_interference_size = alignof(std::max_align_t); +#endif + +constexpr auto hardware_constructive_interference_size = hardware_destructive_interference_size; + +#else + +using std::hardware_constructive_interference_size; +using std::hardware_destructive_interference_size; + +#endif + +} // namespace stdx +} // namespace mongo |