bulk_execute.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // execution/bulk_execute.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_EXECUTION_BULK_EXECUTE_HPP
  11. #define BOOST_ASIO_EXECUTION_BULK_EXECUTE_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. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/execution/bulk_guarantee.hpp>
  19. #include <boost/asio/execution/detail/bulk_sender.hpp>
  20. #include <boost/asio/execution/executor.hpp>
  21. #include <boost/asio/execution/sender.hpp>
  22. #include <boost/asio/traits/bulk_execute_member.hpp>
  23. #include <boost/asio/traits/bulk_execute_free.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. #if defined(GENERATING_DOCUMENTATION)
  26. namespace boost {
  27. namespace asio {
  28. namespace execution {
  29. /// A customisation point that creates a bulk sender.
  30. /**
  31. * The name <tt>execution::bulk_execute</tt> denotes a customisation point
  32. * object. If <tt>is_convertible_v<N, size_t></tt> is true, then the expression
  33. * <tt>execution::bulk_execute(S, F, N)</tt> for some subexpressions
  34. * <tt>S</tt>, <tt>F</tt>, and <tt>N</tt> is expression-equivalent to:
  35. *
  36. * @li <tt>S.bulk_execute(F, N)</tt>, if that expression is valid. If the
  37. * function selected does not execute <tt>N</tt> invocations of the function
  38. * object <tt>F</tt> on the executor <tt>S</tt> in bulk with forward progress
  39. * guarantee <tt>boost::asio::query(S, execution::bulk_guarantee)</tt>, and
  40. * the result of that function does not model <tt>sender<void></tt>, the
  41. * program is ill-formed with no diagnostic required.
  42. *
  43. * @li Otherwise, <tt>bulk_execute(S, F, N)</tt>, if that expression is valid,
  44. * with overload resolution performed in a context that includes the
  45. * declaration <tt>void bulk_execute();</tt> and that does not include a
  46. * declaration of <tt>execution::bulk_execute</tt>. If the function selected
  47. * by overload resolution does not execute <tt>N</tt> invocations of the
  48. * function object <tt>F</tt> on the executor <tt>S</tt> in bulk with forward
  49. * progress guarantee <tt>boost::asio::query(E,
  50. * execution::bulk_guarantee)</tt>, and the result of that function does not
  51. * model <tt>sender<void></tt>, the program is ill-formed with no diagnostic
  52. * required.
  53. *
  54. * @li Otherwise, if the types <tt>F</tt> and
  55. * <tt>executor_index_t<remove_cvref_t<S>></tt> model <tt>invocable</tt> and
  56. * if <tt>boost::asio::query(S, execution::bulk_guarantee)</tt> equals
  57. * <tt>execution::bulk_guarantee.unsequenced</tt>, then
  58. *
  59. * - Evaluates <tt>DECAY_COPY(std::forward<decltype(F)>(F))</tt> on the
  60. * calling thread to create a function object <tt>cf</tt>. [Note:
  61. * Additional copies of <tt>cf</tt> may subsequently be created. --end
  62. * note.]
  63. *
  64. * - For each value of <tt>i</tt> in <tt>N</tt>, <tt>cf(i)</tt> (or copy of
  65. * <tt>cf</tt>)) will be invoked at most once by an execution agent that is
  66. * unique for each value of <tt>i</tt>.
  67. *
  68. * - May block pending completion of one or more invocations of <tt>cf</tt>.
  69. *
  70. * - Synchronizes with (C++Std [intro.multithread]) the invocations of
  71. * <tt>cf</tt>.
  72. *
  73. * @li Otherwise, <tt>execution::bulk_execute(S, F, N)</tt> is ill-formed.
  74. */
  75. inline constexpr unspecified bulk_execute = unspecified;
  76. /// A type trait that determines whether a @c bulk_execute expression is
  77. /// well-formed.
  78. /**
  79. * Class template @c can_bulk_execute is a trait that is derived from @c
  80. * true_type if the expression <tt>execution::bulk_execute(std::declval<S>(),
  81. * std::declval<F>(), std::declval<N>)</tt> is well formed; otherwise @c
  82. * false_type.
  83. */
  84. template <typename S, typename F, typename N>
  85. struct can_bulk_execute :
  86. integral_constant<bool, automatically_determined>
  87. {
  88. };
  89. } // namespace execution
  90. } // namespace asio
  91. } // namespace boost
  92. #else // defined(GENERATING_DOCUMENTATION)
  93. namespace boost_asio_execution_bulk_execute_fn {
  94. using boost::asio::declval;
  95. using boost::asio::enable_if;
  96. using boost::asio::execution::bulk_guarantee_t;
  97. using boost::asio::execution::detail::bulk_sender;
  98. using boost::asio::execution::executor_index;
  99. using boost::asio::execution::is_sender;
  100. using boost::asio::is_convertible;
  101. using boost::asio::is_same;
  102. using boost::asio::remove_cvref;
  103. using boost::asio::result_of;
  104. using boost::asio::traits::bulk_execute_free;
  105. using boost::asio::traits::bulk_execute_member;
  106. using boost::asio::traits::static_require;
  107. void bulk_execute();
  108. enum overload_type
  109. {
  110. call_member,
  111. call_free,
  112. adapter,
  113. ill_formed
  114. };
  115. template <typename S, typename Args, typename = void, typename = void,
  116. typename = void, typename = void, typename = void, typename = void>
  117. struct call_traits
  118. {
  119. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = ill_formed);
  120. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
  121. typedef void result_type;
  122. };
  123. template <typename S, typename F, typename N>
  124. struct call_traits<S, void(F, N),
  125. typename enable_if<
  126. is_convertible<N, std::size_t>::value
  127. >::type,
  128. typename enable_if<
  129. bulk_execute_member<S, F, N>::is_valid
  130. >::type,
  131. typename enable_if<
  132. is_sender<
  133. typename bulk_execute_member<S, F, N>::result_type
  134. >::value
  135. >::type> :
  136. bulk_execute_member<S, F, N>
  137. {
  138. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = call_member);
  139. };
  140. template <typename S, typename F, typename N>
  141. struct call_traits<S, void(F, N),
  142. typename enable_if<
  143. is_convertible<N, std::size_t>::value
  144. >::type,
  145. typename enable_if<
  146. !bulk_execute_member<S, F, N>::is_valid
  147. >::type,
  148. typename enable_if<
  149. bulk_execute_free<S, F, N>::is_valid
  150. >::type,
  151. typename enable_if<
  152. is_sender<
  153. typename bulk_execute_free<S, F, N>::result_type
  154. >::value
  155. >::type> :
  156. bulk_execute_free<S, F, N>
  157. {
  158. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = call_free);
  159. };
  160. template <typename S, typename F, typename N>
  161. struct call_traits<S, void(F, N),
  162. typename enable_if<
  163. is_convertible<N, std::size_t>::value
  164. >::type,
  165. typename enable_if<
  166. !bulk_execute_member<S, F, N>::is_valid
  167. >::type,
  168. typename enable_if<
  169. !bulk_execute_free<S, F, N>::is_valid
  170. >::type,
  171. typename enable_if<
  172. is_sender<S>::value
  173. >::type,
  174. typename enable_if<
  175. is_same<
  176. typename result_of<
  177. F(typename executor_index<typename remove_cvref<S>::type>::type)
  178. >::type,
  179. typename result_of<
  180. F(typename executor_index<typename remove_cvref<S>::type>::type)
  181. >::type
  182. >::value
  183. >::type,
  184. typename enable_if<
  185. static_require<S, bulk_guarantee_t::unsequenced_t>::is_valid
  186. >::type>
  187. {
  188. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = adapter);
  189. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
  190. typedef bulk_sender<S, F, N> result_type;
  191. };
  192. struct impl
  193. {
  194. #if defined(BOOST_ASIO_HAS_MOVE)
  195. template <typename S, typename F, typename N>
  196. BOOST_ASIO_CONSTEXPR typename enable_if<
  197. call_traits<S, void(F, N)>::overload == call_member,
  198. typename call_traits<S, void(F, N)>::result_type
  199. >::type
  200. operator()(S&& s, F&& f, N&& n) const
  201. BOOST_ASIO_NOEXCEPT_IF((
  202. call_traits<S, void(F, N)>::is_noexcept))
  203. {
  204. return BOOST_ASIO_MOVE_CAST(S)(s).bulk_execute(
  205. BOOST_ASIO_MOVE_CAST(F)(f), BOOST_ASIO_MOVE_CAST(N)(n));
  206. }
  207. template <typename S, typename F, typename N>
  208. BOOST_ASIO_CONSTEXPR typename enable_if<
  209. call_traits<S, void(F, N)>::overload == call_free,
  210. typename call_traits<S, void(F, N)>::result_type
  211. >::type
  212. operator()(S&& s, F&& f, N&& n) const
  213. BOOST_ASIO_NOEXCEPT_IF((
  214. call_traits<S, void(F, N)>::is_noexcept))
  215. {
  216. return bulk_execute(BOOST_ASIO_MOVE_CAST(S)(s),
  217. BOOST_ASIO_MOVE_CAST(F)(f), BOOST_ASIO_MOVE_CAST(N)(n));
  218. }
  219. template <typename S, typename F, typename N>
  220. BOOST_ASIO_CONSTEXPR typename enable_if<
  221. call_traits<S, void(F, N)>::overload == adapter,
  222. typename call_traits<S, void(F, N)>::result_type
  223. >::type
  224. operator()(S&& s, F&& f, N&& n) const
  225. BOOST_ASIO_NOEXCEPT_IF((
  226. call_traits<S, void(F, N)>::is_noexcept))
  227. {
  228. return typename call_traits<S, void(F, N)>::result_type(
  229. BOOST_ASIO_MOVE_CAST(S)(s), BOOST_ASIO_MOVE_CAST(F)(f),
  230. BOOST_ASIO_MOVE_CAST(N)(n));
  231. }
  232. #else // defined(BOOST_ASIO_HAS_MOVE)
  233. template <typename S, typename F, typename N>
  234. BOOST_ASIO_CONSTEXPR typename enable_if<
  235. call_traits<S, void(const F&, const N&)>::overload == call_member,
  236. typename call_traits<S, void(const F&, const N&)>::result_type
  237. >::type
  238. operator()(S& s, const F& f, const N& n) const
  239. BOOST_ASIO_NOEXCEPT_IF((
  240. call_traits<S, void(const F&, const N&)>::is_noexcept))
  241. {
  242. return s.bulk_execute(BOOST_ASIO_MOVE_CAST(F)(f),
  243. BOOST_ASIO_MOVE_CAST(N)(n));
  244. }
  245. template <typename S, typename F, typename N>
  246. BOOST_ASIO_CONSTEXPR typename enable_if<
  247. call_traits<S, void(const F&, const N&)>::overload == call_member,
  248. typename call_traits<S, void(const F&, const N&)>::result_type
  249. >::type
  250. operator()(const S& s, const F& f, const N& n) const
  251. BOOST_ASIO_NOEXCEPT_IF((
  252. call_traits<S, void(const F&, const N&)>::is_noexcept))
  253. {
  254. return s.bulk_execute(BOOST_ASIO_MOVE_CAST(F)(f),
  255. BOOST_ASIO_MOVE_CAST(N)(n));
  256. }
  257. template <typename S, typename F, typename N>
  258. BOOST_ASIO_CONSTEXPR typename enable_if<
  259. call_traits<S, void(const F&, const N&)>::overload == call_free,
  260. typename call_traits<S, void(const F&, const N&)>::result_type
  261. >::type
  262. operator()(S& s, const F& f, const N& n) const
  263. BOOST_ASIO_NOEXCEPT_IF((
  264. call_traits<S, void(const F&, const N&)>::is_noexcept))
  265. {
  266. return bulk_execute(s, BOOST_ASIO_MOVE_CAST(F)(f),
  267. BOOST_ASIO_MOVE_CAST(N)(n));
  268. }
  269. template <typename S, typename F, typename N>
  270. BOOST_ASIO_CONSTEXPR typename enable_if<
  271. call_traits<S, void(const F&, const N&)>::overload == call_free,
  272. typename call_traits<S, void(const F&, const N&)>::result_type
  273. >::type
  274. operator()(const S& s, const F& f, const N& n) const
  275. BOOST_ASIO_NOEXCEPT_IF((
  276. call_traits<S, void(const F&, const N&)>::is_noexcept))
  277. {
  278. return bulk_execute(s, BOOST_ASIO_MOVE_CAST(F)(f),
  279. BOOST_ASIO_MOVE_CAST(N)(n));
  280. }
  281. template <typename S, typename F, typename N>
  282. BOOST_ASIO_CONSTEXPR typename enable_if<
  283. call_traits<S, void(const F&, const N&)>::overload == adapter,
  284. typename call_traits<S, void(const F&, const N&)>::result_type
  285. >::type
  286. operator()(S& s, const F& f, const N& n) const
  287. BOOST_ASIO_NOEXCEPT_IF((
  288. call_traits<S, void(const F&, const N&)>::is_noexcept))
  289. {
  290. return typename call_traits<S, void(const F&, const N&)>::result_type(
  291. s, BOOST_ASIO_MOVE_CAST(F)(f), BOOST_ASIO_MOVE_CAST(N)(n));
  292. }
  293. template <typename S, typename F, typename N>
  294. BOOST_ASIO_CONSTEXPR typename enable_if<
  295. call_traits<S, void(const F&, const N&)>::overload == adapter,
  296. typename call_traits<S, void(const F&, const N&)>::result_type
  297. >::type
  298. operator()(const S& s, const F& f, const N& n) const
  299. BOOST_ASIO_NOEXCEPT_IF((
  300. call_traits<S, void(const F&, const N&)>::is_noexcept))
  301. {
  302. return typename call_traits<S, void(const F&, const N&)>::result_type(
  303. s, BOOST_ASIO_MOVE_CAST(F)(f), BOOST_ASIO_MOVE_CAST(N)(n));
  304. }
  305. #endif // defined(BOOST_ASIO_HAS_MOVE)
  306. };
  307. template <typename T = impl>
  308. struct static_instance
  309. {
  310. static const T instance;
  311. };
  312. template <typename T>
  313. const T static_instance<T>::instance = {};
  314. } // namespace boost_asio_execution_bulk_execute_fn
  315. namespace boost {
  316. namespace asio {
  317. namespace execution {
  318. namespace {
  319. static BOOST_ASIO_CONSTEXPR
  320. const boost_asio_execution_bulk_execute_fn::impl& bulk_execute =
  321. boost_asio_execution_bulk_execute_fn::static_instance<>::instance;
  322. } // namespace
  323. template <typename S, typename F, typename N>
  324. struct can_bulk_execute :
  325. integral_constant<bool,
  326. boost_asio_execution_bulk_execute_fn::call_traits<
  327. S, void(F, N)>::overload !=
  328. boost_asio_execution_bulk_execute_fn::ill_formed>
  329. {
  330. };
  331. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  332. template <typename S, typename F, typename N>
  333. constexpr bool can_bulk_execute_v = can_bulk_execute<S, F, N>::value;
  334. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  335. template <typename S, typename F, typename N>
  336. struct is_nothrow_bulk_execute :
  337. integral_constant<bool,
  338. boost_asio_execution_bulk_execute_fn::call_traits<
  339. S, void(F, N)>::is_noexcept>
  340. {
  341. };
  342. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  343. template <typename S, typename F, typename N>
  344. constexpr bool is_nothrow_bulk_execute_v
  345. = is_nothrow_bulk_execute<S, F, N>::value;
  346. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  347. template <typename S, typename F, typename N>
  348. struct bulk_execute_result
  349. {
  350. typedef typename boost_asio_execution_bulk_execute_fn::call_traits<
  351. S, void(F, N)>::result_type type;
  352. };
  353. } // namespace execution
  354. } // namespace asio
  355. } // namespace boost
  356. #endif // defined(GENERATING_DOCUMENTATION)
  357. #include <boost/asio/detail/pop_options.hpp>
  358. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  359. #endif // BOOST_ASIO_EXECUTION_BULK_EXECUTE_HPP