Kea: io_service_thread_pool.h Source File

| Kea 3.2.0-git

io_service_thread_pool.h
Go to the documentation of this file.
1// Copyright (C) 2022-2024 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 IOSERVICE_THREAD_POOL_H
8#define IOSERVICE_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 asiolink {
22
24 class IoServiceThreadPool {
25public:
27 enum class State {
28 STOPPED,
29 RUNNING,
30 PAUSED,
31 };
32
43 IoServiceThreadPool(asiolink::IOServicePtr io_service, size_t pool_size,
44 bool defer_start = false);
45
49 ~IoServiceThreadPool();
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
105 void checkPausePermissions();
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:
213 asiolink::IOServicePtr getIOService() const;
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
260 typedef boost::shared_ptr<IoServiceThreadPool> IoServiceThreadPoolPtr;
261
262} // end of namespace isc::asiolink
263} // end of namespace isc
264
265#endif
isc::asiolink::IoServiceThreadPool::getIOService
asiolink::IOServicePtr getIOService() const
Fetches the IOService that drives the pool.
Definition io_service_thread_pool.cc:283
isc::asiolink::IoServiceThreadPool::checkPausePermissions
void checkPausePermissions()
Check current thread permissions to transition to the new PAUSED state.
Definition io_service_thread_pool.cc:106
isc::asiolink::IoServiceThreadPool::getPoolSize
uint16_t getPoolSize() const
Fetches the maximum size of the thread pool.
Definition io_service_thread_pool.cc:288
isc::asiolink::IoServiceThreadPool::~IoServiceThreadPool
~IoServiceThreadPool()
Destructor.
Definition io_service_thread_pool.cc:54
isc::asiolink::IoServiceThreadPool::stop
void stop()
Transitions the pool from RUNNING or PAUSED to STOPPED.
Definition io_service_thread_pool.cc:69
isc::asiolink::IoServiceThreadPool::pause
void pause()
Transitions the pool from RUNNING to PAUSED.
Definition io_service_thread_pool.cc:64
isc::asiolink::IoServiceThreadPool::IoServiceThreadPool
IoServiceThreadPool(asiolink::IOServicePtr io_service, size_t pool_size, bool defer_start=false)
Constructor.
Definition io_service_thread_pool.cc:30
isc::asiolink::IoServiceThreadPool::isPaused
bool isPaused()
Check if the thread pool is paused.
Definition io_service_thread_pool.h:84
isc::asiolink::IoServiceThreadPool::isStopped
bool isStopped()
Check if the thread pool is stopped.
Definition io_service_thread_pool.h:91
isc::asiolink::IoServiceThreadPool::run
void run()
Transitions the pool from STOPPED or PAUSED to RUNNING.
Definition io_service_thread_pool.cc:59
isc::asiolink::IoServiceThreadPool::getThreadCount
uint16_t getThreadCount() const
Fetches the number of threads in the pool.
Definition io_service_thread_pool.cc:293
isc::asiolink::IoServiceThreadPool::State
State
Describes the possible operational state of the thread pool.
Definition io_service_thread_pool.h:27
isc::asiolink::IoServiceThreadPool::State::STOPPED
@ STOPPED
Definition io_service_thread_pool.h:28
isc::asiolink::IoServiceThreadPool::State::RUNNING
@ RUNNING
Pool is not operational.
Definition io_service_thread_pool.h:29
isc::asiolink::IoServiceThreadPool::State::PAUSED
@ PAUSED
Pool is populated with running threads.
Definition io_service_thread_pool.h:30
isc::asiolink::IoServiceThreadPool::isRunning
bool isRunning()
Check if the thread pool is running.
Definition io_service_thread_pool.h:77
io_service.h
isc::asiolink
Definition addr_utilities.cc:170
isc::asiolink::IoServiceThreadPoolPtr
boost::shared_ptr< IoServiceThreadPool > IoServiceThreadPoolPtr
Defines a pointer to a thread pool.
Definition io_service_thread_pool.h:260
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
unlock_guard.h