]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/impl/spawn.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / asio / impl / spawn.hpp
1 //
2 // impl/spawn.hpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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/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>
31
32 #include <boost/asio/detail/push_options.hpp>
33
34 namespace boost {
35 namespace asio {
36 namespace detail {
37
38 template <typename Handler, typename T>
39 class coro_handler
40 {
41 public:
42 coro_handler(basic_yield_context<Handler> ctx)
43 : coro_(ctx.coro_.lock()),
44 ca_(ctx.ca_),
45 handler_(ctx.handler_),
46 ready_(0),
47 ec_(ctx.ec_),
48 value_(0)
49 {
50 }
51
52 void operator()(T value)
53 {
54 *ec_ = boost::system::error_code();
55 *value_ = BOOST_ASIO_MOVE_CAST(T)(value);
56 if (--*ready_ == 0)
57 (*coro_)();
58 }
59
60 void operator()(boost::system::error_code ec, T value)
61 {
62 *ec_ = ec;
63 *value_ = BOOST_ASIO_MOVE_CAST(T)(value);
64 if (--*ready_ == 0)
65 (*coro_)();
66 }
67
68 //private:
69 shared_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
70 typename basic_yield_context<Handler>::caller_type& ca_;
71 Handler handler_;
72 atomic_count* ready_;
73 boost::system::error_code* ec_;
74 T* value_;
75 };
76
77 template <typename Handler>
78 class coro_handler<Handler, void>
79 {
80 public:
81 coro_handler(basic_yield_context<Handler> ctx)
82 : coro_(ctx.coro_.lock()),
83 ca_(ctx.ca_),
84 handler_(ctx.handler_),
85 ready_(0),
86 ec_(ctx.ec_)
87 {
88 }
89
90 void operator()()
91 {
92 *ec_ = boost::system::error_code();
93 if (--*ready_ == 0)
94 (*coro_)();
95 }
96
97 void operator()(boost::system::error_code ec)
98 {
99 *ec_ = ec;
100 if (--*ready_ == 0)
101 (*coro_)();
102 }
103
104 //private:
105 shared_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
106 typename basic_yield_context<Handler>::caller_type& ca_;
107 Handler handler_;
108 atomic_count* ready_;
109 boost::system::error_code* ec_;
110 };
111
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)
116 {
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)
124 }
125
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)
130 {
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)
136 }
137
138 template <typename Handler, typename T>
139 inline bool asio_handler_is_continuation(coro_handler<Handler, T>*)
140 {
141 return true;
142 }
143
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)
148 {
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)
154 }
155
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)
160 {
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)
166 }
167
168 template <typename Handler, typename T>
169 class coro_async_result
170 {
171 public:
172 typedef coro_handler<Handler, T> completion_handler_type;
173 typedef T return_type;
174
175 explicit coro_async_result(completion_handler_type& h)
176 : handler_(h),
177 ca_(h.ca_),
178 ready_(2)
179 {
180 h.ready_ = &ready_;
181 out_ec_ = h.ec_;
182 if (!out_ec_) h.ec_ = &ec_;
183 h.value_ = &value_;
184 }
185
186 return_type get()
187 {
188 // Must not hold shared_ptr to coro while suspended.
189 handler_.coro_.reset();
190
191 if (--ready_ != 0)
192 ca_();
193 if (!out_ec_ && ec_) throw boost::system::system_error(ec_);
194 return BOOST_ASIO_MOVE_CAST(return_type)(value_);
195 }
196
197 private:
198 completion_handler_type& handler_;
199 typename basic_yield_context<Handler>::caller_type& ca_;
200 atomic_count ready_;
201 boost::system::error_code* out_ec_;
202 boost::system::error_code ec_;
203 return_type value_;
204 };
205
206 template <typename Handler>
207 class coro_async_result<Handler, void>
208 {
209 public:
210 typedef coro_handler<Handler, void> completion_handler_type;
211 typedef void return_type;
212
213 explicit coro_async_result(completion_handler_type& h)
214 : handler_(h),
215 ca_(h.ca_),
216 ready_(2)
217 {
218 h.ready_ = &ready_;
219 out_ec_ = h.ec_;
220 if (!out_ec_) h.ec_ = &ec_;
221 }
222
223 void get()
224 {
225 // Must not hold shared_ptr to coro while suspended.
226 handler_.coro_.reset();
227
228 if (--ready_ != 0)
229 ca_();
230 if (!out_ec_ && ec_) throw boost::system::system_error(ec_);
231 }
232
233 private:
234 completion_handler_type& handler_;
235 typename basic_yield_context<Handler>::caller_type& ca_;
236 atomic_count ready_;
237 boost::system::error_code* out_ec_;
238 boost::system::error_code ec_;
239 };
240
241 } // namespace detail
242
243 #if !defined(GENERATING_DOCUMENTATION)
244
245 template <typename Handler, typename ReturnType>
246 class async_result<basic_yield_context<Handler>, ReturnType()>
247 : public detail::coro_async_result<Handler, void>
248 {
249 public:
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)
254 {
255 }
256 };
257
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>
261 {
262 public:
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)
267 {
268 }
269 };
270
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>
275 {
276 public:
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)
281 {
282 }
283 };
284
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>
289 {
290 public:
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)
295 {
296 }
297 };
298
299 template <typename Handler, typename T, typename Allocator>
300 struct associated_allocator<detail::coro_handler<Handler, T>, Allocator>
301 {
302 typedef typename associated_allocator<Handler, Allocator>::type type;
303
304 static type get(const detail::coro_handler<Handler, T>& h,
305 const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
306 {
307 return associated_allocator<Handler, Allocator>::get(h.handler_, a);
308 }
309 };
310
311 template <typename Handler, typename T, typename Executor>
312 struct associated_executor<detail::coro_handler<Handler, T>, Executor>
313 : detail::associated_executor_forwarding_base<Handler, Executor>
314 {
315 typedef typename associated_executor<Handler, Executor>::type type;
316
317 static type get(const detail::coro_handler<Handler, T>& h,
318 const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
319 {
320 return associated_executor<Handler, Executor>::get(h.handler_, ex);
321 }
322 };
323
324 namespace detail {
325
326 template <typename Handler, typename Function>
327 struct spawn_data : private noncopyable
328 {
329 template <typename Hand, typename Func>
330 spawn_data(BOOST_ASIO_MOVE_ARG(Hand) handler,
331 bool call_handler, BOOST_ASIO_MOVE_ARG(Func) function)
332 : handler_(BOOST_ASIO_MOVE_CAST(Hand)(handler)),
333 call_handler_(call_handler),
334 function_(BOOST_ASIO_MOVE_CAST(Func)(function))
335 {
336 }
337
338 weak_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
339 Handler handler_;
340 bool call_handler_;
341 Function function_;
342 };
343
344 template <typename Handler, typename Function>
345 struct coro_entry_point
346 {
347 void operator()(typename basic_yield_context<Handler>::caller_type& ca)
348 {
349 shared_ptr<spawn_data<Handler, Function> > data(data_);
350 #if !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
351 ca(); // Yield until coroutine pointer has been initialised.
352 #endif // !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
353 const basic_yield_context<Handler> yield(
354 data->coro_, ca, data->handler_);
355
356 (data->function_)(yield);
357 if (data->call_handler_)
358 (data->handler_)();
359 }
360
361 shared_ptr<spawn_data<Handler, Function> > data_;
362 };
363
364 template <typename Handler, typename Function>
365 struct spawn_helper
366 {
367 typedef typename associated_allocator<Handler>::type allocator_type;
368
369 allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
370 {
371 return (get_associated_allocator)(data_->handler_);
372 }
373
374 typedef typename associated_executor<Handler>::type executor_type;
375
376 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
377 {
378 return (get_associated_executor)(data_->handler_);
379 }
380
381 void operator()()
382 {
383 typedef typename basic_yield_context<Handler>::callee_type callee_type;
384 coro_entry_point<Handler, Function> entry_point = { data_ };
385 shared_ptr<callee_type> coro(new callee_type(entry_point, attributes_));
386 data_->coro_ = coro;
387 (*coro)();
388 }
389
390 shared_ptr<spawn_data<Handler, Function> > data_;
391 boost::coroutines::attributes attributes_;
392 };
393
394 template <typename Function, typename Handler, typename Function1>
395 inline asio_handler_invoke_is_deprecated
396 asio_handler_invoke(Function& function,
397 spawn_helper<Handler, Function1>* this_handler)
398 {
399 boost_asio_handler_invoke_helpers::invoke(
400 function, this_handler->data_->handler_);
401 #if defined(BOOST_ASIO_NO_DEPRECATED)
402 return asio_handler_invoke_is_no_longer_used();
403 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
404 }
405
406 template <typename Function, typename Handler, typename Function1>
407 inline asio_handler_invoke_is_deprecated
408 asio_handler_invoke(const Function& function,
409 spawn_helper<Handler, Function1>* this_handler)
410 {
411 boost_asio_handler_invoke_helpers::invoke(
412 function, this_handler->data_->handler_);
413 #if defined(BOOST_ASIO_NO_DEPRECATED)
414 return asio_handler_invoke_is_no_longer_used();
415 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
416 }
417
418 inline void default_spawn_handler() {}
419
420 } // namespace detail
421
422 template <typename Function>
423 inline void spawn(BOOST_ASIO_MOVE_ARG(Function) function,
424 const boost::coroutines::attributes& attributes)
425 {
426 typedef typename decay<Function>::type function_type;
427
428 typename associated_executor<function_type>::type ex(
429 (get_associated_executor)(function));
430
431 boost::asio::spawn(ex, BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
432 }
433
434 template <typename Handler, typename Function>
435 void spawn(BOOST_ASIO_MOVE_ARG(Handler) handler,
436 BOOST_ASIO_MOVE_ARG(Function) function,
437 const boost::coroutines::attributes& attributes,
438 typename enable_if<
439 !is_executor<typename decay<Handler>::type>::value &&
440 !execution::is_executor<typename decay<Handler>::type>::value &&
441 !is_convertible<Handler&, execution_context&>::value>::type*)
442 {
443 typedef typename decay<Handler>::type handler_type;
444 typedef typename decay<Function>::type function_type;
445
446 detail::spawn_helper<handler_type, function_type> helper;
447 helper.data_.reset(
448 new detail::spawn_data<handler_type, function_type>(
449 BOOST_ASIO_MOVE_CAST(Handler)(handler), true,
450 BOOST_ASIO_MOVE_CAST(Function)(function)));
451 helper.attributes_ = attributes;
452
453 boost::asio::dispatch(helper);
454 }
455
456 template <typename Handler, typename Function>
457 void spawn(basic_yield_context<Handler> ctx,
458 BOOST_ASIO_MOVE_ARG(Function) function,
459 const boost::coroutines::attributes& attributes)
460 {
461 typedef typename decay<Function>::type function_type;
462
463 Handler handler(ctx.handler_); // Explicit copy that might be moved from.
464
465 detail::spawn_helper<Handler, function_type> helper;
466 helper.data_.reset(
467 new detail::spawn_data<Handler, function_type>(
468 BOOST_ASIO_MOVE_CAST(Handler)(handler), false,
469 BOOST_ASIO_MOVE_CAST(Function)(function)));
470 helper.attributes_ = attributes;
471
472 boost::asio::dispatch(helper);
473 }
474
475 template <typename Function, typename Executor>
476 inline void spawn(const Executor& ex,
477 BOOST_ASIO_MOVE_ARG(Function) function,
478 const boost::coroutines::attributes& attributes,
479 typename enable_if<
480 is_executor<Executor>::value || execution::is_executor<Executor>::value
481 >::type*)
482 {
483 boost::asio::spawn(boost::asio::strand<Executor>(ex),
484 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
485 }
486
487 template <typename Function, typename Executor>
488 inline void spawn(const strand<Executor>& ex,
489 BOOST_ASIO_MOVE_ARG(Function) function,
490 const boost::coroutines::attributes& attributes)
491 {
492 boost::asio::spawn(boost::asio::bind_executor(
493 ex, &detail::default_spawn_handler),
494 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
495 }
496
497 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
498
499 template <typename Function>
500 inline void spawn(const boost::asio::io_context::strand& s,
501 BOOST_ASIO_MOVE_ARG(Function) function,
502 const boost::coroutines::attributes& attributes)
503 {
504 boost::asio::spawn(boost::asio::bind_executor(
505 s, &detail::default_spawn_handler),
506 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
507 }
508
509 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
510
511 template <typename Function, typename ExecutionContext>
512 inline void spawn(ExecutionContext& ctx,
513 BOOST_ASIO_MOVE_ARG(Function) function,
514 const boost::coroutines::attributes& attributes,
515 typename enable_if<is_convertible<
516 ExecutionContext&, execution_context&>::value>::type*)
517 {
518 boost::asio::spawn(ctx.get_executor(),
519 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
520 }
521
522 #endif // !defined(GENERATING_DOCUMENTATION)
523
524 } // namespace asio
525 } // namespace boost
526
527 #include <boost/asio/detail/pop_options.hpp>
528
529 #endif // BOOST_ASIO_IMPL_SPAWN_HPP