diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2018-12-12 17:20:05 +0000 |
---|---|---|
committer | Sara Golemon <sara.golemon@mongodb.com> | 2018-12-13 17:55:52 +0000 |
commit | c6b193afb10e7bed126985f12d32b0b07e493711 (patch) | |
tree | 07bcf0c83b1d6a9c29c8d725aa8a4e542f836aa3 | |
parent | acefbb86080b63c847700c764d08c5d8bcd9c58f (diff) | |
download | mongo-c6b193afb10e7bed126985f12d32b0b07e493711.tar.gz |
SERVER-38566 Use static_assert() for runtime thread safety violations
-rw-r--r-- | buildscripts/idl/idl/generator.py | 2 | ||||
-rw-r--r-- | src/mongo/idl/server_parameter_with_storage.h | 17 | ||||
-rw-r--r-- | src/mongo/idl/server_parameter_with_storage_test.cpp | 4 |
3 files changed, 11 insertions, 12 deletions
diff --git a/buildscripts/idl/idl/generator.py b/buildscripts/idl/idl/generator.py index 4c3e77dc1c7..93ec9386565 100644 --- a/buildscripts/idl/idl/generator.py +++ b/buildscripts/idl/idl/generator.py @@ -1737,7 +1737,7 @@ class _CppSourceFileWriter(_CppFileWriterBase): if param.cpp_varname is not None: self._writer.write_line( common.template_args( - 'auto* ret = makeIDLServerParameterWithStorage(${name}, ${storage}, ${spt});', + 'auto* ret = makeIDLServerParameterWithStorage<${spt}>(${name}, ${storage});', storage=param.cpp_varname, spt=param.set_at, name=_encaps(param.name))) if param.on_update is not None: diff --git a/src/mongo/idl/server_parameter_with_storage.h b/src/mongo/idl/server_parameter_with_storage.h index 9e3ec0960a3..962312e78b4 100644 --- a/src/mongo/idl/server_parameter_with_storage.h +++ b/src/mongo/idl/server_parameter_with_storage.h @@ -175,7 +175,7 @@ struct storage_wrapper { /** * Specialization of ServerParameter used by IDL generator. */ -template <typename T> +template <ServerParameterType paramType, typename T> class IDLServerParameterWithStorage : public ServerParameter { private: using SPT = ServerParameterType; @@ -185,14 +185,14 @@ public: static constexpr bool thread_safe = SW::thread_safe; using element_type = typename SW::type; - IDLServerParameterWithStorage(StringData name, T& storage, ServerParameterType paramType) + IDLServerParameterWithStorage(StringData name, T& storage) : ServerParameter(ServerParameterSet::getGlobal(), name, paramType == SPT::kStartupOnly || paramType == SPT::kStartupAndRuntime, paramType == SPT::kRuntimeOnly || paramType == SPT::kStartupAndRuntime), _storage(storage) { - invariant(thread_safe || paramType == SPT::kStartupOnly, - "Runtime server parameters must be thread safe"); + static_assert(thread_safe || paramType == SPT::kStartupOnly, + "Runtime server parameters must be thread safe"); } /** @@ -318,11 +318,10 @@ private: // MSVC has trouble resolving T=decltype(param) through the above class template. // Avoid that by using this proxy factory to infer storage type. -template <typename T> -IDLServerParameterWithStorage<T>* makeIDLServerParameterWithStorage(StringData name, - T& storage, - ServerParameterType spt) { - return new IDLServerParameterWithStorage<T>(name, storage, spt); +template <ServerParameterType paramType, typename T> +IDLServerParameterWithStorage<paramType, T>* makeIDLServerParameterWithStorage(StringData name, + T& storage) { + return new IDLServerParameterWithStorage<paramType, T>(name, storage); } } // namespace mongo diff --git a/src/mongo/idl/server_parameter_with_storage_test.cpp b/src/mongo/idl/server_parameter_with_storage_test.cpp index b5965006be2..154fdfb210f 100644 --- a/src/mongo/idl/server_parameter_with_storage_test.cpp +++ b/src/mongo/idl/server_parameter_with_storage_test.cpp @@ -58,7 +58,7 @@ void doStorageTest(StringData name, const std::vector<std::string>& valid, const std::vector<std::string>& invalid) { T val; - IDLServerParameterWithStorage<T> param(name, val, spt); + IDLServerParameterWithStorage<spt, T> param(name, val); using element_type = typename decltype(param)::element_type; // Check type coersion. @@ -162,7 +162,7 @@ TEST(ServerParameterWithStorage, BoundsTest) { using idl_server_parameter_detail::LT; int val; - IDLServerParameterWithStorage<int> param("BoundsTest", val, SPT::kStartupOnly); + IDLServerParameterWithStorage<SPT::kStartupOnly, int> param("BoundsTest", val); param.addBound<GT>(10); auto status = param.setFromString("5"); |