5 // Copyright (c) 2003-2017 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 void* asio_handler_allocate(std::size_t size,
114 coro_handler<Handler, T>* this_handler)
116 return boost_asio_handler_alloc_helpers::allocate(
117 size, this_handler->handler_);
120 template <typename Handler, typename T>
121 inline void asio_handler_deallocate(void* pointer, std::size_t size,
122 coro_handler<Handler, T>* this_handler)
124 boost_asio_handler_alloc_helpers::deallocate(
125 pointer, size, this_handler->handler_);
128 template <typename Handler, typename T>
129 inline bool asio_handler_is_continuation(coro_handler<Handler, T>*)
134 template <typename Function, typename Handler, typename T>
135 inline void asio_handler_invoke(Function& function,
136 coro_handler<Handler, T>* this_handler)
138 boost_asio_handler_invoke_helpers::invoke(
139 function, this_handler->handler_);
142 template <typename Function, typename Handler, typename T>
143 inline void asio_handler_invoke(const Function& function,
144 coro_handler<Handler, T>* this_handler)
146 boost_asio_handler_invoke_helpers::invoke(
147 function, this_handler->handler_);
150 template <typename Handler, typename T>
151 class coro_async_result
154 typedef coro_handler<Handler, T> completion_handler_type;
155 typedef T return_type;
157 explicit coro_async_result(completion_handler_type& h)
164 if (!out_ec_) h.ec_ = &ec_;
170 // Must not hold shared_ptr to coro while suspended.
171 handler_.coro_.reset();
175 if (!out_ec_ && ec_) throw boost::system::system_error(ec_);
176 return BOOST_ASIO_MOVE_CAST(return_type)(value_);
180 completion_handler_type& handler_;
181 typename basic_yield_context<Handler>::caller_type& ca_;
183 boost::system::error_code* out_ec_;
184 boost::system::error_code ec_;
188 template <typename Handler>
189 class coro_async_result<Handler, void>
192 typedef coro_handler<Handler, void> completion_handler_type;
193 typedef void return_type;
195 explicit coro_async_result(completion_handler_type& h)
202 if (!out_ec_) h.ec_ = &ec_;
207 // Must not hold shared_ptr to coro while suspended.
208 handler_.coro_.reset();
212 if (!out_ec_ && ec_) throw boost::system::system_error(ec_);
216 completion_handler_type& handler_;
217 typename basic_yield_context<Handler>::caller_type& ca_;
219 boost::system::error_code* out_ec_;
220 boost::system::error_code ec_;
223 } // namespace detail
225 #if !defined(GENERATING_DOCUMENTATION)
227 template <typename Handler, typename ReturnType>
228 class async_result<basic_yield_context<Handler>, ReturnType()>
229 : public detail::coro_async_result<Handler, void>
232 explicit async_result(
233 typename detail::coro_async_result<Handler,
234 void>::completion_handler_type& h)
235 : detail::coro_async_result<Handler, void>(h)
240 template <typename Handler, typename ReturnType, typename Arg1>
241 class async_result<basic_yield_context<Handler>, ReturnType(Arg1)>
242 : public detail::coro_async_result<Handler, typename decay<Arg1>::type>
245 explicit async_result(
246 typename detail::coro_async_result<Handler,
247 typename decay<Arg1>::type>::completion_handler_type& h)
248 : detail::coro_async_result<Handler, Arg1>(h)
253 template <typename Handler, typename ReturnType>
254 class async_result<basic_yield_context<Handler>,
255 ReturnType(boost::system::error_code)>
256 : public detail::coro_async_result<Handler, void>
259 explicit async_result(
260 typename detail::coro_async_result<Handler,
261 void>::completion_handler_type& h)
262 : detail::coro_async_result<Handler, void>(h)
267 template <typename Handler, typename ReturnType, typename Arg2>
268 class async_result<basic_yield_context<Handler>,
269 ReturnType(boost::system::error_code, Arg2)>
270 : public detail::coro_async_result<Handler, typename decay<Arg2>::type>
273 explicit async_result(
274 typename detail::coro_async_result<Handler,
275 typename decay<Arg2>::type>::completion_handler_type& h)
276 : detail::coro_async_result<Handler, Arg2>(h)
281 #if !defined(BOOST_ASIO_NO_DEPRECATED)
283 template <typename Handler, typename ReturnType>
284 struct handler_type<basic_yield_context<Handler>, ReturnType()>
286 typedef detail::coro_handler<Handler, void> type;
289 template <typename Handler, typename ReturnType, typename Arg1>
290 struct handler_type<basic_yield_context<Handler>, ReturnType(Arg1)>
292 typedef detail::coro_handler<Handler, typename decay<Arg1>::type> type;
295 template <typename Handler, typename ReturnType>
296 struct handler_type<basic_yield_context<Handler>,
297 ReturnType(boost::system::error_code)>
299 typedef detail::coro_handler<Handler, void> type;
302 template <typename Handler, typename ReturnType, typename Arg2>
303 struct handler_type<basic_yield_context<Handler>,
304 ReturnType(boost::system::error_code, Arg2)>
306 typedef detail::coro_handler<Handler, typename decay<Arg2>::type> type;
309 template <typename Handler, typename T>
310 class async_result<detail::coro_handler<Handler, T> >
311 : public detail::coro_async_result<Handler, T>
314 typedef typename detail::coro_async_result<Handler, T>::return_type type;
316 explicit async_result(
317 typename detail::coro_async_result<Handler,
318 void>::completion_handler_type& h)
319 : detail::coro_async_result<Handler, T>(h)
324 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
326 template <typename Handler, typename T, typename Allocator>
327 struct associated_allocator<detail::coro_handler<Handler, T>, Allocator>
329 typedef typename associated_allocator<Handler, Allocator>::type type;
331 static type get(const detail::coro_handler<Handler, T>& h,
332 const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
334 return associated_allocator<Handler, Allocator>::get(h.handler_, a);
338 template <typename Handler, typename T, typename Executor>
339 struct associated_executor<detail::coro_handler<Handler, T>, Executor>
341 typedef typename associated_executor<Handler, Executor>::type type;
343 static type get(const detail::coro_handler<Handler, T>& h,
344 const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
346 return associated_executor<Handler, Executor>::get(h.handler_, ex);
352 template <typename Handler, typename Function>
353 struct spawn_data : private noncopyable
355 spawn_data(BOOST_ASIO_MOVE_ARG(Handler) handler,
356 bool call_handler, BOOST_ASIO_MOVE_ARG(Function) function)
357 : handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
358 call_handler_(call_handler),
359 function_(BOOST_ASIO_MOVE_CAST(Function)(function))
363 weak_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
369 template <typename Handler, typename Function>
370 struct coro_entry_point
372 void operator()(typename basic_yield_context<Handler>::caller_type& ca)
374 shared_ptr<spawn_data<Handler, Function> > data(data_);
375 #if !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
376 ca(); // Yield until coroutine pointer has been initialised.
377 #endif // !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
378 const basic_yield_context<Handler> yield(
379 data->coro_, ca, data->handler_);
381 (data->function_)(yield);
382 if (data->call_handler_)
386 shared_ptr<spawn_data<Handler, Function> > data_;
389 template <typename Handler, typename Function>
394 typedef typename basic_yield_context<Handler>::callee_type callee_type;
395 coro_entry_point<Handler, Function> entry_point = { data_ };
396 shared_ptr<callee_type> coro(new callee_type(entry_point, attributes_));
401 shared_ptr<spawn_data<Handler, Function> > data_;
402 boost::coroutines::attributes attributes_;
405 template <typename Function, typename Handler, typename Function1>
406 inline void asio_handler_invoke(Function& function,
407 spawn_helper<Handler, Function1>* this_handler)
409 boost_asio_handler_invoke_helpers::invoke(
410 function, this_handler->data_->handler_);
413 template <typename Function, typename Handler, typename Function1>
414 inline void asio_handler_invoke(const Function& function,
415 spawn_helper<Handler, Function1>* this_handler)
417 boost_asio_handler_invoke_helpers::invoke(
418 function, this_handler->data_->handler_);
421 inline void default_spawn_handler() {}
423 } // namespace detail
425 template <typename Function>
426 inline void spawn(BOOST_ASIO_MOVE_ARG(Function) function,
427 const boost::coroutines::attributes& attributes)
429 typedef typename decay<Function>::type function_type;
431 typename associated_executor<function_type>::type ex(
432 (get_associated_executor)(function));
434 boost::asio::spawn(ex, BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
437 template <typename Handler, typename Function>
438 void spawn(BOOST_ASIO_MOVE_ARG(Handler) handler,
439 BOOST_ASIO_MOVE_ARG(Function) function,
440 const boost::coroutines::attributes& attributes,
441 typename enable_if<!is_executor<typename decay<Handler>::type>::value &&
442 !is_convertible<Handler&, execution_context&>::value>::type*)
444 typedef typename decay<Handler>::type handler_type;
446 typename associated_executor<handler_type>::type ex(
447 (get_associated_executor)(handler));
449 typename associated_allocator<handler_type>::type a(
450 (get_associated_allocator)(handler));
452 detail::spawn_helper<handler_type, Function> helper;
454 new detail::spawn_data<handler_type, Function>(
455 BOOST_ASIO_MOVE_CAST(Handler)(handler), true,
456 BOOST_ASIO_MOVE_CAST(Function)(function)));
457 helper.attributes_ = attributes;
459 ex.dispatch(helper, a);
462 template <typename Handler, typename Function>
463 void spawn(basic_yield_context<Handler> ctx,
464 BOOST_ASIO_MOVE_ARG(Function) function,
465 const boost::coroutines::attributes& attributes)
467 Handler handler(ctx.handler_); // Explicit copy that might be moved from.
469 typename associated_executor<Handler>::type ex(
470 (get_associated_executor)(handler));
472 typename associated_allocator<Handler>::type a(
473 (get_associated_allocator)(handler));
475 detail::spawn_helper<Handler, Function> helper;
477 new detail::spawn_data<Handler, Function>(
478 BOOST_ASIO_MOVE_CAST(Handler)(handler), false,
479 BOOST_ASIO_MOVE_CAST(Function)(function)));
480 helper.attributes_ = attributes;
482 ex.dispatch(helper, a);
485 template <typename Function, typename Executor>
486 inline void spawn(const Executor& ex,
487 BOOST_ASIO_MOVE_ARG(Function) function,
488 const boost::coroutines::attributes& attributes,
489 typename enable_if<is_executor<Executor>::value>::type*)
491 boost::asio::spawn(boost::asio::strand<Executor>(ex),
492 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
495 template <typename Function, typename Executor>
496 inline void spawn(const strand<Executor>& ex,
497 BOOST_ASIO_MOVE_ARG(Function) function,
498 const boost::coroutines::attributes& attributes)
500 boost::asio::spawn(boost::asio::bind_executor(
501 ex, &detail::default_spawn_handler),
502 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
505 template <typename Function>
506 inline void spawn(const boost::asio::io_context::strand& s,
507 BOOST_ASIO_MOVE_ARG(Function) function,
508 const boost::coroutines::attributes& attributes)
510 boost::asio::spawn(boost::asio::bind_executor(
511 s, &detail::default_spawn_handler),
512 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
515 template <typename Function, typename ExecutionContext>
516 inline void spawn(ExecutionContext& ctx,
517 BOOST_ASIO_MOVE_ARG(Function) function,
518 const boost::coroutines::attributes& attributes,
519 typename enable_if<is_convertible<
520 ExecutionContext&, execution_context&>::value>::type*)
522 boost::asio::spawn(ctx.get_executor(),
523 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
526 #endif // !defined(GENERATING_DOCUMENTATION)
531 #include <boost/asio/detail/pop_options.hpp>
533 #endif // BOOST_ASIO_IMPL_SPAWN_HPP