io_context.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // impl/io_context.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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/recycling_allocator.hpp>
  21. #include <boost/asio/detail/service_registry.hpp>
  22. #include <boost/asio/detail/throw_error.hpp>
  23. #include <boost/asio/detail/type_traits.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. #if !defined(GENERATING_DOCUMENTATION)
  26. namespace boost {
  27. namespace asio {
  28. template <typename Service>
  29. inline Service& use_service(io_context& ioc)
  30. {
  31. // Check that Service meets the necessary type requirements.
  32. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  33. (void)static_cast<const execution_context::id*>(&Service::id);
  34. return ioc.service_registry_->template use_service<Service>(ioc);
  35. }
  36. template <>
  37. inline detail::io_context_impl& use_service<detail::io_context_impl>(
  38. io_context& ioc)
  39. {
  40. return ioc.impl_;
  41. }
  42. } // namespace asio
  43. } // namespace boost
  44. #endif // !defined(GENERATING_DOCUMENTATION)
  45. #include <boost/asio/detail/pop_options.hpp>
  46. #if defined(BOOST_ASIO_HAS_IOCP)
  47. # include <boost/asio/detail/win_iocp_io_context.hpp>
  48. #else
  49. # include <boost/asio/detail/scheduler.hpp>
  50. #endif
  51. #include <boost/asio/detail/push_options.hpp>
  52. namespace boost {
  53. namespace asio {
  54. inline io_context::executor_type
  55. io_context::get_executor() BOOST_ASIO_NOEXCEPT
  56. {
  57. return executor_type(*this);
  58. }
  59. #if defined(BOOST_ASIO_HAS_CHRONO)
  60. template <typename Rep, typename Period>
  61. std::size_t io_context::run_for(
  62. const chrono::duration<Rep, Period>& rel_time)
  63. {
  64. return this->run_until(chrono::steady_clock::now() + rel_time);
  65. }
  66. template <typename Clock, typename Duration>
  67. std::size_t io_context::run_until(
  68. const chrono::time_point<Clock, Duration>& abs_time)
  69. {
  70. std::size_t n = 0;
  71. while (this->run_one_until(abs_time))
  72. if (n != (std::numeric_limits<std::size_t>::max)())
  73. ++n;
  74. return n;
  75. }
  76. template <typename Rep, typename Period>
  77. std::size_t io_context::run_one_for(
  78. const chrono::duration<Rep, Period>& rel_time)
  79. {
  80. return this->run_one_until(chrono::steady_clock::now() + rel_time);
  81. }
  82. template <typename Clock, typename Duration>
  83. std::size_t io_context::run_one_until(
  84. const chrono::time_point<Clock, Duration>& abs_time)
  85. {
  86. typename Clock::time_point now = Clock::now();
  87. while (now < abs_time)
  88. {
  89. typename Clock::duration rel_time = abs_time - now;
  90. if (rel_time > chrono::seconds(1))
  91. rel_time = chrono::seconds(1);
  92. boost::system::error_code ec;
  93. std::size_t s = impl_.wait_one(
  94. static_cast<long>(chrono::duration_cast<
  95. chrono::microseconds>(rel_time).count()), ec);
  96. boost::asio::detail::throw_error(ec);
  97. if (s || impl_.stopped())
  98. return s;
  99. now = Clock::now();
  100. }
  101. return 0;
  102. }
  103. #endif // defined(BOOST_ASIO_HAS_CHRONO)
  104. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  105. inline void io_context::reset()
  106. {
  107. restart();
  108. }
  109. struct io_context::initiate_dispatch
  110. {
  111. template <typename LegacyCompletionHandler>
  112. void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
  113. io_context* self) const
  114. {
  115. // If you get an error on the following line it means that your handler does
  116. // not meet the documented type requirements for a LegacyCompletionHandler.
  117. BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  118. LegacyCompletionHandler, handler) type_check;
  119. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  120. if (self->impl_.can_dispatch())
  121. {
  122. detail::fenced_block b(detail::fenced_block::full);
  123. boost_asio_handler_invoke_helpers::invoke(
  124. handler2.value, handler2.value);
  125. }
  126. else
  127. {
  128. // Allocate and construct an operation to wrap the handler.
  129. typedef detail::completion_handler<
  130. typename decay<LegacyCompletionHandler>::type> op;
  131. typename op::ptr p = { detail::addressof(handler2.value),
  132. op::ptr::allocate(handler2.value), 0 };
  133. p.p = new (p.v) op(handler2.value);
  134. BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
  135. "io_context", self, 0, "dispatch"));
  136. self->impl_.do_dispatch(p.p);
  137. p.v = p.p = 0;
  138. }
  139. }
  140. };
  141. template <typename LegacyCompletionHandler>
  142. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
  143. io_context::dispatch(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
  144. {
  145. return async_initiate<LegacyCompletionHandler, void ()>(
  146. initiate_dispatch(), handler, this);
  147. }
  148. struct io_context::initiate_post
  149. {
  150. template <typename LegacyCompletionHandler>
  151. void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
  152. io_context* self) const
  153. {
  154. // If you get an error on the following line it means that your handler does
  155. // not meet the documented type requirements for a LegacyCompletionHandler.
  156. BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
  157. LegacyCompletionHandler, handler) type_check;
  158. detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
  159. bool is_continuation =
  160. boost_asio_handler_cont_helpers::is_continuation(handler2.value);
  161. // Allocate and construct an operation to wrap the handler.
  162. typedef detail::completion_handler<
  163. typename decay<LegacyCompletionHandler>::type> op;
  164. typename op::ptr p = { detail::addressof(handler2.value),
  165. op::ptr::allocate(handler2.value), 0 };
  166. p.p = new (p.v) op(handler2.value);
  167. BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
  168. "io_context", self, 0, "post"));
  169. self->impl_.post_immediate_completion(p.p, is_continuation);
  170. p.v = p.p = 0;
  171. }
  172. };
  173. template <typename LegacyCompletionHandler>
  174. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
  175. io_context::post(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
  176. {
  177. return async_initiate<LegacyCompletionHandler, void ()>(
  178. initiate_post(), handler, this);
  179. }
  180. template <typename Handler>
  181. #if defined(GENERATING_DOCUMENTATION)
  182. unspecified
  183. #else
  184. inline detail::wrapped_handler<io_context&, Handler>
  185. #endif
  186. io_context::wrap(Handler handler)
  187. {
  188. return detail::wrapped_handler<io_context&, Handler>(*this, handler);
  189. }
  190. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  191. inline io_context&
  192. io_context::executor_type::context() const BOOST_ASIO_NOEXCEPT
  193. {
  194. return io_context_;
  195. }
  196. inline void
  197. io_context::executor_type::on_work_started() const BOOST_ASIO_NOEXCEPT
  198. {
  199. io_context_.impl_.work_started();
  200. }
  201. inline void
  202. io_context::executor_type::on_work_finished() const BOOST_ASIO_NOEXCEPT
  203. {
  204. io_context_.impl_.work_finished();
  205. }
  206. template <typename Function, typename Allocator>
  207. void io_context::executor_type::dispatch(
  208. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  209. {
  210. typedef typename decay<Function>::type function_type;
  211. // Invoke immediately if we are already inside the thread pool.
  212. if (io_context_.impl_.can_dispatch())
  213. {
  214. // Make a local, non-const copy of the function.
  215. function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
  216. detail::fenced_block b(detail::fenced_block::full);
  217. boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
  218. return;
  219. }
  220. // Allocate and construct an operation to wrap the function.
  221. typedef detail::executor_op<function_type, Allocator, detail::operation> op;
  222. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  223. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  224. BOOST_ASIO_HANDLER_CREATION((this->context(), *p.p,
  225. "io_context", &this->context(), 0, "dispatch"));
  226. io_context_.impl_.post_immediate_completion(p.p, false);
  227. p.v = p.p = 0;
  228. }
  229. template <typename Function, typename Allocator>
  230. void io_context::executor_type::post(
  231. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  232. {
  233. typedef typename decay<Function>::type function_type;
  234. // Allocate and construct an operation to wrap the function.
  235. typedef detail::executor_op<function_type, Allocator, detail::operation> op;
  236. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  237. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  238. BOOST_ASIO_HANDLER_CREATION((this->context(), *p.p,
  239. "io_context", &this->context(), 0, "post"));
  240. io_context_.impl_.post_immediate_completion(p.p, false);
  241. p.v = p.p = 0;
  242. }
  243. template <typename Function, typename Allocator>
  244. void io_context::executor_type::defer(
  245. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  246. {
  247. typedef typename decay<Function>::type function_type;
  248. // Allocate and construct an operation to wrap the function.
  249. typedef detail::executor_op<function_type, Allocator, detail::operation> op;
  250. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  251. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  252. BOOST_ASIO_HANDLER_CREATION((this->context(), *p.p,
  253. "io_context", &this->context(), 0, "defer"));
  254. io_context_.impl_.post_immediate_completion(p.p, true);
  255. p.v = p.p = 0;
  256. }
  257. inline bool
  258. io_context::executor_type::running_in_this_thread() const BOOST_ASIO_NOEXCEPT
  259. {
  260. return io_context_.impl_.can_dispatch();
  261. }
  262. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  263. inline io_context::work::work(boost::asio::io_context& io_context)
  264. : io_context_impl_(io_context.impl_)
  265. {
  266. io_context_impl_.work_started();
  267. }
  268. inline io_context::work::work(const work& other)
  269. : io_context_impl_(other.io_context_impl_)
  270. {
  271. io_context_impl_.work_started();
  272. }
  273. inline io_context::work::~work()
  274. {
  275. io_context_impl_.work_finished();
  276. }
  277. inline boost::asio::io_context& io_context::work::get_io_context()
  278. {
  279. return static_cast<boost::asio::io_context&>(io_context_impl_.context());
  280. }
  281. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  282. inline boost::asio::io_context& io_context::service::get_io_context()
  283. {
  284. return static_cast<boost::asio::io_context&>(context());
  285. }
  286. } // namespace asio
  287. } // namespace boost
  288. #include <boost/asio/detail/pop_options.hpp>
  289. #endif // BOOST_ASIO_IMPL_IO_CONTEXT_HPP