spawn.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // impl/spawn.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  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. #ifndef ASIO_IMPL_SPAWN_HPP
  11. #define ASIO_IMPL_SPAWN_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/async_result.hpp"
  17. #include "asio/detail/atomic_count.hpp"
  18. #include "asio/detail/handler_alloc_helpers.hpp"
  19. #include "asio/detail/handler_cont_helpers.hpp"
  20. #include "asio/detail/handler_invoke_helpers.hpp"
  21. #include "asio/detail/noncopyable.hpp"
  22. #include "asio/detail/shared_ptr.hpp"
  23. #include "asio/handler_type.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace detail {
  27. template <typename Handler, typename T>
  28. class coro_handler
  29. {
  30. public:
  31. coro_handler(basic_yield_context<Handler> ctx)
  32. : coro_(ctx.coro_.lock()),
  33. ca_(ctx.ca_),
  34. handler_(ctx.handler_),
  35. ready_(0),
  36. ec_(ctx.ec_),
  37. value_(0)
  38. {
  39. }
  40. void operator()(T value)
  41. {
  42. *ec_ = asio::error_code();
  43. *value_ = ASIO_MOVE_CAST(T)(value);
  44. if (--*ready_ == 0)
  45. (*coro_)();
  46. }
  47. void operator()(asio::error_code ec, T value)
  48. {
  49. *ec_ = ec;
  50. *value_ = ASIO_MOVE_CAST(T)(value);
  51. if (--*ready_ == 0)
  52. (*coro_)();
  53. }
  54. //private:
  55. shared_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
  56. typename basic_yield_context<Handler>::caller_type& ca_;
  57. Handler& handler_;
  58. atomic_count* ready_;
  59. asio::error_code* ec_;
  60. T* value_;
  61. };
  62. template <typename Handler>
  63. class coro_handler<Handler, void>
  64. {
  65. public:
  66. coro_handler(basic_yield_context<Handler> ctx)
  67. : coro_(ctx.coro_.lock()),
  68. ca_(ctx.ca_),
  69. handler_(ctx.handler_),
  70. ready_(0),
  71. ec_(ctx.ec_)
  72. {
  73. }
  74. void operator()()
  75. {
  76. *ec_ = asio::error_code();
  77. if (--*ready_ == 0)
  78. (*coro_)();
  79. }
  80. void operator()(asio::error_code ec)
  81. {
  82. *ec_ = ec;
  83. if (--*ready_ == 0)
  84. (*coro_)();
  85. }
  86. //private:
  87. shared_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
  88. typename basic_yield_context<Handler>::caller_type& ca_;
  89. Handler& handler_;
  90. atomic_count* ready_;
  91. asio::error_code* ec_;
  92. };
  93. template <typename Handler, typename T>
  94. inline void* asio_handler_allocate(std::size_t size,
  95. coro_handler<Handler, T>* this_handler)
  96. {
  97. return asio_handler_alloc_helpers::allocate(
  98. size, this_handler->handler_);
  99. }
  100. template <typename Handler, typename T>
  101. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  102. coro_handler<Handler, T>* this_handler)
  103. {
  104. asio_handler_alloc_helpers::deallocate(
  105. pointer, size, this_handler->handler_);
  106. }
  107. template <typename Handler, typename T>
  108. inline bool asio_handler_is_continuation(coro_handler<Handler, T>*)
  109. {
  110. return true;
  111. }
  112. template <typename Function, typename Handler, typename T>
  113. inline void asio_handler_invoke(Function& function,
  114. coro_handler<Handler, T>* this_handler)
  115. {
  116. asio_handler_invoke_helpers::invoke(
  117. function, this_handler->handler_);
  118. }
  119. template <typename Function, typename Handler, typename T>
  120. inline void asio_handler_invoke(const Function& function,
  121. coro_handler<Handler, T>* this_handler)
  122. {
  123. asio_handler_invoke_helpers::invoke(
  124. function, this_handler->handler_);
  125. }
  126. } // namespace detail
  127. #if !defined(GENERATING_DOCUMENTATION)
  128. template <typename Handler, typename ReturnType>
  129. struct handler_type<basic_yield_context<Handler>, ReturnType()>
  130. {
  131. typedef detail::coro_handler<Handler, void> type;
  132. };
  133. template <typename Handler, typename ReturnType, typename Arg1>
  134. struct handler_type<basic_yield_context<Handler>, ReturnType(Arg1)>
  135. {
  136. typedef detail::coro_handler<Handler, Arg1> type;
  137. };
  138. template <typename Handler, typename ReturnType>
  139. struct handler_type<basic_yield_context<Handler>,
  140. ReturnType(asio::error_code)>
  141. {
  142. typedef detail::coro_handler<Handler, void> type;
  143. };
  144. template <typename Handler, typename ReturnType, typename Arg2>
  145. struct handler_type<basic_yield_context<Handler>,
  146. ReturnType(asio::error_code, Arg2)>
  147. {
  148. typedef detail::coro_handler<Handler, Arg2> type;
  149. };
  150. template <typename Handler, typename T>
  151. class async_result<detail::coro_handler<Handler, T> >
  152. {
  153. public:
  154. typedef T type;
  155. explicit async_result(detail::coro_handler<Handler, T>& h)
  156. : handler_(h),
  157. ca_(h.ca_),
  158. ready_(2)
  159. {
  160. h.ready_ = &ready_;
  161. out_ec_ = h.ec_;
  162. if (!out_ec_) h.ec_ = &ec_;
  163. h.value_ = &value_;
  164. }
  165. type get()
  166. {
  167. handler_.coro_.reset(); // Must not hold shared_ptr to coro while suspended.
  168. if (--ready_ != 0)
  169. ca_();
  170. if (!out_ec_ && ec_) throw asio::system_error(ec_);
  171. return ASIO_MOVE_CAST(type)(value_);
  172. }
  173. private:
  174. detail::coro_handler<Handler, T>& handler_;
  175. typename basic_yield_context<Handler>::caller_type& ca_;
  176. detail::atomic_count ready_;
  177. asio::error_code* out_ec_;
  178. asio::error_code ec_;
  179. type value_;
  180. };
  181. template <typename Handler>
  182. class async_result<detail::coro_handler<Handler, void> >
  183. {
  184. public:
  185. typedef void type;
  186. explicit async_result(detail::coro_handler<Handler, void>& h)
  187. : handler_(h),
  188. ca_(h.ca_),
  189. ready_(2)
  190. {
  191. h.ready_ = &ready_;
  192. out_ec_ = h.ec_;
  193. if (!out_ec_) h.ec_ = &ec_;
  194. }
  195. void get()
  196. {
  197. handler_.coro_.reset(); // Must not hold shared_ptr to coro while suspended.
  198. if (--ready_ != 0)
  199. ca_();
  200. if (!out_ec_ && ec_) throw asio::system_error(ec_);
  201. }
  202. private:
  203. detail::coro_handler<Handler, void>& handler_;
  204. typename basic_yield_context<Handler>::caller_type& ca_;
  205. detail::atomic_count ready_;
  206. asio::error_code* out_ec_;
  207. asio::error_code ec_;
  208. };
  209. namespace detail {
  210. template <typename Handler, typename Function>
  211. struct spawn_data : private noncopyable
  212. {
  213. spawn_data(ASIO_MOVE_ARG(Handler) handler,
  214. bool call_handler, ASIO_MOVE_ARG(Function) function)
  215. : handler_(ASIO_MOVE_CAST(Handler)(handler)),
  216. call_handler_(call_handler),
  217. function_(ASIO_MOVE_CAST(Function)(function))
  218. {
  219. }
  220. weak_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
  221. Handler handler_;
  222. bool call_handler_;
  223. Function function_;
  224. };
  225. template <typename Handler, typename Function>
  226. struct coro_entry_point
  227. {
  228. void operator()(typename basic_yield_context<Handler>::caller_type& ca)
  229. {
  230. shared_ptr<spawn_data<Handler, Function> > data(data_);
  231. #if !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
  232. ca(); // Yield until coroutine pointer has been initialised.
  233. #endif // !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
  234. const basic_yield_context<Handler> yield(
  235. data->coro_, ca, data->handler_);
  236. (data->function_)(yield);
  237. if (data->call_handler_)
  238. (data->handler_)();
  239. }
  240. shared_ptr<spawn_data<Handler, Function> > data_;
  241. };
  242. template <typename Handler, typename Function>
  243. struct spawn_helper
  244. {
  245. void operator()()
  246. {
  247. typedef typename basic_yield_context<Handler>::callee_type callee_type;
  248. coro_entry_point<Handler, Function> entry_point = { data_ };
  249. shared_ptr<callee_type> coro(new callee_type(entry_point, attributes_));
  250. data_->coro_ = coro;
  251. (*coro)();
  252. }
  253. shared_ptr<spawn_data<Handler, Function> > data_;
  254. boost::coroutines::attributes attributes_;
  255. };
  256. inline void default_spawn_handler() {}
  257. } // namespace detail
  258. template <typename Handler, typename Function>
  259. void spawn(ASIO_MOVE_ARG(Handler) handler,
  260. ASIO_MOVE_ARG(Function) function,
  261. const boost::coroutines::attributes& attributes)
  262. {
  263. detail::spawn_helper<Handler, Function> helper;
  264. helper.data_.reset(
  265. new detail::spawn_data<Handler, Function>(
  266. ASIO_MOVE_CAST(Handler)(handler), true,
  267. ASIO_MOVE_CAST(Function)(function)));
  268. helper.attributes_ = attributes;
  269. asio_handler_invoke_helpers::invoke(helper, helper.data_->handler_);
  270. }
  271. template <typename Handler, typename Function>
  272. void spawn(basic_yield_context<Handler> ctx,
  273. ASIO_MOVE_ARG(Function) function,
  274. const boost::coroutines::attributes& attributes)
  275. {
  276. Handler handler(ctx.handler_); // Explicit copy that might be moved from.
  277. detail::spawn_helper<Handler, Function> helper;
  278. helper.data_.reset(
  279. new detail::spawn_data<Handler, Function>(
  280. ASIO_MOVE_CAST(Handler)(handler), false,
  281. ASIO_MOVE_CAST(Function)(function)));
  282. helper.attributes_ = attributes;
  283. asio_handler_invoke_helpers::invoke(helper, helper.data_->handler_);
  284. }
  285. template <typename Function>
  286. void spawn(asio::io_service::strand strand,
  287. ASIO_MOVE_ARG(Function) function,
  288. const boost::coroutines::attributes& attributes)
  289. {
  290. asio::spawn(strand.wrap(&detail::default_spawn_handler),
  291. ASIO_MOVE_CAST(Function)(function), attributes);
  292. }
  293. template <typename Function>
  294. void spawn(asio::io_service& io_service,
  295. ASIO_MOVE_ARG(Function) function,
  296. const boost::coroutines::attributes& attributes)
  297. {
  298. asio::spawn(asio::io_service::strand(io_service),
  299. ASIO_MOVE_CAST(Function)(function), attributes);
  300. }
  301. #endif // !defined(GENERATING_DOCUMENTATION)
  302. } // namespace asio
  303. #include "asio/detail/pop_options.hpp"
  304. #endif // ASIO_IMPL_SPAWN_HPP