io_context.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //
  2. // impl/io_context.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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/detail/completion_handler.hpp>
  16. #include <boost/asio/detail/executor_op.hpp>
  17. #include <boost/asio/detail/fenced_block.hpp>
  18. #include <boost/asio/detail/handler_type_requirements.hpp>
  19. #include <boost/asio/detail/non_const_lvalue.hpp>
  20. #include <boost/asio/detail/service_registry.hpp>
  21. #include <boost/asio/detail/throw_error.hpp>
  22. #include <boost/asio/detail/type_traits.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. #if !defined(GENERATING_DOCUMENTATION)
  27. template <typename Service>
  28. inline Service& use_service(io_context& ioc)
  29. {
  30. // Check that Service meets the necessary type requirements.
  31. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  32. (void)static_cast<const execution_context::id*>(&Service::id);
  33. return ioc.service_registry_->template use_service<Service>(ioc);
  34. }
  35. template <>
  36. inline detail::io_context_impl& use_service<detail::io_context_impl>(
  37. io_context& ioc)
  38. {
  39. return ioc.impl_;
  40. }
  41. #endif // !defined(GENERATING_DOCUMENTATION)
  42. inline io_context::executor_type
  43. io_context::get_executor() noexcept
  44. {
  45. return executor_type(*this);
  46. }
  47. template <typename Rep, typename Period>
  48. std::size_t io_context::run_for(
  49. const chrono::duration<Rep, Period>& rel_time)
  50. {
  51. return this->run_until(chrono::steady_clock::now() + rel_time);
  52. }
  53. template <typename Clock, typename Duration>
  54. std::size_t io_context::run_until(
  55. const chrono::time_point<Clock, Duration>& abs_time)
  56. {
  57. std::size_t n = 0;
  58. while (this->run_one_until(abs_time))
  59. if (n != (std::numeric_limits<std::size_t>::max)())
  60. ++n;
  61. return n;
  62. }
  63. template <typename Rep, typename Period>
  64. std::size_t io_context::run_one_for(
  65. const chrono::duration<Rep, Period>& rel_time)
  66. {
  67. return this->run_one_until(chrono::steady_clock::now() + rel_time);
  68. }
  69. template <typename Clock, typename Duration>
  70. std::size_t io_context::run_one_until(
  71. const chrono::time_point<Clock, Duration>& abs_time)
  72. {
  73. typename Clock::time_point now = Clock::now();
  74. while (now < abs_time)
  75. {
  76. typename Clock::duration rel_time = abs_time - now;
  77. if (rel_time > chrono::seconds(1))
  78. rel_time = chrono::seconds(1);
  79. boost::system::error_code ec;
  80. std::size_t s = impl_.wait_one(
  81. static_cast<long>(chrono::duration_cast<
  82. chrono::microseconds>(rel_time).count()), ec);
  83. boost::asio::detail::throw_error(ec);
  84. if (s || impl_.stopped())
  85. return s;
  86. now = Clock::now();
  87. }
  88. return 0;
  89. }
  90. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  91. inline void io_context::reset()
  92. {
  93. restart();
  94. }
  95. struct io_context::initiate_dispatch
  96. {
  97. template <typename LegacyCompletionHandler>
  98. void operator()(LegacyCompletionHandler&& handler,
  99. io_context* self) const
  100. {
  101. // If you get an error on the following line it means that your handler does
  102. // not meet the documented type requirements for a LegacyCompletionHandler.
  103. BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  104. LegacyCompletionHandler, handler) type_check;
  105. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  106. if (self->impl_.can_dispatch())
  107. {
  108. detail::fenced_block b(detail::fenced_block::full);
  109. static_cast<decay_t<LegacyCompletionHandler>&&>(handler2.value)();
  110. }
  111. else
  112. {
  113. // Allocate and construct an operation to wrap the handler.
  114. typedef detail::completion_handler<
  115. decay_t<LegacyCompletionHandler>, executor_type> op;
  116. typename op::ptr p = { detail::addressof(handler2.value),
  117. op::ptr::allocate(handler2.value), 0 };
  118. p.p = new (p.v) op(handler2.value, self->get_executor());
  119. BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
  120. "io_context", self, 0, "dispatch"));
  121. self->impl_.do_dispatch(p.p);
  122. p.v = p.p = 0;
  123. }
  124. }
  125. };
  126. template <typename LegacyCompletionHandler>
  127. auto io_context::dispatch(LegacyCompletionHandler&& handler)
  128. -> decltype(
  129. async_initiate<LegacyCompletionHandler, void ()>(
  130. declval<initiate_dispatch>(), handler, this))
  131. {
  132. return async_initiate<LegacyCompletionHandler, void ()>(
  133. initiate_dispatch(), handler, this);
  134. }
  135. struct io_context::initiate_post
  136. {
  137. template <typename LegacyCompletionHandler>
  138. void operator()(LegacyCompletionHandler&& handler,
  139. io_context* self) const
  140. {
  141. // If you get an error on the following line it means that your handler does
  142. // not meet the documented type requirements for a LegacyCompletionHandler.
  143. BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  144. LegacyCompletionHandler, handler) type_check;
  145. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  146. bool is_continuation =
  147. boost_asio_handler_cont_helpers::is_continuation(handler2.value);
  148. // Allocate and construct an operation to wrap the handler.
  149. typedef detail::completion_handler<
  150. decay_t<LegacyCompletionHandler>, executor_type> op;
  151. typename op::ptr p = { detail::addressof(handler2.value),
  152. op::ptr::allocate(handler2.value), 0 };
  153. p.p = new (p.v) op(handler2.value, self->get_executor());
  154. BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
  155. "io_context", self, 0, "post"));
  156. self->impl_.post_immediate_completion(p.p, is_continuation);
  157. p.v = p.p = 0;
  158. }
  159. };
  160. template <typename LegacyCompletionHandler>
  161. auto io_context::post(LegacyCompletionHandler&& handler)
  162. -> decltype(
  163. async_initiate<LegacyCompletionHandler, void ()>(
  164. declval<initiate_post>(), handler, this))
  165. {
  166. return async_initiate<LegacyCompletionHandler, void ()>(
  167. initiate_post(), handler, this);
  168. }
  169. template <typename Handler>
  170. #if defined(GENERATING_DOCUMENTATION)
  171. unspecified
  172. #else
  173. inline detail::wrapped_handler<io_context&, Handler>
  174. #endif
  175. io_context::wrap(Handler handler)
  176. {
  177. return detail::wrapped_handler<io_context&, Handler>(*this, handler);
  178. }
  179. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  180. template <typename Allocator, uintptr_t Bits>
  181. io_context::basic_executor_type<Allocator, Bits>&
  182. io_context::basic_executor_type<Allocator, Bits>::operator=(
  183. const basic_executor_type& other) noexcept
  184. {
  185. if (this != &other)
  186. {
  187. static_cast<Allocator&>(*this) = static_cast<const Allocator&>(other);
  188. io_context* old_io_context = context_ptr();
  189. target_ = other.target_;
  190. if (Bits & outstanding_work_tracked)
  191. {
  192. if (context_ptr())
  193. context_ptr()->impl_.work_started();
  194. if (old_io_context)
  195. old_io_context->impl_.work_finished();
  196. }
  197. }
  198. return *this;
  199. }
  200. template <typename Allocator, uintptr_t Bits>
  201. io_context::basic_executor_type<Allocator, Bits>&
  202. io_context::basic_executor_type<Allocator, Bits>::operator=(
  203. basic_executor_type&& other) noexcept
  204. {
  205. if (this != &other)
  206. {
  207. static_cast<Allocator&>(*this) = static_cast<Allocator&&>(other);
  208. io_context* old_io_context = context_ptr();
  209. target_ = other.target_;
  210. if (Bits & outstanding_work_tracked)
  211. {
  212. other.target_ = 0;
  213. if (old_io_context)
  214. old_io_context->impl_.work_finished();
  215. }
  216. }
  217. return *this;
  218. }
  219. template <typename Allocator, uintptr_t Bits>
  220. inline bool io_context::basic_executor_type<Allocator,
  221. Bits>::running_in_this_thread() const noexcept
  222. {
  223. return context_ptr()->impl_.can_dispatch();
  224. }
  225. template <typename Allocator, uintptr_t Bits>
  226. template <typename Function>
  227. void io_context::basic_executor_type<Allocator, Bits>::execute(
  228. Function&& f) const
  229. {
  230. typedef decay_t<Function> function_type;
  231. // Invoke immediately if the blocking.possibly property is enabled and we are
  232. // already inside the thread pool.
  233. if ((bits() & blocking_never) == 0 && context_ptr()->impl_.can_dispatch())
  234. {
  235. // Make a local, non-const copy of the function.
  236. function_type tmp(static_cast<Function&&>(f));
  237. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  238. try
  239. {
  240. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  241. detail::fenced_block b(detail::fenced_block::full);
  242. static_cast<function_type&&>(tmp)();
  243. return;
  244. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  245. }
  246. catch (...)
  247. {
  248. context_ptr()->impl_.capture_current_exception();
  249. return;
  250. }
  251. #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
  252. }
  253. // Allocate and construct an operation to wrap the function.
  254. typedef detail::executor_op<function_type, Allocator, detail::operation> op;
  255. typename op::ptr p = {
  256. detail::addressof(static_cast<const Allocator&>(*this)),
  257. op::ptr::allocate(static_cast<const Allocator&>(*this)), 0 };
  258. p.p = new (p.v) op(static_cast<Function&&>(f),
  259. static_cast<const Allocator&>(*this));
  260. BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  261. "io_context", context_ptr(), 0, "execute"));
  262. context_ptr()->impl_.post_immediate_completion(p.p,
  263. (bits() & relationship_continuation) != 0);
  264. p.v = p.p = 0;
  265. }
  266. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  267. template <typename Allocator, uintptr_t Bits>
  268. inline io_context& io_context::basic_executor_type<
  269. Allocator, Bits>::context() const noexcept
  270. {
  271. return *context_ptr();
  272. }
  273. template <typename Allocator, uintptr_t Bits>
  274. inline void io_context::basic_executor_type<Allocator,
  275. Bits>::on_work_started() const noexcept
  276. {
  277. context_ptr()->impl_.work_started();
  278. }
  279. template <typename Allocator, uintptr_t Bits>
  280. inline void io_context::basic_executor_type<Allocator,
  281. Bits>::on_work_finished() const noexcept
  282. {
  283. context_ptr()->impl_.work_finished();
  284. }
  285. template <typename Allocator, uintptr_t Bits>
  286. template <typename Function, typename OtherAllocator>
  287. void io_context::basic_executor_type<Allocator, Bits>::dispatch(
  288. Function&& f, const OtherAllocator& a) const
  289. {
  290. typedef decay_t<Function> function_type;
  291. // Invoke immediately if we are already inside the thread pool.
  292. if (context_ptr()->impl_.can_dispatch())
  293. {
  294. // Make a local, non-const copy of the function.
  295. function_type tmp(static_cast<Function&&>(f));
  296. detail::fenced_block b(detail::fenced_block::full);
  297. static_cast<function_type&&>(tmp)();
  298. return;
  299. }
  300. // Allocate and construct an operation to wrap the function.
  301. typedef detail::executor_op<function_type,
  302. OtherAllocator, detail::operation> op;
  303. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  304. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  305. BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  306. "io_context", context_ptr(), 0, "dispatch"));
  307. context_ptr()->impl_.post_immediate_completion(p.p, false);
  308. p.v = p.p = 0;
  309. }
  310. template <typename Allocator, uintptr_t Bits>
  311. template <typename Function, typename OtherAllocator>
  312. void io_context::basic_executor_type<Allocator, Bits>::post(
  313. Function&& f, const OtherAllocator& a) const
  314. {
  315. // Allocate and construct an operation to wrap the function.
  316. typedef detail::executor_op<decay_t<Function>,
  317. OtherAllocator, detail::operation> op;
  318. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  319. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  320. BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  321. "io_context", context_ptr(), 0, "post"));
  322. context_ptr()->impl_.post_immediate_completion(p.p, false);
  323. p.v = p.p = 0;
  324. }
  325. template <typename Allocator, uintptr_t Bits>
  326. template <typename Function, typename OtherAllocator>
  327. void io_context::basic_executor_type<Allocator, Bits>::defer(
  328. Function&& f, const OtherAllocator& a) const
  329. {
  330. // Allocate and construct an operation to wrap the function.
  331. typedef detail::executor_op<decay_t<Function>,
  332. OtherAllocator, detail::operation> op;
  333. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  334. p.p = new (p.v) op(static_cast<Function&&>(f), a);
  335. BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  336. "io_context", context_ptr(), 0, "defer"));
  337. context_ptr()->impl_.post_immediate_completion(p.p, true);
  338. p.v = p.p = 0;
  339. }
  340. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  341. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  342. inline io_context::work::work(boost::asio::io_context& io_context)
  343. : io_context_impl_(io_context.impl_)
  344. {
  345. io_context_impl_.work_started();
  346. }
  347. inline io_context::work::work(const work& other)
  348. : io_context_impl_(other.io_context_impl_)
  349. {
  350. io_context_impl_.work_started();
  351. }
  352. inline io_context::work::~work()
  353. {
  354. io_context_impl_.work_finished();
  355. }
  356. inline boost::asio::io_context& io_context::work::get_io_context()
  357. {
  358. return static_cast<boost::asio::io_context&>(io_context_impl_.context());
  359. }
  360. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  361. inline boost::asio::io_context& io_context::service::get_io_context()
  362. {
  363. return static_cast<boost::asio::io_context&>(context());
  364. }
  365. } // namespace asio
  366. } // namespace boost
  367. #include <boost/asio/detail/pop_options.hpp>
  368. #endif // BOOST_ASIO_IMPL_IO_CONTEXT_HPP