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