]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1818] Minor refactor and cosmetics
authorThomas Markwalder <tmark@isc.org>
Fri, 14 May 2021 15:39:42 +0000 (11:39 -0400)
committerThomas Markwalder <tmark@isc.org>
Mon, 17 May 2021 14:56:50 +0000 (10:56 -0400)
src/lib/http/http_thread_pool.*
    HttpThreadPool::setRunState() - extracted thread lambda
    into it's own function, HttpThreadPool::threadWork().

src/lib/config/tests/cmd_http_listener_unittests.cc
    Removed dead function, fixed typos., fixed typos., fixed typos., fixed typos.

src/lib/http/client.*
    Fixed typos.

src/lib/config/tests/cmd_http_listener_unittests.cc
src/lib/http/client.cc
src/lib/http/client.h
src/lib/http/http_thread_pool.cc
src/lib/http/http_thread_pool.h

index f2e390fd0195ab026f5ce38e621328a7f675f7fa..13e12a0aa2223e01ae626488cb146b45d605158b 100644 (file)
@@ -208,19 +208,6 @@ public:
         }
     }
 
-    /// @brief Determines if the listener should be paused.
-    ///
-    /// @param num_pauses desired number of pauses
-    /// @param num_done number of clients that have completed their requests.
-    ///
-    /// @return True if the listener should be paused.
-    bool shouldPause(size_t num_pauses, size_t num_done) {
-        size_t request_limit = (pause_cnt_ < num_pauses ?
-                               (num_done + ((clients_.size() - num_done) / num_pauses))
-                                    : clients_.size());
-        return (request_limit);
-    }
-
     /// @brief Create an HttpResponse from a response string.
     ///
     /// @param response_str a string containing the whole HTTP
@@ -359,7 +346,7 @@ public:
 
     /// @brief Submits one or more thread commands to a CmdHttpListener.
     ///
-    /// This function command will creates a CmdHttpListener
+    /// This function command will create a CmdHttpListener
     /// with the given number of threads, initiates the given
     /// number of clients, each requesting the "thread" command,
     /// and then iteratively runs the test's IOService until all
@@ -517,7 +504,7 @@ public:
     /// @brief Pauses and resumes a CmdHttpListener while it processes command
     /// requests.
     ///
-    /// This function command will creates a CmdHttpListener
+    /// This function command will create a CmdHttpListener
     /// with the given number of threads, initiates the given
     /// number of clients, each requesting the "thread" command,
     /// and then iteratively runs the test's IOService until all
@@ -549,7 +536,7 @@ public:
 
         // Create a listener with prescribed number of threads.
         ASSERT_NO_THROW_LOG(listener_.reset(new CmdHttpListener(IOAddress(SERVER_ADDRESS),
-                                                               SERVER_PORT, num_threads)));
+                                                                SERVER_PORT, num_threads)));
         ASSERT_TRUE(listener_);
 
         // Start it and verify it is running.
@@ -568,16 +555,16 @@ public:
 
         // Now we run the client-side IOService until all requests are done,
         // errors occur or the test times out.  We'll pause and resume the
-        // given the number of pauses
+        // number of times given by num_pauses.
         size_t num_done = 0;
         size_t total_requests = clients_.size();
         while (num_done < total_requests) {
             // Calculate how many more requests to process before we pause again.
-            // We divide the number of oustanding requests by the number of pauses
-            // and stop after we've done at least that many more.
+            // We divide the number of outstanding requests by the number of pauses
+            // and stop after we've done at least that many more requests.
             size_t request_limit = (pause_cnt_ < num_pauses ?
                                     (num_done + ((total_requests - num_done) / num_pauses))
-                                    : total_requests);
+                                     : total_requests);
 
             // Run test IOService until we hit the limit.
             runIOService(request_limit);
index a2c9c68fa3a2aff57f16801d0687f68ed1f4e2b9..21c07765d7c84040ecf98fac7c5c41e53f6ae082 100644 (file)
@@ -1745,7 +1745,7 @@ public:
     /// deferred until a subsequent call to @ref start(). In this case the
     /// pool's operational state post-construction is STOPPED.  Otherwise,
     /// the thread pool threads will be created and started, with the post-
