]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/impl/spawn.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / impl / spawn.hpp
1 //
2 // impl/spawn.hpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 void* asio_handler_allocate(std::size_t size,
114 coro_handler<Handler, T>* this_handler)
115 {
116 return boost_asio_handler_alloc_helpers::allocate(
117 size, this_handler->handler_);
118 }
119
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)
123 {
124 boost_asio_handler_alloc_helpers::deallocate(
125 pointer, size, this_handler->handler_);
126 }
127
128 template <typename Handler, typename T>
129 inline bool asio_handler_is_continuation(coro_handler<Handler, T>*)
130 {
131 return true;
132 }
133
134 template <typename Function, typename Handler, typename T>
135 inline void asio_handler_invoke(Function& function,
136 coro_handler<Handler, T>* this_handler)
137 {
138 boost_asio_handler_invoke_helpers::invoke(
139 function, this_handler->handler_);
140 }
141
142 template <typename Function, typename Handler, typename T>
143 inline void asio_handler_invoke(const Function& function,
144 coro_handler<Handler, T>* this_handler)
145 {
146 boost_asio_handler_invoke_helpers::invoke(
147 function, this_handler->handler_);
148 }
149
150 template <typename Handler, typename T>
151 class coro_async_result
152 {
153 public:
154 typedef coro_handler<Handler, T> completion_handler_type;
155 typedef T return_type;
156
157 explicit coro_async_result(completion_handler_type& h)
158 : handler_(h),
159 ca_(h.ca_),
160 ready_(2)
161 {
162 h.ready_ = &ready_;
163 out_ec_ = h.ec_;
164 if (!out_ec_) h.ec_ = &ec_;
165 h.value_ = &value_;
166 }
167
168 return_type get()
169 {
170 // Must not hold shared_ptr to coro while suspended.
171 handler_.coro_.reset();
172
173 if (--ready_ != 0)
174 ca_();
175 if (!out_ec_ && ec_) throw boost::system::system_error(ec_);
176 return BOOST_ASIO_MOVE_CAST(return_type)(value_);
177 }
178
179 private:
180 completion_handler_type& handler_;
181 typename basic_yield_context<Handler>::caller_type& ca_;
182 atomic_count ready_;
183 boost::system::error_code* out_ec_;
184 boost::system::error_code ec_;
185 return_type value_;
186 };
187
188 template <typename Handler>
189 class coro_async_result<Handler, void>
190 {
191 public:
192 typedef coro_handler<Handler, void> completion_handler_type;
193 typedef void return_type;
194
195 explicit coro_async_result(completion_handler_type& h)
196 : handler_(h),
197 ca_(h.ca_),
198 ready_(2)
199 {
200 h.ready_ = &ready_;
201 out_ec_ = h.ec_;
202 if (!out_ec_) h.ec_ = &ec_;
203 }
204
205 void get()
206 {
207 // Must not hold shared_ptr to coro while suspended.
208 handler_.coro_.reset();
209
210 if (--ready_ != 0)
211 ca_();
212 if (!out_ec_ && ec_) throw boost::system::system_error(ec_);
213 }
214
215 private:
216 completion_handler_type& handler_;
217 typename basic_yield_context<Handler>::caller_type& ca_;
218 atomic_count ready_;
219 boost::system::error_code* out_ec_;
220 boost::system::error_code ec_;
221 };
222
223 } // namespace detail
224
225 #if !defined(GENERATING_DOCUMENTATION)
226
227 template <typename Handler, typename ReturnType>
228 class async_result<basic_yield_context<Handler>, ReturnType()>
229 : public detail::coro_async_result<Handler, void>
230 {
231 public:
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)
236 {
237 }
238 };
239
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>
243 {
244 public:
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)
249 {
250 }
251 };
252
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>
257 {
258 public:
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)
263 {
264 }
265 };
266
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>
271 {
272 public:
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)
277 {
278 }
279 };
280
281 #if !defined(BOOST_ASIO_NO_DEPRECATED)
282
283 template <typename Handler, typename ReturnType>
284 struct handler_type<basic_yield_context<Handler>, ReturnType()>
285 {
286 typedef detail::coro_handler<Handler, void> type;
287 };
288
289 template <typename Handler, typename ReturnType, typename Arg1>
290 struct handler_type<basic_yield_context<Handler>, ReturnType(Arg1)>
291 {
292 typedef detail::coro_handler<Handler, typename decay<Arg1>::type> type;
293 };
294
295 template <typename Handler, typename ReturnType>
296 struct handler_type<basic_yield_context<Handler>,
297 ReturnType(boost::system::error_code)>
298 {
299 typedef detail::coro_handler<Handler, void> type;
300 };
301
302 template <typename Handler, typename ReturnType, typename Arg2>
303 struct handler_type<basic_yield_context<Handler>,
304 ReturnType(boost::system::error_code, Arg2)>
305 {
306 typedef detail::coro_handler<Handler, typename decay<Arg2>::type> type;
307 };
308
309 template <typename Handler, typename T>
310 class async_result<detail::coro_handler<Handler, T> >
311 : public detail::coro_async_result<Handler, T>
312 {
313 public:
314 typedef typename detail::coro_async_result<Handler, T>::return_type type;
315
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)
320 {
321 }
322 };
323
324 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
325
326 template <typename Handler, typename T, typename Allocator>
327 struct associated_allocator<detail::coro_handler<Handler, T>, Allocator>
328 {
329 typedef typename associated_allocator<Handler, Allocator>::type type;
330
331 static type get(const detail::coro_handler<Handler, T>& h,
332 const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
333 {
334 return associated_allocator<Handler, Allocator>::get(h.handler_, a);
335 }
336 };
337
338 template <typename Handler, typename T, typename Executor>
339 struct associated_executor<detail::coro_handler<Handler, T>, Executor>
340 {
341 typedef typename associated_executor<Handler, Executor>::type type;
342
343 static type get(const detail::coro_handler<Handler, T>& h,
344 const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
345 {
346 return associated_executor<Handler, Executor>::get(h.handler_, ex);
347 }
348 };
349
350 namespace detail {
351
352 template <typename Handler, typename Function>
353 struct spawn_data : private noncopyable
354 {
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))
360 {
361 }
362
363 weak_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
364 Handler handler_;
365 bool call_handler_;
366 Function function_;
367 };
368
369 template <typename Handler, typename Function>
370 struct coro_entry_point
371 {
372 void operator()(typename basic_yield_context<Handler>::caller_type& ca)
373 {
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_);
380
381 (data->function_)(yield);
382 if (data->call_handler_)
383 (data->handler_)();
384 }
385
386 shared_ptr<spawn_data<Handler, Function> > data_;
387 };
388
389 template <typename Handler, typename Function>
390 struct spawn_helper
391 {
392 void operator()()
393 {
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_));
397 data_->coro_ = coro;
398 (*coro)();
399 }
400
401 shared_ptr<spawn_data<Handler, Function> > data_;
402 boost::coroutines::attributes attributes_;
403 };
404
405 template <typename Function, typename Handler, typename Function1>
406 inline void asio_handler_invoke(Function& function,
407 spawn_helper<Handler, Function1>* this_handler)
408 {
409 boost_asio_handler_invoke_helpers::invoke(
410 function, this_handler->data_->handler_);
411 }
412
413 template <typename Function, typename Handler, typename Function1>
414 inline void asio_handler_invoke(const Function& function,
415 spawn_helper<Handler, Function1>* this_handler)
416 {
417 boost_asio_handler_invoke_helpers::invoke(
418 function, this_handler->data_->handler_);
419 }
420
421 inline void default_spawn_handler() {}
422
423 } // namespace detail
424
425 template <typename Function>
426 inline void spawn(BOOST_ASIO_MOVE_ARG(Function) function,
427 const boost::coroutines::attributes& attributes)
428 {
429 typedef typename decay<Function>::type function_type;
430
431 typename associated_executor<function_type>::type ex(
432 (get_associated_executor)(function));
433
434 boost::asio::spawn(ex, BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
435 }
436
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*)
443 {
444 typedef typename decay<Handler>::type handler_type;
445
446 typename associated_executor<handler_type>::type ex(
447 (get_associated_executor)(handler));
448
449 typename associated_allocator<handler_type>::type a(
450 (get_associated_allocator)(handler));
451
452 detail::spawn_helper<handler_type, Function> helper;
453 helper.data_.reset(
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;
458
459 ex.dispatch(helper, a);
460 }
461
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)
466 {
467 Handler handler(ctx.handler_); // Explicit copy that might be moved from.
468
469 typename associated_executor<Handler>::type ex(
470 (get_associated_executor)(handler));
471
472 typename associated_allocator<Handler>::type a(
473 (get_associated_allocator)(handler));
474
475 detail::spawn_helper<Handler, Function> helper;
476 helper.data_.reset(
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;
481
482 ex.dispatch(helper, a);
483 }
484
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*)
490 {
491 boost::asio::spawn(boost::asio::strand<Executor>(ex),
492 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
493 }
494
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)
499 {
500 boost::asio::spawn(boost::asio::bind_executor(
501 ex, &detail::default_spawn_handler),
502 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
503 }
504
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)
509 {
510 boost::asio::spawn(boost::asio::bind_executor(
511 s, &detail::default_spawn_handler),
512 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
513 }
514
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*)
521 {
522 boost::asio::spawn(ctx.get_executor(),
523 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
524 }
525
526 #endif // !defined(GENERATING_DOCUMENTATION)
527
528 } // namespace asio
529 } // namespace boost
530
531 #include <boost/asio/detail/pop_options.hpp>
532
533 #endif // BOOST_ASIO_IMPL_SPAWN_HPP