parallel_group.hpp 14 KB

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