thread_pool.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // impl/thread_pool.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #ifndef BOOST_ASIO_IMPL_THREAD_POOL_HPP
  11. #define BOOST_ASIO_IMPL_THREAD_POOL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/executor_op.hpp>
  16. #include <boost/asio/detail/fenced_block.hpp>
  17. #include <boost/asio/detail/recycling_allocator.hpp>
  18. #include <boost/asio/detail/type_traits.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. inline thread_pool::executor_type
  24. thread_pool::get_executor() BOOST_ASIO_NOEXCEPT
  25. {
  26. return executor_type(*this);
  27. }
  28. inline thread_pool&
  29. thread_pool::executor_type::context() const BOOST_ASIO_NOEXCEPT
  30. {
  31. return pool_;
  32. }
  33. inline void
  34. thread_pool::executor_type::on_work_started() const BOOST_ASIO_NOEXCEPT
  35. {
  36. pool_.scheduler_.work_started();
  37. }
  38. inline void thread_pool::executor_type::on_work_finished()
  39. const BOOST_ASIO_NOEXCEPT
  40. {
  41. pool_.scheduler_.work_finished();
  42. }
  43. template <typename Function, typename Allocator>
  44. void thread_pool::executor_type::dispatch(
  45. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  46. {
  47. typedef typename decay<Function>::type function_type;
  48. // Invoke immediately if we are already inside the thread pool.
  49. if (pool_.scheduler_.can_dispatch())
  50. {
  51. // Make a local, non-const copy of the function.
  52. function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
  53. detail::fenced_block b(detail::fenced_block::full);
  54. boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
  55. return;
  56. }
  57. // Allocate and construct an operation to wrap the function.
  58. typedef detail::executor_op<function_type, Allocator> op;
  59. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  60. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  61. BOOST_ASIO_HANDLER_CREATION((pool_, *p.p,
  62. "thread_pool", &this->context(), 0, "dispatch"));
  63. pool_.scheduler_.post_immediate_completion(p.p, false);
  64. p.v = p.p = 0;
  65. }
  66. template <typename Function, typename Allocator>
  67. void thread_pool::executor_type::post(
  68. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  69. {
  70. typedef typename decay<Function>::type function_type;
  71. // Allocate and construct an operation to wrap the function.
  72. typedef detail::executor_op<function_type, Allocator> op;
  73. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  74. 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, "post"));
  77. pool_.scheduler_.post_immediate_completion(p.p, false);
  78. p.v = p.p = 0;
  79. }
  80. template <typename Function, typename Allocator>
  81. void thread_pool::executor_type::defer(
  82. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  83. {
  84. typedef typename decay<Function>::type function_type;
  85. // Allocate and construct an operation to wrap the function.
  86. typedef detail::executor_op<function_type, Allocator> op;
  87. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  88. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  89. BOOST_ASIO_HANDLER_CREATION((pool_, *p.p,
  90. "thread_pool", &this->context(), 0, "defer"));
  91. pool_.scheduler_.post_immediate_completion(p.p, true);
  92. p.v = p.p = 0;
  93. }
  94. inline bool
  95. thread_pool::executor_type::running_in_this_thread() const BOOST_ASIO_NOEXCEPT
  96. {
  97. return pool_.scheduler_.can_dispatch();
  98. }
  99. } // namespace asio
  100. } // namespace boost
  101. #include <boost/asio/detail/pop_options.hpp>
  102. #endif // BOOST_ASIO_IMPL_THREAD_POOL_HPP