Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / asio_io_service_pool.h
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 stop();
66
67   /// Join on all io_service objects in the pool.
68   void join();
69
70   /// Get an io_service to use.
71   boost::asio::io_service &get_io_service();
72
73 private:
74   /// The pool of io_services.
75   std::vector<std::shared_ptr<boost::asio::io_service>> io_services_;
76
77   /// The work that keeps the io_services running.
78   std::vector<std::shared_ptr<boost::asio::io_service::work>> work_;
79
80   /// The next io_service to use for a connection.
81   std::size_t next_io_service_;
82
83   /// Futures to all the io_service objects
84   std::vector<std::future<std::size_t>> futures_;
85 };
86
87 } // namespace asio_http2
88
89 } // namespace nghttp2
90
91 #endif // ASIO_IO_SERVICE_POOL_H