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.

thread_pool.hpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // impl/thread_pool.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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. // Make a local, non-const copy of the function.
  47. typedef typename decay<Function>::type function_type;
  48. function_type tmp(ASIO_MOVE_CAST(Function)(f));
  49. // Invoke immediately if we are already inside the thread pool.
  50. if (pool_.scheduler_.can_dispatch())
  51. {
  52. detail::fenced_block b(detail::fenced_block::full);
  53. asio_handler_invoke_helpers::invoke(tmp, tmp);
  54. return;
  55. }
  56. // Construct an allocator to be used for the operation.
  57. typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
  58. alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));
  59. // Allocate and construct an operation to wrap the function.
  60. typedef detail::executor_op<function_type, alloc_type> op;
  61. typename op::ptr p = { allocator, 0, 0 };
  62. p.v = p.a.allocate(1);
  63. p.p = new (p.v) op(tmp, allocator);
  64. ASIO_HANDLER_CREATION((pool_, *p.p,
  65. "thread_pool", &this->context(), 0, "dispatch"));
  66. pool_.scheduler_.post_immediate_completion(p.p, false);
  67. p.v = p.p = 0;
  68. }
  69. template <typename Function, typename Allocator>
  70. void thread_pool::executor_type::post(
  71. ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  72. {
  73. // Make a local, non-const copy of the function.
  74. typedef typename decay<Function>::type function_type;
  75. function_type tmp(ASIO_MOVE_CAST(Function)(f));
  76. // Construct an allocator to be used for the operation.
  77. typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
  78. alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));
  79. // Allocate and construct an operation to wrap the function.
  80. typedef detail::executor_op<function_type, alloc_type> op;
  81. typename op::ptr p = { allocator, 0, 0 };
  82. p.v = p.a.allocate(1);
  83. p.p = new (p.v) op(tmp, allocator);
  84. ASIO_HANDLER_CREATION((pool_, *p.p,
  85. "thread_pool", &this->context(), 0, "post"));
  86. pool_.scheduler_.post_immediate_completion(p.p, false);
  87. p.v = p.p = 0;
  88. }
  89. template <typename Function, typename Allocator>
  90. void thread_pool::executor_type::defer(
  91. ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  92. {
  93. // Make a local, non-const copy of the function.
  94. typedef typename decay<Function>::type function_type;
  95. function_type tmp(ASIO_MOVE_CAST(Function)(f));
  96. // Construct an allocator to be used for the operation.
  97. typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
  98. alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));
  99. // Allocate and construct an operation to wrap the function.
  100. typedef detail::executor_op<function_type, alloc_type> op;
  101. typename op::ptr p = { allocator, 0, 0 };
  102. p.v = p.a.allocate(1);
  103. p.p = new (p.v) op(tmp, allocator);
  104. ASIO_HANDLER_CREATION((pool_, *p.p,
  105. "thread_pool", &this->context(), 0, "defer"));
  106. pool_.scheduler_.post_immediate_completion(p.p, true);
  107. p.v = p.p = 0;
  108. }
  109. inline bool
  110. thread_pool::executor_type::running_in_this_thread() const ASIO_NOEXCEPT
  111. {
  112. return pool_.scheduler_.can_dispatch();
  113. }
  114. } // namespace asio
  115. #include "asio/detail/pop_options.hpp"
  116. #endif // ASIO_IMPL_THREAD_POOL_HPP