13 Asynchronous model [async]

13.2 Requirements [async.reqmts]

13.2.7 Requirements on asynchronous operations [async.reqmts.async]

This subclause uses the names Alloc1, Alloc2, alloc1, alloc2, Args, CompletionHandler, completion_handler, Executor1, Executor2, ex1, ex2, f, i, N, Signature, token, Ti, ti, work1, and work2 as placeholders for specifying the requirements below.

13.2.7.1 General asynchronous operation concepts [async.reqmts.async.concepts]

An initiating function is a function which may be called to start an asynchronous operation. A completion handler is a function object that will be invoked, at most once, with the result of the asynchronous operation.

The life cycle of an asynchronous operation is comprised of the following events and phases:

  • Event 1: The asynchronous operation is started by a call to the initiating function.

  • Phase 1: The asynchronous operation is now outstanding.

  • Event 2: The externally observable side effects of the asynchronous operation, if any, are fully established. The completion handler is submitted to an executor.

  • Phase 2: The asynchronous operation is now completed.

  • Event 3: The completion handler is called with the result of the asynchronous operation.

In this document, all functions with the prefix async_ are initiating functions.

13.2.7.2 Completion tokens and handlers [async.reqmts.async.token]

Initiating functions:

  • are function templates with template parameter CompletionToken;

  • accept, as the final parameter, a completion token object token of type CompletionToken;

  • specify a completion signature, which is a call signature (C++ 2014 [func.def]) Signature that determines the arguments to the completion handler.

An initiating function determines the type CompletionHandler of its completion handler function object by performing typename async_result<decay_t<CompletionToken>, Signature>::completion_handler_type. The completion handler object completion_handler is initialized with std::forward<CompletionToken>(token). [ Note: No other requirements are placed on the type CompletionToken.  — end note ]

The type CompletionHandler shall satisfy the requirements of Destructible (C++ 2014 [destructible]) and MoveConstructible (C++ 2014 [moveconstructible]), and be callable with the specified call signature.

In this document, all initiating functions specify a Completion signature: element that defines the call signature Signature. The Completion signature: elements in this document have named parameters, and the results of an asynchronous operation are specified in terms of these names.

13.2.7.3 Deduction of initiating function return type [async.reqmts.async.return.type]

The return type of an initiating function is typename async_result<decay_t<CompletionToken>, Signature>::return_type.

For the sake of exposition, this document sometimes annotates functions with a return type DEDUCED. For every function declaration that returns DEDUCED, the meaning is equivalent to specifying the return type as typename async_result<decay_t<CompletionToken>, Signature>::return_type.

13.2.7.4 Production of initiating function return value [async.reqmts.async.return.value]

An initiating function produces its return type as follows:

  • constructing an object result of type async_result<decay_t<CompletionToken>, Signature>, initialized as result(completion_handler); and

  • using result.get() as the operand of the return statement.

Example: Given an asynchronous operation with Completion signature void(R1 r1, R2 r2), an initiating function meeting these requirements may be implemented as follows:

template<class CompletionToken>
auto async_xyz(T1 t1, T2 t2, CompletionToken&& token){
  typename async_result<decay_t<CompletionToken>, void(R1, R2)>::completion_handler_type
    completion_handler(forward<CompletionToken>(token));

  async_result<decay_t<CompletionToken>, void(R1, R2)> result(completion_handler);

  // initiate the operation and cause completion_handler to be invoked with
  // the result

  return result.get();
}

For convenience, initiating functions may be implemented using the async_completion template:

template<class CompletionToken>
auto async_xyz(T1 t1, T2 t2, CompletionToken&& token){
  async_completion<CompletionToken, void(R1, R2)> init(token);

  // initiate the operation and cause init.completion_handler to be invoked
  // with the result

  return init.result.get();
}

 — end example ]

13.2.7.5 Lifetime of initiating function arguments [async.reqmts.async.lifetime]

Unless otherwise specified, the lifetime of arguments to initiating functions shall be treated as follows:

  • If the parameter has a pointer type or has a type of lvalue reference to non-const, the implementation may assume the validity of the pointee or referent, respectively, until the completion handler is invoked. [ Note: In other words, the program is responsible for guaranteeing the validity of the argument until the completion handler is invoked.  — end note ]

  • Otherwise, the implementation does not assume the validity of the argument after the initiating function completes. [ Note: In other words, the program is not required to guarantee the validity of the argument after the initiating function completes.  — end note ] The implementation may make copies of the argument, and all copies shall be destroyed no later than immediately after invocation of the completion handler.

13.2.7.6 Non-blocking requirements on initiating functions [async.reqmts.async.non.blocking]

An initiating function shall not block (C++ 2014 [defns.block]) the calling thread pending completion of the outstanding operation.

Note: Initiating functions can still block the calling thread for other reasons. For example, if an initiating function locks a mutex in order to synchronize access to shared data.  — end note ]

