1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 // We wrote this code based on the original code which has the
26 // following license:
27 //
28 // io_service_pool.hpp
29 // ~~~~~~~~~~~~~~~~~~~
30 //
31 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
32 //
33 // Distributed under the Boost Software License, Version 1.0. (See accompanying
34 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
35 //
36 
37 #ifndef ASIO_IO_SERVICE_POOL_H
38 #define ASIO_IO_SERVICE_POOL_H
39 
40 #include "nghttp2_config.h"
41 
42 #include <vector>
43 #include <memory>
44 #include <future>
45 
46 #include <boost/noncopyable.hpp>
47 #include <boost/thread.hpp>
48 
49 #include <nghttp2/asio_http2.h>
50 
51 namespace nghttp2 {
52 
53 namespace asio_http2 {
54 
55 /// A pool of io_service objects.
56 class io_service_pool : private boost::noncopyable {
57 public:
58   /// Construct the io_service pool.
59   explicit io_service_pool(std::size_t pool_size);
60 
61   /// Run all io_service objects in the pool.
62   void run(bool asynchronous = false);
63 
64   /// Stop all io_service objects in the pool.
65   void force_stop();
66 
67   /// Destroy all work objects to signals end of work
68   void stop();
69 
70   /// Join on all io_service objects in the pool.
71   void join();
72 
73   /// Get an io_service to use.
74   boost::asio::io_service &get_io_service();
75 
76   /// Get access to all io_service objects.
77   const std::vector<std::shared_ptr<boost::asio::io_service>> &
78   io_services() const;
79 
80 private:
81   /// The pool of io_services.
82   std::vector<std::shared_ptr<boost::asio::io_service>> io_services_;
83 
84   /// The work that keeps the io_services running.
85   std::vector<std::shared_ptr<boost::asio::io_service::work>> work_;
86 
87   /// The next io_service to use for a connection.
88   std::size_t next_io_service_;
89 
90   /// Futures to all the io_service objects
91   std::vector<std::future<std::size_t>> futures_;
92 };
93 
94 } // namespace asio_http2
95 
96 } // namespace nghttp2
97 
98 #endif // ASIO_IO_SERVICE_POOL_H
99