Kea  2.5.3
io_service_thread_pool.h
Go to the documentation of this file.
1 // Copyright (C) 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 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 
20 namespace isc {
21 namespace asiolink {
22 
25 public:
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 
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 
107 private:
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 
209 public:
214 
218  uint16_t getPoolSize() const;
219 
223  uint16_t getThreadCount() const;
224 
225 private:
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
Defines the logger used by the top-level component of kea-lfc.