13.2.7.7 Associated executor [async.reqmts.async.assoc.exec]

Certain objects that participate in asynchronous operations have an associated executor. These are obtained as specified below.

13.2.7.8 I/O executor [async.reqmts.async.io.exec]

An asynchronous operation has an associated executor satisfying the Executor ([async.reqmts.executor]) requirements. If not otherwise specified by the asynchronous operation, this associated executor is an object of type system_executor.

All asynchronous operations in this document have an associated executor object that is determined as follows:

  • If the initiating function is a member function, the associated executor is that returned by the get_executor member function on the same object.

  • If the initiating function is not a member function, the associated executor is that returned by the get_executor member function of the first argument to the initiating function.

Let Executor1 be the type of the associated executor. Let ex1 be a value of type Executor1, representing the associated executor object obtained as described above.

13.2.7.9 Completion handler executor [async.reqmts.async.handler.exec]

A completion handler object of type CompletionHandler has an associated executor satisfying the Executor requirements ([async.reqmts.executor]). The type of this associated executor is associated_executor_t<CompletionHandler, Executor1>. Let Executor2 be the type associated_executor_t<CompletionHandler, Executor1>. Let ex2 be a value of type Executor2 obtained by performing get_associated_executor(completion_handler, ex1).

13.2.7.10 Outstanding work [async.reqmts.async.work]

Until the asynchronous operation has completed, the asynchronous operation shall maintain:

  • an object work1 of type executor_work_guard<Executor1>, initialized as work1(ex1), and where work1.owns_work() == true; and

  • an object work2 of type executor_work_guard<Executor2>, initialized as work2(ex2), and where work2.owns_work() == true.

13.2.7.11 Allocation of intermediate storage [async.reqmts.async.alloc]

Asynchronous operations may allocate memory. [ Note: Such as a data structure to store copies of the completion_handler object and the initiating function's arguments.  — end note ]

Let Alloc1 be a type, satisfying the ProtoAllocator ([async.reqmts.proto.allocator]) requirements, that represents the asynchronous operation's default allocation strategy. [ Note: Typically allocator<void>.  — end note ] Let alloc1 be a value of type Alloc1.

A completion handler object of type CompletionHandler has an associated allocator object alloc2 of type Alloc2 satisfying the ProtoAllocator ([async.reqmts.proto.allocator]) requirements. The type Alloc2 is associated_allocator_t<CompletionHandler, Alloc1>. Let alloc2 be a value of type Alloc2 obtained by performing get_associated_allocator(completion_handler, alloc1).

The asynchronous operations defined in this document:

  • If required, allocate memory using only the completion handler's associated allocator.

  • Prior to completion handler execution, deallocate any memory allocated using the completion handler's associated allocator.

Note: The implementation can perform operating system or underlying API calls that perform memory allocations not using the associated allocator. Invocations of the allocator functions do not introduce data races (see C++ 2014 [res.on.data.races]).  — end note ]

13.2.7.12 Execution of completion handler on completion of asynchronous operation [async.reqmts.async.completion]

Let Args... be the argument types of the completion signature Signature and let N be sizeof...(Args). Let i be in the range [0, N). Let Ti be the ith type in Args... and let ti be the ith completion handler argument associated with Ti.

Let f be a function object, callable as f(), that invokes completion_handler as if by completion_handler(forward<T0>(t0), ..., forward<TN-1>(tN-1)).

If an asynchronous operation completes immediately (that is, within the thread of execution calling the initiating function, and before the initiating function returns), the completion handler shall be submitted for execution as if by performing ex2.post(std::move(f), alloc2). Otherwise, the completion handler shall be submitted for execution as if by performing ex2.dispatch(std::move(f), alloc2).

13.2.7.13 Completion handlers and exceptions [async.reqmts.async.exceptions]

Completion handlers are permitted to throw exceptions. The effect of any exception propagated from the execution of a completion handler is determined by the executor which is executing the completion handler.

13.2.7.14 Composed asynchronous operations [async.reqmts.async.composed]

In this document, a composed asynchronous operation is an asynchronous operation that is implemented in terms of zero or more intermediate calls to other asynchronous operations. The intermediate asynchronous operations are performed sequentially. [ Note: That is, the completion handler of an intermediate operation initiates the next operation in the sequence.  — end note ]

An intermediate operation's completion handler shall have an associated executor that is either:

  • the type Executor2 and object ex2 obtained from the completion handler type CompletionHandler and object completion_handler; or

  • an object of an unspecified type satisfying the Executor requirements ([async.reqmts.executor]), that delegates executor operations to the type Executor2 and object ex2.

An intermediate operation's completion handler shall have an associated allocator that is either:

  • the type Alloc2 and object alloc2 obtained from the completion handler type CompletionHandler and object completion_handler; or

  • an object of an unspecified type satisfying the ProtoAllocator requirements ([async.reqmts.proto.allocator]), that delegates allocator operations to the type Alloc2 and object alloc2.