io_context.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. //
  2. // impl/io_context.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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() ASIO_NOEXCEPT
  43. {
  44. return executor_type(*this);
  45. }
  46. #if defined(ASIO_HAS_CHRONO)
  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. asio::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. asio::detail::throw_error(ec);
  84. if (s || impl_.stopped())
  85. return s;
  86. now = Clock::now();
  87. }
  88. return 0;
  89. }
  90. #endif // defined(ASIO_HAS_CHRONO)
  91. #if !defined(ASIO_NO_DEPRECATED)
  92. inline void io_context::reset()
  93. {
  94. restart();
  95. }
  96. struct io_context::initiate_dispatch
  97. {
  98. template <typename LegacyCompletionHandler>
  99. void operator()(ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
  100. io_context* self) const
  101. {
  102. // If you get an error on the following line it means that your handler does
  103. // not meet the documented type requirements for a LegacyCompletionHandler.
  104. ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  105. LegacyCompletionHandler, handler) type_check;
  106. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  107. if (self->impl_.can_dispatch())
  108. {
  109. detail::fenced_block b(detail::fenced_block::full);
  110. asio_handler_invoke_helpers::invoke(
  111. handler2.value, handler2.value);
  112. }
  113. else
  114. {
  115. // Allocate and construct an operation to wrap the handler.
  116. typedef detail::completion_handler<
  117. typename decay<LegacyCompletionHandler>::type, executor_type> op;
  118. typename op::ptr p = { detail::addressof(handler2.value),
  119. op::ptr::allocate(handler2.value), 0 };
  120. p.p = new (p.v) op(handler2.value, self->get_executor());
  121. ASIO_HANDLER_CREATION((*self, *p.p,
  122. "io_context", self, 0, "dispatch"));
  123. self->impl_.do_dispatch(p.p);
  124. p.v = p.p = 0;
  125. }
  126. }
  127. };
  128. template <typename LegacyCompletionHandler>
  129. ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
  130. io_context::dispatch(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
  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()(ASIO_MOVE_ARG(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. ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  144. LegacyCompletionHandler, handler) type_check;
  145. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  146. bool is_continuation =
  147. asio_handler_cont_helpers::is_continuation(handler2.value);
  148. // Allocate and construct an operation to wrap the handler.
  149. typedef detail::completion_handler<
  150. typename decay<LegacyCompletionHandler>::type, 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. 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. ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
  162. io_context::post(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
  163. {
  164. return async_initiate<LegacyCompletionHandler, void ()>(
  165. initiate_post(), handler, this);
  166. }
  167. template <typename Handler>
  168. #if defined(GENERATING_DOCUMENTATION)
  169. unspecified
  170. #else
  171. inline detail::wrapped_handler<io_context&, Handler>
  172. #endif
  173. io_context::wrap(Handler handler)
  174. {
  175. return detail::wrapped_handler<io_context&, Handler>(*this, handler);
  176. }
  177. #endif // !defined(ASIO_NO_DEPRECATED)
  178. template <typename Allocator, uintptr_t Bits>
  179. io_context::basic_executor_type<Allocator, Bits>&
  180. io_context::basic_executor_type<Allocator, Bits>::operator=(
  181. const basic_executor_type& other) ASIO_NOEXCEPT
  182. {
  183. if (this != &other)
  184. {
  185. static_cast<Allocator&>(*this) = static_cast<const Allocator&>(other);
  186. io_context* old_io_context = context_ptr();
  187. target_ = other.target_;
  188. if (Bits & outstanding_work_tracked)
  189. {
  190. if (context_ptr())
  191. context_ptr()->impl_.work_started();
  192. if (old_io_context)
  193. old_io_context->impl_.work_finished();
  194. }
  195. }
  196. return *this;
  197. }
  198. #if defined(ASIO_HAS_MOVE)
  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) ASIO_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. #endif // defined(ASIO_HAS_MOVE)
  219. template <typename Allocator, uintptr_t Bits>
  220. inline bool io_context::basic_executor_type<Allocator,
  221. Bits>::running_in_this_thread() const ASIO_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. ASIO_MOVE_ARG(Function) f) const
  229. {
  230. typedef typename decay<Function>::type 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(ASIO_MOVE_CAST(Function)(f));
  237. #if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
  238. && !defined(ASIO_NO_EXCEPTIONS)
  239. try
  240. {
  241. #endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
  242. // && !defined(ASIO_NO_EXCEPTIONS)
  243. detail::fenced_block b(detail::fenced_block::full);
  244. asio_handler_invoke_helpers::invoke(tmp, tmp);
  245. return;
  246. #if defined(ASIO_HAS_STD_EXCEPTION_PTR) \
  247. && !defined(ASIO_NO_EXCEPTIONS)
  248. }
  249. catch (...)
  250. {
  251. context_ptr()->impl_.capture_current_exception();
  252. return;
  253. }
  254. #endif // defined(ASIO_HAS_STD_EXCEPTION_PTR)
  255. // && !defined(ASIO_NO_EXCEPTIONS)
  256. }
  257. // Allocate and construct an operation to wrap the function.
  258. typedef detail::executor_op<function_type, Allocator, detail::operation> op;
  259. typename op::ptr p = {
  260. detail::addressof(static_cast<const Allocator&>(*this)),
  261. op::ptr::allocate(static_cast<const Allocator&>(*this)), 0 };
  262. p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f),
  263. static_cast<const Allocator&>(*this));
  264. ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  265. "io_context", context_ptr(), 0, "execute"));
  266. context_ptr()->impl_.post_immediate_completion(p.p,
  267. (bits() & relationship_continuation) != 0);
  268. p.v = p.p = 0;
  269. }
  270. #if !defined(ASIO_NO_TS_EXECUTORS)
  271. template <typename Allocator, uintptr_t Bits>
  272. inline io_context& io_context::basic_executor_type<
  273. Allocator, Bits>::context() const ASIO_NOEXCEPT
  274. {
  275. return *context_ptr();
  276. }
  277. template <typename Allocator, uintptr_t Bits>
  278. inline void io_context::basic_executor_type<Allocator,
  279. Bits>::on_work_started() const ASIO_NOEXCEPT
  280. {
  281. context_ptr()->impl_.work_started();
  282. }
  283. template <typename Allocator, uintptr_t Bits>
  284. inline void io_context::basic_executor_type<Allocator,
  285. Bits>::on_work_finished() const ASIO_NOEXCEPT
  286. {
  287. context_ptr()->impl_.work_finished();
  288. }
  289. template <typename Allocator, uintptr_t Bits>
  290. template <typename Function, typename OtherAllocator>
  291. void io_context::basic_executor_type<Allocator, Bits>::dispatch(
  292. ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
  293. {
  294. typedef typename decay<Function>::type function_type;
  295. // Invoke immediately if we are already inside the thread pool.
  296. if (context_ptr()->impl_.can_dispatch())
  297. {
  298. // Make a local, non-const copy of the function.
  299. function_type tmp(ASIO_MOVE_CAST(Function)(f));
  300. detail::fenced_block b(detail::fenced_block::full);
  301. asio_handler_invoke_helpers::invoke(tmp, tmp);
  302. return;
  303. }
  304. // Allocate and construct an operation to wrap the function.
  305. typedef detail::executor_op<function_type,
  306. OtherAllocator, detail::operation> op;
  307. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  308. p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
  309. ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  310. "io_context", context_ptr(), 0, "dispatch"));
  311. context_ptr()->impl_.post_immediate_completion(p.p, false);
  312. p.v = p.p = 0;
  313. }
  314. template <typename Allocator, uintptr_t Bits>
  315. template <typename Function, typename OtherAllocator>
  316. void io_context::basic_executor_type<Allocator, Bits>::post(
  317. ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
  318. {
  319. typedef typename decay<Function>::type function_type;
  320. // Allocate and construct an operation to wrap the function.
  321. typedef detail::executor_op<function_type,
  322. OtherAllocator, detail::operation> op;
  323. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  324. p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
  325. ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  326. "io_context", context_ptr(), 0, "post"));
  327. context_ptr()->impl_.post_immediate_completion(p.p, false);
  328. p.v = p.p = 0;
  329. }
  330. template <typename Allocator, uintptr_t Bits>
  331. template <typename Function, typename OtherAllocator>
  332. void io_context::basic_executor_type<Allocator, Bits>::defer(
  333. ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
  334. {
  335. typedef typename decay<Function>::type function_type;
  336. // Allocate and construct an operation to wrap the function.
  337. typedef detail::executor_op<function_type,
  338. OtherAllocator, detail::operation> op;
  339. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  340. p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
  341. ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
  342. "io_context", context_ptr(), 0, "defer"));
  343. context_ptr()->impl_.post_immediate_completion(p.p, true);
  344. p.v = p.p = 0;
  345. }
  346. #endif // !defined(ASIO_NO_TS_EXECUTORS)
  347. #if !defined(ASIO_NO_DEPRECATED)
  348. inline io_context::work::work(asio::io_context& io_context)
  349. : io_context_impl_(io_context.impl_)
  350. {
  351. io_context_impl_.work_started();
  352. }
  353. inline io_context::work::work(const work& other)
  354. : io_context_impl_(other.io_context_impl_)
  355. {
  356. io_context_impl_.work_started();
  357. }
  358. inline io_context::work::~work()
  359. {
  360. io_context_impl_.work_finished();
  361. }
  362. inline asio::io_context& io_context::work::get_io_context()
  363. {
  364. return static_cast<asio::io_context&>(io_context_impl_.context());
  365. }
  366. #endif // !defined(ASIO_NO_DEPRECATED)
  367. inline asio::io_context& io_context::service::get_io_context()
  368. {
  369. return static_cast<asio::io_context&>(context());
  370. }
  371. } // namespace asio
  372. #include "asio/detail/pop_options.hpp"
  373. #endif // ASIO_IMPL_IO_CONTEXT_HPP