1 //
2 // io_context_pool.hpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef HTTP_SERVER2_IO_SERVICE_POOL_HPP
12 #define HTTP_SERVER2_IO_SERVICE_POOL_HPP
13 
14 #include <asio.hpp>
15 #include <list>
16 #include <vector>
17 #include <boost/noncopyable.hpp>
18 #include <boost/shared_ptr.hpp>
19 
20 namespace http {
21 namespace server2 {
22 
23 /// A pool of io_context objects.
24 class io_context_pool
25   : private boost::noncopyable
26 {
27 public:
28   /// Construct the io_context pool.
29   explicit io_context_pool(std::size_t pool_size);
30 
31   /// Run all io_context objects in the pool.
32   void run();
33 
34   /// Stop all io_context objects in the pool.
35   void stop();
36 
37   /// Get an io_context to use.
38   asio::io_context& get_io_context();
39 
40 private:
41   typedef boost::shared_ptr<asio::io_context> io_context_ptr;
42 
43   /// The pool of io_contexts.
44   std::vector<io_context_ptr> io_contexts_;
45 
46   /// The work-tracking executors that keep the io_contexts running.
47   std::list<asio::any_io_executor> work_;
48 
49   /// The next io_context to use for a connection.
50   std::size_t next_io_context_;
51 };
52 
53 } // namespace server2
54 } // namespace http
55 
56 #endif // HTTP_SERVER2_IO_SERVICE_POOL_HPP
57