]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // impl/thread_pool.ipp | |
3 | // ~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
b32b8144 FG |
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_THREAD_POOL_IPP | |
12 | #define BOOST_ASIO_IMPL_THREAD_POOL_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> | |
20effc67 | 19 | #include <stdexcept> |
b32b8144 | 20 | #include <boost/asio/thread_pool.hpp> |
20effc67 | 21 | #include <boost/asio/detail/throw_exception.hpp> |
b32b8144 FG |
22 | |
23 | #include <boost/asio/detail/push_options.hpp> | |
24 | ||
25 | namespace boost { | |
26 | namespace asio { | |
27 | ||
28 | struct thread_pool::thread_function | |
29 | { | |
30 | detail::scheduler* scheduler_; | |
31 | ||
32 | void operator()() | |
33 | { | |
20effc67 TL |
34 | #if !defined(BOOST_ASIO_NO_EXCEPTIONS) |
35 | try | |
36 | { | |
37 | #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS) | |
38 | boost::system::error_code ec; | |
39 | scheduler_->run(ec); | |
40 | #if !defined(BOOST_ASIO_NO_EXCEPTIONS) | |
41 | } | |
42 | catch (...) | |
43 | { | |
44 | std::terminate(); | |
45 | } | |
46 | #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS) | |
b32b8144 FG |
47 | } |
48 | }; | |
49 | ||
20effc67 TL |
50 | #if !defined(BOOST_ASIO_NO_TS_EXECUTORS) |
51 | namespace detail { | |
52 | ||
53 | inline long default_thread_pool_size() | |
54 | { | |
55 | std::size_t num_threads = thread::hardware_concurrency() * 2; | |
56 | num_threads = num_threads == 0 ? 2 : num_threads; | |
57 | return static_cast<long>(num_threads); | |
58 | } | |
59 | ||
60 | } // namespace detail | |
61 | ||
b32b8144 | 62 | thread_pool::thread_pool() |
20effc67 TL |
63 | : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false))), |
64 | num_threads_(detail::default_thread_pool_size()) | |
b32b8144 FG |
65 | { |
66 | scheduler_.work_started(); | |
67 | ||
68 | thread_function f = { &scheduler_ }; | |
20effc67 | 69 | threads_.create_threads(f, static_cast<std::size_t>(num_threads_)); |
b32b8144 | 70 | } |
20effc67 TL |
71 | #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS) |
72 | ||
73 | namespace detail { | |
74 | ||
75 | inline long clamp_thread_pool_size(std::size_t n) | |
76 | { | |
77 | if (n > 0x7FFFFFFF) | |
78 | { | |
79 | std::out_of_range ex("thread pool size"); | |
80 | boost::asio::detail::throw_exception(ex); | |
81 | } | |
82 | return static_cast<long>(n & 0x7FFFFFFF); | |
83 | } | |
84 | ||
85 | } // namespace detail | |
b32b8144 FG |
86 | |
87 | thread_pool::thread_pool(std::size_t num_threads) | |
92f5a8d4 | 88 | : scheduler_(add_scheduler(new detail::scheduler( |
20effc67 TL |
89 | *this, num_threads == 1 ? 1 : 0, false))), |
90 | num_threads_(detail::clamp_thread_pool_size(num_threads)) | |
b32b8144 FG |
91 | { |
92 | scheduler_.work_started(); | |
93 | ||
94 | thread_function f = { &scheduler_ }; | |
20effc67 | 95 | threads_.create_threads(f, static_cast<std::size_t>(num_threads_)); |
b32b8144 FG |
96 | } |
97 | ||
98 | thread_pool::~thread_pool() | |
99 | { | |
100 | stop(); | |
101 | join(); | |
102 | } | |
103 | ||
104 | void thread_pool::stop() | |
105 | { | |
106 | scheduler_.stop(); | |
107 | } | |
108 | ||
20effc67 TL |
109 | void thread_pool::attach() |
110 | { | |
111 | ++num_threads_; | |
112 | thread_function f = { &scheduler_ }; | |
113 | f(); | |
114 | } | |
115 | ||
b32b8144 FG |
116 | void thread_pool::join() |
117 | { | |
92f5a8d4 TL |
118 | if (!threads_.empty()) |
119 | { | |
120 | scheduler_.work_finished(); | |
121 | threads_.join(); | |
122 | } | |
123 | } | |
124 | ||
125 | detail::scheduler& thread_pool::add_scheduler(detail::scheduler* s) | |
126 | { | |
127 | detail::scoped_ptr<detail::scheduler> scoped_impl(s); | |
128 | boost::asio::add_service<detail::scheduler>(*this, scoped_impl.get()); | |
129 | return *scoped_impl.release(); | |
b32b8144 FG |
130 | } |
131 | ||
20effc67 TL |
132 | void thread_pool::wait() |
133 | { | |
134 | scheduler_.work_finished(); | |
135 | threads_.join(); | |
136 | } | |
137 | ||
b32b8144 FG |
138 | } // namespace asio |
139 | } // namespace boost | |
140 | ||
141 | #include <boost/asio/detail/pop_options.hpp> | |
142 | ||
143 | #endif // BOOST_ASIO_IMPL_THREAD_POOL_IPP |