Kea 2.2.0
http_thread_pool.h
Go to the documentation of this file.
1// Copyright (C) 2021-2022 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
20namespace isc {
21namespace http {
22
25public:
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
106
107private:
117 void checkPermissions(State state);
118
122 bool checkThreadId(std::thread::id id);
123
158 void setState(State state);
159
163 State getState();
164
170 bool validateStateChange(State state) const;
171
176 static std::string stateToText(State state);
177
207 void threadWork();
208
209public:
214
218 uint16_t getPoolSize() const;
219
223 uint16_t getThreadCount() const;
224
225private:
227 size_t pool_size_;
228
230 asiolink::IOServicePtr io_service_;
231
233 State run_state_;
234
236 std::mutex mutex_;
237
239 std::condition_variable thread_cv_;
240
243 std::condition_variable main_cv_;
244
246 size_t paused_;
247
249 size_t running_;
250
252 size_t exited_;
253
256 std::list<boost::shared_ptr<std::thread> > threads_;
257};
258
260typedef boost::shared_ptr<HttpThreadPool> HttpThreadPoolPtr;
261
262} // end of namespace isc::http
263} // end of namespace isc
264
265#endif
Implements a pausable pool of IOService driven threads.
void pause()
Transitions the pool from RUNNING to PAUSED.
asiolink::IOServicePtr getIOService() const
Fetches the IOService that drives the pool.
bool isStopped()
Check if the thread pool is stopped.
bool isPaused()
Check if the thread pool is paused.
uint16_t getPoolSize() const
Fetches the maximum size of the thread pool.
void stop()
Transitions the pool from RUNNING or PAUSED to STOPPED.
void checkPausePermissions()
Check current thread permissions to transition to the new PAUSED state.
bool isRunning()
Check if the thread pool is running.
HttpThreadPool(asiolink::IOServicePtr io_service, size_t pool_size, bool defer_start=false)
Constructor.
void run()
Transitions the pool from STOPPED or PAUSED to RUNNING.
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
State
Describes the possible operational state of the thread pool.
@ RUNNING
Pool is not operational.
@ PAUSED
Pool is populated with running threads.
boost::shared_ptr< HttpThreadPool > HttpThreadPoolPtr
Defines a pointer to a thread pool.
Defines the logger used by the top-level component of kea-lfc.