parallel_group.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 ASIO_EXPERIMENTAL_PARALLEL_GROUP_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <vector>
  17. #include "asio/detail/array.hpp"
  18. #include "asio/detail/memory.hpp"
  19. #include "asio/detail/type_traits.hpp"
  20. #include "asio/detail/utility.hpp"
  21. #include "asio/experimental/cancellation_condition.hpp"
  22. #include "asio/detail/push_options.hpp"
  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<decay_t<Args>...> 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 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 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 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, 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. ASIO_REBIND_ALLOC(Allocator, std::size_t)> order_type;
  77. typedef R raw_type(
  78. std::vector<Args, ASIO_REBIND_ALLOC(Allocator, Args)>...);
  79. typedef R type(order_type,
  80. std::vector<Args, 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 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, 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. completion_signature_of_t<Ops>...>::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 asio::cancellation_type value other
  149. * than <tt>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 asio::experimental::wait_for_all
  158. * @li asio::experimental::wait_for_one
  159. * @li asio::experimental::wait_for_one_error
  160. * @li asio::experimental::wait_for_one_success
  161. */
  162. template <typename CancellationCondition,
  163. ASIO_COMPLETION_TOKEN_FOR(signature) CompletionToken>
  164. auto async_wait(CancellationCondition cancellation_condition,
  165. CompletionToken&& token)
  166. -> decltype(
  167. asio::async_initiate<CompletionToken, signature>(
  168. declval<initiate_async_wait>(), token,
  169. std::move(cancellation_condition), std::move(ops_)))
  170. {
  171. return asio::async_initiate<CompletionToken, signature>(
  172. initiate_async_wait(), token,
  173. std::move(cancellation_condition), std::move(ops_));
  174. }
  175. };
  176. /// Create a group of operations that may be launched in parallel.
  177. /**
  178. * For example:
  179. * @code asio::experimental::make_parallel_group(
  180. * [&](auto token)
  181. * {
  182. * return in.async_read_some(asio::buffer(data), token);
  183. * },
  184. * [&](auto token)
  185. * {
  186. * return timer.async_wait(token);
  187. * }
  188. * ).async_wait(
  189. * asio::experimental::wait_for_all(),
  190. * [](
  191. * std::array<std::size_t, 2> completion_order,
  192. * std::error_code ec1, std::size_t n1,
  193. * std::error_code ec2
  194. * )
  195. * {
  196. * switch (completion_order[0])
  197. * {
  198. * case 0:
  199. * {
  200. * std::cout << "descriptor finished: " << ec1 << ", " << n1 << "\n";
  201. * }
  202. * break;
  203. * case 1:
  204. * {
  205. * std::cout << "timer finished: " << ec2 << "\n";
  206. * }
  207. * break;
  208. * }
  209. * }
  210. * );
  211. * @endcode
  212. */
  213. template <typename... Ops>
  214. ASIO_NODISCARD inline parallel_group<Ops...>
  215. make_parallel_group(Ops... ops)
  216. {
  217. return parallel_group<Ops...>(std::move(ops)...);
  218. }
  219. /// A range-based group of asynchronous operations that may be launched in
  220. /// parallel.
  221. /**
  222. * See the documentation for asio::experimental::make_parallel_group for
  223. * a usage example.
  224. */
  225. template <typename Range, typename Allocator = std::allocator<void>>
  226. class ranged_parallel_group
  227. {
  228. private:
  229. struct initiate_async_wait
  230. {
  231. template <typename Handler, typename Condition>
  232. void operator()(Handler&& h, Condition&& c,
  233. Range&& range, const Allocator& allocator) const
  234. {
  235. detail::ranged_parallel_group_launch(std::move(c),
  236. std::move(h), std::forward<Range>(range), allocator);
  237. }
  238. };
  239. Range range_;
  240. Allocator allocator_;
  241. public:
  242. /// Constructor.
  243. explicit ranged_parallel_group(Range range,
  244. const Allocator& allocator = Allocator())
  245. : range_(std::move(range)),
  246. allocator_(allocator)
  247. {
  248. }
  249. /// The completion signature for the group of operations.
  250. typedef typename detail::ranged_parallel_group_signature<
  251. completion_signature_of_t<
  252. decay_t<decltype(*std::declval<typename Range::iterator>())>>,
  253. Allocator>::type signature;
  254. /// Initiate an asynchronous wait for the group of operations.
  255. /**
  256. * Launches the group and asynchronously waits for completion.
  257. *
  258. * @param cancellation_condition A function object, called on completion of
  259. * an operation within the group, that is used to determine whether to cancel
  260. * the remaining operations. The function object is passed the arguments of
  261. * the completed operation's handler. To trigger cancellation of the remaining
  262. * operations, it must return a asio::cancellation_type value other
  263. * than <tt>asio::cancellation_type::none</tt>.
  264. *
  265. * @param token A @ref completion_token whose signature is comprised of
  266. * a @c std::vector<std::size_t, Allocator> indicating the completion order of
  267. * the operations, followed by a vector for each of the completion signature's
  268. * arguments.
  269. *
  270. * The library provides the following @c cancellation_condition types:
  271. *
  272. * @li asio::experimental::wait_for_all
  273. * @li asio::experimental::wait_for_one
  274. * @li asio::experimental::wait_for_one_error
  275. * @li asio::experimental::wait_for_one_success
  276. */
  277. template <typename CancellationCondition,
  278. ASIO_COMPLETION_TOKEN_FOR(signature) CompletionToken>
  279. auto async_wait(CancellationCondition cancellation_condition,
  280. CompletionToken&& token)
  281. -> decltype(
  282. asio::async_initiate<CompletionToken, signature>(
  283. declval<initiate_async_wait>(), token,
  284. std::move(cancellation_condition),
  285. std::move(range_), allocator_))
  286. {
  287. return asio::async_initiate<CompletionToken, signature>(
  288. initiate_async_wait(), token,
  289. std::move(cancellation_condition),
  290. std::move(range_), allocator_);
  291. }
  292. };
  293. /// Create a group of operations that may be launched in parallel.
  294. /**
  295. * @param range A range containing the operations to be launched.
  296. *
  297. * For example:
  298. * @code
  299. * using op_type = decltype(
  300. * socket1.async_read_some(
  301. * asio::buffer(data1),
  302. * asio::deferred
  303. * )
  304. * );
  305. *
  306. * std::vector<op_type> ops;
  307. *
  308. * ops.push_back(
  309. * socket1.async_read_some(
  310. * asio::buffer(data1),
  311. * asio::deferred
  312. * )
  313. * );
  314. *
  315. * ops.push_back(
  316. * socket2.async_read_some(
  317. * asio::buffer(data2),
  318. * asio::deferred
  319. * )
  320. * );
  321. *
  322. * asio::experimental::make_parallel_group(ops).async_wait(
  323. * asio::experimental::wait_for_all(),
  324. * [](
  325. * std::vector<std::size_t> completion_order,
  326. * std::vector<std::error_code> e,
  327. * std::vector<std::size_t> n
  328. * )
  329. * {
  330. * for (std::size_t i = 0; i < completion_order.size(); ++i)
  331. * {
  332. * std::size_t idx = completion_order[i];
  333. * std::cout << "socket " << idx << " finished: ";
  334. * std::cout << e[idx] << ", " << n[idx] << "\n";
  335. * }
  336. * }
  337. * );
  338. * @endcode
  339. */
  340. template <typename Range>
  341. ASIO_NODISCARD inline ranged_parallel_group<decay_t<Range>>
  342. make_parallel_group(Range&& range,
  343. constraint_t<
  344. is_async_operation_range<decay_t<Range>>::value
  345. > = 0)
  346. {
  347. return ranged_parallel_group<decay_t<Range>>(std::forward<Range>(range));
  348. }
  349. /// Create a group of operations that may be launched in parallel.
  350. /**
  351. * @param allocator Specifies the allocator to be used with the result vectors.
  352. *
  353. * @param range A range containing the operations to be launched.
  354. *
  355. * For example:
  356. * @code
  357. * using op_type = decltype(
  358. * socket1.async_read_some(
  359. * asio::buffer(data1),
  360. * asio::deferred
  361. * )
  362. * );
  363. *
  364. * std::vector<op_type> ops;
  365. *
  366. * ops.push_back(
  367. * socket1.async_read_some(
  368. * asio::buffer(data1),
  369. * asio::deferred
  370. * )
  371. * );
  372. *
  373. * ops.push_back(
  374. * socket2.async_read_some(
  375. * asio::buffer(data2),
  376. * asio::deferred
  377. * )
  378. * );
  379. *
  380. * asio::experimental::make_parallel_group(
  381. * std::allocator_arg_t,
  382. * my_allocator,
  383. * ops
  384. * ).async_wait(
  385. * asio::experimental::wait_for_all(),
  386. * [](
  387. * std::vector<std::size_t> completion_order,
  388. * std::vector<std::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. 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. #include "asio/detail/pop_options.hpp"
  415. #include "asio/experimental/impl/parallel_group.hpp"
  416. #endif // ASIO_EXPERIMENTAL_PARALLEL_GROUP_HPP