From: Razvan Becheriu Date: Tue, 5 Apr 2022 12:26:38 +0000 (+0300) Subject: [#1599] updated unittests X-Git-Tag: Kea-2.5.4~38 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4d0df6dcd7ce579ff8bf09345fcef7526400b7b3;p=thirdparty%2Fkea.git [#1599] updated unittests --- 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 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_;