awaitable.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //
  2. // impl/awaitable.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_AWAITABLE_HPP
  11. #define BOOST_ASIO_IMPL_AWAITABLE_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/config.hpp>
  16. #include <exception>
  17. #include <new>
  18. #include <tuple>
  19. #include <utility>
  20. #include <boost/asio/detail/thread_context.hpp>
  21. #include <boost/asio/detail/thread_info_base.hpp>
  22. #include <boost/asio/detail/type_traits.hpp>
  23. #include <boost/asio/post.hpp>
  24. #include <boost/system/system_error.hpp>
  25. #include <boost/asio/this_coro.hpp>
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. // An awaitable_thread represents a thread-of-execution that is composed of one
  31. // or more "stack frames", with each frame represented by an awaitable_frame.
  32. // All execution occurs in the context of the awaitable_thread's executor. An
  33. // awaitable_thread continues to "pump" the stack frames by repeatedly resuming
  34. // the top stack frame until the stack is empty, or until ownership of the
  35. // stack is transferred to another awaitable_thread object.
  36. //
  37. // +------------------------------------+
  38. // | top_of_stack_ |
  39. // | V
  40. // +--------------+---+ +-----------------+
  41. // | | | |
  42. // | awaitable_thread |<---------------------------+ awaitable_frame |
  43. // | | attached_thread_ | |
  44. // +--------------+---+ (Set only when +---+-------------+
  45. // | frames are being |
  46. // | actively pumped | caller_
  47. // | by a thread, and |
  48. // | then only for V
  49. // | the top frame.) +-----------------+
  50. // | | |
  51. // | | awaitable_frame |
  52. // | | |
  53. // | +---+-------------+
  54. // | |
  55. // | | caller_
  56. // | :
  57. // | :
  58. // | |
  59. // | V
  60. // | +-----------------+
  61. // | bottom_of_stack_ | |
  62. // +------------------------------->| awaitable_frame |
  63. // | |
  64. // +-----------------+
  65. template <typename Executor>
  66. class awaitable_frame_base
  67. {
  68. public:
  69. #if !defined(BOOST_ASIO_DISABLE_AWAITABLE_FRAME_RECYCLING)
  70. void* operator new(std::size_t size)
  71. {
  72. return boost::asio::detail::thread_info_base::allocate(
  73. boost::asio::detail::thread_info_base::awaitable_frame_tag(),
  74. boost::asio::detail::thread_context::thread_call_stack::top(),
  75. size);
  76. }
  77. void operator delete(void* pointer, std::size_t size)
  78. {
  79. boost::asio::detail::thread_info_base::deallocate(
  80. boost::asio::detail::thread_info_base::awaitable_frame_tag(),
  81. boost::asio::detail::thread_context::thread_call_stack::top(),
  82. pointer, size);
  83. }
  84. #endif // !defined(BOOST_ASIO_DISABLE_AWAITABLE_FRAME_RECYCLING)
  85. // The frame starts in a suspended state until the awaitable_thread object
  86. // pumps the stack.
  87. auto initial_suspend() noexcept
  88. {
  89. return suspend_always();
  90. }
  91. // On final suspension the frame is popped from the top of the stack.
  92. auto final_suspend() noexcept
  93. {
  94. struct result
  95. {
  96. awaitable_frame_base* this_;
  97. bool await_ready() const noexcept
  98. {
  99. return false;
  100. }
  101. void await_suspend(coroutine_handle<void>) noexcept
  102. {
  103. this_->pop_frame();
  104. }
  105. void await_resume() const noexcept
  106. {
  107. }
  108. };
  109. return result{this};
  110. }
  111. void set_except(std::exception_ptr e) noexcept
  112. {
  113. pending_exception_ = e;
  114. }
  115. void set_error(const boost::system::error_code& ec)
  116. {
  117. this->set_except(std::make_exception_ptr(boost::system::system_error(ec)));
  118. }
  119. void unhandled_exception()
  120. {
  121. set_except(std::current_exception());
  122. }
  123. void rethrow_exception()
  124. {
  125. if (pending_exception_)
  126. {
  127. std::exception_ptr ex = std::exchange(pending_exception_, nullptr);
  128. std::rethrow_exception(ex);
  129. }
  130. }
  131. template <typename T>
  132. auto await_transform(awaitable<T, Executor> a) const
  133. {
  134. return a;
  135. }
  136. // This await transformation obtains the associated executor of the thread of
  137. // execution.
  138. auto await_transform(this_coro::executor_t) noexcept
  139. {
  140. struct result
  141. {
  142. awaitable_frame_base* this_;
  143. bool await_ready() const noexcept
  144. {
  145. return true;
  146. }
  147. void await_suspend(coroutine_handle<void>) noexcept
  148. {
  149. }
  150. auto await_resume() const noexcept
  151. {
  152. return this_->attached_thread_->get_executor();
  153. }
  154. };
  155. return result{this};
  156. }
  157. // This await transformation is used to run an async operation's initiation
  158. // function object after the coroutine has been suspended. This ensures that
  159. // immediate resumption of the coroutine in another thread does not cause a
  160. // race condition.
  161. template <typename Function>
  162. auto await_transform(Function f,
  163. typename enable_if<
  164. is_convertible<
  165. typename result_of<Function(awaitable_frame_base*)>::type,
  166. awaitable_thread<Executor>*
  167. >::value
  168. >::type* = 0)
  169. {
  170. struct result
  171. {
  172. Function function_;
  173. awaitable_frame_base* this_;
  174. bool await_ready() const noexcept
  175. {
  176. return false;
  177. }
  178. void await_suspend(coroutine_handle<void>) noexcept
  179. {
  180. function_(this_);
  181. }
  182. void await_resume() const noexcept
  183. {
  184. }
  185. };
  186. return result{std::move(f), this};
  187. }
  188. void attach_thread(awaitable_thread<Executor>* handler) noexcept
  189. {
  190. attached_thread_ = handler;
  191. }
  192. awaitable_thread<Executor>* detach_thread() noexcept
  193. {
  194. return std::exchange(attached_thread_, nullptr);
  195. }
  196. void push_frame(awaitable_frame_base<Executor>* caller) noexcept
  197. {
  198. caller_ = caller;
  199. attached_thread_ = caller_->attached_thread_;
  200. attached_thread_->top_of_stack_ = this;
  201. caller_->attached_thread_ = nullptr;
  202. }
  203. void pop_frame() noexcept
  204. {
  205. if (caller_)
  206. caller_->attached_thread_ = attached_thread_;
  207. attached_thread_->top_of_stack_ = caller_;
  208. attached_thread_ = nullptr;
  209. caller_ = nullptr;
  210. }
  211. void resume()
  212. {
  213. coro_.resume();
  214. }
  215. void destroy()
  216. {
  217. coro_.destroy();
  218. }
  219. protected:
  220. coroutine_handle<void> coro_ = nullptr;
  221. awaitable_thread<Executor>* attached_thread_ = nullptr;
  222. awaitable_frame_base<Executor>* caller_ = nullptr;
  223. std::exception_ptr pending_exception_ = nullptr;
  224. };
  225. template <typename T, typename Executor>
  226. class awaitable_frame
  227. : public awaitable_frame_base<Executor>
  228. {
  229. public:
  230. awaitable_frame() noexcept
  231. {
  232. }
  233. awaitable_frame(awaitable_frame&& other) noexcept
  234. : awaitable_frame_base<Executor>(std::move(other))
  235. {
  236. }
  237. ~awaitable_frame()
  238. {
  239. if (has_result_)
  240. static_cast<T*>(static_cast<void*>(result_))->~T();
  241. }
  242. awaitable<T, Executor> get_return_object() noexcept
  243. {
  244. this->coro_ = coroutine_handle<awaitable_frame>::from_promise(*this);
  245. return awaitable<T, Executor>(this);
  246. };
  247. template <typename U>
  248. void return_value(U&& u)
  249. {
  250. new (&result_) T(std::forward<U>(u));
  251. has_result_ = true;
  252. }
  253. template <typename... Us>
  254. void return_values(Us&&... us)
  255. {
  256. this->return_value(std::forward_as_tuple(std::forward<Us>(us)...));
  257. }
  258. T get()
  259. {
  260. this->caller_ = nullptr;
  261. this->rethrow_exception();
  262. return std::move(*static_cast<T*>(static_cast<void*>(result_)));
  263. }
  264. private:
  265. alignas(T) unsigned char result_[sizeof(T)];
  266. bool has_result_ = false;
  267. };
  268. template <typename Executor>
  269. class awaitable_frame<void, Executor>
  270. : public awaitable_frame_base<Executor>
  271. {
  272. public:
  273. awaitable<void, Executor> get_return_object()
  274. {
  275. this->coro_ = coroutine_handle<awaitable_frame>::from_promise(*this);
  276. return awaitable<void, Executor>(this);
  277. };
  278. void return_void()
  279. {
  280. }
  281. void get()
  282. {
  283. this->caller_ = nullptr;
  284. this->rethrow_exception();
  285. }
  286. };
  287. template <typename Executor>
  288. class awaitable_thread
  289. {
  290. public:
  291. typedef Executor executor_type;
  292. // Construct from the entry point of a new thread of execution.
  293. awaitable_thread(awaitable<void, Executor> p, const Executor& ex)
  294. : bottom_of_stack_(std::move(p)),
  295. top_of_stack_(bottom_of_stack_.frame_),
  296. executor_(ex)
  297. {
  298. }
  299. // Transfer ownership from another awaitable_thread.
  300. awaitable_thread(awaitable_thread&& other) noexcept
  301. : bottom_of_stack_(std::move(other.bottom_of_stack_)),
  302. top_of_stack_(std::exchange(other.top_of_stack_, nullptr)),
  303. executor_(std::move(other.executor_))
  304. {
  305. }
  306. // Clean up with a last ditch effort to ensure the thread is unwound within
  307. // the context of the executor.
  308. ~awaitable_thread()
  309. {
  310. if (bottom_of_stack_.valid())
  311. {
  312. // Coroutine "stack unwinding" must be performed through the executor.
  313. (post)(executor_,
  314. [a = std::move(bottom_of_stack_)]() mutable
  315. {
  316. awaitable<void, Executor>(std::move(a));
  317. });
  318. }
  319. }
  320. executor_type get_executor() const noexcept
  321. {
  322. return executor_;
  323. }
  324. // Launch a new thread of execution.
  325. void launch()
  326. {
  327. top_of_stack_->attach_thread(this);
  328. pump();
  329. }
  330. protected:
  331. template <typename> friend class awaitable_frame_base;
  332. // Repeatedly resume the top stack frame until the stack is empty or until it
  333. // has been transferred to another resumable_thread object.
  334. void pump()
  335. {
  336. do top_of_stack_->resume(); while (top_of_stack_);
  337. if (bottom_of_stack_.valid())
  338. {
  339. awaitable<void, Executor> a(std::move(bottom_of_stack_));
  340. a.frame_->rethrow_exception();
  341. }
  342. }
  343. awaitable<void, Executor> bottom_of_stack_;
  344. awaitable_frame_base<Executor>* top_of_stack_;
  345. executor_type executor_;
  346. };
  347. } // namespace detail
  348. } // namespace asio
  349. } // namespace boost
  350. #if !defined(GENERATING_DOCUMENTATION)
  351. namespace std { namespace experimental {
  352. template <typename T, typename Executor, typename... Args>
  353. struct coroutine_traits<boost::asio::awaitable<T, Executor>, Args...>
  354. {
  355. typedef boost::asio::detail::awaitable_frame<T, Executor> promise_type;
  356. };
  357. }} // namespace std::experimental
  358. #endif // !defined(GENERATING_DOCUMENTATION)
  359. #include <boost/asio/detail/pop_options.hpp>
  360. #endif // BOOST_ASIO_IMPL_AWAITABLE_HPP