• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cpp
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 #include "asio_io_service_pool.h"
37 
38 namespace nghttp2 {
39 
40 namespace asio_http2 {
41 
io_service_pool(std::size_t pool_size)42 io_service_pool::io_service_pool(std::size_t pool_size) : next_io_service_(0) {
43   if (pool_size == 0) {
44     throw std::runtime_error("io_service_pool size is 0");
45   }
46 
47   // Give all the io_services work to do so that their run() functions will not
48   // exit until they are explicitly stopped.
49   for (std::size_t i = 0; i < pool_size; ++i) {
50     auto io_service = std::make_shared<boost::asio::io_service>();
51     auto work = std::make_shared<boost::asio::io_service::work>(*io_service);
52     io_services_.push_back(io_service);
53     work_.push_back(work);
54   }
55 }
56 
run(bool asynchronous)57 void io_service_pool::run(bool asynchronous) {
58   // Create a pool of threads to run all of the io_services.
59   for (std::size_t i = 0; i < io_services_.size(); ++i) {
60     futures_.push_back(std::async(std::launch::async,
61                                   (size_t(boost::asio::io_service::*)(void)) &
62                                       boost::asio::io_service::run,
63                                   io_services_[i]));
64   }
65 
66   if (!asynchronous) {
67     join();
68   }
69 }
70 
join()71 void io_service_pool::join() {
72   // Wait for all threads in the pool to exit.
73   for (auto &fut : futures_) {
74     fut.get();
75   }
76 }
77 
force_stop()78 void io_service_pool::force_stop() {
79   // Explicitly stop all io_services.
80   for (auto &iosv : io_services_) {
81     iosv->stop();
82   }
83 }
84 
stop()85 void io_service_pool::stop() {
86   // Destroy all work objects to signals end of work
87   work_.clear();
88 }
89 
get_io_service()90 boost::asio::io_service &io_service_pool::get_io_service() {
91   // Use a round-robin scheme to choose the next io_service to use.
92   auto &io_service = *io_services_[next_io_service_];
93   ++next_io_service_;
94   if (next_io_service_ == io_services_.size()) {
95     next_io_service_ = 0;
96   }
97   return io_service;
98 }
99 
100 const std::vector<std::shared_ptr<boost::asio::io_service>> &
io_services() const101 io_service_pool::io_services() const {
102   return io_services_;
103 }
104 
105 } // namespace asio_http2
106 
107 } // namespace nghttp2
108