-    /// construction state being RUN.  Applicable only when thread-pool size
+    /// construction state being RUNNING.  Applicable only when thread-pool size
     /// is greater than zero.
     HttpClientImpl(IOService& io_service, size_t thread_pool_size = 0,
                    bool defer_thread_start = false)
index a9aae459f3bfd9d5728c66f3bf3bb066c6807022..2de0265f36d55dae868106ab68142c0fd6830c35 100644 (file)
@@ -145,7 +145,7 @@ public:
     /// deferred until a subsequent call to @ref start(). In this case the
     /// pool's operational state post-construction is STOPPED.  Otherwise,
     /// the thread pool threads will be created and started, with the post-
-    /// construction state being RUN.  Applicable only when thread-pool size
+    /// construction state being RUNNING.  Applicable only when thread-pool size
     /// is greater than zero.
     explicit HttpClient(asiolink::IOService& io_service, size_t thread_pool_size = 0,
                         bool defer_thread_start = false);
@@ -312,7 +312,7 @@ public:
 
     /// @brief Indicates if the thread pool processing is running.
     ///
-    /// @return True if the thread pool exists and is in the RUN state,
+    /// @return True if the thread pool exists and is in the RUNNING state,
     /// false otherwise.
     bool isRunning();
 
index 901a0aeb62d12090f4402b3a9cb550c660e8e250..a709cb80c4b6daa92f5b02a148ac41a9a6b29d79 100644 (file)
@@ -108,69 +108,13 @@ HttpThreadPool::setRunState (RunState new_state) {
 
     switch(new_state) {
     case RunState::RUNNING: {
-        // Restart the IOSerivce.
+        // Restart the IOService.
         io_service_->restart();
 
         // While we have fewer threads than we should, make more.
         while (threads_.size() < pool_size_) {
             boost::shared_ptr<std::thread> thread(new std::thread(
-            [this]() {
-                bool done = false;
-                while (!done) {
-                    switch (getRunState()) {
-                    case RunState::RUNNING: {
-                        {
-                            std::unique_lock<std::mutex> lck(mutex_);
-                            running_++;
-
-                            // If We're all running notify main thread.
-                            if (running_ == pool_size_) {
-                                main_cv_.notify_all();
-                            }
-                         }
-
-                         // Run the IOService.
-                         io_service_->run();
-
-                         {
-                            std::unique_lock<std::mutex> lck(mutex_);
-                            running_--;
-                         }
-
-                        break;
-                    }
-
-                    case RunState::PAUSED: {
-                        std::unique_lock<std::mutex> lck(mutex_);
-                        paused_++;
-
-                        // If we're all paused notify main.
-                        if (paused_ == threads_.size()) {
-                            main_cv_.notify_all();
-                        }
-
-                        // Wait here till I'm released.
-                        thread_cv_.wait(lck,
-                            [&]() {
-                                return (run_state_ != RunState::PAUSED);
-                            });
-
-                        paused_--;
-                        break;
-                    }
-
-                    case RunState::STOPPED: {
-                        done = true;
-                        break;
-                    }}
-                }
-
-                std::unique_lock<std::mutex> lck(mutex_);
-                exited_++;
-                if (exited_ == threads_.size()) {
-                    main_cv_.notify_all();
-                }
-            }));
+                std::bind(&HttpThreadPool::threadWork, this)));
 
             // Add thread to the pool.
             threads_.push_back(thread);
@@ -224,6 +168,67 @@ HttpThreadPool::setRunState (RunState new_state) {
     }}
 }
 
