parallel_group.hpp 14 KB

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