asynchronous_operations.qbk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:asynchronous_operations Requirements on asynchronous operations]
  8. This section uses the names `Alloc1`, `Alloc2`, `alloc1`, `alloc2`, `Args`,
  9. `CompletionHandler`, `completion_handler`, `Executor1`, `Executor2`, `ex1`,
  10. `ex2`, `f`, [^['i]], [^['N]], `Signature`, `token`, [^T[sub ['i]]], [^t[sub
  11. ['i]]], `work1`, and `work2` as placeholders for specifying the requirements
  12. below.
  13. [heading General asynchronous operation concepts]
  14. An ['initiating function] is a function which may be called to start an
  15. asynchronous operation. A ['completion handler] is a function object that will
  16. be invoked, at most once, with the result of the asynchronous operation.
  17. The lifecycle of an asynchronous operation is comprised of the following events
  18. and phases:
  19. [mdash] Event 1: The asynchronous operation is started by a call to the
  20. initiating function.
  21. [mdash] Phase 1: The asynchronous operation is now ['outstanding].
  22. [mdash] Event 2: The externally observable side effects of the asynchronous
  23. operation, if any, are fully established. The completion handler is submitted
  24. to an executor.
  25. [mdash] Phase 2: The asynchronous operation is now ['completed].
  26. [mdash] Event 3: The completion handler is called with the result of the
  27. asynchronous operation.
  28. In this library, all functions with the prefix `async_` are initiating
  29. functions.
  30. [heading Completion tokens and handlers]
  31. Initiating functions:
  32. [mdash] are function templates with template parameter `CompletionToken`;
  33. [mdash] accept, as the final parameter, a ['completion token] object `token`
  34. of type `CompletionToken`;
  35. [mdash] specify a ['completion signature], which is a call signature (C++Std
  36. [func.def]) `Signature` that determines the arguments to the completion
  37. handler.
  38. An initiating function determines the type `CompletionHandler` of its
  39. completion handler function object by performing `typename
  40. async_result<decay_t<CompletionToken>, Signature>::completion_handler_type`.
  41. The completion handler object `completion_handler` is initialized with
  42. `forward<CompletionToken>(token)`. [inline_note No other requirements are
  43. placed on the type `CompletionToken`.]
  44. The type `CompletionHandler` must satisfy the requirements of `Destructible`
  45. (C++Std [destructible]) and `MoveConstructible` (C++Std
  46. [moveconstructible]), and be callable with the specified call signature.
  47. In this library, all initiating functions specify a ['Completion signature]
  48. element that defines the call signature `Signature`. The ['Completion
  49. signature] elements in this Technical Specification have named parameters, and
  50. the results of an asynchronous operation are specified in terms of these names.
  51. [heading Automatic deduction of initiating function return type]
  52. The return type of an initiating function is `typename
  53. async_result<decay_t<CompletionToken>, Signature>::return_type`.
  54. For the sake of exposition, this library sometimes annotates functions with a
  55. return type ['[^DEDUCED]]. For every function declaration that returns
  56. ['[^DEDUCED]], the meaning is equivalent to specifying the return type as
  57. `typename async_result<decay_t<CompletionToken>, Signature>::return_type`.
  58. [heading Production of initiating function return value]
  59. An initiating function produces its return type as follows:
  60. [mdash] constructing an object `result` of type
  61. `async_result<decay_t<CompletionToken>, Signature>`, initialized as
  62. `result(completion_handler)`; and
  63. [mdash] using `result.get()` as the operand of the return statement.
  64. \[['Example:] Given an asynchronous operation with ['Completion signature]
  65. `void(R1 r1, R2 r2)`, an initiating function meeting these requirements may be
  66. implemented as follows:
  67. template<class CompletionToken>
  68. auto async_xyz(T1 t1, T2 t2, CompletionToken&& token)
  69. {
  70. typename async_result<decay_t<CompletionToken>, void(R1, R2)>::completion_handler_type
  71. completion_handler(forward<CompletionToken>(token));
  72. async_result<decay_t<CompletionToken>, void(R1, R2)> result(completion_handler);
  73. // initiate the operation and cause completion_handler to be invoked with
  74. // the result
  75. return result.get();
  76. }
  77. For convenience, initiating functions may be implemented using the
  78. `async_completion` template:
  79. template<class CompletionToken>
  80. auto async_xyz(T1 t1, T2 t2, CompletionToken&& token)
  81. {
  82. async_completion<CompletionToken, void(R1, R2)> init(token);
  83. // initiate the operation and cause init.completion_handler to be invoked
  84. // with the result
  85. return init.result.get();
  86. }
  87. '''&mdash;'''['end example]\]
  88. [heading Lifetime of initiating function arguments]
  89. Unless otherwise specified, the lifetime of arguments to initiating functions
  90. shall be treated as follows:
  91. [mdash] If the parameter has a pointer type or has a type of lvalue reference
  92. to non-const, the implementation may assume the validity of the pointee or
  93. referent, respectively, until the completion handler is invoked. [inline_note
  94. In other words, the program must guarantee the validity of the argument until
  95. the completion handler is invoked.]
  96. [mdash] Otherwise, the implementation must not assume the validity of the
  97. argument after the initiating function completes. [inline_note In other words,
  98. the program is not required to guarantee the validity of the argument after the
  99. initiating function completes.] The implementation may make copies of the
  100. argument, and all copies shall be destroyed no later than immediately after
  101. invocation of the completion handler.
  102. [heading Non-blocking requirements on initiating functions]
  103. An initiating function shall not block (C++Std [defns.block]) the calling
  104. thread pending completion of the outstanding operation.
  105. [std_note Initiating functions may still block the calling thread for other
  106. reasons. For example, an initiating function may lock a mutex in order to
  107. synchronize access to shared data.]
  108. [heading Associated executor]
  109. Certain objects that participate in asynchronous operations have an
  110. ['associated executor]. These are obtained as specified in the sections below.
  111. [heading Associated I/O executor]
  112. An asynchronous operation has an associated executor satisfying the [link
  113. boost_asio.reference.Executor1 `Executor`] requirements. If not otherwise specified by
  114. the asynchronous operation, this associated executor is an object of type
  115. `system_executor`.
  116. All asynchronous operations in this library have an associated executor object
  117. that is determined as follows:
  118. [mdash] If the initiating function is a member function, the associated
  119. executor is that returned by the `get_executor` member function on the same
  120. object.
  121. [mdash] If the initiating function is not a member function, the associated
  122. executor is that returned by the `get_executor` member function of the first
  123. argument to the initiating function.
  124. Let `Executor1` be the type of the associated executor. Let `ex1` be a value of
  125. type `Executor1`, representing the associated executor object obtained as
  126. described above.
  127. [heading Associated completion handler executor]
  128. A completion handler object of type `CompletionHandler` has an associated
  129. executor of type `Executor2` satisfying the [link boost_asio.reference.Executor1
  130. Executor requirements]. The type `Executor2` is
  131. `associated_executor_t<CompletionHandler, Executor1>`. Let `ex2` be a value of
  132. type `Executor2` obtained by performing
  133. `get_associated_executor(completion_handler, ex1)`.
  134. [heading Outstanding work]
  135. Until the asynchronous operation has completed, the asynchronous operation
  136. shall maintain:
  137. [mdash] an object `work1` of type `executor_work_guard<Executor1>`, initialized
  138. as `work1(ex1)`, and where `work1.owns_work() == true`; and
  139. [mdash] an object `work2` of type `executor_work_guard<Executor2>`, initialized
  140. as `work2(ex2)`, and where `work2.owns_work() == true`.
  141. [heading Allocation of intermediate storage]
  142. Asynchronous operations may allocate memory. [inline_note Such as a data
  143. structure to store copies of the `completion_handler` object and the initiating
  144. function's arguments.]
  145. Let `Alloc1` be a type, satisfying the [link boost_asio.reference.ProtoAllocator
  146. `ProtoAllocator`] requirements, that represents the asynchronous operation's
  147. default allocation strategy. [inline_note Typically `std::allocator<void>`.]
  148. Let `alloc1` be a value of type `Alloc1`.
  149. A completion handler object of type `CompletionHandler` has an associated
  150. allocator object `alloc2` of type `Alloc2` satisfying the [link
  151. boost_asio.reference.ProtoAllocator `ProtoAllocator`] requirements. The type `Alloc2`
  152. is `associated_allocator_t<CompletionHandler, Alloc1>`. Let `alloc2` be a value
  153. of type `Alloc2` obtained by performing
  154. `get_associated_allocator(completion_handler, alloc1)`.
  155. The asynchronous operations defined in this library:
  156. [mdash] If required, allocate memory using only the completion handler's
  157. associated allocator.
  158. [mdash] Prior to completion handler execution, deallocate any memory allocated
  159. using the completion handler's associated allocator.
  160. [std_note The implementation may perform operating system or underlying API
  161. calls that perform memory allocations not using the associated allocator.
  162. Invocations of the allocator functions may not introduce data races (See C++Std
  163. \[res.on.data.races\]).]
  164. [heading Execution of completion handler on completion of asynchronous operation]
  165. Let `Args...` be the argument types of the completion signature `Signature` and
  166. let [^['N]] be `sizeof...(Args)`. Let [^['i]] be in the range [half_open_range
  167. `0`,[^['N]]]. Let [^T[sub ['i]]] be the [^['i]]th type in `Args...` and let
  168. [^t[sub ['i]]] be the [^['i]]th completion handler argument associated with
  169. [^T[sub ['i]]].
  170. Let `f` be a function object, callable as `f()`, that invokes
  171. `completion_handler` as if by [^completion_handler(forward<T[sub ['0]]>(t[sub
  172. ['0]]), ..., forward<T[sub ['N-1]]>(t[sub ['N-1]]))].
  173. If an asynchonous operation completes immediately (that is, within the thread
  174. of execution calling the initiating function, and before the initiating
  175. function returns), the completion handler shall be submitted for execution as
  176. if by performing `ex2.post(std::move(f), alloc2)`. Otherwise, the completion
  177. handler shall be submitted for execution as if by performing
  178. `ex2.dispatch(std::move(f), alloc2)`.
  179. [heading Completion handlers and exceptions]
  180. Completion handlers are permitted to throw exceptions. The effect of any
  181. exception propagated from the execution of a completion handler is determined
  182. by the executor which is executing the completion handler.
  183. [heading Default completion tokens]
  184. Every I/O executor type has an associated default completion token type. This is
  185. specified via the `default_completion_token` trait. This trait may be used in
  186. asynchronous operation declarations as follows:
  187. template <
  188. typename IoObject,
  189. typename CompletionToken =
  190. typename default_completion_token<
  191. typename IoObject::executor_type
  192. >::type
  193. >
  194. auto async_xyz(
  195. IoObject& io_object,
  196. CompletionToken&& token =
  197. typename default_completion_token<
  198. typename IoObject::executor_type
  199. >::type{}
  200. );
  201. If not specialised, this trait type is `void`, meaning no default completion
  202. token type is available for the given I/O executor.
  203. \[['Example:] The `default_completion_token` trait is specialised for the
  204. `use_awaitable` completion token so that it may be used as shown in the
  205. following example:
  206. auto socket = use_awaitable.as_default_on(tcp::socket(my_context));
  207. // ...
  208. co_await socket.async_connect(my_endpoint); // Defaults to use_awaitable.
  209. In this example, the type of the `socket` object is transformed from
  210. `tcp::socket` to have an I/O executor with the default completion token set to
  211. `use_awaitable`.
  212. Alternatively, the socket type may be computed directly:
  213. using tcp_socket = use_awaitable_t<>::as_default_on_t<tcp::socket>;
  214. tcp_socket socket(my_context);
  215. // ...
  216. co_await socket.async_connect(my_endpoint); // Defaults to use_awaitable.
  217. '''&mdash;'''['end example]\]
  218. [endsect]