Commit 59b9166a authored by Francis Dupont's avatar Francis Dupont

[962-implement-the-multi_threading_mgr-h-idea] Added the Multi Threading Manager

parent 70477262
...@@ -16,6 +16,7 @@ libkea_util_la_SOURCES += hash.h ...@@ -16,6 +16,7 @@ libkea_util_la_SOURCES += hash.h
libkea_util_la_SOURCES += labeled_value.h labeled_value.cc libkea_util_la_SOURCES += labeled_value.h labeled_value.cc
libkea_util_la_SOURCES += memory_segment.h libkea_util_la_SOURCES += memory_segment.h
libkea_util_la_SOURCES += memory_segment_local.h memory_segment_local.cc 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 += optional.h
libkea_util_la_SOURCES += pid_file.h pid_file.cc libkea_util_la_SOURCES += pid_file.h pid_file.cc
libkea_util_la_SOURCES += pointer_util.h libkea_util_la_SOURCES += pointer_util.h
......
// 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
// 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
...@@ -44,6 +44,7 @@ run_unittests_SOURCES += labeled_value_unittest.cc ...@@ -44,6 +44,7 @@ run_unittests_SOURCES += labeled_value_unittest.cc
run_unittests_SOURCES += memory_segment_local_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.h
run_unittests_SOURCES += memory_segment_common_unittest.cc 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 += optional_unittest.cc
run_unittests_SOURCES += pid_file_unittest.cc run_unittests_SOURCES += pid_file_unittest.cc
run_unittests_SOURCES += process_spawn_unittest.cc run_unittests_SOURCES += process_spawn_unittest.cc
......
// 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());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment