5 // Copyright (c) 2003-2022 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 <template <typename, typename> class Associator,
300 typename Handler, typename T, typename DefaultCandidate>
301 struct associator<Associator,
302 detail::coro_handler<Handler, T>,
304 : Associator<Handler, DefaultCandidate>
306 static typename Associator<Handler, DefaultCandidate>::type get(
307 const detail::coro_handler<Handler, T>& h,
308 const DefaultCandidate& c = DefaultCandidate()) BOOST_ASIO_NOEXCEPT
310 return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
316 template <typename Handler, typename Function>
317 struct spawn_data : private noncopyable
319 template <typename Hand, typename Func>
320 spawn_data(BOOST_ASIO_MOVE_ARG(Hand) handler,
321 bool call_handler, BOOST_ASIO_MOVE_ARG(Func) function)
322 : handler_(BOOST_ASIO_MOVE_CAST(Hand)(handler)),
323 call_handler_(call_handler),
324 function_(BOOST_ASIO_MOVE_CAST(Func)(function))
328 weak_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
334 template <typename Handler, typename Function>
335 struct coro_entry_point
337 void operator()(typename basic_yield_context<Handler>::caller_type& ca)
339 shared_ptr<spawn_data<Handler, Function> > data(data_);
340 #if !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
341 ca(); // Yield until coroutine pointer has been initialised.
342 #endif // !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
343 const basic_yield_context<Handler> yield(
344 data->coro_, ca, data->handler_);
346 (data->function_)(yield);
347 if (data->call_handler_)
348 BOOST_ASIO_MOVE_OR_LVALUE(Handler)(data->handler_)();
351 shared_ptr<spawn_data<Handler, Function> > data_;
354 template <typename Handler, typename Function>
357 typedef typename associated_allocator<Handler>::type allocator_type;
359 allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
361 return (get_associated_allocator)(data_->handler_);
364 typedef typename associated_executor<Handler>::type executor_type;
366 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
368 return (get_associated_executor)(data_->handler_);
373 typedef typename basic_yield_context<Handler>::callee_type callee_type;
374 coro_entry_point<Handler, Function> entry_point = { data_ };
375 shared_ptr<callee_type> coro(new callee_type(entry_point, attributes_));
380 shared_ptr<spawn_data<Handler, Function> > data_;
381 boost::coroutines::attributes attributes_;
384 template <typename Function, typename Handler, typename Function1>
385 inline asio_handler_invoke_is_deprecated
386 asio_handler_invoke(Function& function,
387 spawn_helper<Handler, Function1>* this_handler)
389 boost_asio_handler_invoke_helpers::invoke(
390 function, this_handler->data_->handler_);
391 #if defined(BOOST_ASIO_NO_DEPRECATED)
392 return asio_handler_invoke_is_no_longer_used();
393 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
396 template <typename Function, typename Handler, typename Function1>
397 inline asio_handler_invoke_is_deprecated
398 asio_handler_invoke(const Function& function,
399 spawn_helper<Handler, Function1>* this_handler)
401 boost_asio_handler_invoke_helpers::invoke(
402 function, this_handler->data_->handler_);
403 #if defined(BOOST_ASIO_NO_DEPRECATED)
404 return asio_handler_invoke_is_no_longer_used();
405 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
408 inline void default_spawn_handler() {}
410 } // namespace detail
412 template <typename Function>
413 inline void spawn(BOOST_ASIO_MOVE_ARG(Function) function,
414 const boost::coroutines::attributes& attributes)
416 typedef typename decay<Function>::type function_type;
418 typename associated_executor<function_type>::type ex(
419 (get_associated_executor)(function));
421 boost::asio::spawn(ex, BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
424 template <typename Handler, typename Function>
425 void spawn(BOOST_ASIO_MOVE_ARG(Handler) handler,
426 BOOST_ASIO_MOVE_ARG(Function) function,
427 const boost::coroutines::attributes& attributes,
429 !is_executor<typename decay<Handler>::type>::value &&
430 !execution::is_executor<typename decay<Handler>::type>::value &&
431 !is_convertible<Handler&, execution_context&>::value>::type)
433 typedef typename decay<Handler>::type handler_type;
434 typedef typename decay<Function>::type function_type;
436 detail::spawn_helper<handler_type, function_type> helper;
438 new detail::spawn_data<handler_type, function_type>(
439 BOOST_ASIO_MOVE_CAST(Handler)(handler), true,
440 BOOST_ASIO_MOVE_CAST(Function)(function)));
441 helper.attributes_ = attributes;
443 boost::asio::dispatch(helper);
446 template <typename Handler, typename Function>
447 void spawn(basic_yield_context<Handler> ctx,
448 BOOST_ASIO_MOVE_ARG(Function) function,
449 const boost::coroutines::attributes& attributes)
451 typedef typename decay<Function>::type function_type;
453 Handler handler(ctx.handler_); // Explicit copy that might be moved from.
455 detail::spawn_helper<Handler, function_type> helper;
457 new detail::spawn_data<Handler, function_type>(
458 BOOST_ASIO_MOVE_CAST(Handler)(handler), false,
459 BOOST_ASIO_MOVE_CAST(Function)(function)));
460 helper.attributes_ = attributes;
462 boost::asio::dispatch(helper);
465 template <typename Function, typename Executor>
466 inline void spawn(const Executor& ex,
467 BOOST_ASIO_MOVE_ARG(Function) function,
468 const boost::coroutines::attributes& attributes,
470 is_executor<Executor>::value || execution::is_executor<Executor>::value
473 boost::asio::spawn(boost::asio::strand<Executor>(ex),
474 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
477 template <typename Function, typename Executor>
478 inline void spawn(const strand<Executor>& ex,
479 BOOST_ASIO_MOVE_ARG(Function) function,
480 const boost::coroutines::attributes& attributes)
482 boost::asio::spawn(boost::asio::bind_executor(
483 ex, &detail::default_spawn_handler),
484 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
487 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
489 template <typename Function>
490 inline void spawn(const boost::asio::io_context::strand& s,
491 BOOST_ASIO_MOVE_ARG(Function) function,
492 const boost::coroutines::attributes& attributes)
494 boost::asio::spawn(boost::asio::bind_executor(
495 s, &detail::default_spawn_handler),
496 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
499 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
501 template <typename Function, typename ExecutionContext>
502 inline void spawn(ExecutionContext& ctx,
503 BOOST_ASIO_MOVE_ARG(Function) function,
504 const boost::coroutines::attributes& attributes,
505 typename constraint<is_convertible<
506 ExecutionContext&, execution_context&>::value>::type)
508 boost::asio::spawn(ctx.get_executor(),
509 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
512 #endif // !defined(GENERATING_DOCUMENTATION)
517 #include <boost/asio/detail/pop_options.hpp>
519 #endif // BOOST_ASIO_IMPL_SPAWN_HPP