io_context.hpp 14 KB

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