io_context.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //
  2. // impl/io_context.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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(LegacyCompletionHandler, void ())
  131. io_context::dispatch(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
  132. {
  133. return async_initiate<LegacyCompletionHandler, void ()>(
  134. initiate_dispatch(), handler, this);
  135. }
  136. struct io_context::initiate_post
  137. {
  138. template <typename LegacyCompletionHandler>
  139. void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
  140. io_context* self) const
  141. {
  142. // If you get an error on the following line it means that your handler does
  143. // not meet the documented type requirements for a LegacyCompletionHandler.
  144. BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  145. LegacyCompletionHandler, handler) type_check;
  146. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  147. bool is_continuation =
  148. boost_asio_handler_cont_helpers::is_continuation(handler2.value);
  149. // Allocate and construct an operation to wrap the handler.
  150. typedef detail::completion_handler<
  151. typename decay<LegacyCompletionHandler>::type, executor_type> op;
  152. typename op::ptr p = { detail::addressof(handler2.value),
  153. op::ptr::allocate(handler2.value), 0 };
  154. p.p = new (p.v) op(handler2.value, self->get_executor());
  155. BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
  156. "io_context", self, 0, "post"));
  157. self->impl_.post_immediate_completion(p.p, is_continuation);
  158. p.v = p.p = 0;
  159. }
  160. };
  161. template <typename LegacyCompletionHandler>
  162. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
  163. io_context::post(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
  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(BOOST_ASIO_NO_DEPRECATED)
  179. template <typename Allocator, unsigned int Bits>
  180. io_context::basic_executor_type<Allocator, Bits>&
  181. io_context::basic_executor_type<Allocator, Bits>::operator=(
  182. const basic_executor_type& other) BOOST_ASIO_NOEXCEPT
  183. {
  184. if (this != &other)
  185. {
  186. io_context* old_io_context = io_context_;
  187. io_context_ = other.io_context_;
  188. allocator_ = other.allocator_;
  189. bits_ = other.bits_;
  190. if (Bits & outstanding_work_tracked)
  191. {
  192. if (io_context_)
  193. io_context_->impl_.work_started();
  194. if (old_io_context)
  195. old_io_context->impl_.work_finished();
  196. }
  197. }
  198. return *this;
  199. }
  200. #if defined(BOOST_ASIO_HAS_MOVE)
  201. template <typename Allocator, unsigned int Bits>
  202. io_context::basic_executor_type<Allocator, Bits>&
  203. io_context::basic_executor_type<Allocator, Bits>::operator=(
  204. basic_executor_type&& other) BOOST_ASIO_NOEXCEPT
  205. {
  206. if (this != &other)
  207. {
  208. io_context* old_io_context = io_context_;
  209. io_context_ = other.io_context_;
  210. allocator_ = std::move(other.allocator_);
  211. bits_ = other.bits_;
  212. if (Bits & outstanding_work_tracked)
  213. {
  214. other.io_context_ = 0;
  215. if (old_io_context)
  216. old_io_context->impl_.work_finished();
  217. }
  218. }
  219. return *this;
  220. }
  221. #endif // defined(BOOST_ASIO_HAS_MOVE)
  222. template <typename Allocator, unsigned int Bits>
  223. inline bool io_context::basic_executor_type<Allocator,
  224. Bits>::running_in_this_thread() const BOOST_ASIO_NOEXCEPT
  225. {
  226. return io_context_->impl_.can_dispatch();
  227. }
  228. template <typename Allocator, unsigned int Bits>
  229. template <typename Function>
  230. void io_context::basic_executor_type<Allocator, Bits>::execute(
  231. BOOST_ASIO_MOVE_ARG(Function) f) const
  232. {
  233. typedef typename decay<Function>::type function_type;
  234. // Invoke immediately if the blocking.possibly property is enabled and we are
  235. // already inside the thread pool.
  236. if ((bits_ & blocking_never) == 0 && io_context_->impl_.can_dispatch())
  237. {
  238. // Make a local, non-const copy of the function.
  239. function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
  240. #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
  241. && !defined(BOOST_ASIO_NO_EXCEPTIONS)
  242. try
  243. {
  244. #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
  245. // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
  246. detail::fenced_block b(detail::fenced_block::full);
  247. boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
  248. return;
  249. #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
  250. && !defined(BOOST_ASIO_NO_EXCEPTIONS)
  251. }
  252. catch (...)
  253. {
  254. io_context_->impl_.capture_current_exception();
  255. return;
  256. }
  257. #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
  258. // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
  259. }
  260. // Allocate and construct an operation to wrap the function.
  261. typedef detail::executor_op<function_type, Allocator, detail::operation> op;
  262. typename op::ptr p = { detail::addressof(allocator_),
  263. op::ptr::allocate(allocator_), 0 };
  264. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), allocator_);
  265. BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
  266. "io_context", io_context_, 0, "execute"));
  267. io_context_->impl_.post_immediate_completion(p.p,
  268. (bits_ & relationship_continuation) != 0);
  269. p.v = p.p = 0;
  270. }
  271. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  272. template <typename Allocator, unsigned int Bits>
  273. inline io_context& io_context::basic_executor_type<
  274. Allocator, Bits>::context() const BOOST_ASIO_NOEXCEPT
  275. {
  276. return *io_context_;
  277. }
  278. template <typename Allocator, unsigned int Bits>
  279. inline void io_context::basic_executor_type<Allocator,
  280. Bits>::on_work_started() const BOOST_ASIO_NOEXCEPT
  281. {
  282. io_context_->impl_.work_started();
  283. }
  284. template <typename Allocator, unsigned int Bits>
  285. inline void io_context::basic_executor_type<Allocator,
  286. Bits>::on_work_finished() const BOOST_ASIO_NOEXCEPT
  287. {
  288. io_context_->impl_.work_finished();
  289. }
  290. template <typename Allocator, unsigned int Bits>
  291. template <typename Function, typename OtherAllocator>
  292. void io_context::basic_executor_type<Allocator, Bits>::dispatch(
  293. BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
  294. {
  295. typedef typename decay<Function>::type function_type;
  296. // Invoke immediately if we are already inside the thread pool.
  297. if (io_context_->impl_.can_dispatch())
  298. {
  299. // Make a local, non-const copy of the function.
  300. function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
  301. detail::fenced_block b(detail::fenced_block::full);
  302. boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
  303. return;
  304. }
  305. // Allocate and construct an operation to wrap the function.
  306. typedef detail::executor_op<function_type,
  307. OtherAllocator, detail::operation> op;
  308. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  309. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  310. BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
  311. "io_context", io_context_, 0, "dispatch"));
  312. io_context_->impl_.post_immediate_completion(p.p, false);
  313. p.v = p.p = 0;
  314. }
  315. template <typename Allocator, unsigned int Bits>
  316. template <typename Function, typename OtherAllocator>
  317. void io_context::basic_executor_type<Allocator, Bits>::post(
  318. BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
  319. {
  320. typedef typename decay<Function>::type function_type;
  321. // Allocate and construct an operation to wrap the function.
  322. typedef detail::executor_op<function_type,
  323. OtherAllocator, detail::operation> op;
  324. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  325. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  326. BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
  327. "io_context", io_context_, 0, "post"));
  328. io_context_->impl_.post_immediate_completion(p.p, false);
  329. p.v = p.p = 0;
  330. }
  331. template <typename Allocator, unsigned int Bits>
  332. template <typename Function, typename OtherAllocator>
  333. void io_context::basic_executor_type<Allocator, Bits>::defer(
  334. BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
  335. {
  336. typedef typename decay<Function>::type function_type;
  337. // Allocate and construct an operation to wrap the function.
  338. typedef detail::executor_op<function_type,
  339. OtherAllocator, detail::operation> op;
  340. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  341. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  342. BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
  343. "io_context", io_context_, 0, "defer"));
  344. io_context_->impl_.post_immediate_completion(p.p, true);
  345. p.v = p.p = 0;
  346. }
  347. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  348. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  349. inline io_context::work::work(boost::asio::io_context& io_context)
  350. : io_context_impl_(io_context.impl_)
  351. {
  352. io_context_impl_.work_started();
  353. }
  354. inline io_context::work::work(const work& other)
  355. : io_context_impl_(other.io_context_impl_)
  356. {
  357. io_context_impl_.work_started();
  358. }
  359. inline io_context::work::~work()
  360. {
  361. io_context_impl_.work_finished();
  362. }
  363. inline boost::asio::io_context& io_context::work::get_io_context()
  364. {
  365. return static_cast<boost::asio::io_context&>(io_context_impl_.context());
  366. }
  367. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  368. inline boost::asio::io_context& io_context::service::get_io_context()
  369. {
  370. return static_cast<boost::asio::io_context&>(context());
  371. }
  372. } // namespace asio
  373. } // namespace boost
  374. #include <boost/asio/detail/pop_options.hpp>
  375. #endif // BOOST_ASIO_IMPL_IO_CONTEXT_HPP