parallel_group.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // experimental/parallel_group.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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 BOOST_ASIO_EXPERIMENTAL_PARALLEL_GROUP_HPP
  11. #define BOOST_ASIO_EXPERIMENTAL_PARALLEL_GROUP_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <vector>
  17. #include <boost/asio/async_result.hpp>
  18. #include <boost/asio/detail/array.hpp>
  19. #include <boost/asio/detail/memory.hpp>
  20. #include <boost/asio/detail/type_traits.hpp>
  21. #include <boost/asio/detail/utility.hpp>
  22. #include <boost/asio/experimental/cancellation_condition.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace experimental {
  27. namespace detail {
  28. // Helper trait for getting a tuple from a completion signature.
  29. template <typename Signature>
  30. struct parallel_op_signature_as_tuple;
  31. template <typename R, typename... Args>
  32. struct parallel_op_signature_as_tuple<R(Args...)>
  33. {
  34. typedef std::tuple<decay_t<Args>...> type;
  35. };
  36. // Helper trait for concatenating completion signatures.
  37. template <std::size_t N, typename Offsets, typename... Signatures>
  38. struct parallel_group_signature;
  39. template <std::size_t N, typename R0, typename... Args0>
  40. struct parallel_group_signature<N, R0(Args0...)>
  41. {
  42. typedef boost::asio::detail::array<std::size_t, N> order_type;
  43. typedef R0 raw_type(Args0...);
  44. typedef R0 type(order_type, Args0...);
  45. };
  46. template <std::size_t N,
  47. typename R0, typename... Args0,
  48. typename R1, typename... Args1>
  49. struct parallel_group_signature<N, R0(Args0...), R1(Args1...)>
  50. {
  51. typedef boost::asio::detail::array<std::size_t, N> order_type;
  52. typedef R0 raw_type(Args0..., Args1...);
  53. typedef R0 type(order_type, Args0..., Args1...);
  54. };
  55. template <std::size_t N, typename Sig0,
  56. typename Sig1, typename... SigN>
  57. struct parallel_group_signature<N, Sig0, Sig1, SigN...>
  58. {
  59. typedef boost::asio::detail::array<std::size_t, N> order_type;
  60. typedef typename parallel_group_signature<N,
  61. typename parallel_group_signature<N, Sig0, Sig1>::raw_type,
  62. SigN...>::raw_type raw_type;
  63. typedef typename parallel_group_signature<N,
  64. typename parallel_group_signature<N, Sig0, Sig1>::raw_type,
  65. SigN...>::type type;
  66. };
  67. template <typename Condition, typename Handler,
  68. typename... Ops, std::size_t... I>
  69. void parallel_group_launch(Condition cancellation_condition, Handler handler,
  70. std::tuple<Ops...>& ops, boost::asio::detail::index_sequence<I...>);
  71. // Helper trait for determining ranged parallel group completion signatures.
  72. template <typename Signature, typename Allocator>
  73. struct ranged_parallel_group_signature;
  74. template <typename R, typename... Args, typename Allocator>
  75. struct ranged_parallel_group_signature<R(Args...), Allocator>
  76. {
  77. typedef std::vector<std::size_t,
  78. BOOST_ASIO_REBIND_ALLOC(Allocator, std::size_t)> order_type;
  79. typedef R raw_type(
  80. std::vector<Args, BOOST_ASIO_REBIND_ALLOC(Allocator, Args)>...);
  81. typedef R type(order_type,
  82. std::vector<Args, BOOST_ASIO_REBIND_ALLOC(Allocator, Args)>...);
  83. };
  84. template <typename Condition, typename Handler,
  85. typename Range, typename Allocator>
  86. void ranged_parallel_group_launch(Condition cancellation_condition,
  87. Handler handler, Range&& range, const Allocator& allocator);
  88. char (&parallel_group_has_iterator_helper(...))[2];
  89. template <typename T>
  90. char parallel_group_has_iterator_helper(T*, typename T::iterator* = 0);
  91. template <typename T>
  92. struct parallel_group_has_iterator_typedef
  93. {
  94. enum { value = (sizeof((parallel_group_has_iterator_helper)((T*)(0))) == 1) };
  95. };
  96. } // namespace detail
  97. /// Type trait used to determine whether a type is a range of asynchronous
  98. /// operations that can be used with with @c make_parallel_group.
  99. template <typename T>
  100. struct is_async_operation_range
  101. {
  102. #if defined(GENERATING_DOCUMENTATION)
  103. /// The value member is true if the type may be used as a range of
  104. /// asynchronous operations.
  105. static const bool value;
  106. #else
  107. enum
  108. {
  109. value = detail::parallel_group_has_iterator_typedef<T>::value
  110. };
  111. #endif
  112. };
  113. /// A group of asynchronous operations that may be launched in parallel.
  114. /**
  115. * See the documentation for boost::asio::experimental::make_parallel_group for
  116. * a usage example.
  117. */
  118. template <typename... Ops>
  119. class parallel_group
  120. {
  121. private:
  122. struct initiate_async_wait
  123. {
  124. template <typename Handler, typename Condition>
  125. void operator()(Handler&& h, Condition&& c, std::tuple<Ops...>&& ops) const
  126. {
  127. detail::parallel_group_launch(
  128. std::forward<Condition>(c), std::forward<Handler>(h),
  129. ops, boost::asio::detail::index_sequence_for<Ops...>());
  130. }
  131. };
  132. std::tuple<Ops...> ops_;
  133. public:
  134. /// Constructor.
  135. explicit parallel_group(Ops... ops)
  136. : ops_(std::move(ops)...)
  137. {
  138. }
  139. /// The completion signature for the group of operations.
  140. typedef typename detail::parallel_group_signature<sizeof...(Ops),
  141. completion_signature_of_t<Ops>...>::type signature;
  142. /// Initiate an asynchronous wait for the group of operations.
  143. /**
  144. * Launches the group and asynchronously waits for completion.
  145. *
  146. * @param cancellation_condition A function object, called on completion of
  147. * an operation within the group, that is used to determine whether to cancel
  148. * the remaining operations. The function object is passed the arguments of
  149. * the completed operation's handler. To trigger cancellation of the remaining
  150. * operations, it must return a boost::asio::cancellation_type value other
  151. * than <tt>boost::asio::cancellation_type::none</tt>.
  152. *
  153. * @param token A @ref completion_token whose signature is comprised of
  154. * a @c std::array<std::size_t, N> indicating the completion order of the
  155. * operations, followed by all operations' completion handler arguments.
  156. *
  157. * The library provides the following @c cancellation_condition types:
  158. *
  159. * @li boost::asio::experimental::wait_for_all
  160. * @li boost::asio::experimental::wait_for_one
  161. * @li boost::asio::experimental::wait_for_one_error
  162. * @li boost::asio::experimental::wait_for_one_success
  163. */
  164. template <typename CancellationCondition,
  165. BOOST_ASIO_COMPLETION_TOKEN_FOR(signature) CompletionToken>
  166. auto async_wait(CancellationCondition cancellation_condition,
  167. CompletionToken&& token)
  168. -> decltype(
  169. boost::asio::async_initiate<CompletionToken, signature>(
  170. declval<initiate_async_wait>(), token,
  171. std::move(cancellation_condition), std::move(ops_)))
  172. {
  173. return boost::asio::async_initiate<CompletionToken, signature>(
  174. initiate_async_wait(), token,
  175. std::move(cancellation_condition), std::move(ops_));
  176. }
  177. };
  178. /// Create a group of operations that may be launched in parallel.
  179. /**
  180. * For example:
  181. * @code boost::asio::experimental::make_parallel_group(
  182. * in.async_read_some(boost::asio::buffer(data)),
  183. * timer.async_wait()
  184. * ).async_wait(
  185. * boost::asio::experimental::wait_for_all(),
  186. * [](
  187. * std::array<std::size_t, 2> completion_order,
  188. * boost::system::error_code ec1, std::size_t n1,
  189. * boost::system::error_code ec2
  190. * )
  191. * {
  192. * switch (completion_order[0])
  193. * {
  194. * case 0:
  195. * {
  196. * std::cout << "descriptor finished: " << ec1 << ", " << n1 << "\n";
  197. * }
  198. * break;
  199. * case 1:
  200. * {
  201. * std::cout << "timer finished: " << ec2 << "\n";
  202. * }
  203. * break;
  204. * }
  205. * }
  206. * );
  207. * @endcode
  208. *
  209. * If preferred, the asynchronous operations may be explicitly packaged as
  210. * function objects:
  211. * @code boost::asio::experimental::make_parallel_group(
  212. * [&](auto token)
  213. * {
  214. * return in.async_read_some(boost::asio::buffer(data), token);
  215. * },
  216. * [&](auto token)
  217. * {
  218. * return timer.async_wait(token);
  219. * }
  220. * ).async_wait(
  221. * boost::asio::experimental::wait_for_all(),
  222. * [](
  223. * std::array<std::size_t, 2> completion_order,
  224. * boost::system::error_code ec1, std::size_t n1,
  225. * boost::system::error_code ec2
  226. * )
  227. * {
  228. * switch (completion_order[0])
  229. * {
  230. * case 0:
  231. * {
  232. * std::cout << "descriptor finished: " << ec1 << ", " << n1 << "\n";
  233. * }
  234. * break;
  235. * case 1:
  236. * {
  237. * std::cout << "timer finished: " << ec2 << "\n";
  238. * }
  239. * break;
  240. * }
  241. * }
  242. * );
  243. * @endcode
  244. */
  245. template <typename... Ops>
  246. BOOST_ASIO_NODISCARD inline parallel_group<Ops...>
  247. make_parallel_group(Ops... ops)
  248. {
  249. return parallel_group<Ops...>(std::move(ops)...);
  250. }
  251. /// A range-based group of asynchronous operations that may be launched in
  252. /// parallel.
  253. /**
  254. * See the documentation for boost::asio::experimental::make_parallel_group for
  255. * a usage example.
  256. */
  257. template <typename Range, typename Allocator = std::allocator<void>>
  258. class ranged_parallel_group
  259. {
  260. private:
  261. struct initiate_async_wait
  262. {
  263. template <typename Handler, typename Condition>
  264. void operator()(Handler&& h, Condition&& c,
  265. Range&& range, const Allocator& allocator) const
  266. {
  267. detail::ranged_parallel_group_launch(std::move(c),
  268. std::move(h), std::forward<Range>(range), allocator);
  269. }
  270. };
  271. Range range_;
  272. Allocator allocator_;
  273. public:
  274. /// Constructor.
  275. explicit ranged_parallel_group(Range range,
  276. const Allocator& allocator = Allocator())
  277. : range_(std::move(range)),
  278. allocator_(allocator)
  279. {
  280. }
  281. /// The completion signature for the group of operations.
  282. typedef typename detail::ranged_parallel_group_signature<
  283. completion_signature_of_t<
  284. decay_t<decltype(*std::declval<typename Range::iterator>())>>,
  285. Allocator>::type signature;
  286. /// Initiate an asynchronous wait for the group of operations.
  287. /**
  288. * Launches the group and asynchronously waits for completion.
  289. *
  290. * @param cancellation_condition A function object, called on completion of
  291. * an operation within the group, that is used to determine whether to cancel
  292. * the remaining operations. The function object is passed the arguments of
  293. * the completed operation's handler. To trigger cancellation of the remaining
  294. * operations, it must return a boost::asio::cancellation_type value other
  295. * than <tt>boost::asio::cancellation_type::none</tt>.
  296. *
  297. * @param token A @ref completion_token whose signature is comprised of
  298. * a @c std::vector<std::size_t, Allocator> indicating the completion order of
  299. * the operations, followed by a vector for each of the completion signature's
  300. * arguments.
  301. *
  302. * The library provides the following @c cancellation_condition types:
  303. *
  304. * @li boost::asio::experimental::wait_for_all
  305. * @li boost::asio::experimental::wait_for_one
  306. * @li boost::asio::experimental::wait_for_one_error
  307. * @li boost::asio::experimental::wait_for_one_success
  308. */
  309. template <typename CancellationCondition,
  310. BOOST_ASIO_COMPLETION_TOKEN_FOR(signature) CompletionToken>
  311. auto async_wait(CancellationCondition cancellation_condition,
  312. CompletionToken&& token)
  313. -> decltype(
  314. boost::asio::async_initiate<CompletionToken, signature>(
  315. declval<initiate_async_wait>(), token,
  316. std::move(cancellation_condition),
  317. std::move(range_), allocator_))
  318. {
  319. return boost::asio::async_initiate<CompletionToken, signature>(
  320. initiate_async_wait(), token,
  321. std::move(cancellation_condition),
  322. std::move(range_), allocator_);
  323. }
  324. };
  325. /// Create a group of operations that may be launched in parallel.
  326. /**
  327. * @param range A range containing the operations to be launched.
  328. *
  329. * For example:
  330. * @code
  331. * using op_type =
  332. * decltype(socket1.async_read_some(boost::asio::buffer(data1)));
  333. *
  334. * std::vector<op_type> ops;
  335. * ops.push_back(socket1.async_read_some(boost::asio::buffer(data1)));
  336. * ops.push_back(socket2.async_read_some(boost::asio::buffer(data2)));
  337. *
  338. * boost::asio::experimental::make_parallel_group(ops).async_wait(
  339. * boost::asio::experimental::wait_for_all(),
  340. * [](
  341. * std::vector<std::size_t> completion_order,
  342. * std::vector<boost::system::error_code> e,
  343. * std::vector<std::size_t> n
  344. * )
  345. * {
  346. * for (std::size_t i = 0; i < completion_order.size(); ++i)
  347. * {
  348. * std::size_t idx = completion_order[i];
  349. * std::cout << "socket " << idx << " finished: ";
  350. * std::cout << e[idx] << ", " << n[idx] << "\n";
  351. * }
  352. * }
  353. * );
  354. * @endcode
  355. */
  356. template <typename Range>
  357. BOOST_ASIO_NODISCARD inline ranged_parallel_group<decay_t<Range>>
  358. make_parallel_group(Range&& range,
  359. constraint_t<
  360. is_async_operation_range<decay_t<Range>>::value
  361. > = 0)
  362. {
  363. return ranged_parallel_group<decay_t<Range>>(std::forward<Range>(range));
  364. }
  365. /// Create a group of operations that may be launched in parallel.
  366. /**
  367. * @param allocator Specifies the allocator to be used with the result vectors.
  368. *
  369. * @param range A range containing the operations to be launched.
  370. *
  371. * For example:
  372. * @code
  373. * using op_type =
  374. * decltype(socket1.async_read_some(boost::asio::buffer(data1)));
  375. *
  376. * std::vector<op_type> ops;
  377. * ops.push_back(socket1.async_read_some(boost::asio::buffer(data1)));
  378. * ops.push_back(socket2.async_read_some(boost::asio::buffer(data2)));
  379. *
  380. * boost::asio::experimental::make_parallel_group(
  381. * std::allocator_arg_t,
  382. * my_allocator,
  383. * ops
  384. * ).async_wait(
  385. * boost::asio::experimental::wait_for_all(),
  386. * [](
  387. * std::vector<std::size_t> completion_order,
  388. * std::vector<boost::system::error_code> e,
  389. * std::vector<std::size_t> n
  390. * )
  391. * {
  392. * for (std::size_t i = 0; i < completion_order.size(); ++i)
  393. * {
  394. * std::size_t idx = completion_order[i];
  395. * std::cout << "socket " << idx << " finished: ";
  396. * std::cout << e[idx] << ", " << n[idx] << "\n";
  397. * }
  398. * }
  399. * );
  400. * @endcode
  401. */
  402. template <typename Allocator, typename Range>
  403. BOOST_ASIO_NODISCARD inline ranged_parallel_group<decay_t<Range>, Allocator>
  404. make_parallel_group(allocator_arg_t, const Allocator& allocator, Range&& range,
  405. constraint_t<
  406. is_async_operation_range<decay_t<Range>>::value
  407. > = 0)
  408. {
  409. return ranged_parallel_group<decay_t<Range>, Allocator>(
  410. std::forward<Range>(range), allocator);
  411. }
  412. } // namespace experimental
  413. } // namespace asio
  414. } // namespace boost
  415. #include <boost/asio/detail/pop_options.hpp>
  416. #include <boost/asio/experimental/impl/parallel_group.hpp>
  417. #endif // BOOST_ASIO_EXPERIMENTAL_PARALLEL_GROUP_HPP