Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.6KB

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