]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/http/server2/io_service_pool.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / http / server2 / io_service_pool.cpp
1 //
2 // io_service_pool.cpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2016 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 #include "server.hpp"
12 #include <stdexcept>
13 #include <boost/thread/thread.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/shared_ptr.hpp>
16
17 namespace http {
18 namespace server2 {
19
20 io_service_pool::io_service_pool(std::size_t pool_size)
21 : next_io_service_(0)
22 {
23 if (pool_size == 0)
24 throw std::runtime_error("io_service_pool size is 0");
25
26 // Give all the io_services work to do so that their run() functions will not
27 // exit until they are explicitly stopped.
28 for (std::size_t i = 0; i < pool_size; ++i)
29 {
30 io_service_ptr io_service(new boost::asio::io_service);
31 work_ptr work(new boost::asio::io_service::work(*io_service));
32 io_services_.push_back(io_service);
33 work_.push_back(work);
34 }
35 }
36
37 void io_service_pool::run()
38 {
39 // Create a pool of threads to run all of the io_services.
40 std::vector<boost::shared_ptr<boost::thread> > threads;
41 for (std::size_t i = 0; i < io_services_.size(); ++i)
42 {
43 boost::shared_ptr<boost::thread> thread(new boost::thread(
44 boost::bind(&boost::asio::io_service::run, io_services_[i])));
45 threads.push_back(thread);
46 }
47
48 // Wait for all threads in the pool to exit.
49 for (std::size_t i = 0; i < threads.size(); ++i)
50 threads[i]->join();
51 }
52
53 void io_service_pool::stop()
54 {
55 // Explicitly stop all io_services.
56 for (std::size_t i = 0; i < io_services_.size(); ++i)
57 io_services_[i]->stop();
58 }
59
60 boost::asio::io_service& io_service_pool::get_io_service()
61 {
62 // Use a round-robin scheme to choose the next io_service to use.
63 boost::asio::io_service& io_service = *io_services_[next_io_service_];
64 ++next_io_service_;
65 if (next_io_service_ == io_services_.size())
66 next_io_service_ = 0;
67 return io_service;
68 }
69
70 } // namespace server2
71 } // namespace http