From: Francis Dupont Date: Tue, 22 Oct 2019 10:51:29 +0000 (+0200) Subject: [962-implement-the-multi_threading_mgr-h-idea] Added the Multi Threading Manager X-Git-Tag: Kea-1.7.1~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59b9166aa4b449d89c14bb8bbde9921057b339d4;p=thirdparty%2Fkea.git [962-implement-the-multi_threading_mgr-h-idea] Added the Multi Threading Manager --- diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am index 4280b608b3..45d2b2ab36 100644 --- a/src/lib/util/Makefile.am +++ b/src/lib/util/Makefile.am @@ -16,6 +16,7 @@ libkea_util_la_SOURCES += hash.h libkea_util_la_SOURCES += labeled_value.h labeled_value.cc libkea_util_la_SOURCES += memory_segment.h libkea_util_la_SOURCES += memory_segment_local.h memory_segment_local.cc +libkea_util_la_SOURCES += multi_threading_mgr.h multi_threading_mgr.cc libkea_util_la_SOURCES += optional.h libkea_util_la_SOURCES += pid_file.h pid_file.cc libkea_util_la_SOURCES += pointer_util.h diff --git a/src/lib/util/multi_threading_mgr.cc b/src/lib/util/multi_threading_mgr.cc new file mode 100644 index 0000000000..2185946cbf --- /dev/null +++ b/src/lib/util/multi_threading_mgr.cc @@ -0,0 +1,42 @@ +// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +namespace isc { +namespace util { + +BaseMultiThreadingMgr::BaseMultiThreadingMgr() : enabled_(false) { +} + +BaseMultiThreadingMgr::~BaseMultiThreadingMgr() { +} + +bool +BaseMultiThreadingMgr::getMode() const +{ + return (enabled_); +} + +void +BaseMultiThreadingMgr::setMode(bool enabled) { + enabled_ = enabled; +} + +MultiThreadingMgr::MultiThreadingMgr() : BaseMultiThreadingMgr() { +} + +MultiThreadingMgr::~MultiThreadingMgr() { +} + +BaseMultiThreadingMgr& +MultiThreadingMgr::instance() { + static MultiThreadingMgr manager; + return (manager); +} + +} // namespace isc::util +} // namespace isc diff --git a/src/lib/util/multi_threading_mgr.h b/src/lib/util/multi_threading_mgr.h new file mode 100644 index 0000000000..4cbe6aee44 --- /dev/null +++ b/src/lib/util/multi_threading_mgr.h @@ -0,0 +1,100 @@ +// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef MULTI_THREADING_MGR_H +#define MULTI_THREADING_MGR_H + +#include + +namespace isc { +namespace util { + +/// @file multi_threading_mgr.h Multi Threading Manager. +/// +/// This singleton class holds the multi-threading mode. +/// +/// It is split into two classes to hide the setMode method from the +/// instance static method. +/// +/// The standard way to use it is: +/// @code +/// if (MultiThreadingMgr::instance().getMode) { +/// multi-threaded code +/// } else { +/// single-threaded code +/// } +/// @endcode +/// +/// For instance for a class protected by its mutex: +/// @code +/// namespace locked { +/// void foo() { ... } +/// } // end of locked namespace +/// +/// void foo() { +/// if (MultiThreadingMgr::instance().getMode) { +/// lock_guard lock(mutex_); +/// locked::foo(); +/// } else { +/// locked::foo(); +/// } +/// } +/// @endcode + +/// @brief The base class hiding the setter. +class BaseMultiThreadingMgr : public boost::noncopyable { +public: + + /// @brief Get the mode. + /// + /// @return the current mode: true if multi-threading is enabled, + /// false otherwise. + bool getMode() const; + +protected: + + /// @brief Constructor. + BaseMultiThreadingMgr(); + + /// @brief Destructor. + virtual ~BaseMultiThreadingMgr(); + + /// @brief Set the mode. + /// + /// @param mode The new mode. + void setMode(bool enabled); + +private: + /// @brief the current mode. + bool enabled_; +}; + +/// @brief The class providing instance and setter. +class MultiThreadingMgr : public BaseMultiThreadingMgr { +public: + + /// @brief Returns a single instance of Multi Threading Manager. + /// + /// MultiThreadingMgr is a singleton and this method is the only way + /// of accessing it. + /// + /// @return the single instance. + static BaseMultiThreadingMgr& instance(); + + using BaseMultiThreadingMgr::setMode; + +protected: + /// @brief Constructor. + MultiThreadingMgr(); + + /// @brief Destructor. + virtual ~MultiThreadingMgr(); +}; + +} // namespace isc::dhcp +} // namespace isc + +#endif // MULTI_THREADING_MGR_H diff --git a/src/lib/util/tests/Makefile.am b/src/lib/util/tests/Makefile.am index b8b4eeedcb..68b3e31ea3 100644 --- a/src/lib/util/tests/Makefile.am +++ b/src/lib/util/tests/Makefile.am @@ -44,6 +44,7 @@ run_unittests_SOURCES += labeled_value_unittest.cc run_unittests_SOURCES += memory_segment_local_unittest.cc run_unittests_SOURCES += memory_segment_common_unittest.h run_unittests_SOURCES += memory_segment_common_unittest.cc +run_unittests_SOURCES += multi_threading_mgr_unittest.cc run_unittests_SOURCES += optional_unittest.cc run_unittests_SOURCES += pid_file_unittest.cc run_unittests_SOURCES += process_spawn_unittest.cc diff --git a/src/lib/util/tests/multi_threading_mgr_unittest.cc b/src/lib/util/tests/multi_threading_mgr_unittest.cc new file mode 100644 index 0000000000..0bc42ce05f --- /dev/null +++ b/src/lib/util/tests/multi_threading_mgr_unittest.cc @@ -0,0 +1,26 @@ +// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +#include + +#include + +using namespace isc::util; + +// Verifies that the default mode is false (MT disabled). +TEST(MultiThreadingMgrTest, default) { + EXPECT_FALSE(MultiThreadingMgr::instance().getMode()); +} + +// Verifies that the instance can be dynamic cast and setter works. +TEST(MultiThreadingMgrTest, setMode) { + EXPECT_NO_THROW(dynamic_cast(MultiThreadingMgr::instance()).setMode(true)); + EXPECT_TRUE(MultiThreadingMgr::instance().getMode()); + EXPECT_NO_THROW(dynamic_cast(MultiThreadingMgr::instance()).setMode(false)); + EXPECT_FALSE(MultiThreadingMgr::instance().getMode()); +}