diff options
author | Razvan Becheriu <razvan@isc.org> | 2020-03-06 22:10:18 +0100 |
---|---|---|
committer | Razvan Becheriu <razvan@isc.org> | 2020-03-06 22:10:18 +0100 |
commit | e1501e0af22c31f4891bcf69037e71cd65c34886 (patch) | |
tree | 667ee10034952a0eeeb3770bd70cab0b4de37e11 | |
parent | [#1016] moved critical section to lib util (diff) | |
download | kea-e1501e0af22c31f4891bcf69037e71cd65c34886.tar.xz kea-e1501e0af22c31f4891bcf69037e71cd65c34886.zip |
[#1016] removed unnecessary files
-rw-r--r-- | src/lib/util/Makefile.am | 1 | ||||
-rw-r--r-- | src/lib/util/multi_threading_mgr.cc | 24 | ||||
-rw-r--r-- | src/lib/util/multi_threading_mgr.h | 38 | ||||
-rw-r--r-- | src/lib/util/multi_threading_utils.cc | 52 | ||||
-rw-r--r-- | src/lib/util/multi_threading_utils.h | 58 | ||||
-rw-r--r-- | src/lib/util/tests/Makefile.am | 1 | ||||
-rw-r--r-- | src/lib/util/tests/multi_threading_mgr_unittest.cc | 83 | ||||
-rw-r--r-- | src/lib/util/tests/multi_threading_utils_unittest.cc | 99 |
8 files changed, 144 insertions, 212 deletions
diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am index 77bf9a1b65..d020e77332 100644 --- a/src/lib/util/Makefile.am +++ b/src/lib/util/Makefile.am @@ -17,7 +17,6 @@ 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 += multi_threading_utils.h multi_threading_utils.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 index 0132ac3b12..3606e5cf2f 100644 --- a/src/lib/util/multi_threading_mgr.cc +++ b/src/lib/util/multi_threading_mgr.cc @@ -100,5 +100,29 @@ MultiThreadingMgr::apply(bool enabled, uint32_t thread_count) { } } +void +MultiThreadingMgr::stopPktProcessing() { + if (getMode() && getPktThreadPoolSize() && !isInCriticalSection()) { + pkt_thread_pool_.stop(); + } +} + +void +MultiThreadingMgr::startPktProcessing() { + if (getMode() && getPktThreadPoolSize() && !isInCriticalSection()) { + pkt_thread_pool_.start(getPktThreadPoolSize()); + } +} + +MultiThreadingCriticalSection::MultiThreadingCriticalSection() { + MultiThreadingMgr::instance().stopPktProcessing(); + MultiThreadingMgr::instance().enterCriticalSection(); +} + +MultiThreadingCriticalSection::~MultiThreadingCriticalSection() { + MultiThreadingMgr::instance().exitCriticalSection(); + MultiThreadingMgr::instance().startPktProcessing(); +} + } // namespace util } // namespace isc diff --git a/src/lib/util/multi_threading_mgr.h b/src/lib/util/multi_threading_mgr.h index 5779676aed..62bfb22502 100644 --- a/src/lib/util/multi_threading_mgr.h +++ b/src/lib/util/multi_threading_mgr.h @@ -115,6 +115,16 @@ public: /// configured, 0 if auto scaling is desired void apply(bool enabled, uint32_t thread_count); + /// @brief Class method stopping and joining all threads of the pool. + /// + /// Stop the packet thread pool if running. + void stopPktProcessing(); + + /// @brief Class method (re)starting threads of the pool. + /// + /// Start the packet thread pool according to current configuration. + void startPktProcessing(); + protected: /// @brief Constructor. @@ -146,6 +156,34 @@ private: ThreadPool<std::function<void()>> pkt_thread_pool_; }; +/// @note: everything here MUST be used ONLY from the main thread. +/// When called from a thread of the pool it can deadlock. + +/// @brief RAII class creating a critical section. +/// +/// @note: the multi-threading mode MUST NOT be changed in the RAII +/// @c MultiThreadingCriticalSection body. +/// @note: starting and stopping the packet thread pool should be handled +/// in the main thread, if done on one of the processing threads will cause a +/// deadlock. +/// This is mainly useful in hook commands which handle configuration +/// changes. +class MultiThreadingCriticalSection : public boost::noncopyable { +public: + + /// @brief Constructor. + /// + /// Entering the critical section. The packet thread pool instance will be + /// stopped so that all configuration changes can be safely applied. + MultiThreadingCriticalSection(); + + /// @brief Destructor. + /// + /// Leaving the critical section. The packet thread pool instance will be + /// started according to the new configuration. + virtual ~MultiThreadingCriticalSection(); +}; + } // namespace util } // namespace isc diff --git a/src/lib/util/multi_threading_utils.cc b/src/lib/util/multi_threading_utils.cc deleted file mode 100644 index 98f553a3b6..0000000000 --- a/src/lib/util/multi_threading_utils.cc +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (C) 2019-2020 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 <exceptions/exceptions.h> -#include <util/multi_threading_mgr.h> -#include <util/multi_threading_utils.h> - - -namespace isc { -namespace util { - -void -MultiThreadingCriticalSection::stopPktProcessing() { - auto& thread_pool = MultiThreadingMgr::instance().getPktThreadPool(); - bool flag = MultiThreadingMgr::instance().isInCriticalSection(); - auto size = MultiThreadingMgr::instance().getPktThreadPoolSize(); - if (size && !flag) { - thread_pool.stop(); - } -} - -void -MultiThreadingCriticalSection::startPktProcessing() { - auto& thread_pool = MultiThreadingMgr::instance().getPktThreadPool(); - bool flag = MultiThreadingMgr::instance().isInCriticalSection(); - auto size = MultiThreadingMgr::instance().getPktThreadPoolSize(); - if (size && !flag) { - thread_pool.start(size); - } -} - -MultiThreadingCriticalSection::MultiThreadingCriticalSection() { - if (MultiThreadingMgr::instance().getMode()) { - stopPktProcessing(); - } - MultiThreadingMgr::instance().enterCriticalSection(); -} - -MultiThreadingCriticalSection::~MultiThreadingCriticalSection() { - MultiThreadingMgr::instance().exitCriticalSection(); - if (MultiThreadingMgr::instance().getMode()) { - startPktProcessing(); - } -} - -} // namespace util -} // namespace isc diff --git a/src/lib/util/multi_threading_utils.h b/src/lib/util/multi_threading_utils.h deleted file mode 100644 index cfc2e3fdca..0000000000 --- a/src/lib/util/multi_threading_utils.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (C) 2019-2020 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_UTILS_H -#define MULTI_THREADING_UTILS_H - -#include <boost/noncopyable.hpp> - -namespace isc { -namespace util { - -/// @note: everything here MUST be used ONLY from the main thread. -/// When called from a thread of the pool it can deadlock. - -/// @brief RAII class creating a critical section. -/// -/// @note: the multi-threading mode MUST NOT be changed in the RAII -/// @c MultiThreadingCriticalSection body. -/// @note: starting and stopping the packet thread pool should be handled -/// in the main thread, if done on one of the processing threads will cause a -/// deadlock. -/// This is mainly useful in hook commands which handle configuration -/// changes. -class MultiThreadingCriticalSection : public boost::noncopyable { -public: - - /// @brief Constructor. - /// - /// Entering the critical section. The packet thread pool instance will be - /// stopped so that all configuration changes can be safely applied. - MultiThreadingCriticalSection(); - - /// @brief Destructor. - /// - /// Leaving the critical section. The packet thread pool instance will be - /// started according to the new configuration. - virtual ~MultiThreadingCriticalSection(); - -private: - - /// @brief Class method stopping and joining all threads of the pool. - /// - /// Stop the packet thread pool if running. - static void stopPktProcessing(); - - /// @brief Class method (re)starting threads of the pool. - /// - /// Start the packet thread pool according to current configuration. - static void startPktProcessing(); -}; - -} // namespace util -} // namespace isc - -#endif // MULTI_THREADING_UTILS_H diff --git a/src/lib/util/tests/Makefile.am b/src/lib/util/tests/Makefile.am index 0398f2bf2d..02750dd921 100644 --- a/src/lib/util/tests/Makefile.am +++ b/src/lib/util/tests/Makefile.am @@ -45,7 +45,6 @@ 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 += multi_threading_utils_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 index ee685ace11..341d21f67c 100644 --- a/src/lib/util/tests/multi_threading_mgr_unittest.cc +++ b/src/lib/util/tests/multi_threading_mgr_unittest.cc @@ -15,7 +15,7 @@ using namespace isc::util; using namespace isc; /// @brief Verifies that the default mode is false (MT disabled). -TEST(MultiThreadingMgrTest, default) { +TEST(MultiThreadingMgrTest, defaultMode) { // MT should be disabled EXPECT_FALSE(MultiThreadingMgr::instance().getMode()); } @@ -147,3 +147,84 @@ TEST(MultiThreadingMgrTest, criticalSection) { // thread pool should be stopped EXPECT_EQ(thread_pool.size(), 0); } + +TEST(MultiThreadingMgrTest, criticalSection) { + // get the thread pool instance + auto & thread_pool = MultiThreadingMgr::instance().getPktThreadPool(); + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + // apply multi-threading configuration with 16 threads + MultiThreadingMgr::instance().apply(true, 16); + // thread count should match + EXPECT_EQ(thread_pool.size(), 16); + // use scope to test constructor and destructor + { + MultiThreadingCriticalSection cs; + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + // use scope to test constructor and destructor + { + MultiThreadingCriticalSection inner_cs; + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + } + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + } + // thread count should match + EXPECT_EQ(thread_pool.size(), 16); + // use scope to test constructor and destructor + { + MultiThreadingCriticalSection cs; + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + // apply multi-threading configuration with 64 threads + MultiThreadingMgr::instance().apply(true, 64); + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + } + // thread count should match + EXPECT_EQ(thread_pool.size(), 64); + // use scope to test constructor and destructor + { + MultiThreadingCriticalSection cs; + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + // apply multi-threading configuration with 0 threads + MultiThreadingMgr::instance().apply(false, 64); + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + } + // thread count should match + EXPECT_EQ(thread_pool.size(), 0); + // use scope to test constructor and destructor + { + MultiThreadingCriticalSection cs; + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + // use scope to test constructor and destructor + { + MultiThreadingCriticalSection inner_cs; + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + } + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + } + // thread count should match + EXPECT_EQ(thread_pool.size(), 0); + // use scope to test constructor and destructor + { + MultiThreadingCriticalSection cs; + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + // apply multi-threading configuration with 64 threads + MultiThreadingMgr::instance().apply(true, 64); + // thread pool should be stopped + EXPECT_EQ(thread_pool.size(), 0); + } + // thread count should match + EXPECT_EQ(thread_pool.size(), 64); + // apply multi-threading configuration with 0 threads + MultiThreadingMgr::instance().apply(false, 0); +} diff --git a/src/lib/util/tests/multi_threading_utils_unittest.cc b/src/lib/util/tests/multi_threading_utils_unittest.cc deleted file mode 100644 index a025ac1a48..0000000000 --- a/src/lib/util/tests/multi_threading_utils_unittest.cc +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (C) 2020 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 <util/multi_threading_utils.h> - -#include <gtest/gtest.h> - -using namespace isc::util; - -namespace { - -TEST(MultiThreadingUtil, constructorAndDestructor) { - // get the thread pool instance - auto & thread_pool = MultiThreadingMgr::instance().getPktThreadPool(); - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - // apply multi-threading configuration with 16 threads - MultiThreadingMgr::instance().apply(true, 16); - // thread count should match - EXPECT_EQ(thread_pool.size(), 16); - // use scope to test constructor and destructor - { - MultiThreadingCriticalSection cs; - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - // use scope to test constructor and destructor - { - MultiThreadingCriticalSection inner_cs; - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - } - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - } - // thread count should match - EXPECT_EQ(thread_pool.size(), 16); - // use scope to test constructor and destructor - { - MultiThreadingCriticalSection cs; - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - // apply multi-threading configuration with 64 threads - MultiThreadingMgr::instance().apply(true, 64); - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - } - // thread count should match - EXPECT_EQ(thread_pool.size(), 64); - // use scope to test constructor and destructor - { - MultiThreadingCriticalSection cs; - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - // apply multi-threading configuration with 0 threads - MultiThreadingMgr::instance().apply(false, 64); - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - } - // thread count should match - EXPECT_EQ(thread_pool.size(), 0); - // use scope to test constructor and destructor - { - MultiThreadingCriticalSection cs; - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - // use scope to test constructor and destructor - { - MultiThreadingCriticalSection inner_cs; - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - } - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - } - // thread count should match - EXPECT_EQ(thread_pool.size(), 0); - // use scope to test constructor and destructor - { - MultiThreadingCriticalSection cs; - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - // apply multi-threading configuration with 64 threads - MultiThreadingMgr::instance().apply(true, 64); - // thread pool should be stopped - EXPECT_EQ(thread_pool.size(), 0); - } - // thread count should match - EXPECT_EQ(thread_pool.size(), 64); - // apply multi-threading configuration with 0 threads - MultiThreadingMgr::instance().apply(false, 0); -} - -} // namespace |