// // impl/spawn.hpp // ~~~~~~~~~~~~~~ // // Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_ASIO_IMPL_SPAWN_HPP #define BOOST_ASIO_IMPL_SPAWN_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include #include #include #include #include namespace boost { namespace asio { namespace detail { template class coro_handler { public: coro_handler(basic_yield_context ctx) : coro_(ctx.coro_.lock()), ca_(ctx.ca_), handler_(ctx.handler_), ec_(ctx.ec_), value_(0) { } void operator()(T value) { *ec_ = boost::system::error_code(); *value_ = value; (*coro_)(); } void operator()(boost::system::error_code ec, T value) { *ec_ = ec; *value_ = value; (*coro_)(); } //private: shared_ptr::callee_type> coro_; typename basic_yield_context::caller_type& ca_; Handler& handler_; boost::system::error_code* ec_; T* value_; }; template class coro_handler { public: coro_handler(basic_yield_context ctx) : coro_(ctx.coro_.lock()), ca_(ctx.ca_), handler_(ctx.handler_), ec_(ctx.ec_) { } void operator()() { *ec_ = boost::system::error_code(); (*coro_)(); } void operator()(boost::system::error_code ec) { *ec_ = ec; (*coro_)(); } //private: shared_ptr::callee_type> coro_; typename basic_yield_context::caller_type& ca_; Handler& handler_; boost::system::error_code* ec_; }; template inline void* asio_handler_allocate(std::size_t size, coro_handler* this_handler) { return boost_asio_handler_alloc_helpers::allocate( size, this_handler->handler_); } template inline void asio_handler_deallocate(void* pointer, std::size_t size, coro_handler* this_handler) { boost_asio_handler_alloc_helpers::deallocate( pointer, size, this_handler->handler_); } template inline bool asio_handler_is_continuation(coro_handler*) { return true; } template inline void asio_handler_invoke(Function& function, coro_handler* this_handler) { boost_asio_handler_invoke_helpers::invoke( function, this_handler->handler_); } template inline void asio_handler_invoke(const Function& function, coro_handler* this_handler) { boost_asio_handler_invoke_helpers::invoke( function, this_handler->handler_); } } // namespace detail #if !defined(GENERATING_DOCUMENTATION) template struct handler_type, ReturnType()> { typedef detail::coro_handler type; }; template struct handler_type, ReturnType(Arg1)> { typedef detail::coro_handler type; }; template struct handler_type, ReturnType(boost::system::error_code)> { typedef detail::coro_handler type; }; template struct handler_type, ReturnType(boost::system::error_code, Arg2)> { typedef detail::coro_handler type; }; template class async_result > { public: typedef T type; explicit async_result(detail::coro_handler& h) : handler_(h), ca_(h.ca_) { out_ec_ = h.ec_; if (!out_ec_) h.ec_ = &ec_; h.value_ = &value_; } type get() { handler_.coro_.reset(); // Must not hold shared_ptr to coro while suspended. ca_(); if (!out_ec_ && ec_) throw boost::system::system_error(ec_); return value_; } private: detail::coro_handler& handler_; typename basic_yield_context::caller_type& ca_; boost::system::error_code* out_ec_; boost::system::error_code ec_; type value_; }; template class async_result > { public: typedef void type; explicit async_result(detail::coro_handler& h) : handler_(h), ca_(h.ca_) { out_ec_ = h.ec_; if (!out_ec_) h.ec_ = &ec_; } void get() { handler_.coro_.reset(); // Must not hold shared_ptr to coro while suspended. ca_(); if (!out_ec_ && ec_) throw boost::system::system_error(ec_); } private: detail::coro_handler& handler_; typename basic_yield_context::caller_type& ca_; boost::system::error_code* out_ec_; boost::system::error_code ec_; }; namespace detail { template struct spawn_data : private noncopyable { spawn_data(BOOST_ASIO_MOVE_ARG(Handler) handler, bool call_handler, BOOST_ASIO_MOVE_ARG(Function) function) : handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), call_handler_(call_handler), function_(BOOST_ASIO_MOVE_CAST(Function)(function)) { } weak_ptr::callee_type> coro_; Handler handler_; bool call_handler_; Function function_; }; template struct coro_entry_point { void operator()(typename basic_yield_context::caller_type& ca) { shared_ptr > data(data_); #if !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2) ca(); // Yield until coroutine pointer has been initialised. #endif // !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2) const basic_yield_context yield( data->coro_, ca, data->handler_); (data->function_)(yield); if (data->call_handler_) (data->handler_)(); } shared_ptr > data_; }; template struct spawn_helper { void operator()() { typedef typename basic_yield_context::callee_type callee_type; coro_entry_point entry_point = { data_ }; shared_ptr coro(new callee_type(entry_point, attributes_)); data_->coro_ = coro; (*coro)(); } shared_ptr > data_; boost::coroutines::attributes attributes_; }; inline void default_spawn_handler() {} } // namespace detail template void spawn(BOOST_ASIO_MOVE_ARG(Handler) handler, BOOST_ASIO_MOVE_ARG(Function) function, const boost::coroutines::attributes& attributes) { detail::spawn_helper helper; helper.data_.reset( new detail::spawn_data( BOOST_ASIO_MOVE_CAST(Handler)(handler), true, BOOST_ASIO_MOVE_CAST(Function)(function))); helper.attributes_ = attributes; boost_asio_handler_invoke_helpers::invoke(helper, helper.data_->handler_); } template void spawn(basic_yield_context ctx, BOOST_ASIO_MOVE_ARG(Function) function, const boost::coroutines::attributes& attributes) { Handler handler(ctx.handler_); // Explicit copy that might be moved from. detail::spawn_helper helper; helper.data_.reset( new detail::spawn_data( BOOST_ASIO_MOVE_CAST(Handler)(handler), false, BOOST_ASIO_MOVE_CAST(Function)(function))); helper.attributes_ = attributes; boost_asio_handler_invoke_helpers::invoke(helper, helper.data_->handler_); } template void spawn(boost::asio::io_service::strand strand, BOOST_ASIO_MOVE_ARG(Function) function, const boost::coroutines::attributes& attributes) { boost::asio::spawn(strand.wrap(&detail::default_spawn_handler), BOOST_ASIO_MOVE_CAST(Function)(function), attributes); } template void spawn(boost::asio::io_service& io_service, BOOST_ASIO_MOVE_ARG(Function) function, const boost::coroutines::attributes& attributes) { boost::asio::spawn(boost::asio::io_service::strand(io_service), BOOST_ASIO_MOVE_CAST(Function)(function), attributes); } #endif // !defined(GENERATING_DOCUMENTATION) } // namespace asio } // namespace boost #include #endif // BOOST_ASIO_IMPL_SPAWN_HPP