Kea  1.9.9-git
http_thread_pool.h
Go to the documentation of this file.
1 // Copyright (C) 2021 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef HTTP_THREAD_POOL_H
8 #define HTTP_THREAD_POOL_H
9 
10 #include <asiolink/io_service.h>
11 #include <util/unlock_guard.h>
12 
13 #include <boost/shared_ptr.hpp>
14 
15 #include <condition_variable>
16 #include <list>
17 #include <mutex>
18 #include <thread>
19 
20 namespace isc {
21 namespace http {
22 
25 public:
27  enum class State {
28  STOPPED,
29  RUNNING,
30  PAUSED,
31  };
32 
43  HttpThreadPool(asiolink::IOServicePtr io_service, size_t pool_size,
44  bool defer_start = false);
45 
50 
58  void run();
59 
66  void pause();
67 
72  void stop();
73 
77  bool isRunning() {
78  return (getState() == State::RUNNING);
79  }
80 
84  bool isPaused() {
85  return (getState() == State::PAUSED);
86  }
87 
91  bool isStopped() {
92  return (getState() == State::STOPPED);
93  }
94 
95 private:
128  void setState(State state);
129 
133  State getState();
134 
140  bool validateStateChange(State state) const;
141 
171  void threadWork();
172 
173 public:
178 
182  uint16_t getPoolSize() const;
183 
187  uint16_t getThreadCount() const;
188 
189 private:
191  size_t pool_size_;
192 
194  asiolink::IOServicePtr io_service_;
195 
197  State run_state_;
198 
200  std::mutex mutex_;
201 
203  std::condition_variable thread_cv_;
204 
207  std::condition_variable main_cv_;
208 
210  size_t paused_;
211 
213  size_t running_;
214 
216  size_t exited_;
217 
220  std::list<boost::shared_ptr<std::thread> > threads_;
221 };
222 
224 typedef boost::shared_ptr<HttpThreadPool> HttpThreadPoolPtr;
225 
226 } // end of namespace isc::http
227 } // end of namespace isc
228 
229 #endif
HttpThreadPool(asiolink::IOServicePtr io_service, size_t pool_size, bool defer_start=false)
Constructor.
Pool is populated with running threads.
uint16_t getPoolSize() const
Fetches the maximum size of the thread pool.
bool isPaused()
Check if the thread pool is paused.
void pause()
Transitions the pool from RUNNING to PAUSED.
bool isRunning()
Check if the thread pool is running.
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
void run()
Transitions the pool from STOPPED or PAUSED to RUNNING.
bool isStopped()
Check if the thread pool is stopped.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Implements a pausable pool of IOService driven threads.
boost::shared_ptr< HttpThreadPool > HttpThreadPoolPtr
Defines a pointer to a thread pool.
State
Describes the possible operational state of the thread pool.
asiolink::IOServicePtr getIOService() const
Fetches the IOService that drives the pool.
void stop()
Transitions the pool from RUNNING or PAUSED to STOPPED.