1 //
2 // io_context_pool.hpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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   typedef asio::executor_work_guard<
43     asio::io_context::executor_type> io_context_work;
44 
45   /// The pool of io_contexts.
46   std::vector<io_context_ptr> io_contexts_;
47 
48   /// The work that keeps the io_contexts running.
49   std::list<io_context_work> work_;
50 
51   /// The next io_context to use for a connection.
52   std::size_t next_io_context_;
53 };
54 
55 } // namespace server2
56 } // namespace http
57 
58 #endif // HTTP_SERVER2_IO_SERVICE_POOL_HPP
59