]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/impl/spawn.hpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / boost / asio / impl / spawn.hpp
1 //
2 // impl/spawn.hpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 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 <template <typename, typename> class Associator,
300 typename Handler, typename T, typename DefaultCandidate>
301 struct associator<Associator,
302 detail::coro_handler<Handler, T>,
303 DefaultCandidate>
304 : Associator<Handler, DefaultCandidate>
305 {
306 static typename Associator<Handler, DefaultCandidate>::type get(
307 const detail::coro_handler<Handler, T>& h,
308 const DefaultCandidate& c = DefaultCandidate()) BOOST_ASIO_NOEXCEPT
309 {
310 return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
311 }
312 };
313
314 namespace detail {
315
316 template <typename Handler, typename Function>
317 struct spawn_data : private noncopyable
318 {
319 template <typename Hand, typename Func>
320 spawn_data(BOOST_ASIO_MOVE_ARG(Hand) handler,
321 bool call_handler, BOOST_ASIO_MOVE_ARG(Func) function)
322 : handler_(BOOST_ASIO_MOVE_CAST(Hand)(handler)),
323 call_handler_(call_handler),
324 function_(BOOST_ASIO_MOVE_CAST(Func)(function))
325 {
326 }
327
328 weak_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
329 Handler handler_;
330 bool call_handler_;
331 Function function_;
332 };
333
334 template <typename Handler, typename Function>
335 struct coro_entry_point
336 {
337 void operator()(typename basic_yield_context<Handler>::caller_type& ca)
338 {
339 shared_ptr<spawn_data<Handler, Function> > data(data_);
340 #if !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
341 ca(); // Yield until coroutine pointer has been initialised.
342 #endif // !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
343 const basic_yield_context<Handler> yield(
344 data->coro_, ca, data->handler_);
345
346 (data->function_)(yield);
347 if (data->call_handler_)
348 BOOST_ASIO_MOVE_OR_LVALUE(Handler)(data->handler_)();
349 }
350
351 shared_ptr<spawn_data<Handler, Function> > data_;
352 };
353
354 template <typename Handler, typename Function>
355 struct spawn_helper
356 {
357 typedef typename associated_allocator<Handler>::type allocator_type;
358
359 allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
360 {
361 return (get_associated_allocator)(data_->handler_);
362 }
363
364 typedef typename associated_executor<Handler>::type executor_type;
365
366 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
367 {
368 return (get_associated_executor)(data_->handler_);
369 }
370
371 void operator()()
372 {
373 typedef typename basic_yield_context<Handler>::callee_type callee_type;
374 coro_entry_point<Handler, Function> entry_point = { data_ };
375 shared_ptr<callee_type> coro(new callee_type(entry_point, attributes_));
376 data_->coro_ = coro;
377 (*coro)();
378 }
379
380 shared_ptr<spawn_data<Handler, Function> > data_;
381 boost::coroutines::attributes attributes_;
382 };
383
384 template <typename Function, typename Handler, typename Function1>
385 inline asio_handler_invoke_is_deprecated
386 asio_handler_invoke(Function& function,
387 spawn_helper<Handler, Function1>* this_handler)
388 {
389 boost_asio_handler_invoke_helpers::invoke(
390 function, this_handler->data_->handler_);
391 #if defined(BOOST_ASIO_NO_DEPRECATED)
392 return asio_handler_invoke_is_no_longer_used();
393 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
394 }
395
396 template <typename Function, typename Handler, typename Function1>
397 inline asio_handler_invoke_is_deprecated
398 asio_handler_invoke(const Function& function,
399 spawn_helper<Handler, Function1>* this_handler)
400 {
401 boost_asio_handler_invoke_helpers::invoke(
402 function, this_handler->data_->handler_);
403 #if defined(BOOST_ASIO_NO_DEPRECATED)
404 return asio_handler_invoke_is_no_longer_used();
405 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
406 }
407
408 inline void default_spawn_handler() {}
409
410 } // namespace detail
411
412 template <typename Function>
413 inline void spawn(BOOST_ASIO_MOVE_ARG(Function) function,
414 const boost::coroutines::attributes& attributes)
415 {
416 typedef typename decay<Function>::type function_type;
417
418 typename associated_executor<function_type>::type ex(
419 (get_associated_executor)(function));
420
421 boost::asio::spawn(ex, BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
422 }
423
424 template <typename Handler, typename Function>
425 void spawn(BOOST_ASIO_MOVE_ARG(Handler) handler,
426 BOOST_ASIO_MOVE_ARG(Function) function,
427 const boost::coroutines::attributes& attributes,
428 typename constraint<
429 !is_executor<typename decay<Handler>::type>::value &&
430 !execution::is_executor<typename decay<Handler>::type>::value &&
431 !is_convertible<Handler&, execution_context&>::value>::type)
432 {
433 typedef typename decay<Handler>::type handler_type;
434 typedef typename decay<Function>::type function_type;
435
436 detail::spawn_helper<handler_type, function_type> helper;
437 helper.data_.reset(
438 new detail::spawn_data<handler_type, function_type>(
439 BOOST_ASIO_MOVE_CAST(Handler)(handler), true,
440 BOOST_ASIO_MOVE_CAST(Function)(function)));
441 helper.attributes_ = attributes;
442
443 boost::asio::dispatch(helper);
444 }
445
446 template <typename Handler, typename Function>
447 void spawn(basic_yield_context<Handler> ctx,
448 BOOST_ASIO_MOVE_ARG(Function) function,
449 const boost::coroutines::attributes& attributes)
450 {
451 typedef typename decay<Function>::type function_type;
452
453 Handler handler(ctx.handler_); // Explicit copy that might be moved from.
454
455 detail::spawn_helper<Handler, function_type> helper;
456 helper.data_.reset(
457 new detail::spawn_data<Handler, function_type>(
458 BOOST_ASIO_MOVE_CAST(Handler)(handler), false,
459 BOOST_ASIO_MOVE_CAST(Function)(function)));
460 helper.attributes_ = attributes;
461
462 boost::asio::dispatch(helper);
463 }
464
465 template <typename Function, typename Executor>
466 inline void spawn(const Executor& ex,
467 BOOST_ASIO_MOVE_ARG(Function) function,
468 const boost::coroutines::attributes& attributes,
469 typename constraint<
470 is_executor<Executor>::value || execution::is_executor<Executor>::value
471 >::type)
472 {
473 boost::asio::spawn(boost::asio::strand<Executor>(ex),
474 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
475 }
476
477 template <typename Function, typename Executor>
478 inline void spawn(const strand<Executor>& ex,
479 BOOST_ASIO_MOVE_ARG(Function) function,
480 const boost::coroutines::attributes& attributes)
481 {
482 boost::asio::spawn(boost::asio::bind_executor(
483 ex, &detail::default_spawn_handler),
484 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
485 }
486
487 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
488
489 template <typename Function>
490 inline void spawn(const boost::asio::io_context::strand& s,
491 BOOST_ASIO_MOVE_ARG(Function) function,
492 const boost::coroutines::attributes& attributes)
493 {
494 boost::asio::spawn(boost::asio::bind_executor(
495 s, &detail::default_spawn_handler),
496 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
497 }
498
499 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
500
501 template <typename Function, typename ExecutionContext>
502 inline void spawn(ExecutionContext& ctx,
503 BOOST_ASIO_MOVE_ARG(Function) function,
504 const boost::coroutines::attributes& attributes,
505 typename constraint<is_convertible<
506 ExecutionContext&, execution_context&>::value>::type)
507 {
508 boost::asio::spawn(ctx.get_executor(),
509 BOOST_ASIO_MOVE_CAST(Function)(function), attributes);
510 }
511
512 #endif // !defined(GENERATING_DOCUMENTATION)
513
514 } // namespace asio
515 } // namespace boost
516
517 #include <boost/asio/detail/pop_options.hpp>
518
519 #endif // BOOST_ASIO_IMPL_SPAWN_HPP