]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[962-implement-the-multi_threading_mgr-h-idea] Added the Multi Threading Manager
authorFrancis Dupont <fdupont@isc.org>
Tue, 22 Oct 2019 10:51:29 +0000 (12:51 +0200)
committerFrancis Dupont <fdupont@isc.org>
Wed, 23 Oct 2019 15:15:40 +0000 (17:15 +0200)
src/lib/util/Makefile.am
src/lib/util/multi_threading_mgr.cc [new file with mode: 0644]
src/lib/util/multi_threading_mgr.h [new file with mode: 0644]
src/lib/util/tests/Makefile.am
src/lib/util/tests/multi_threading_mgr_unittest.cc [new file with mode: 0644]

index 4280b608b3c48ba60455e48ff07d5ccbce720a3b..45d2b2ab36582bcf9f991b1ba3c8dd73a7dffb88 100644 (file)
@@ -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 (file)
index 0000000..2185946
--- /dev/null
@@ -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 <util/multi_threading_mgr.h>
+
+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 (file)
index 0000000..4cbe6ae
--- /dev/null
@@ -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 <boost/noncopyable.hpp>
+
+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<mutex> 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
index b8b4eeedcb5a10774b906f250f3e2ebde91c4108..68b3e31ea39fbd6e6079de685ab9db471f2831a9 100644 (file)
@@ -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 (file)
index 0000000..0bc42ce
--- /dev/null
@@ -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 <config.h>
+
+#include <util/multi_threading_mgr.h>
+
+#include <gtest/gtest.h>
+
+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&>(MultiThreadingMgr::instance()).setMode(true));
+    EXPECT_TRUE(MultiThreadingMgr::instance().getMode());
+    EXPECT_NO_THROW(dynamic_cast<MultiThreadingMgr&>(MultiThreadingMgr::instance()).setMode(false));
+    EXPECT_FALSE(MultiThreadingMgr::instance().getMode());
+}