]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | // |
2 | // impl/co_spawn.hpp | |
3 | // ~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
92f5a8d4 TL |
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 | ||
11 | #ifndef BOOST_ASIO_IMPL_CO_SPAWN_HPP | |
12 | #define BOOST_ASIO_IMPL_CO_SPAWN_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | #include <boost/asio/awaitable.hpp> | |
20 | #include <boost/asio/dispatch.hpp> | |
21 | #include <boost/asio/post.hpp> | |
22 | #include <boost/asio/use_awaitable.hpp> | |
23 | ||
24 | #include <boost/asio/detail/push_options.hpp> | |
25 | ||
26 | namespace boost { | |
27 | namespace asio { | |
28 | namespace detail { | |
29 | ||
30 | template <typename T, typename Executor, typename F, typename Handler> | |
31 | awaitable<void, Executor> co_spawn_entry_point( | |
32 | awaitable<T, Executor>*, Executor ex, F f, Handler handler) | |
33 | { | |
34 | auto spawn_work = make_work_guard(ex); | |
35 | auto handler_work = make_work_guard(handler, ex); | |
36 | ||
37 | (void) co_await (post)(spawn_work.get_executor(), | |
38 | use_awaitable_t<Executor>{}); | |
39 | ||
40 | bool done = false; | |
41 | try | |
42 | { | |
43 | T t = co_await f(); | |
44 | ||
45 | done = true; | |
46 | ||
47 | (dispatch)(handler_work.get_executor(), | |
48 | [handler = std::move(handler), t = std::move(t)]() mutable | |
49 | { | |
50 | handler(std::exception_ptr(), std::move(t)); | |
51 | }); | |
52 | } | |
53 | catch (...) | |
54 | { | |
55 | if (done) | |
56 | throw; | |
57 | ||
58 | (dispatch)(handler_work.get_executor(), | |
59 | [handler = std::move(handler), e = std::current_exception()]() mutable | |
60 | { | |
61 | handler(e, T()); | |
62 | }); | |
63 | } | |
64 | } | |
65 | ||
66 | template <typename Executor, typename F, typename Handler> | |
67 | awaitable<void, Executor> co_spawn_entry_point( | |
68 | awaitable<void, Executor>*, Executor ex, F f, Handler handler) | |
69 | { | |
70 | auto spawn_work = make_work_guard(ex); | |
71 | auto handler_work = make_work_guard(handler, ex); | |
72 | ||
73 | (void) co_await (post)(spawn_work.get_executor(), | |
74 | use_awaitable_t<Executor>{}); | |
75 | ||
76 | std::exception_ptr e = nullptr; | |
77 | try | |
78 | { | |
79 | co_await f(); | |
80 | } | |
81 | catch (...) | |
82 | { | |
83 | e = std::current_exception(); | |
84 | } | |
85 | ||
86 | (dispatch)(handler_work.get_executor(), | |
87 | [handler = std::move(handler), e]() mutable | |
88 | { | |
89 | handler(e); | |
90 | }); | |
91 | } | |
92 | ||
93 | template <typename Executor> | |
94 | class initiate_co_spawn | |
95 | { | |
96 | public: | |
97 | typedef Executor executor_type; | |
98 | ||
99 | template <typename OtherExecutor> | |
100 | explicit initiate_co_spawn(const OtherExecutor& ex) | |
101 | : ex_(ex) | |
102 | { | |
103 | } | |
104 | ||
105 | executor_type get_executor() const BOOST_ASIO_NOEXCEPT | |
106 | { | |
107 | return ex_; | |
108 | } | |
109 | ||
110 | template <typename Handler, typename F> | |
111 | void operator()(Handler&& handler, F&& f) const | |
112 | { | |
113 | typedef typename result_of<F()>::type awaitable_type; | |
114 | ||
115 | auto a = (co_spawn_entry_point)(static_cast<awaitable_type*>(nullptr), | |
116 | ex_, std::forward<F>(f), std::forward<Handler>(handler)); | |
117 | awaitable_handler<executor_type, void>(std::move(a), ex_).launch(); | |
118 | } | |
119 | ||
120 | private: | |
121 | Executor ex_; | |
122 | }; | |
123 | ||
124 | } // namespace detail | |
125 | ||
126 | template <typename Executor, typename F, | |
127 | BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature< | |
128 | typename result_of<F()>::type>::type) CompletionToken> | |
129 | inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, | |
130 | typename detail::awaitable_signature<typename result_of<F()>::type>::type) | |
131 | co_spawn(const Executor& ex, F&& f, CompletionToken&& token, | |
132 | typename enable_if< | |
133 | is_executor<Executor>::value | |
134 | >::type*) | |
135 | { | |
136 | return async_initiate<CompletionToken, | |
137 | typename detail::awaitable_signature<typename result_of<F()>::type>>( | |
138 | detail::initiate_co_spawn< | |
139 | typename result_of<F()>::type::executor_type>(ex), | |
140 | token, std::forward<F>(f)); | |
141 | } | |
142 | ||
143 | template <typename ExecutionContext, typename F, | |
144 | BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature< | |
145 | typename result_of<F()>::type>::type) CompletionToken> | |
146 | inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, | |
147 | typename detail::awaitable_signature<typename result_of<F()>::type>::type) | |
148 | co_spawn(ExecutionContext& ctx, F&& f, CompletionToken&& token, | |
149 | typename enable_if< | |
150 | is_convertible<ExecutionContext&, execution_context&>::value | |
151 | >::type*) | |
152 | { | |
153 | return (co_spawn)(ctx.get_executor(), std::forward<F>(f), | |
154 | std::forward<CompletionToken>(token)); | |
155 | } | |
156 | ||
157 | } // namespace asio | |
158 | } // namespace boost | |
159 | ||
160 | #include <boost/asio/detail/pop_options.hpp> | |
161 | ||
162 | #endif // BOOST_ASIO_IMPL_CO_SPAWN_HPP |