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.

303 lines
8.6KB

  1. //
  2. // impl/io_context.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_IO_CONTEXT_HPP
  11. #define ASIO_IMPL_IO_CONTEXT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/completion_handler.hpp"
  16. #include "asio/detail/executor_op.hpp"
  17. #include "asio/detail/fenced_block.hpp"
  18. #include "asio/detail/handler_type_requirements.hpp"
  19. #include "asio/detail/recycling_allocator.hpp"
  20. #include "asio/detail/service_registry.hpp"
  21. #include "asio/detail/type_traits.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. template <typename Service>
  25. inline Service& use_service(io_context& ioc)
  26. {
  27. // Check that Service meets the necessary type requirements.
  28. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  29. (void)static_cast<const execution_context::id*>(&Service::id);
  30. return ioc.service_registry_->template use_service<Service>(ioc);
  31. }
  32. template <>
  33. inline detail::io_context_impl& use_service<detail::io_context_impl>(
  34. io_context& ioc)
  35. {
  36. return ioc.impl_;
  37. }
  38. } // namespace asio
  39. #include "asio/detail/pop_options.hpp"
  40. #if defined(ASIO_HAS_IOCP)
  41. # include "asio/detail/win_iocp_io_context.hpp"
  42. #else
  43. # include "asio/detail/scheduler.hpp"
  44. #endif
  45. #include "asio/detail/push_options.hpp"
  46. namespace asio {
  47. inline io_context::executor_type
  48. io_context::get_executor() ASIO_NOEXCEPT
  49. {
  50. return executor_type(*this);
  51. }
  52. #if !defined(ASIO_NO_DEPRECATED)
  53. inline void io_context::reset()
  54. {
  55. restart();
  56. }
  57. template <typename CompletionHandler>
  58. ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
  59. io_context::dispatch(ASIO_MOVE_ARG(CompletionHandler) handler)
  60. {
  61. // If you get an error on the following line it means that your handler does
  62. // not meet the documented type requirements for a CompletionHandler.
  63. ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
  64. async_completion<CompletionHandler, void ()> init(handler);
  65. if (impl_.can_dispatch())
  66. {
  67. detail::fenced_block b(detail::fenced_block::full);
  68. asio_handler_invoke_helpers::invoke(init.handler, init.handler);
  69. }
  70. else
  71. {
  72. // Allocate and construct an operation to wrap the handler.
  73. typedef detail::completion_handler<
  74. typename handler_type<CompletionHandler, void ()>::type> op;
  75. typename op::ptr p = { detail::addressof(init.handler),
  76. op::ptr::allocate(init.handler), 0 };
  77. p.p = new (p.v) op(init.handler);
  78. ASIO_HANDLER_CREATION((*this, *p.p,
  79. "io_context", this, 0, "dispatch"));
  80. impl_.do_dispatch(p.p);
  81. p.v = p.p = 0;
  82. }
  83. return init.result.get();
  84. }
  85. template <typename CompletionHandler>
  86. ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
  87. io_context::post(ASIO_MOVE_ARG(CompletionHandler) handler)
  88. {
  89. // If you get an error on the following line it means that your handler does
  90. // not meet the documented type requirements for a CompletionHandler.
  91. ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
  92. async_completion<CompletionHandler, void ()> init(handler);
  93. bool is_continuation =
  94. asio_handler_cont_helpers::is_continuation(init.handler);
  95. // Allocate and construct an operation to wrap the handler.
  96. typedef detail::completion_handler<
  97. typename handler_type<CompletionHandler, void ()>::type> op;
  98. typename op::ptr p = { detail::addressof(init.handler),
  99. op::ptr::allocate(init.handler), 0 };
  100. p.p = new (p.v) op(init.handler);
  101. ASIO_HANDLER_CREATION((*this, *p.p,
  102. "io_context", this, 0, "post"));
  103. impl_.post_immediate_completion(p.p, is_continuation);
  104. p.v = p.p = 0;
  105. return init.result.get();
  106. }
  107. template <typename Handler>
  108. #if defined(GENERATING_DOCUMENTATION)
  109. unspecified
  110. #else
  111. inline detail::wrapped_handler<io_context&, Handler>
  112. #endif
  113. io_context::wrap(Handler handler)
  114. {
  115. return detail::wrapped_handler<io_context&, Handler>(*this, handler);
  116. }
  117. #endif // !defined(ASIO_NO_DEPRECATED)
  118. inline io_context&
  119. io_context::executor_type::context() const ASIO_NOEXCEPT
  120. {
  121. return io_context_;
  122. }
  123. inline void
  124. io_context::executor_type::on_work_started() const ASIO_NOEXCEPT
  125. {
  126. io_context_.impl_.work_started();
  127. }
  128. inline void
  129. io_context::executor_type::on_work_finished() const ASIO_NOEXCEPT
  130. {
  131. io_context_.impl_.work_finished();
  132. }
  133. template <typename Function, typename Allocator>
  134. void io_context::executor_type::dispatch(
  135. ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  136. {
  137. // Make a local, non-const copy of the function.
  138. typedef typename decay<Function>::type function_type;
  139. function_type tmp(ASIO_MOVE_CAST(Function)(f));
  140. // Invoke immediately if we are already inside the thread pool.
  141. if (io_context_.impl_.can_dispatch())
  142. {
  143. detail::fenced_block b(detail::fenced_block::full);
  144. asio_handler_invoke_helpers::invoke(tmp, tmp);
  145. return;
  146. }
  147. // Construct an allocator to be used for the operation.
  148. typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
  149. alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));
  150. // Allocate and construct an operation to wrap the function.
  151. typedef detail::executor_op<function_type, alloc_type, detail::operation> op;
  152. typename op::ptr p = { allocator, 0, 0 };
  153. p.v = p.a.allocate(1);
  154. p.p = new (p.v) op(tmp, allocator);
  155. ASIO_HANDLER_CREATION((this->context(), *p.p,
  156. "io_context", &this->context(), 0, "post"));
  157. io_context_.impl_.post_immediate_completion(p.p, false);
  158. p.v = p.p = 0;
  159. }
  160. template <typename Function, typename Allocator>
  161. void io_context::executor_type::post(
  162. ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  163. {
  164. // Make a local, non-const copy of the function.
  165. typedef typename decay<Function>::type function_type;
  166. function_type tmp(ASIO_MOVE_CAST(Function)(f));
  167. // Construct an allocator to be used for the operation.
  168. typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
  169. alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));
  170. // Allocate and construct an operation to wrap the function.
  171. typedef detail::executor_op<function_type, alloc_type, detail::operation> op;
  172. typename op::ptr p = { allocator, 0, 0 };
  173. p.v = p.a.allocate(1);
  174. p.p = new (p.v) op(tmp, allocator);
  175. ASIO_HANDLER_CREATION((this->context(), *p.p,
  176. "io_context", &this->context(), 0, "post"));
  177. io_context_.impl_.post_immediate_completion(p.p, false);
  178. p.v = p.p = 0;
  179. }
  180. template <typename Function, typename Allocator>
  181. void io_context::executor_type::defer(
  182. ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  183. {
  184. // Make a local, non-const copy of the function.
  185. typedef typename decay<Function>::type function_type;
  186. function_type tmp(ASIO_MOVE_CAST(Function)(f));
  187. // Construct an allocator to be used for the operation.
  188. typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
  189. alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));
  190. // Allocate and construct an operation to wrap the function.
  191. typedef detail::executor_op<function_type, alloc_type, detail::operation> op;
  192. typename op::ptr p = { allocator, 0, 0 };
  193. p.v = p.a.allocate(1);
  194. p.p = new (p.v) op(tmp, allocator);
  195. ASIO_HANDLER_CREATION((this->context(), *p.p,
  196. "io_context", &this->context(), 0, "defer"));
  197. io_context_.impl_.post_immediate_completion(p.p, true);
  198. p.v = p.p = 0;
  199. }
  200. inline bool
  201. io_context::executor_type::running_in_this_thread() const ASIO_NOEXCEPT
  202. {
  203. return io_context_.impl_.can_dispatch();
  204. }
  205. inline io_context::work::work(asio::io_context& io_context)
  206. : io_context_impl_(io_context.impl_)
  207. {
  208. io_context_impl_.work_started();
  209. }
  210. inline io_context::work::work(const work& other)
  211. : io_context_impl_(other.io_context_impl_)
  212. {
  213. io_context_impl_.work_started();
  214. }
  215. inline io_context::work::~work()
  216. {
  217. io_context_impl_.work_finished();
  218. }
  219. inline asio::io_context& io_context::work::get_io_context()
  220. {
  221. return static_cast<asio::io_context&>(io_context_impl_.context());
  222. }
  223. #if !defined(ASIO_NO_DEPRECATED)
  224. inline asio::io_context& io_context::work::get_io_service()
  225. {
  226. return static_cast<asio::io_context&>(io_context_impl_.context());
  227. }
  228. #endif // !defined(ASIO_NO_DEPRECATED)
  229. inline asio::io_context& io_context::service::get_io_context()
  230. {
  231. return static_cast<asio::io_context&>(context());
  232. }
  233. #if !defined(ASIO_NO_DEPRECATED)
  234. inline asio::io_context& io_context::service::get_io_service()
  235. {
  236. return static_cast<asio::io_context&>(context());
  237. }
  238. #endif // !defined(ASIO_NO_DEPRECATED)
  239. } // namespace asio
  240. #include "asio/detail/pop_options.hpp"
  241. #endif // ASIO_IMPL_IO_CONTEXT_HPP