]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1599] updated unittests
authorRazvan Becheriu <razvan@isc.org>
Tue, 5 Apr 2022 12:26:38 +0000 (15:26 +0300)
committerRazvan Becheriu <razvan@isc.org>
Wed, 15 Nov 2023 06:36:55 +0000 (08:36 +0200)
src/lib/util/multi_threading_mgr.cc
src/lib/util/tests/multi_threading_mgr_unittest.cc
src/lib/util/tests/thread_pool_unittest.cc
src/lib/util/thread_pool.h

index a48a1b14eed83747af9b5a7bdd6a97c0cdf18d27..57c7f8afd78d3f4f97a75bad25540e93a01b9802 100644 (file)
@@ -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();
index 9c9828a23398887b32f580253db42d7017692be4..ddaf779dcfd5aa78ec97f6a6e7de36dab21b180b 100644 (file)
@@ -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
index 439f7a3ed3cb34b01c6e35e96bb29ee40dbec658..b45c09b2b9db4e18869997004c2aca1fa3d86cd8 100644 (file)
@@ -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
index 28e66c09d9ccbf8c89bfc7a3658b799da0275ab7..aecd261ca313af7018066050d27888172bba2cd8 100644 (file)
@@ -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_;