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