#include <userver/engine/task/task.hpp>
Asynchronous task that has a unique ownership of the payload.
See engine::TaskWithResult for a type that could return a value or report an exception from the payload.
Inheritance diagram for engine::Task:Public Types | |
| enum class | Importance { kNormal , kCritical } |
| Task importance. More... | |
| enum class | State { kInvalid , kNew , kQueued , kRunning , kSuspended , kCancelled , kCompleted } |
| Task state. More... | |
| enum class | WaitMode { kSingleAwaiter , kMultipleAwaiters } |
| Task wait mode. More... | |
Public Member Functions | |
| Task () | |
| Default constructor. | |
| ~Task () | |
| If the task is still valid and is not finished, cancels it and waits until it finishes. | |
| Task (Task &&other) noexcept | |
| Moves the other task into this, leaving the other in an invalid state. | |
| Task & | operator= (Task &&other) noexcept |
| If this Task is still valid and is not finished, cancels it and waits until it finishes before moving the other. Otherwise just moves the other task into this, leaving the other in invalid state. | |
| Task (const Task &)=delete | |
| Task & | operator= (const Task &)=delete |
| bool | IsValid () const |
| Checks whether this object owns an actual task (not State::kInvalid) | |
| State | GetState () const |
| Gets the task State. | |
| bool | IsFinished () const |
| Returns whether the task finished execution. | |
| void | Wait () const noexcept(false) |
| Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait(). | |
| template<typename Rep, typename Period> | |
| void | WaitFor (const std::chrono::duration< Rep, Period > &) const noexcept(false) |
| Suspends execution until the task finishes or after the specified timeout or until caller is cancelled. | |
| template<typename Clock, typename Duration> | |
| void | WaitUntil (const std::chrono::time_point< Clock, Duration > &) const noexcept(false) |
| Suspends execution until the task finishes or until the specified time point is reached or until caller is cancelled. | |
| void | WaitUntil (Deadline) const |
| Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled. | |
| bool | WaitNothrow () const noexcept |
| Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait(). | |
| FutureStatus | WaitNothrowUntil (Deadline) const noexcept |
| Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled. | |
| void | RequestCancel () |
| Queues task cancellation request. | |
| void | SyncCancel () noexcept |
| Cancels the task and suspends execution until it is finished. Can be called from coroutine context only. For non-coroutine context use RequestCancel() + BlockingWait(). | |
| TaskCancellationReason | CancellationReason () const |
| Gets task cancellation reason. | |
| void | BlockingWait () const |
Static Public Member Functions | |
| static std::string_view | GetStateName (State state) |
|
stronginherited |
Task importance.
Definition at line 36 of file task_base.hpp.
|
stronginherited |
Task state.
| Enumerator | |
|---|---|
| kInvalid | Unusable. |
| kNew | just created, not registered with task processor |
| kQueued | awaits execution |
| kRunning | executing user code |
| kSuspended | suspended, e.g. waiting for blocking call to complete |
| kCancelled | The task is cancelled and was finished without returning a value or throwing a user-provided exception. This can happen for two reasons:
In both cases, engine::TaskWithResult::Get throws engine::TaskCancelledException. Unintuitively, this status is not set when the task was cancelled after it started running, which caused the user code to exit early. Use TaskBase::CancellationReason instead to check whether the task was cancelled. |
| kCompleted | Exited user code with return or throw. This includes cases where the task was cancelled after it started running, which caused the user code to exit early. Use TaskBase::IsFinished instead to check whether the task finished execution. |
Definition at line 48 of file task_base.hpp.
|
stronginherited |
Task wait mode.
| Enumerator | |
|---|---|
| kSingleAwaiter | Can be awaited by at most one task at a time. |
| kMultipleAwaiters | Can be awaited by multiple tasks simultaneously. |
Definition at line 85 of file task_base.hpp.
| engine::Task::Task | ( | ) |
Default constructor.
Creates an invalid task.
|
inherited |
Waits for the task in non-coroutine context (e.g. non-TaskProcessor's std::thread).
|
inherited |
Checks whether this object owns an actual task (not State::kInvalid)
An invalid task cannot be used. The task becomes invalid after each of the following calls:
Notably, the task does not become invalid immediately after it finishes execution. (That would always cause race conditions when trying to await a task.) It means that some of the task's resources are held onto until the task object is invalidated or destroyed.
|
inherited |
Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait().
| WaitInterruptedException | when current_task::IsCancelRequested() and no TaskCancellationBlockers are present. |
|
inherited |
Suspends execution until the task finishes or after the specified timeout or until caller is cancelled.
| WaitInterruptedException | when current_task::IsCancelRequested() and no TaskCancellationBlockers are present. |
Definition at line 210 of file task_base.hpp.
|
nodiscardnoexceptinherited |
Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait().
false when current_task::IsCancelRequested() and no TaskCancellationBlockers are present.
|
nodiscardnoexceptinherited |
Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled.
current_task::IsCancelRequested() and no TaskCancellationBlockers are present.
|
inherited |
Suspends execution until the task finishes or until the specified time point is reached or until caller is cancelled.
| WaitInterruptedException | when current_task::IsCancelRequested() and no TaskCancellationBlockers are present. |
Definition at line 215 of file task_base.hpp.
|
inherited |
Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled.
| WaitInterruptedException | when current_task::IsCancelRequested() and no TaskCancellationBlockers are present. |
|
friend |
Detaches task, allowing it to continue execution out of scope; memory safety is much better with concurrent::BackgroundTaskStorage.
Async*, should outlive the task execution. This is hard to achieve in general, detached tasks may outlive all the components! Use concurrent::BackgroundTaskStorage as a safe and efficient alternative.