17#include <boost/shared_ptr.hpp>
32 : pool_size_(pool_size), io_service_(io_service),
33 run_state_(
State::STOPPED), mutex_(), thread_cv_(),
34 main_cv_(), paused_(0), running_(0), exited_(0) {
70IoServiceThreadPool::getState() {
71 std::lock_guard<std::mutex> lck(mutex_);
76IoServiceThreadPool::validateStateChange(
State state)
const {
89IoServiceThreadPool::stateToText(
State state) {
92 return (std::string(
"stopped"));
94 return (std::string(
"running"));
96 return (std::string(
"paused"));
98 return (std::string(
"unknown-state"));
107IoServiceThreadPool::checkPermissions(
State state) {
108 auto id = std::this_thread::get_id();
109 if (checkThreadId(
id)) {
111 << IoServiceThreadPool::stateToText(state) <<
" performed by worker thread");
116IoServiceThreadPool::checkThreadId(std::thread::id
id) {
117 for (
auto const& thread : threads_) {
118 if (
id == thread->get_id()) {
126IoServiceThreadPool::setState(
State state) {
127 checkPermissions(state);
129 std::unique_lock<std::mutex> main_lck(mutex_);
132 if (!validateStateChange(state)) {
138 thread_cv_.notify_all();
143 io_service_->restart();
146 while (threads_.size() < pool_size_) {
147 boost::shared_ptr<std::thread> thread(
new std::thread(
148 std::bind(&IoServiceThreadPool::threadWork,
this)));
151 threads_.push_back(thread);
155 main_cv_.wait(main_lck,
157 return (running_ == threads_.size());
166 if (!io_service_->stopped()) {
177 main_cv_.wait(main_lck,
179 return (paused_ == threads_.size());
187 if (!io_service_->stopped()) {
198 main_cv_.wait(main_lck,
200 return (exited_ == threads_.size());
203 for (
auto const& thread : threads_) {
213IoServiceThreadPool::threadWork() {
216 switch (getState()) {
219 std::unique_lock<std::mutex> lck(mutex_);
223 if (running_ == pool_size_) {
224 main_cv_.notify_all();
237 std::unique_lock<std::mutex> lck(mutex_);
245 std::unique_lock<std::mutex> lck(mutex_);
249 if (paused_ == threads_.size()) {
250 main_cv_.notify_all();
269 std::unique_lock<std::mutex> lck(mutex_);
273 if (exited_ == threads_.size()) {
274 main_cv_.notify_all();
280 return (io_service_);
290 return (threads_.size());
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Exception thrown when a worker thread is trying to stop or pause the respective thread pool (which wo...
The IOService class is a wrapper for the ASIO io_service class.
asiolink::IOServicePtr getIOService() const
Fetches the IOService that drives the pool.
void checkPausePermissions()
Check current thread permissions to transition to the new PAUSED state.
uint16_t getPoolSize() const
Fetches the maximum size of the thread pool.
~IoServiceThreadPool()
Destructor.
void stop()
Transitions the pool from RUNNING or PAUSED to STOPPED.
void pause()
Transitions the pool from RUNNING to PAUSED.
IoServiceThreadPool(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.
Defines a State within the State Model.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Defines the logger used by the top-level component of kea-lfc.