io_context.hpp 13 KB

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