19 #include <boost/shared_ptr.hpp>
33 HttpThreadPool::HttpThreadPool(
IOServicePtr io_service,
size_t pool_size,
35 : pool_size_(pool_size), io_service_(io_service),
36 run_state_(
State::STOPPED), mutex_(), thread_cv_(),
37 main_cv_(), paused_(0), running_(0), exited_(0) {
73 HttpThreadPool::getState() {
74 std::lock_guard<std::mutex> lck(mutex_);
79 HttpThreadPool::validateStateChange(
State new_state)
const {
93 HttpThreadPool::setState(
State new_state) {
94 std::unique_lock<std::mutex> main_lck(mutex_);
97 if (!validateStateChange(new_state)) {
101 run_state_ = new_state;
103 thread_cv_.notify_all();
108 io_service_->restart();
111 while (threads_.size() < pool_size_) {
112 boost::shared_ptr<std::thread> thread(
new std::thread(
113 std::bind(&HttpThreadPool::threadWork,
this)));
116 threads_.push_back(thread);
120 main_cv_.wait(main_lck,
122 return (running_ == threads_.size());
131 if (!io_service_->stopped()) {
137 main_cv_.wait(main_lck,
139 return (paused_ == threads_.size());
147 if (!io_service_->stopped()) {
153 main_cv_.wait(main_lck,
155 return (exited_ == threads_.size());
158 for (
auto const& thread : threads_) {
168 HttpThreadPool::threadWork() {
171 switch (getState()) {
174 std::unique_lock<std::mutex> lck(mutex_);
178 if (running_ == pool_size_) {
179 main_cv_.notify_all();
187 std::unique_lock<std::mutex> lck(mutex_);
195 std::unique_lock<std::mutex> lck(mutex_);
199 if (paused_ == threads_.size()) {
200 main_cv_.notify_all();
219 std::unique_lock<std::mutex> lck(mutex_);
223 if (exited_ == threads_.size()) {
224 main_cv_.notify_all();
230 return (io_service_);
240 return (threads_.size());
Pool is populated with running threads.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
uint16_t getPoolSize() const
Fetches the maximum size of the thread pool.
The IOService class is a wrapper for the ASIO io_service class.
void pause()
Transitions the pool from RUNNING to PAUSED.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
void run()
Transitions the pool from STOPPED or PAUSED to RUNNING.
Defines a State within the State Model.
Defines the logger used by the top-level component of kea-dhcp-ddns.
~HttpThreadPool()
Destructor.
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.