io_context.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //
  2. // impl/io_context.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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_IO_CONTEXT_HPP
  11. #define BOOST_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 <boost/asio/config.hpp>
  16. #include <boost/asio/detail/completion_handler.hpp>
  17. #include <boost/asio/detail/executor_op.hpp>
  18. #include <boost/asio/detail/fenced_block.hpp>
  19. #include <boost/asio/detail/handler_type_requirements.hpp>
  20. #include <boost/asio/detail/non_const_lvalue.hpp>
  21. #include <boost/asio/detail/service_registry.hpp>
  22. #include <boost/asio/detail/throw_error.hpp>
  23. #include <boost/asio/detail/type_traits.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. template <typename Allocator>
  28. io_context::io_context(allocator_arg_t, const Allocator& a)
  29. : execution_context(std::allocator_arg, a, config_from_concurrency_hint()),
  30. impl_(boost::asio::make_service<impl_type>(*this, false))
  31. {
  32. }
  33. template <typename Allocator>
  34. io_context::io_context(allocator_arg_t,
  35. const Allocator& a, int concurrency_hint)
  36. : execution_context(std::allocator_arg, a,
  37. config_from_concurrency_hint(concurrency_hint)),
  38. impl_(boost::asio::make_service<impl_type>(*this, false))
  39. {
  40. }
  41. template <typename Allocator>
  42. io_context::io_context(allocator_arg_t, const Allocator& a,
  43. const execution_context::service_maker& initial_services)
  44. : execution_context(std::allocator_arg, a, initial_services),
  45. impl_(boost::asio::make_service<impl_type>(*this, false))
  46. {
  47. }
  48. #if !defined(GENERATING_DOCUMENTATION)
  49. template <typename Service>
  50. inline Service& use_service(io_context& ioc)
  51. {
  52. // Check that Service meets the necessary type requirements.
  53. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  54. (void)static_cast<const execution_context::id*>(&Service::id);
  55. return ioc.service_registry_->template use_service<Service>(ioc);
  56. }
  57. template <>
  58. inline detail::io_context_impl& use_service<detail::io_context_impl>(
  59. io_context& ioc)
  60. {
  61. return ioc.impl_;
  62. }
  63. #endif // !defined(GENERATING_DOCUMENTATION)
  64. inline io_context::executor_type
  65. io_context::get_executor() noexcept
  66. {
  67. return executor_type(*this);
  68. }
  69. template <typename Rep, typename Period>
  70. std::size_t io_context::run_for(
  71. const chrono::duration<Rep, Period>& rel_time)
  72. {
  73. return this->run_until(chrono::steady_clock::now() + rel_time);
  74. }
  75. template <typename Clock, typename Duration>
  76. std::size_t io_context::run_until(
  77. const chrono::time_point<Clock, Duration>& abs_time)
  78. {
  79. std::size_t n = 0;
  80. while (this->run_one_until(abs_time))
  81. if (n != (std::numeric_limits<std::size_t>::max)())
  82. ++n;
  83. return n;
  84. }
  85. template <typename Rep, typename Period>
  86. std::size_t io_context::run_one_for(
  87. const chrono::duration<Rep, Period>& rel_time)
  88. {
  89. return this->run_one_until(chrono::steady_clock::now() + rel_time);
  90. }
  91. template <typename Clock, typename Duration>
  92. std::size_t io_context::run_one_until(
  93. const chrono::time_point<Clock, Duration>& abs_time)
  94. {
  95. typename Clock::time_point now = Clock::now();
  96. while (now < abs_time)
  97. {
  98. typename Clock::duration rel_time = abs_time - now;
  99. if (rel_time > chrono::seconds(1))
  100. rel_time = chrono::seconds(1);
  101. boost::system::error_code ec;
  102. std::size_t s = impl_.wait_one(
  103. static_cast<long>(chrono::duration_cast<
  104. chrono::microseconds>(rel_time).count()), ec);
  105. boost::asio::detail::throw_error(ec);
  106. if (s || impl_.stopped())
  107. return s;
  108. now = Clock::now();
  109. }
  110. return 0;
  111. }
  112. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  113. template <typename Handler>
  114. #if defined(GENERATING_DOCUMENTATION)
  115. unspecified
  116. #else
  117. inline detail::wrapped_handler<io_context&, Handler>
  118. #endif
  119. io_context::wrap(Handler handler)
  120. {
  121. return detail::wrapped_handler<io_context&, Handler>(*this, handler);
  122. }
  123. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  124. template <typename Allocator, uintptr_t Bits>
  125. io_context::basic_executor_type<Allocator, Bits>&
  126. io_context::basic_executor_type<Allocator, Bits>::operator=(
  127. const basic_executor_type& other) noexcept
  128. {
  129. if (this != &other)
  130. {
  131. static_cast<Allocator&>(*this) = static_cast<const Allocator&>(other);
  132. io_context* old_io_context = context_ptr();
  133. target_ = other.target_;
  134. if (Bits & outstanding_work_tracked)
  135. {
  136. if (context_ptr())
  137. context_ptr()->impl_.work_started();
  138. if (old_io_context)
  139. old_io_context->impl_.work_finished();
  140. }
  141. }
  142. return *this;
  143. }
  144. template <typename Allocator, uintptr_t Bits>
  145. io_context::basic_executor_type<Allocator, Bits>&
  146. io_context::basic_executor_type<Allocator, Bits>::operator=(
  147. basic_executor_type&& other) noexcept
  148. {
  149. if (this != &other)
  150. {
  151. static_cast<Allocator&>(*this) = static_cast<Allocator&&>(other);
  152. io_context* old_io_context = context_ptr();
  153. target_ = other.target_;
  154. if (Bits & outstanding_work_tracked)
  155. {
  156. other.target_ = 0;
  157. if (old_io_context)
  158. old_io_context->impl_.work_finished();
  159. }
  160. }
  161. return *this;
  162. }
  163. template <typename Allocator, uintptr_t Bits>
  164. inline bool io_context::basic_executor_type<Allocator,
  165. Bits>::running_in_this_thread() const noexcept
  166. {
  167. return context_ptr()->impl_.can_dispatch();
  168. }
  169. template <typename Allocator, uintptr_t Bits>
  170. template <typename Function>
  171. void io_context::basic_executor_type<Allocator, Bits>::execute(
  172. Function&& f) const
  173. {
  174. typedef decay_t<Function> function_type;
  175. // Invoke immediately if the blocking.possibly property is enabled and we are
  176. // already inside the thread pool.
  177. if ((bits() & blocking_never) == 0 && context_ptr()->impl_.can_dispatch())
  178. {
  179. // Make a local, non-const copy of the function.
  180. function_type tmp(static_cast<Function&&>(f));
  181. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  182. try
  183. {
  184. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  185. detail::fenced_block b(detail::fenced_block::full);
  186. static_cast<function_type&&>(tmp)();
  187. return;
  188. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  189. }
  190. catch (...)
  191. {
  192. context_ptr()->impl_.capture_current_exception();
  193. return;
  194. }
  195. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  196. }
  197. // Allocate and construct an operation to wrap the function.
  198. typedef detail::executor_op<function_type, Allocator, detail::operation> op;
  199. typename op::ptr p = {
  200. detail::addressof(static_cast<const Allocator&>(*this)),
  201. op::ptr::allocate(static_cast<const Allocator&>(*this)), 0 };
  202. p.p = new (p.v) op(static_cast<Function&&>(f),
  203. static_cast<const Allocator&>(*this));
  204. BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  205. "io_context", context_ptr(), 0, "execute"));
  206. context_ptr()->impl_.post_immediate_completion(p.p,
  207. (bits() & relationship_continuation) != 0);
  208. p.v = p.p = 0;
  209. }
  210. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  211. template <typename Allocator, uintptr_t Bits>
  212. inline io_context& io_context::basic_executor_type<
  213. Allocator, Bits>::context() const noexcept
  214. {
  215. return *context_ptr();
  216. }
  217. template <typename Allocator, uintptr_t Bits>
  218. inline void io_context::basic_executor_type<Allocator,
  219. Bits>::on_work_started() const noexcept
  220. {
  221. context_ptr()->impl_.work_started();
  222. }
  223. template <typename Allocator, uintptr_t Bits>
  224. inline void io_context::basic_executor_type<Allocator,
  225. Bits>::on_work_finished() const noexcept
  226. {
  227. context_ptr()->impl_.work_finished();
  228. }
  229. template <typename Allocator, uintptr_t Bits>
  230. template <typename Function, typename OtherAllocator>
  231. void io_context::basic_executor_type<Allocator, Bits>::dispatch(
  232. Function&& f, const OtherAllocator& a) const
  233. {
  234. typedef decay_t<Function> function_type;
  235. // Invoke immediately if we are already inside the thread pool.
  236. if (context_ptr()->impl_.can_dispatch())
  237. {
  238. // Make a local, non-const copy of the function.
  239. function_type tmp(static_cast<Function&&>(f));
  240. detail::fenced_block b(detail::fenced_block::full);
  241. static_cast<function_type&&>(tmp)();
  242. return;
  243. }
  244. // Allocate and construct an operation to wrap the function.
  245. typedef detail::executor_op<function_type,
  246. OtherAllocator, detail::operation> op;
  247. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  248. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  249. BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  250. "io_context", context_ptr(), 0, "dispatch"));
  251. context_ptr()->impl_.post_immediate_completion(p.p, false);
  252. p.v = p.p = 0;
  253. }
  254. template <typename Allocator, uintptr_t Bits>
  255. template <typename Function, typename OtherAllocator>
  256. void io_context::basic_executor_type<Allocator, Bits>::post(
  257. Function&& f, const OtherAllocator& a) const
  258. {
  259. // Allocate and construct an operation to wrap the function.
  260. typedef detail::executor_op<decay_t<Function>,
  261. OtherAllocator, detail::operation> op;
  262. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  263. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  264. BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  265. "io_context", context_ptr(), 0, "post"));
  266. context_ptr()->impl_.post_immediate_completion(p.p, false);
  267. p.v = p.p = 0;
  268. }
  269. template <typename Allocator, uintptr_t Bits>
  270. template <typename Function, typename OtherAllocator>
  271. void io_context::basic_executor_type<Allocator, Bits>::defer(
  272. Function&& f, const OtherAllocator& a) const
  273. {
  274. // Allocate and construct an operation to wrap the function.
  275. typedef detail::executor_op<decay_t<Function>,
  276. OtherAllocator, detail::operation> op;
  277. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  278. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  279. BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  280. "io_context", context_ptr(), 0, "defer"));
  281. context_ptr()->impl_.post_immediate_completion(p.p, true);
  282. p.v = p.p = 0;
  283. }
  284. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  285. inline boost::asio::io_context& io_context::service::get_io_context()
  286. {
  287. return static_cast<boost::asio::io_context&>(context());
  288. }
  289. } // namespace asio
  290. } // namespace boost
  291. #include <boost/asio/detail/pop_options.hpp>
  292. #endif // BOOST_ASIO_IMPL_IO_CONTEXT_HPP