]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // impl/thread_pool.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2017 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_THREAD_POOL_HPP | |
12 | #define BOOST_ASIO_IMPL_THREAD_POOL_HPP | |
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/executor_op.hpp> | |
19 | #include <boost/asio/detail/fenced_block.hpp> | |
20 | #include <boost/asio/detail/recycling_allocator.hpp> | |
21 | #include <boost/asio/detail/type_traits.hpp> | |
22 | #include <boost/asio/execution_context.hpp> | |
23 | ||
24 | #include <boost/asio/detail/push_options.hpp> | |
25 | ||
26 | namespace boost { | |
27 | namespace asio { | |
28 | ||
29 | inline thread_pool::executor_type | |
30 | thread_pool::get_executor() BOOST_ASIO_NOEXCEPT | |
31 | { | |
32 | return executor_type(*this); | |
33 | } | |
34 | ||
35 | inline thread_pool& | |
36 | thread_pool::executor_type::context() const BOOST_ASIO_NOEXCEPT | |
37 | { | |
38 | return pool_; | |
39 | } | |
40 | ||
41 | inline void | |
42 | thread_pool::executor_type::on_work_started() const BOOST_ASIO_NOEXCEPT | |
43 | { | |
44 | pool_.scheduler_.work_started(); | |
45 | } | |
46 | ||
47 | inline void thread_pool::executor_type::on_work_finished() | |
48 | const BOOST_ASIO_NOEXCEPT | |
49 | { | |
50 | pool_.scheduler_.work_finished(); | |
51 | } | |
52 | ||
53 | template <typename Function, typename Allocator> | |
54 | void thread_pool::executor_type::dispatch( | |
55 | BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const | |
56 | { | |
57 | typedef typename decay<Function>::type function_type; | |
58 | ||
59 | // Invoke immediately if we are already inside the thread pool. | |
60 | if (pool_.scheduler_.can_dispatch()) | |
61 | { | |
62 | // Make a local, non-const copy of the function. | |
63 | function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f)); | |
64 | ||
65 | detail::fenced_block b(detail::fenced_block::full); | |
66 | boost_asio_handler_invoke_helpers::invoke(tmp, tmp); | |
67 | return; | |
68 | } | |
69 | ||
70 | // Allocate and construct an operation to wrap the function. | |
71 | typedef detail::executor_op<function_type, Allocator> op; | |
72 | typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 }; | |
73 | p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a); | |
74 | ||
75 | BOOST_ASIO_HANDLER_CREATION((pool_, *p.p, | |
76 | "thread_pool", &this->context(), 0, "dispatch")); | |
77 | ||
78 | pool_.scheduler_.post_immediate_completion(p.p, false); | |
79 | p.v = p.p = 0; | |
80 | } | |
81 | ||
82 | template <typename Function, typename Allocator> | |
83 | void thread_pool::executor_type::post( | |
84 | BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const | |
85 | { | |
86 | typedef typename decay<Function>::type function_type; | |
87 | ||
88 | // Allocate and construct an operation to wrap the function. | |
89 | typedef detail::executor_op<function_type, Allocator> op; | |
90 | typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 }; | |
91 | p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a); | |
92 | ||
93 | BOOST_ASIO_HANDLER_CREATION((pool_, *p.p, | |
94 | "thread_pool", &this->context(), 0, "post")); | |
95 | ||
96 | pool_.scheduler_.post_immediate_completion(p.p, false); | |
97 | p.v = p.p = 0; | |
98 | } | |
99 | ||
100 | template <typename Function, typename Allocator> | |
101 | void thread_pool::executor_type::defer( | |
102 | BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const | |
103 | { | |
104 | typedef typename decay<Function>::type function_type; | |
105 | ||
106 | // Allocate and construct an operation to wrap the function. | |
107 | typedef detail::executor_op<function_type, Allocator> op; | |
108 | typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 }; | |
109 | p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a); | |
110 | ||
111 | BOOST_ASIO_HANDLER_CREATION((pool_, *p.p, | |
112 | "thread_pool", &this->context(), 0, "defer")); | |
113 | ||
114 | pool_.scheduler_.post_immediate_completion(p.p, true); | |
115 | p.v = p.p = 0; | |
116 | } | |
117 | ||
118 | inline bool | |
119 | thread_pool::executor_type::running_in_this_thread() const BOOST_ASIO_NOEXCEPT | |
120 | { | |
121 | return pool_.scheduler_.can_dispatch(); | |
122 | } | |
123 | ||
124 | } // namespace asio | |
125 | } // namespace boost | |
126 | ||
127 | #include <boost/asio/detail/pop_options.hpp> | |
128 | ||
129 | #endif // BOOST_ASIO_IMPL_THREAD_POOL_HPP |