+void
+HttpThreadPool::threadWork() {
+    bool done = false;
+    while (!done) {
+        switch (getRunState()) {
+        case RunState::RUNNING: {
+            {
+                std::unique_lock<std::mutex> lck(mutex_);
+                running_++;
+
+                // If We're all running notify main thread.
+                if (running_ == pool_size_) {
+                    main_cv_.notify_all();
+                }
+            }
+
+            // Run the IOService.
+            io_service_->run();
+
+            {
+                std::unique_lock<std::mutex> lck(mutex_);
+                running_--;
+            }
+
+            break;
+        }
+
+        case RunState::PAUSED: {
+            std::unique_lock<std::mutex> lck(mutex_);
+            paused_++;
+
+            // If we're all paused notify main.
+            if (paused_ == threads_.size()) {
+                main_cv_.notify_all();
+            }
+
+            // Wait here till I'm released.
+            thread_cv_.wait(lck,
+                [&]() {
+                    return (run_state_ != RunState::PAUSED);
+                });
+
+            paused_--;
+            break;
+        }
+
+        case RunState::STOPPED: {
+            done = true;
+            break;
+        }}
+    }
+
+    std::unique_lock<std::mutex> lck(mutex_);
+    exited_++;
+
+    // If we've all exited, notify main.
+    if (exited_ == threads_.size()) {
+        main_cv_.notify_all();
+    }
+}
+
 IOServicePtr
 HttpThreadPool::getIOService() const {
     return (io_service_);
index d311468b173796a29d9f94094e7c4a4a084c76d5..e3bb91dc875e41120bbfc0ba76b7c113b29041bc 100644 (file)
@@ -39,7 +39,7 @@ public:
     /// @param defer_start If true, creation of the threads is deferred until
     /// a subsequent call to @ref start().  In this case the pool's operational
     /// state post construction is STOPPED.  If false, the constructor will
-    /// invoke run() to tranistion the pool into the RUNNING state.
+    /// invoke run() to transition the pool into the RUNNING state.
     HttpThreadPool(asiolink::IOServicePtr io_service, size_t pool_size,
                    bool defer_start = false);
 
@@ -77,9 +77,9 @@ public:
     RunState getRunState();
 
 private:
-    /// @brief Thread-safe change of the pool's operational state.
+    /// @brief Thread-safe change of the pool's run state.
     ///
-    /// Transitions a pool from one state to another:
+    /// Transitions a pool from one run state to another:
     ///
     /// When moving from STOPPED or PAUSED to RUNNING:
     /// -# Sets state to RUNNING.
@@ -88,6 +88,7 @@ private:
     /// -# Creates the threads if they do not yet exist (true only
     /// when transitioning from STOPPED).
     /// -# Waits until threads are running.
+    /// -# Sets the count of exited threads to 0.
     /// -# Returns to caller.
     ///
     /// When moving from RUNNING or PAUSED to STOPPED:
@@ -113,10 +114,41 @@ private:
     /// @brief Validates whether the pool can change to a given state.
     ///
     /// @param state new state for the pool.
-    /// @return true if the changs is valid, false otherwise.
+    /// @return true if the Chang's is valid, false otherwise.
     /// @note Must be called from a thread-safe context.
     bool validateStateChange(RunState state) const;
 
+    /// @brief Work function executed by each thread in the pool.
+    ///
+    /// Implements the run state responsibilities for a given thread.
+    /// It executes a run loop until the pool is stopped. At the top
+    /// of each iteration of the loop the pool's run state is checked
+    /// and when it is:
+    ///
+    /// RUNNING:
+    /// -# The count of threads running is incremented.
+    /// -# If the count has reached the number of threads in pool the
+    ///    main thread is notified.
+    /// -# IOService::run() is invoked.
+    /// -# When IOService::run() returns, the count of threads running
+    ///    is decremented.
+    ///
+    /// PAUSED:
+    /// -# The count of threads paused is incremented.
+    /// -# If the count has reached the number of threads in pool the
+    ///    main thread is notified.
+    /// -# Thread blocks until notified the pool's run state is no
+    ///    longer PAUSED.
+    /// -# The count of threads paused is decremented.
+    ///
+    /// STOPPED:
+    /// -# The run loop is exited.
+    /// -# The count of threads exited is incremented.
+    /// -# If the count has reached the number of threads in pool the
+    ///    main thread is notified.
+    /// -# function exits.
+    void threadWork();
+
 public:
 
     /// @brief Fetches the IOService that drives the pool.
@@ -160,7 +192,7 @@ private:
     /// @brief Number of threads currently running.
     size_t running_;
 
-    /// @brief Number of threads that have exited the work funcion.
+    /// @brief Number of threads that have exited the work function.
     size_t exited_;
 
     /// @brief Pool of threads used to service connections in multi-threaded