diff options
author | Razvan Becheriu <razvan@isc.org> | 2022-04-05 14:26:38 +0200 |
---|---|---|
committer | Razvan Becheriu <razvan@isc.org> | 2023-11-15 07:36:55 +0100 |
commit | 4d0df6dcd7ce579ff8bf09345fcef7526400b7b3 (patch) | |
tree | 4937a0c31f1ac8bfb0ddfdc4fe3c988532968176 | |
parent | [#1599] added pause with wait for threads to stop (diff) | |
download | kea-4d0df6dcd7ce579ff8bf09345fcef7526400b7b3.tar.xz kea-4d0df6dcd7ce579ff8bf09345fcef7526400b7b3.zip |
[#1599] updated unittests
-rw-r--r-- | src/lib/util/multi_threading_mgr.cc | 14 | ||||
-rw-r--r-- | src/lib/util/tests/multi_threading_mgr_unittest.cc | 2 | ||||
-rw-r--r-- | src/lib/util/tests/thread_pool_unittest.cc | 1 | ||||
-rw-r--r-- | src/lib/util/thread_pool.h | 29 |
4 files changed, 40 insertions, 6 deletions
diff --git a/src/lib/util/multi_threading_mgr.cc b/src/lib/util/multi_threading_mgr.cc index a48a1b14ee..57c7f8afd7 100644 --- a/src/lib/util/multi_threading_mgr.cc +++ b/src/lib/util/multi_threading_mgr.cc @@ -46,9 +46,8 @@ MultiThreadingMgr::enterCriticalSection() { if (getMode() && !inside) { if (getThreadPoolSize()) { // We simply pause without waiting for all tasks to complete. - // We could also call pause(false) which does not wait for - // threads to stop and wait() so that all tasks are complete - // and threads are stopped. + // We could also call wait() and pause(false) so that all tasks are + // complete and threads are stopped. thread_pool_.pause(); } // Now it is safe to call callbacks which can also create other CSs. @@ -71,7 +70,14 @@ MultiThreadingMgr::exitCriticalSection() { --critical_section_count_; if (getMode() && !isInCriticalSection()) { if (getThreadPoolSize()) { - thread_pool_.resume(); + // If apply has been called, threads have never been started inside + // a critical section, so start them now, otherwise just resume + // paused threads. + if (!thread_pool_.enabled()) { + thread_pool_.start(getThreadPoolSize()); + } else { + thread_pool_.resume(); + } } // Now it is safe to call callbacks which can also create other CSs. callExitCallbacks(); diff --git a/src/lib/util/tests/multi_threading_mgr_unittest.cc b/src/lib/util/tests/multi_threading_mgr_unittest.cc index 9c9828a233..ddaf779dcf 100644 --- a/src/lib/util/tests/multi_threading_mgr_unittest.cc +++ b/src/lib/util/tests/multi_threading_mgr_unittest.cc @@ -294,7 +294,7 @@ public: /// /// @return True if the pool is running, false otherwise. bool isThreadPoolRunning() { - return (MultiThreadingMgr::instance().getThreadPool().size()); + return (!MultiThreadingMgr::instance().getThreadPool().paused()); } /// @brief Checks callback invocations over a series of nested diff --git a/src/lib/util/tests/thread_pool_unittest.cc b/src/lib/util/tests/thread_pool_unittest.cc index 439f7a3ed3..b45c09b2b9 100644 --- a/src/lib/util/tests/thread_pool_unittest.cc +++ b/src/lib/util/tests/thread_pool_unittest.cc @@ -776,7 +776,6 @@ TEST_F(ThreadPoolTest, pauseAndResume) { ASSERT_EQ(thread_pool.count(), 0); // the thread count should be 0 ASSERT_EQ(thread_pool.size(), 0); ->>>>>>> 0d7199fdd8 ([#1599] implemented pause and resume) } /// @brief test ThreadPool max queue size diff --git a/src/lib/util/thread_pool.h b/src/lib/util/thread_pool.h index 28e66c09d9..aecd261ca3 100644 --- a/src/lib/util/thread_pool.h +++ b/src/lib/util/thread_pool.h @@ -156,6 +156,24 @@ struct ThreadPool { queue_.resume(); } + /// @brief return the state of the queue + /// + /// Returns the state of the queue + /// + /// @return the state + bool enabled() { + return (queue_.enabled()); + } + + /// @brief return the state of the threads + /// + /// Returns the state of the threads + /// + /// @return the state + bool paused() { + return (queue_.paused()); + } + /// @brief set maximum number of work items in the queue /// /// @param max_queue_size the maximum size (0 means unlimited) @@ -490,9 +508,11 @@ private: void disable() { { std::lock_guard<std::mutex> lock(mutex_); + paused_ = false; enabled_ = false; } // Notify pop so that it can exit. + pause_cv_.notify_all(); cv_.notify_all(); } @@ -505,6 +525,15 @@ private: return (enabled_); } + /// @brief return the state of the threads + /// + /// Returns the state of the threads + /// + /// @return the state + bool paused() { + return (paused_); + } + private: /// @brief underlying queue container QueueContainer queue_; |