2 // impl/thread_pool.hpp
3 // ~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_IMPL_THREAD_POOL_HPP
12 #define BOOST_ASIO_IMPL_THREAD_POOL_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
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>
24 #include <boost/asio/detail/push_options.hpp>
29 inline thread_pool::executor_type
30 thread_pool::get_executor() BOOST_ASIO_NOEXCEPT
32 return executor_type(*this);
36 thread_pool::executor_type::context() const BOOST_ASIO_NOEXCEPT
42 thread_pool::executor_type::on_work_started() const BOOST_ASIO_NOEXCEPT
44 pool_.scheduler_.work_started();
47 inline void thread_pool::executor_type::on_work_finished()
48 const BOOST_ASIO_NOEXCEPT
50 pool_.scheduler_.work_finished();
53 template <typename Function, typename Allocator>
54 void thread_pool::executor_type::dispatch(
55 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
57 typedef typename decay<Function>::type function_type;
59 // Invoke immediately if we are already inside the thread pool.
60 if (pool_.scheduler_.can_dispatch())
62 // Make a local, non-const copy of the function.
63 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
65 detail::fenced_block b(detail::fenced_block::full);
66 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
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);
75 BOOST_ASIO_HANDLER_CREATION((pool_, *p.p,
76 "thread_pool", &this->context(), 0, "dispatch"));
78 pool_.scheduler_.post_immediate_completion(p.p, false);
82 template <typename Function, typename Allocator>
83 void thread_pool::executor_type::post(
84 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
86 typedef typename decay<Function>::type function_type;
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);
93 BOOST_ASIO_HANDLER_CREATION((pool_, *p.p,
94 "thread_pool", &this->context(), 0, "post"));
96 pool_.scheduler_.post_immediate_completion(p.p, false);
100 template <typename Function, typename Allocator>
101 void thread_pool::executor_type::defer(
102 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
104 typedef typename decay<Function>::type function_type;
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);
111 BOOST_ASIO_HANDLER_CREATION((pool_, *p.p,
112 "thread_pool", &this->context(), 0, "defer"));
114 pool_.scheduler_.post_immediate_completion(p.p, true);
119 thread_pool::executor_type::running_in_this_thread() const BOOST_ASIO_NOEXCEPT
121 return pool_.scheduler_.can_dispatch();
127 #include <boost/asio/detail/pop_options.hpp>
129 #endif // BOOST_ASIO_IMPL_THREAD_POOL_HPP