C/C++ API Reference
Loading...
Searching...
No Matches
pw::async2::RunOnceTask< Func, ReturnValuePolicy > Class Template Referencefinal

Overview

template<typename Func = Function<Poll<>()>, ReturnValuePolicy = std::is_void_v<std::invoke_result_t<Func>> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
class pw::async2::RunOnceTask< Func, ReturnValuePolicy >

Runs a function once in a task. The task calls the function then returns Ready. The task may be posted again after it is joined.

The function's return value is stored and can be accessed through the value member after joining, or by calling Wait. A void-returning function use a different RunOnceTask specialization that does not provide the return value. To discard the return value for non-void functions, use the ReturnValuePolicy::kDiscard template argument.

Warning
RunOnceTask should be used rarely, such as in tests, truly one-off cases, or as a last resort for sync-async interop. pw_async2 should not be used as a work queue. Overuse of RunOnceTask forfeits the benefits of pw_async2, scattering logic across a mess of callbacks instead of organizing it linearly in a task.
Inheritance diagram for pw::async2::RunOnceTask< Func, ReturnValuePolicy >:
pw::async2::Task pw::containers::future::IntrusiveList< T >::Item

Public Types

using value_type = std::conditional_t< std::is_void_v< std::invoke_result_t< Func > >, ReadyType, std::invoke_result_t< Func > >
 The return value of the function.
 

Public Member Functions

constexpr RunOnceTask (Func &&func)
 Creates a new Task that runs the provided function once.
 
 RunOnceTask (const RunOnceTask &)=delete
 
RunOnceTaskoperator= (const RunOnceTask &)=delete
 
 RunOnceTask (RunOnceTask &&)=delete
 
RunOnceTaskoperator= (RunOnceTask &&)=delete
 
bool has_value () const
 
value_typevalue ()
 
const value_typevalue () const
 
value_typeWait ()
 Blocks until the task completes and returns a reference its return value.
 
- Public Member Functions inherited from pw::async2::Task
constexpr Task (log::Token name=kDefaultName)
 
 Task (const Task &)=delete
 
 Task (Task &&)=delete
 
Taskoperator= (const Task &)=delete
 
Taskoperator= (Task &&)=delete
 
virtual ~Task ()
 
Poll Pend (Context &cx)
 
bool IsRegistered () const
 
void Deregister ()
 
void Join ()
 

Private Member Functions

Poll DoPend (Context &) override
 

Additional Inherited Members

- Protected Member Functions inherited from pw::containers::future::IntrusiveList< T >::Item
constexpr Item ()=default
 

Member Function Documentation

◆ DoPend()

template<typename Func = Function<Poll<>()>, ReturnValuePolicy = std::is_void_v<std::invoke_result_t<Func>> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
Poll pw::async2::RunOnceTask< Func, ReturnValuePolicy >::DoPend ( Context )
inlineoverrideprivatevirtual

Attempts to advance this Task to completion.

This method should not perform synchronous waiting, as doing so may block the main Dispatcher loop and prevent other Task s from progressing. Because of this, Task s should not invoke blocking Dispatcher methods such as RunUntilComplete.

Tasks should also avoid invoking RunUntilStalled on their own Dispatcher.

Returns Ready if complete, or Pending if the Task was not yet able to complete.

If Pending is returned, the Task must ensure it is woken up when it is able to make progress. To do this, Task::Pend must arrange for Waker::Wake to be called, either by storing a copy of the Waker away to be awoken by another system (such as an interrupt handler).

Implements pw::async2::Task.

◆ has_value()

template<typename Func = Function<Poll<>()>, ReturnValuePolicy = std::is_void_v<std::invoke_result_t<Func>> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
bool pw::async2::RunOnceTask< Func, ReturnValuePolicy >::has_value ( ) const
inline

Returns whether the task ran and set that value to the function's return value.

◆ value() [1/2]

template<typename Func = Function<Poll<>()>, ReturnValuePolicy = std::is_void_v<std::invoke_result_t<Func>> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
value_type & pw::async2::RunOnceTask< Func, ReturnValuePolicy >::value ( )
inline

The return value from the function.

Precondition
The task must have completed. Call Join to ensure it finished.

◆ value() [2/2]

template<typename Func = Function<Poll<>()>, ReturnValuePolicy = std::is_void_v<std::invoke_result_t<Func>> ? ReturnValuePolicy::kDiscard : ReturnValuePolicy::kKeep>
const value_type & pw::async2::RunOnceTask< Func, ReturnValuePolicy >::value ( ) const
inline

The return value from the function.

Precondition
The task must have completed. Call Join to ensure it finished.

The documentation for this class was generated from the following file: