5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_IMPL_SPAWN_HPP
12 #define BOOST_ASIO_IMPL_SPAWN_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/associated_allocator.hpp>
20 #include <boost/asio/associated_executor.hpp>
21 #include <boost/asio/async_result.hpp>
22 #include <boost/asio/bind_executor.hpp>
23 #include <boost/asio/detail/atomic_count.hpp>
24 #include <boost/asio/detail/handler_alloc_helpers.hpp>
25 #include <boost/asio/detail/handler_cont_helpers.hpp>
26 #include <boost/asio/detail/handler_invoke_helpers.hpp>
27 #include <boost/asio/detail/memory.hpp>
28 #include <boost/asio/detail/noncopyable.hpp>
29 #include <boost/asio/detail/type_traits.hpp>
30 #include <boost/system/system_error.hpp>
32 #include <boost/asio/detail/push_options.hpp>
38 template <typename Handler, typename T>
42 coro_handler(basic_yield_context<Handler> ctx)
43 : coro_(ctx.coro_.lock()),
45 handler_(ctx.handler_),
52 void operator()(T value)
54 *ec_ = boost::system::error_code();
55 *value_ = BOOST_ASIO_MOVE_CAST(T)(value);
60 void operator()(boost::system::error_code ec, T value)
63 *value_ = BOOST_ASIO_MOVE_CAST(T)(value);
69 shared_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
70 typename basic_yield_context<Handler>::caller_type& ca_;
73 boost::system::error_code* ec_;
77 template <typename Handler>
78 class coro_handler<Handler, void>
81 coro_handler(basic_yield_context<Handler> ctx)
82 : coro_(ctx.coro_.lock()),
84 handler_(ctx.handler_),
92 *ec_ = boost::system::error_code();
97 void operator()(boost::system::error_code ec)
105 shared_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
106 typename basic_yield_context<Handler>::caller_type& ca_;
108 atomic_count* ready_;
109 boost::system::error_code* ec_;
112 template <typename Handler, typename T>
113 inline asio_handler_allocate_is_deprecated
114 asio_handler_allocate(std::size_t size,
115 coro_handler<Handler, T>* this_handler)
117 #if defined(BOOST_ASIO_NO_DEPRECATED)
118 boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
119 return asio_handler_allocate_is_no_longer_used();
120 #else // defined(BOOST_ASIO_NO_DEPRECATED)
121 return boost_asio_handler_alloc_helpers::allocate(
122 size, this_handler->handler_);
123 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
126 template <typename Handler, typename T>
127 inline asio_handler_deallocate_is_deprecated
128 asio_handler_deallocate(void* pointer, std::size_t size,
129 coro_handler<Handler, T>* this_handler)
131 boost_asio_handler_alloc_helpers::deallocate(
132 pointer, size, this_handler->handler_);
133 #if defined(BOOST_ASIO_NO_DEPRECATED)
134 return asio_handler_deallocate_is_no_longer_used();
135 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
138 template <typename Handler, typename T>
139 inline bool asio_handler_is_continuation(coro_handler<Handler, T>*)
144 template <typename Function, typename Handler, typename T>
145 inline asio_handler_invoke_is_deprecated
146 asio_handler_invoke(Function& function,
147 coro_handler<Handler, T>* this_handler)
149 boost_asio_handler_invoke_helpers::invoke(
150 function, this_handler->handler_);
151 #if defined(BOOST_ASIO_NO_DEPRECATED)
152 return asio_handler_invoke_is_no_longer_used();
153 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
156 template <typename Function, typename Handler, typename T>
157 inline asio_handler_invoke_is_deprecated
158 asio_handler_invoke(const Function& function,
159 coro_handler<Handler, T>* this_handler)
161 boost_asio_handler_invoke_helpers::invoke(
162 function, this_handler->handler_);
163 #if defined(BOOST_ASIO_NO_DEPRECATED)
164 return asio_handler_invoke_is_no_longer_used();
165 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
168 template <typename Handler, typename T>
169 class coro_async_result
172 typedef coro_handler<Handler, T> completion_handler_type;
173 typedef T return_type;
175 explicit coro_async_result(completion_handler_type& h)
182 if (!out_ec_) h.ec_ = &ec_;
188 // Must not hold shared_ptr to coro while suspended.
189 handler_.coro_.reset();
193 if (!out_ec_ && ec_) throw boost::system::system_error(ec_);
194 return BOOST_ASIO_MOVE_CAST(return_type)(value_);
198 completion_handler_type& handler_;
199 typename basic_yield_context<Handler>::caller_type& ca_;
201 boost::system::error_code* out_ec_;
202 boost::system::error_code ec_;
206 template <typename Handler>
207 class coro_async_result<Handler, void>
210 typedef coro_handler<Handler, void> completion_handler_type;
211 typedef void return_type;
213 explicit coro_async_result(completion_handler_type& h)
220 if (!out_ec_) h.ec_ = &ec_;
225 // Must not hold shared_ptr to coro while suspended.
226 handler_.coro_.reset();
230 if (!out_ec_ && ec_) throw boost::system::system_error(ec_);
234 completion_handler_type& handler_;
235 typename basic_yield_context<Handler>::caller_type& ca_;
237 boost::system::error_code* out_ec_;
238 boost::system::error_code ec_;
241 } // namespace detail
243 #if !defined(GENERATING_DOCUMENTATION)
245 template <typename Handler, typename ReturnType>
246 class async_result<basic_yield_context<Handler>, ReturnType()>
247 : public detail::coro_async_result<Handler, void>
250 explicit async_result(
251 typename detail::coro_async_result<Handler,
252 void>::completion_handler_type& h)
253 : detail::coro_async_result<Handler, void>(h)
258 template <typename Handler, typename ReturnType, typename Arg1>
259 class async_result<basic_yield_context<Handler>, ReturnType(Arg1)>
260 : public detail::coro_async_result<Handler, typename decay<Arg1>::type>
263 explicit async_result(
264 typename detail::coro_async_result<Handler,
265 typename decay<Arg1>::type>::completion_handler_type& h)
266 : detail::coro_async_result<Handler, typename decay<Arg1>::type>(h)
271 template <typename Handler, typename ReturnType>
272 class async_result<basic_yield_context<Handler>,
273 ReturnType(boost::system::error_code)>
274 : public detail::coro_async_result<Handler, void>
277 explicit async_result(
278 typename detail::coro_async_result<Handler,
279 void>::completion_handler_type& h)
280 : detail::coro_async_result<Handler, void>(h)
285 template <typename Handler, typename ReturnType, typename Arg2>
286 class async_result<basic_yield_context<Handler>,
287 ReturnType(boost::system::error_code, Arg2)>
288 : public detail::coro_async_result<Handler, typename decay<Arg2>::type>
291 explicit async_result(
292 typename detail::coro_async_result<Handler,
293 typename decay<Arg2>::type>::completion_handler_type& h)
294 : detail::coro_async_result<Handler, typename decay<Arg2>::type>(h)
299 template <typename Handler, typename T, typename Allocator>
300 struct associated_allocator<detail::coro_handler<Handler, T>, Allocator>
302 typedef typename associated_allocator<Handler, Allocator>::type type;
304 static type get(const detail::coro_handler<Handler, T>& h,
305 const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
307 return associated_allocator<Handler, Allocator>::get(h.handler_, a);
311 template <typename Handler, typename T, typename Executor>
312 struct associated_executor<detail::coro_handler<Handler, T>, Executor>
313 : detail::associated_executor_forwarding_base<Handler, Executor>
315 typedef typename associated_executor<Handler, Executor>::type type;
317 static type get(const detail::coro_handler<Handler, T>& h,
318 const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
320 return associated_executor<Handler, Executor>::get(h.handler_, ex);
326 template <typename Handler, typename Function>
327 struct spawn_data : private noncopyable
329 template <typename Hand, typename Func>
330 spawn_data(BOOST_ASIO_MOVE_ARG(Hand) handler,
331 bool call_handler, BOOST_ASIO_MOVE_ARG(Func) function)
332 : handler_(BOOST_ASIO_MOVE_CAST(Hand)(handler)),
333 call_handler_(call_handler),
334 function_(BOOST_ASIO_MOVE_CAST(Func)(function))
338 weak_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
344 template <typename Handler, typename Function>
345 struct coro_entry_point
347 void operator()(typename basic_yield_context<Handler>::caller_type& ca)
349 shared_ptr<spawn_data<Handler, Function> > data(data_);
350 #if !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
351 ca(); // Yield until coroutine pointer has been initialised.
352 #endif // !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
353 const basic_yield_context<Handler> yield(
354 data->coro_, ca, data->handler_);
356 (data->function_)(yield);
357 if (data->call_handler_)
361 shared_ptr<spawn_data<Handler, Function> > data_;
364 template <typename Handler, typename Function>
367 typedef typename associated_allocator<Handler>::type allocator_type;
369 allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
371 return (get_associated_allocator)(data_->handler_);
374 typedef typename associated_executor<Handler>::type executor_type;
376 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
378 return (get_associated_executor)(data_->handler_);
383 typedef typename basic_yield_context<Handler>::callee_type callee_type;
384 coro_entry_point<Handler, Function> entry_point = { data_ };
385 shared_ptr<callee_type> coro(new callee_type(entry_point, attributes_));
390 shared_ptr<spawn_data<Handler, Function> > data_;
391 boost::coroutines::attributes attributes_;
394 template <typename Function, typename Handler, typename Function1>
395 inline asio_handler_invoke_is_deprecated
396 asio_handler_invoke(Function& function,
397 spawn_helper<Handler, Function1>* this_handler)
399 boost_asio_handler_invoke_helpers::invoke(
400 function, this_handler->data_->handler_);
401 #if defined(BOOST_ASIO_NO_DEPRECATED)
402 return asio_handler_invoke_is_no_longer_used();
403 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
406 template <typename Function, typename Handler, typename Function1>
407 inline asio_handler_invoke_is_deprecated
408 asio_handler_invoke(const Function& function,
409 spawn_helper<Handler, Function1>* this_handler)
411 boost_asio_handler_invoke_helpers::invoke(
412 function, this_handler->data_->handler_);
413 #if defined(BOOST_ASIO_NO_DEPRECATED)
414 return asio_handler_invoke_is_no_longer_used();
415 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
418 inline void default_spawn_handler() {}
420 } // namespace detail
422 template <typename Function>
423 inline void spawn(BOOST_ASIO_MOVE_ARG(Function) function,
424 const boost::coroutines::attributes& attributes)
426 typedef typename decay<Function>::type function_type;
428 typename associated_executor<function_type>::type ex(
429 (get_associated_executor)(function));
431 boost::asio::spawn(ex, BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
434 template <typename Handler, typename Function>
435 void spawn(BOOST_ASIO_MOVE_ARG(Handler) handler,
436 BOOST_ASIO_MOVE_ARG(Function) function,
437 const boost::coroutines::attributes& attributes,
439 !is_executor<typename decay<Handler>::type>::value &&
440 !execution::is_executor<typename decay<Handler>::type>::value &&
441 !is_convertible<Handler&, execution_context&>::value>::type*)
443 typedef typename decay<Handler>::type handler_type;
444 typedef typename decay<Function>::type function_type;
446 detail::spawn_helper<handler_type, function_type> helper;
448 new detail::spawn_data<handler_type, function_type>(
449 BOOST_ASIO_MOVE_CAST(Handler)(handler), true,
450 BOOST_ASIO_MOVE_CAST(Function)(function)));
451 helper.attributes_ = attributes;
453 boost::asio::dispatch(helper);
456 template <typename Handler, typename Function>
457 void spawn(basic_yield_context<Handler> ctx,
458 BOOST_ASIO_MOVE_ARG(Function) function,
459 const boost::coroutines::attributes& attributes)
461 typedef typename decay<Function>::type function_type;
463 Handler handler(ctx.handler_); // Explicit copy that might be moved from.
465 detail::spawn_helper<Handler, function_type> helper;
467 new detail::spawn_data<Handler, function_type>(
468 BOOST_ASIO_MOVE_CAST(Handler)(handler), false,
469 BOOST_ASIO_MOVE_CAST(Function)(function)));
470 helper.attributes_ = attributes;
472 boost::asio::dispatch(helper);
475 template <typename Function, typename Executor>
476 inline void spawn(const Executor& ex,
477 BOOST_ASIO_MOVE_ARG(Function) function,
478 const boost::coroutines::attributes& attributes,
480 is_executor<Executor>::value || execution::is_executor<Executor>::value
483 boost::asio::spawn(boost::asio::strand<Executor>(ex),
484 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
487 template <typename Function, typename Executor>
488 inline void spawn(const strand<Executor>& ex,
489 BOOST_ASIO_MOVE_ARG(Function) function,
490 const boost::coroutines::attributes& attributes)
492 boost::asio::spawn(boost::asio::bind_executor(
493 ex, &detail::default_spawn_handler),
494 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
497 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
499 template <typename Function>
500 inline void spawn(const boost::asio::io_context::strand& s,
501 BOOST_ASIO_MOVE_ARG(Function) function,
502 const boost::coroutines::attributes& attributes)
504 boost::asio::spawn(boost::asio::bind_executor(
505 s, &detail::default_spawn_handler),
506 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
509 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
511 template <typename Function, typename ExecutionContext>
512 inline void spawn(ExecutionContext& ctx,
513 BOOST_ASIO_MOVE_ARG(Function) function,
514 const boost::coroutines::attributes& attributes,
515 typename enable_if<is_convertible<
516 ExecutionContext&, execution_context&>::value>::type*)
518 boost::asio::spawn(ctx.get_executor(),
519 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
522 #endif // !defined(GENERATING_DOCUMENTATION)
527 #include <boost/asio/detail/pop_options.hpp>
529 #endif // BOOST_ASIO_IMPL_SPAWN_HPP