]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/impl/system_context.ipp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / asio / impl / system_context.ipp
1 //
2 // impl/system_context.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 BOOST_ASIO_IMPL_SYSTEM_CONTEXT_IPP
12 #define BOOST_ASIO_IMPL_SYSTEM_CONTEXT_IPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/system_context.hpp>
20
21 #include <boost/asio/detail/push_options.hpp>
22
23 namespace boost {
24 namespace asio {
25
26 struct system_context::thread_function
27 {
28 detail::scheduler* scheduler_;
29
30 void operator()()
31 {
32 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
33 try
34 {
35 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
36 boost::system::error_code ec;
37 scheduler_->run(ec);
38 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
39 }
40 catch (...)
41 {
42 std::terminate();
43 }
44 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
45 }
46 };
47
48 system_context::system_context()
49 : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false)))
50 {
51 scheduler_.work_started();
52
53 thread_function f = { &scheduler_ };
54 num_threads_ = detail::thread::hardware_concurrency() * 2;
55 num_threads_ = num_threads_ ? num_threads_ : 2;
56 threads_.create_threads(f, num_threads_);
57 }
58
59 system_context::~system_context()
60 {
61 scheduler_.work_finished();
62 scheduler_.stop();
63 threads_.join();
64 }
65
66 void system_context::stop()
67 {
68 scheduler_.stop();
69 }
70
71 bool system_context::stopped() const BOOST_ASIO_NOEXCEPT
72 {
73 return scheduler_.stopped();
74 }
75
76 void system_context::join()
77 {
78 scheduler_.work_finished();
79 threads_.join();
80 }
81
82 detail::scheduler& system_context::add_scheduler(detail::scheduler* s)
83 {
84 detail::scoped_ptr<detail::scheduler> scoped_impl(s);
85 boost::asio::add_service<detail::scheduler>(*this, scoped_impl.get());
86 return *scoped_impl.release();
87 }
88
89 } // namespace asio
90 } // namespace boost
91
92 #include <boost/asio/detail/pop_options.hpp>
93
94 #endif // BOOST_ASIO_IMPL_SYSTEM_CONTEXT_IPP